diff --git a/Changer.py b/Changer.py new file mode 100644 index 0000000..2a3732e --- /dev/null +++ b/Changer.py @@ -0,0 +1,183 @@ +import wx +from glob import glob +from os import remove, getcwd, mkdir +from os.path import isfile, getsize, isdir +from subprocess import Popen, CREATE_NEW_CONSOLE, DETACHED_PROCESS + +ids = {} +CONFIG = ".config" +HISTORY = ".history" +DESIGN = { + "Interval Text": (10, 13), + "Interval": (10, 30), + "Save": (11, 63), + "Hidden": (10, 93), + "Safe Mode": (10, 123), + "Clear Cache": (10, 150), + "Cache Information": (120,153), + "Clear History": (10, 180), + "History Information": (120,183), + "Refresh": (10, 210), + "Start": (10, 240) +} + +def integrity_check(): + for i in [1]: + if not isfile(CONFIG): + pass + elif len(open(CONFIG, "r").read().split()) < 4: + pass + else: + break + open(CONFIG, "w").write("600\n1\n0\n1") + print(CONFIG + " is missing") + print("generating " + CONFIG + " with default value of\n Interval = 600 seconds\n Save Wallpapers = True\n Start Hidden = False\n Approximate Time = True") + if not isfile(HISTORY): + open(HISTORY, "w").write("") + print(HISTORY + " is missing") + print("generating " + HISTORY) + if not isdir(".\\Images"): + mkdir(".\\Images") + print(getcwd() + "\\Images is missing") + print("generating " + getcwd() + ".\\Images") + +class ChangerUI(wx.Frame): + + def __init__(self, *args, **kw): + super(ChangerUI, self).__init__(*args, **kw) + + self.InitUI() + + def InitUI(self): + + self.itxt = wx.StaticText(self, pos=DESIGN["Interval Text"], label="Interval:") + + self.Intl = wx.TextCtrl(self, pos=DESIGN["Interval"], style=wx.TE_PROCESS_ENTER, size=(100, 24)) + self.Intl.SetValue(open(CONFIG, "r").read().split()[0]) + ids[self.Intl.GetId()] = "Interval" + + self.Save = wx.CheckBox(self, pos=DESIGN["Save"], label="Save Wallpapers") + ids[self.Save.GetId()] = "Save" + self.Save.SetValue(int(open(CONFIG, "r").read().split()[1])) + + self.Hd = wx.CheckBox(self, pos=DESIGN["Hidden"], label="Start Hidden") + ids[self.Hd.GetId()] = "Hidden" + self.Hd.SetValue(int(open(CONFIG, "r").read().split()[2])) + + self.Ap = wx.CheckBox(self, pos=DESIGN["Safe Mode"], label="Safe Mode") + ids[self.Ap.GetId()] = "Safe" + self.Ap.SetValue(int(open(CONFIG, "r").read().split()[3])) + + self.cl = wx.Button(self, label="Clear Cache", pos=DESIGN["Clear Cache"], size=(100, 28)) + ids[self.cl.GetId()] = "Clear Cache" + self.clh = wx.Button(self, label="Clear History", pos=DESIGN["Clear History"], size=(100, 28)) + ids[self.clh.GetId()] = "Clear History" + self.st = wx.Button(self, label="Start", pos=DESIGN["Start"], size=(100, 28)) + ids[self.st.GetId()] = "Start" + self.rf = wx.Button(self, label="Refresh", pos=DESIGN["Refresh"], size=(100, 28)) + ids[self.rf.GetId()] = "Refresh" + + self.cinf = wx.StaticText( self, + label= str(int(sum([getsize(i) for i in glob("./Images/*.*")]) / 1024 / 10.24) / 100) + " MB, " + str(len(glob("./Images/*.*"))) + " Files", + pos=DESIGN["Cache Information"]) + ids[self.cinf.GetId()] = "Cache Information" + self.hinf = wx.StaticText( self, + label= str(len(open(HISTORY, "r").read().split())) + " Entries", + pos=DESIGN["History Information"]) + ids[self.hinf.GetId()] = "History Information" + + self.Bind(wx.EVT_TEXT_ENTER, self.OnEnter) + self.Bind(wx.EVT_BUTTON, self.OnPress) + self.Bind(wx.EVT_CHECKBOX, self.OnPress) + self.Bind(wx.EVT_FSWATCHER, self.DirChanged) + + self.SetSize((self.cinf.GetSize().x + 140, DESIGN["Start"][1] + 70)) + self.SetTitle('Changer') + self.Centre() + + def DirChanged(self, e): + self.cinf.SetLabel(str(int(sum([getsize(i) for i in glob("./Images/*.*")]) / 1024 / 10.24) / 100) + " MB, " + str(len(glob("./Images/*.*"))) + " Files") + + def OnEnter(self, e): + + if ids[e.GetId()] == "Interval": + try: + float(self.Intl.GetValue()) + fr = open(CONFIG, "r") + c = fr.read().split() + fr.close() + fw = open(CONFIG, "w") + c[0] = self.Intl.GetValue() + fw.write("\n".join(c)) + fw.close() + except: + print("Invalid Value!") + self.Intl.SetValue(open(CONFIG, "r").read().split()[0]) + + def OnPress(self, e): + if ids[e.GetId()] == "Clear Cache": + for i in [i for i in glob("./Images/*.*") if i.split(".")[-1] in ["jpg", "jpeg", "png"]]: + remove(i) + self.cinf.SetLabel(str(int(sum([getsize(i) for i in glob("./Images/*.*")]) / 1024 / 10.24) / 100) + " MB, " + str(len(glob("./Images/*.*"))) + " Files") + self.SetSize((self.cinf.GetSize().x + 140, DESIGN["Start"][1] + 70)) + + if ids[e.GetId()] == "Clear History": + open(".history", "w").write("") + self.hinf.SetLabel(str(len(open(HISTORY, "r").read().split())) + " Entries") + + if ids[e.GetId()] == "Save": + fr = open(CONFIG, "r") + c = fr.read().split() + fr.close() + fw = open(CONFIG, "w") + c[1] = str(int(self.Save.GetValue())) + fw.write("\n".join(c)) + fw.close() + + if ids[e.GetId()] == "Hidden": + fr = open(CONFIG, "r") + c = fr.read().split() + fr.close() + fw = open(CONFIG, "w") + c[2] = str(int(self.Hd.GetValue())) + fw.write("\n".join(c)) + fw.close() + + if ids[e.GetId()] == "Safe": + fr = open(CONFIG, "r") + c = fr.read().split() + fr.close() + fw = open(CONFIG, "w") + c[3] = str(int(self.Ap.GetValue())) + fw.write("\n".join(c)) + fw.close() + + if ids[e.GetId()] == "Start": + if int(open(CONFIG, "r").read().split()[2]): + Popen(["wrapper.exe"], shell=True, creationflags=CREATE_NEW_CONSOLE) + else: + Popen(["wrapper.exe"], shell=True, creationflags=DETACHED_PROCESS) + + if ids[e.GetId()] == "Refresh": + integrity_check() + self.hinf.SetLabel(str(len(open(HISTORY, "r").read().split())) + " Entries") + self.cinf.SetLabel(str(int(sum([getsize(i) for i in glob("./Images/*.*")]) / 1024 / 10.24) / 100) + " MB, " + str(len(glob("./Images/*.*"))) + " Files") + self.SetSize((self.cinf.GetSize().x + 140, DESIGN["Start"][1] + 70)) + self.Intl.SetValue(open(CONFIG, "r").read().split()[0]) + self.Save.SetValue(int(open(CONFIG, "r").read().split()[1])) + self.Hd.SetValue(int(open(CONFIG, "r").read().split()[2])) + self.Ap.SetValue(int(open(CONFIG, "r").read().split()[3])) + + + +def main(): + + integrity_check() + app = wx.App() + cu = ChangerUI(None) + cu.Show() + app.MainLoop() + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/backend.py b/backend.py new file mode 100644 index 0000000..cc90397 --- /dev/null +++ b/backend.py @@ -0,0 +1,118 @@ +from ctypes import windll +from praw import Reddit +from glob import glob +from time import sleep +from time import time +from urllib.request import urlretrieve +from os import getcwd, remove, system +import progressbar +import urllib.request +from imgur_downloader import ImgurDownloader +import _thread +from subprocess import Popen, CREATE_NEW_CONSOLE + +CONFIG = ".config" +CLIENT = ".client" +intrvl = float(open(CONFIG, "r").read().split()[0]) +imgpath = "" +start = time() +client = open(CLIENT, "r").read().split() + +class NoImage(Exception): + pass + +reddit = Reddit(client_id=client[0], + client_secret=client[1], + user_agent='Wallpaper changer by u/Surferlul') + +class MyProgressBar(): + def __init__(self): + self.pbar = None + + def __call__(self, block_num, block_size, total_size): + if not self.pbar: + self.pbar=progressbar.ProgressBar(maxval=total_size) + self.pbar.start() + + downloaded = block_num * block_size + if downloaded < total_size: + self.pbar.update(downloaded) + else: + self.pbar.finish() + +def del_file(path, Seconds=10): + Popen("powershell.exe Sleep(" + str(Seconds) + "); Remove-Item .\\Images\\" + path, shell=True, creationflags=CREATE_NEW_CONSOLE) + # global intrvl + # if int(open(CONFIG, "r").read().split()[2]): + # _thread.start_new_thread ( system, tuple(["powershell -WindowStyle Hidden \"Sleep(" + str(Seconds) + "); Remove-Item .\\Images\\" + path + "\""])) + # else: + # if int(open(CONFIG, "r").read().split()[3]): + # if intrvl < 3: + # intrvl = 3 + # else: + # _thread.start_new_thread ( system, tuple(["start powershell -WindowStyle Hidden \"Sleep(" + str(Seconds) + "); Remove-Item .\\Images\\" + path + "\""])) + +posts = open(".history", "r").read() +for i in reddit.subreddit("wallpaper").hot(limit = 1000): + if i.url not in posts: + submission = i + break + +open(".history", "a").write(submission.url + "\n") +try: + if "imgur" in submission.url: + if submission.url.split("/")[-1].split(".")[-1] not in ["jpg", "jpeg", "png"]: + print("Downoading", submission.url) + else: + print("Downoading", submission.url, "as", submission.url.split("\\")[-1]) + ImgurDownloader( + submission.url, + "./Images/", + file_name=submission.url.split("/")[-1] + if submission.url.split("/")[-1].split(".")[-1] not in ["jpg", "jpeg", "png"] + else submission.url.split("/")[-1].split(".")[0] + ).save_images() + if submission.url.split("/")[-1].split(".")[-1] not in ["jpg", "jpeg", "png"]: + windll.user32.SystemParametersInfoW(20, 0, getcwd() + "\\" + glob("./Images/" + submission.url.split("/")[-1] + ".*")[0], 0) + if not int(open(CONFIG, "r").read().split()[1]): + del_file(glob(submission.url.split("/")[-1] + ".*")[0]) + else: + windll.user32.SystemParametersInfoW(20, 0, getcwd() + "\\Images\\" + submission.url.split("/")[-1] , 0) + if not int(open(CONFIG, "r").read().split()[1]): + del_file(submission.url.split("/")[-1]) + else: + if submission.url.split("/")[-1].split(".")[-1] not in ["jpg", "jpeg", "png"]: + raise NoImage + imgpath = "Images\\" + submission.url.split("/")[-1] + print("Downoading", submission.url, "as", submission.url.split("\\")[-1]) + urlretrieve(submission.url, imgpath, MyProgressBar()) + windll.user32.SystemParametersInfoW(20, 0, getcwd() + "\\" + imgpath, 0) + if not int(open(CONFIG, "r").read().split()[1]): + del_file(submission.url.split("/")[-1]) + + end = time() + + intrvl += start - end + if intrvl >= 0: + pbar = progressbar.ProgressBar(maxval=intrvl) + pbar.start() + if intrvl < 10: + for i in range(1, int(intrvl * 10) + 1): + sleep(intrvl / int(intrvl * 10)) + try: + pbar.update(intrvl / int(intrvl * 10) * i) + except: + pbar.update(int(intrvl)) + print("test") + else: + for i in range(1, 101): + sleep(intrvl / 100) + pbar.update(intrvl / 100 * i) + pbar.finish() + elif int(open(CONFIG, "r").read().split()[3]): + intrvl -= 2 * (start - end) + print("Interval was rounded up to " + str(round(intrvl * 100) / 100) + "s because the download needed longer than the interval.") +except NoImage: + print("Error: Url doesn't point to an Image") +except Exception as E: + print("Error: " + type(E).__name__) \ No newline at end of file diff --git a/wrapper.py b/wrapper.py new file mode 100644 index 0000000..5553fc3 --- /dev/null +++ b/wrapper.py @@ -0,0 +1,40 @@ +from os import mkdir, getcwd +from os.path import isfile, isdir +from subprocess import Popen, call +from time import sleep + +CONFIG = ".config" +HISTORY = ".history" + +def integrity_check(): + for i in [1]: + if not isfile(CONFIG): + pass + elif len(open(CONFIG, "r").read().split()) < 4: + pass + else: + break + open(CONFIG, "w").write("600\n1\n0\n1") + print(CONFIG + " is missing") + print("generating " + CONFIG + " with default value of\n Interval = 600 seconds\n Save Wallpapers = True\n Start Hidden = False\n Approximate Time = True") + if not isfile(HISTORY): + open(HISTORY, "w").write("") + print(HISTORY + " is missing") + print("generating " + HISTORY) + if not isdir(".\\Images"): + mkdir(".\\Images") + print(getcwd() + "\\Images is missing") + print("generating " + getcwd() + ".\\Images") + +while True: + try: + integrity_check() + if int(open(CONFIG, "r").read().split()[3]): + call(["backend.exe"], shell=True) + else: + Popen(["backend.exe"], shell=True) + sleep(float(open(CONFIG, "r").read().split()[0])) + except Exception as E: + print("Error: " + type(E).__name__) + if type(E).__name__ == "KeyboardInterrupt": + break