-
Notifications
You must be signed in to change notification settings - Fork 2
/
reddit.py
82 lines (70 loc) · 2.53 KB
/
reddit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
import re
import urllib
import platform
import requests
from bs4 import BeautifulSoup
from PIL import Image
import imgur_album as album
platform = platform.system()
print platform
try:
if platform.lower() == 'windows':
from change_background import apply_background as background
if platform.lower() == 'linux':
from change_background import apply_background_linux as background
except:
print "Import Error. Unsupported OS"
exit()
head = {'User-Agent': 'Mozilla/5.0'}
album_argument = open('random_wallpaper.txt', 'r'); RANDOM_ALBUM = int(album_argument.readline()); album_argument.close()
print RANDOM_ALBUM
def image_check(image):
im=Image.open(image)
is_image = im.size
print "Resolution: {}".format(is_image)
if is_image:
return True
else:
return False
def calling(subreddit):
r = requests.get("https://www.reddit.com/r/"+subreddit, headers = head)
if r.status_code == 200:
print r.status_code
text = r.text
soup = BeautifulSoup(text, "html.parser")
search_string = str(soup.find('a', {'class':'title'}))
print "Found: {}\n".format(search_string)
#https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,}
#[htps:/]{7,8}[a-zA-Z0-9./:.]+[a-zA-Z0-9./:.]
photo_url = str(re.search('[htps:/]{7,8}[a-zA-Z0-9._/:.]+[a-zA-Z0-9./:.-]+', search_string).group())
is_album = re.findall('[htps:/]+imgur.com/a/[a-zA-Z0-9./]+', photo_url)
if is_album:
if RANDOM_ALBUM == 1: print "Imgur album detected. Downloading a random photo from the album..."
else: print "Imgur album detected. Downloading the first photo from the album..."
photo_url = album.get_album_photos(photo_url, RANDOM_ALBUM)
if photo_url[-4:]!=".jpg":
print "Redirect suspected."
photo_url = photo_url+".jpg"
print "URL: {}".format(photo_url)
photo_name = re.findall(r"(>)([\w\s,().'{}\[\]]+)(<?)",search_string)
photo_name = photo_name[0][1]+".jpg"
print "List: {}".format(photo_name)
urllib.urlretrieve(photo_url, photo_name)
print "Name: {}\n".format(photo_name)
try:
if image_check(photo_name):
##
print "\n\n{}".format(photo_name)
##
a = background(photo_name)
print "Applied."
else:
print "Image is not downloaded properly."
except IOError:
print "Image is not downloaded properly or the given domain has restricted bot's access"
else:
print "Something went wrong!"
print r.status_code
calling("Wallpapers")
#<a class="title may-blank " href="http://newbusinesstips.com/wp-content/uploads/2015/12/Wallpaper-1.jpg" tabindex="1">Beautiful Valley View[2560x1440]</a>