|
1
|
|
|
import os |
|
2
|
|
|
import os.path |
|
3
|
|
|
import random |
|
4
|
|
|
import sys |
|
5
|
|
|
import urllib.request |
|
6
|
|
|
import requests |
|
7
|
|
|
from os import listdir |
|
8
|
|
|
from helper import Helper |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class BackGroundChanger(object): |
|
12
|
|
|
def __init__(self, config, random_image=False): |
|
13
|
|
|
self.config = config |
|
14
|
|
|
self.image = None |
|
15
|
|
|
self.base_url = "https://www.reddit.com/r" |
|
16
|
|
|
|
|
17
|
|
|
# if bool(random.getrandbits(1)) and not random_image: |
|
18
|
|
|
# try: |
|
19
|
|
|
self.image_url = self.get_image() |
|
20
|
|
|
self.image_name = self.get_image_name(self.image_url) |
|
21
|
|
|
self.download_image() |
|
22
|
|
|
self.image = self.image_name |
|
23
|
|
|
# except: |
|
24
|
|
|
# self.random_wallpaper() |
|
25
|
|
|
# else: |
|
26
|
|
|
# self.random_wallpaper() |
|
27
|
|
|
|
|
28
|
|
|
def validate_image(self, sub): |
|
29
|
|
|
if sub['over_18'] and not self.config['over_18']: |
|
30
|
|
|
return False |
|
31
|
|
|
|
|
32
|
|
|
file_name = self.get_image_name(sub['url']) |
|
33
|
|
|
file_name = self.config['wallpapers_directory'] + file_name |
|
34
|
|
|
|
|
35
|
|
|
file_exts = ('png', 'bmp', 'jpeg', 'jpg') |
|
36
|
|
|
if not sub['url'].endswith(file_exts): |
|
37
|
|
|
return False |
|
38
|
|
|
|
|
39
|
|
|
if file_name == '' or sub['url'] == '': |
|
40
|
|
|
return False |
|
41
|
|
|
|
|
42
|
|
|
return not os.path.exists(file_name) |
|
43
|
|
|
|
|
44
|
|
|
def get_image(self): |
|
45
|
|
|
sub_reddit = random.choice(self.config['subs']).strip() |
|
46
|
|
|
url = "{}/{}/hot.json".format(self.base_url, sub_reddit) |
|
47
|
|
|
|
|
48
|
|
|
response_data = requests.get(url, headers={'User-agent': 'wallpaper changer for linux by /u/parkourben99'}) |
|
49
|
|
|
|
|
50
|
|
|
images = [] |
|
51
|
|
|
|
|
52
|
|
|
for post in response_data.json()['data']['children']: |
|
53
|
|
|
if self.validate_image(post['data']): |
|
54
|
|
|
images.append(post) |
|
55
|
|
|
|
|
56
|
|
|
return random.choice(images)['data']['url'] |
|
57
|
|
|
|
|
58
|
|
|
def get_image_name(self, file_name): |
|
59
|
|
|
file_name = file_name.split('/') |
|
60
|
|
|
return file_name[len(file_name) - 1] |
|
61
|
|
|
|
|
62
|
|
|
def download_image(self): |
|
63
|
|
|
local_file = self.config['wallpapers_directory'] + self.image_name |
|
64
|
|
|
open(local_file, 'w+').close() |
|
65
|
|
|
print('saving image to ' + local_file) |
|
66
|
|
|
urllib.request.urlretrieve(self.image_url, local_file) |
|
67
|
|
|
|
|
68
|
|
|
def random_wallpaper(self): |
|
69
|
|
|
print('Picking a random one from the matrix') |
|
70
|
|
|
file_name = random.choice(listdir(self.config['wallpapers_directory'])) |
|
71
|
|
|
self.image = self.config['wallpapers_directory'] + file_name |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
if __name__ == "__main__": |
|
75
|
|
|
random_image = False |
|
76
|
|
|
|
|
77
|
|
|
if len(sys.argv) > 1: |
|
78
|
|
|
random_image = True |
|
79
|
|
|
print(sys.argv[1]) |
|
80
|
|
|
|
|
81
|
|
|
helper = Helper() |
|
82
|
|
|
changer = BackGroundChanger(helper.get_config(), random_image) |
|
83
|
|
|
|
|
84
|
|
|
helper.set_wallpaper(changer.image) |
|
85
|
|
|
helper.clean_up() |
|
86
|
|
|
|
|
87
|
|
|
|