1
|
|
|
import os |
2
|
|
|
from configparser import ConfigParser |
3
|
|
|
from os import listdir |
4
|
|
|
import gi |
5
|
|
|
gi.require_version("Notify", "0.7") |
6
|
|
|
from gi.repository import Notify, GdkPixbuf |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class Helper(object): |
10
|
|
|
def get_config(self): |
11
|
|
|
config_file_path = os.path.dirname(os.path.realpath(__file__)) + '/config.ini' |
12
|
|
|
if not os.path.exists(config_file_path): |
13
|
|
|
return self.check_config(None) |
14
|
|
|
|
15
|
|
|
config_reader = ConfigParser() |
16
|
|
|
config_reader.read(config_file_path) |
17
|
|
|
|
18
|
|
|
config = {} |
19
|
|
|
config['wallpapers_directory'] = config_reader.get('settings', 'wallpapers_directory') |
20
|
|
|
config['refresh_rate'] = config_reader.get('settings', 'refresh_rate') |
21
|
|
|
config['subs'] = config_reader.get('settings', 'subs') |
22
|
|
|
config['only_local'] = config_reader.get('settings', 'only_local') |
23
|
|
|
config['over_18'] = config_reader.get('settings', 'allow_nsfw') |
24
|
|
|
|
25
|
|
|
if not config['subs'] == "": |
26
|
|
|
config['subs'] = config['subs'].split(',') |
27
|
|
|
|
28
|
|
|
return self.check_config(config) |
29
|
|
|
|
30
|
|
|
def check_config(self, config): |
31
|
|
|
if config is None: |
32
|
|
|
config = {} |
33
|
|
|
config['wallpapers_directory'] = '~/Pictures/RedditWallpapers/' |
34
|
|
|
config['refresh_rate'] = 120 |
35
|
|
|
config['subs'] = ['wallpapers'] |
36
|
|
|
config['only_local'] = False |
37
|
|
|
config['over_18'] = False |
38
|
|
|
|
39
|
|
|
config['wallpapers_directory'] = config['wallpapers_directory'].replace('~', os.path.expanduser("~")) |
40
|
|
|
os.system("mkdir -p " + config['wallpapers_directory']) |
41
|
|
|
|
42
|
|
|
if len(config['subs']) <= 0: |
43
|
|
|
config['subs'] = ['wallpapers'] |
44
|
|
|
|
45
|
|
|
if not str(config['refresh_rate']).isdigit(): |
46
|
|
|
config['refresh_rate'] = 120 |
47
|
|
|
|
48
|
|
|
if config['only_local'] == '': |
49
|
|
|
config['only_local'] = False |
50
|
|
|
|
51
|
|
|
if config['over_18'] == '': |
52
|
|
|
config['over_18'] = False |
53
|
|
|
|
54
|
|
|
return config |
55
|
|
|
|
56
|
|
|
def set_wallpaper(self, image): |
57
|
|
|
config = self.get_config() |
58
|
|
|
print('loading new wallpaper' + image) |
59
|
|
|
os.system("gsettings set org.gnome.desktop.background picture-uri file://{}/{}".format(config['wallpapers_directory'], image)) |
60
|
|
|
self.send_notifaction() |
61
|
|
|
|
62
|
|
|
def send_notifaction(self): |
63
|
|
|
Notify.init("Wallpaper Changer") |
64
|
|
|
notification = Notify.Notification.new( |
65
|
|
|
'New wallpaper has been set!', |
66
|
|
|
#'this is the body', |
67
|
|
|
) |
68
|
|
|
|
69
|
|
|
image = GdkPixbuf.Pixbuf.new_from_file(os.path.dirname(os.path.realpath(__file__)) + '/alien.png') |
70
|
|
|
notification.set_image_from_pixbuf(image) |
71
|
|
|
|
72
|
|
|
notification.show() |
73
|
|
|
|
74
|
|
|
def clean_up(self): |
75
|
|
|
config = self.get_config() |
76
|
|
|
all_files = listdir(config['wallpapers_directory']) |
77
|
|
|
|
78
|
|
|
for image in all_files: |
79
|
|
|
file = config['wallpapers_directory'] + image |
80
|
|
|
|
81
|
|
|
if os.stat(file).st_size <= 503: |
82
|
|
|
os.remove(file) |
83
|
|
|
|
84
|
|
|
|