-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.py
318 lines (276 loc) · 14 KB
/
funcs.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# func.py
import json
import urllib.request
import webbrowser
import os
import random
import logging
import shutil
from datetime import datetime
from config import SETTINGS as s, debug, debugNight, debugNotify
from imgurpython import ImgurClient
def logging_init():
if debug:
logging.basicConfig(filename='console.log', level=logging.DEBUG) # Setup logging
file = open("console.log", "r+")
file.truncate(0)
file.close()
log('[Init] RedditBackgroundChanger Log Initialised.')
if not debug and os.path.exists('console.log'):
os.remove('console.log')
def log(message):
if not debug: return
now = datetime.now()
print(f'{now.strftime("%H:%M:%S [%d.%m.%Y]")} | {message}')
logging.debug(f'{now.strftime("%H:%M:%S [%d.%m.%Y]")} | {message}')
def jsonFetch(subreddit):
log(f'[jsonFetch] Running')
log(f'[jsonFetch] Subreddit: {subreddit}')
req = urllib.request.Request(f'https://www.reddit.com/r/{subreddit}/top.json', headers={
'User-agent': 'Reddit Background Setter (Created by u/Core_UK and u/Member87)'})
res = urllib.request.urlopen(req)
j = json.load(res)
return j
def Notify(msg, title, link, icon, path):
def linkOpen():
if not link:
from PIL import Image
img = Image.open(path)
img.show()
else:
webbrowser.open_new(link)
from win10toast_click import ToastNotifier
log(f'[Notify] -------------------------------------NEW NOTIFICATION------------------------------------')
log(f'[Notify] Running')
log(f'[Notify] Message: {msg}')
log(f'[Notify] Title: {title}')
log(f'[Notify] Link: {link}')
log(f'[Notify] Icon: {icon}')
log(f'[Notify] Path: {path}')
log(f'[Notify] -----------------------------------------------------------------------------------------')
toaster = ToastNotifier()
toaster.show_toast(msg, title, icon_path=icon,
duration=None, threaded=True, callback_on_click=linkOpen)
def IsNight(city):
if not s['night-backgrounds']['toggle']: return False
from astral.sun import sun
from astral import LocationInfo
log(f'[IsNight] ----------------------------------------------------------------------------------------')
log('[IsNight] Running')
city = LocationInfo(city)
sunTime = sun(city.observer, date=datetime.now())
sunset = sunTime['sunset'].time()
sunrise = sunTime['sunrise'].time()
currentTime = datetime.now().time()
log(f'[IsNight] The current time is {currentTime}')
log(f'[IsNight] Sunset is at {sunset}')
log(f'[IsNight] Sunrise is at {sunrise}')
if debugNight:
log(f'[IsNight] Debug Override')
log(f'[IsNight] Output: {True}')
log(f'[IsNight] ----------------------------------------------------------------------------------------')
return True
if currentTime >= sunset or currentTime <= sunrise:
log(f'[IsNight] Output: {True}')
log(f'[IsNight] ----------------------------------------------------------------------------------------')
return True
log(f'[IsNight] ----------------------------------------------------------------------------------------')
log(f'[IsNight] Output: {False}')
return False
def ImageFilter(x, y, data, night):
FileType = (data['url_overridden_by_dest'].split('/')[-1]).split('.')[-1]
FileID = (data['url_overridden_by_dest'].split('/')[-1]).split('.')[-2]
ExistCheckActive = os.path.join(s['active-path'], data['url_overridden_by_dest'].split('/')[-1])
ExistCheckDownload = os.path.join(s['downloaded-path'], data['url_overridden_by_dest'].split('/')[-1])
log(f'[ImageFilter] ----------------------------------------NEW IMAGE-----------------------------------')
log(f'[ImageFilter][New Filter] FileType: {FileType}')
log(f'[ImageFilter][New Filter] FileID: {FileID}')
if not x > y:
log(f'[ImageFilter][Landscape Check] Failed for {FileID}')
log(f'[ImageFilter][Landscape Check] Got {x}x{y}')
log(f'[ImageFilter] ------------------------------------------------------------------------------------')
return False
if not x >= s['res']['monitor-x']:
log(f'[ImageFilter][Resolution Check - X] Failed for {FileID}')
log(f'[ImageFilter][Resolution Check - X] Got {x} | Needed {s["res"]["monitor-x"]}')
log(f'[ImageFilter] ------------------------------------------------------------------------------------')
return False
if not y >= s['res']['monitor-y']:
log(f'[ImageFilter][Resolution Check - Y] Failed for {FileID}')
log(f'[ImageFilter][Resolution Check - Y] Got {y} | Needed {s["res"]["monitor-y"]}')
log(f'[ImageFilter] ------------------------------------------------------------------------------------')
return False
if FileType != 'jpeg' and FileType != 'png' and FileType != 'jpg':
log(f'[ImageFilter][File Type Check] Failed for {FileID}')
log(f'[ImageFilter][File Type Check] Got {FileType}')
log(f'[ImageFilter] ------------------------------------------------------------------------------------')
return False
if (s['diff-bg'] or (night and s['night-backgrounds']['diff-bg'])) and (os.path.exists(ExistCheckActive) or os.path.exists(ExistCheckDownload)):
log(f'[ImageFilter][File Exists Check] Failed for {FileID}')
log(f'[ImageFilter] ------------------------------------------------------------------------------------')
return False
for v in s['blacklist']:
if FileID == v:
log(f'[ImageFilter] Blacklist Check Failed for {FileID}')
log(f'[ImageFilter] ------------------------------------------------------------------------------------')
return False
log(f'[ImageFilter][Filter Passed] {FileID} Passed Checks.')
log(f'[ImageFilter] ------------------------------------------------------------------------------------')
return True
def DownloadImage(url, FileName):
PathDownloaded = os.path.join(s['downloaded-path'], FileName)
PathActive = os.path.join(s['active-path'], FileName)
log(f'[DownloadImage] ---------------------------------------NEW IMAGE----------------------------------')
log('[DownloadImage] Running')
if os.path.exists(PathDownloaded):
log(f'[DownloadImage] Output: {PathDownloaded}')
log(f'[DownloadImage] Exists: {True}')
log(f'[DownloadImage] ----------------------------------------------------------------------------------')
return PathDownloaded, True
if os.path.exists(PathActive):
log(f'[DownloadImage] Output: {PathActive}')
log(f'[DownloadImage] Exists: {True}')
log(f'[DownloadImage] ----------------------------------------------------------------------------------')
return PathActive, True
log(f'[DownloadImage] Action: Downloading Image from {url}')
urllib.request.urlretrieve(url, PathDownloaded)
log(f'[DownloadImage] Output: {PathDownloaded}')
log(f'[DownloadImage] Exists: {False}')
log(f'[DownloadImage] ----------------------------------------------------------------------------------')
return PathDownloaded, False
def FetchImageFromDirectory():
log('[FetchImageFromDirectory] Running')
FileName = random.choice(os.listdir(s['custom-path']))
Path = s['custom-path'] + FileName
log(f'[FetchImageFromDirectory] Output: {Path}')
return Path
def FetchImageFromLink():
log('[FetchImageFromLink] Running')
linkList = s['night-backgrounds']['methods']['links']
if (s['imgur']['client_id'] and s['imgur']['client_secret']):
client = ImgurClient(s['imgur']['client_id'], s['imgur']['client_secret'])
imgurAlbum = []
for value in linkList:
val = value.split(':')
if val[0] != 'http' or val[0] != 'https':
try:
a = client.get_album(value)
log(f'[FetchImageFromLink][ImgurAlbum] Found Album: {a.title}')
imgurAlbum.append(a.id)
linkList.remove(value)
except:
continue
current = 0
for v in imgurAlbum:
for image in client.get_album_images(v):
log(f'[FetchImageFromLink][ImgurAlbum] Adding {image.link} to Links list.')
linkList.append(image.link)
current = current + 1
if current >= s['imgur']['album_max']:
break
ChosenLink = random.choice(linkList)
FileName = ChosenLink.split('/')[-1]
Path, Exists = DownloadImage(ChosenLink, FileName)
log(f'[FetchImageFromLink] Output: {Path} [{ChosenLink}]')
log(f'[FetchImageFromLink] Exists: {Exists}')
return Path, Exists, ChosenLink
def FetchImageFromReddit(j, Night):
log('[FetchImageFromReddit] Running')
url = None
current = 0
searchLimit = len(j['data']['children'])
log(f'[FetchImageFromReddit] Starting File Search for Image with Correct Parameters. SearchLimit: {searchLimit}')
while not url:
Data = j['data']['children'][current]['data']
try:
img = Data['preview']['images'][0]['source']
FoundURL = Data['url_overridden_by_dest']
if ImageFilter(img['width'], img['height'], Data, Night):
url = FoundURL
else:
current = current + 1
except:
current = current + 1
log(f'[FetchImageFromReddit] Current: {current}')
if current >= searchLimit:
log('[FetchImageFromReddit] Exceeded searchLimit - 1st Image being set as background.')
url = j['data']['children'][0]['data']['url_overridden_by_dest']
FileName = url.split('/')[-1]
Path, Exists = DownloadImage(url, FileName)
if debugNotify or (not Exists and s['notify']):
Notify(f"New Background from {Data['subreddit']}", f"{Data['title']}", f'https://reddit.com{Data["permalink"]}',
"bin/reddit.ico", False)
log(f'[FetchImageFromReddit] ---------------------------IMAGE DETAILS-----------------------------------')
log(f'[FetchImageFromReddit] FileName: {FileName}')
log(f'[FetchImageFromReddit] Path: {Path}')
log(f'[FetchImageFromReddit] Link: {url} | Permanent: https://reddit.com{Data["permalink"]}')
log(f'[FetchImageFromReddit] Output: Image Path ({Path}), File Exists ({Exists}), ImageData')
log(f'[FetchImageFromReddit] ---------------------------------------------------------------------------')
return Path
def FetchMethod(method):
method = method.lower()
if method == 'all':
methodList = []
if s['night-backgrounds']['methods']['links']: methodList.append('link')
if s['night-backgrounds']['methods']['subreddits']: methodList.append('subreddit')
if s['night-backgrounds']['methods']['local'] and os.listdir(s['custom-path']): methodList.append('local')
method = random.choice(methodList)
return method
def NightImageFetch(method):
log(f'[NightImageFetch] --------------------------------NIGHT IMAGE FETCH-------------------------------')
log('[NightImageFetch] Running')
log(f'[NightImageFetch] Method: {method}')
Path, Exists, Data, Title, Source, Link = False, False, False, False, False, False
if method == 'local':
Path = FetchImageFromDirectory()
Source = 'Custom Backgrounds'
Title = Path.split('\\')[-1]
if method == 'subreddit':
Path, Exists, Data = FetchImageFromReddit(jsonFetch(random.choice(s['night-backgrounds']['methods']['subreddits'])), True)
Source = Data['subreddit']
Title = Data['title']
Link = Data['url_overridden_by_dest']
if method == 'link':
Path, Exists, Data = FetchImageFromLink()
Source = (((Data.split('//')[-1]).split('/')[-2])[2:])
Title = Path.split('\\')[-1]
Link = Data
if debugNotify or (not Exists and s['night-backgrounds']['notify']):
Notify(f"New Background from {Source}", Title, Link, None, Path)
log(f'[NightImageFetch] --------------------------------------------------------------------------------')
log(f'[NightImageFetch] Output: {Path} [{Link}]')
log(f'[NightImageFetch] Exists: {Exists}')
log(f'[NightImageFetch] --------------------------------------------------------------------------------')
return Path
def IsCurrentBackground(Path):
log(f'[IsCurrentBackground] ----------------------------------NEW CHECK---------------------------------')
log(f'[IsCurrentBackground] Running')
FileName = Path.split('\\')[-1]
for file in os.listdir(s['active-path']):
log(f'[IsCurrentBackground] Current File: {file}')
log(f'[IsCurrentBackground] Searching For: {FileName}')
if file != FileName:
if "custom" in file.split('_'):
shutil.move(os.path.join(s['active-path'], file), os.path.join(s['custom-path'], file))
log(f"[IsCurrentBackground] Moved {file} to Custom-Path from Active-Path")
else:
shutil.move(os.path.join(s['active-path'], file), os.path.join(s['downloaded-path'], file))
log(f"[IsCurrentBackground] Moved {file} to Downloaded-Path from Active-Path")
else:
log(f"[IsCurrentBackground] {file} is the current active background.")
log(f'[IsCurrentBackground] ----------------------------------------------------------------------------')
return os.path.join(s['active-path'], file)
NewPath = os.path.join(s['active-path'], FileName)
shutil.move(Path, NewPath)
if "custom" in FileName.split('_'):
log(f"[IsCurrentBackground] Moved {FileName} to Active-Path from Custom-Path")
else:
log(f"[IsCurrentBackground] Moved {FileName} to Active-Path from Downloaded-Path")
log(f'[IsCurrentBackground] ----------------------------------------------------------------------------')
return NewPath
def GetCurrentBackground(activePath):
Files = []
for f in os.listdir(activePath):
Files.append(f)
if len(Files) == 1:
return Files[0]