forked from NITDgpOS/UIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.py
58 lines (50 loc) · 1.73 KB
/
scheduler.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
from setWallpaper import change_background
import os
from constants import CURR_DIR,PICS_FOLDER,WEBSITE,TIMEOUT
import random
import time
from scrape import get_images
from threading import Thread
class scheduler():
def __init__(self, offline):
directory = os.path.join(CURR_DIR,PICS_FOLDER)
print (time.time())
if not offline:
fetch = Thread(target=self.initFetch)
fetch.setDaemon(True) # all child threads need to be daemons to die upon main thread exit
fetch.start()
while not os.path.isdir(os.path.join(CURR_DIR,PICS_FOLDER)):
print('Downloading images..')
time.sleep(60)
elif not os.path.exists(directory):
os.makedirs(directory)
self.change_random()
self.setStartTime(time.time())
self.changeCycle()
def initFetch(self):
try:
get_images(WEBSITE)
except ValueError as e:
print("File could not be retrieved.", e)
def change_random(self):
directory = os.path.join(CURR_DIR,PICS_FOLDER)
filename = random.choice(os.listdir(directory))
path = os.path.join(directory, filename)
print("changing desktop wallpaper to: " ,path)
change_background(path)
def changeCycle(self):
while True:
delta = self.deltaTime()
if delta>=TIMEOUT:
self.change_random()
self.time=time.time()
else:
time.sleep(TIMEOUT-delta)
def setStartTime(self,time):
self.time=time
def deltaTime(self):
return (time.time()-self.time)
def checkTime(self):
if self.deltaTime()>30:
return True
return False