22import requests
33import shutil
44import ctypes
5+ import ctypes .wintypes
56import os
7+ import time
8+ from win32com .shell import shell , shellcon
69from WallDit_QT import *
710
811# All hope is lost, abandon now
912
10- counter = 0
13+ counter = 0 # Counter for checking how many posts that don't fit parameters were skipped
1114
1215user_agent = "windows/linux:WallDit:v1 (by /u/FilthyPeasantt)"
1316r = praw .Reddit (user_agent = user_agent )
@@ -38,10 +41,9 @@ def is_ok_submission_url(window, submission, link_search_limit):
3841 elif counter == link_search_limit :
3942 window .handle_status_label ("Error: Submission are all invalid, up the counter or try again" )
4043 else :
41- print ("Submission: no errors.\n \n " )
4244 return True
4345
44- # Grabs link
46+ # Gets links that meet the specified parameter requirements
4547def get_link (window ):
4648 link_search_limit = window .handle_post_spinbox () # how many links it's gonna search for images that fit the criteria till it gives up and dies in a fire
4749
@@ -60,15 +62,16 @@ def get_link(window):
6062 return submission .url
6163
6264# Downloads image
63- def get_image_download (window ):
65+ def get_image_download (window , image_name ):
6466 url = get_link (window )
6567 window .handle_status_label ("Downloading image" )
6668
6769 if ('i.' not in url and 'imgur' in url ) and url :
6870 url += ".png"
6971
7072 response = requests .get (url , stream = True )
71- with open ('DownloadedImage.png' , 'wb' ) as out_file :
73+
74+ with open (image_name , 'wb' ) as out_file :
7275 shutil .copyfileobj (response .raw , out_file )
7376 del response
7477 return url
@@ -87,14 +90,37 @@ def check_connectivity(window):
8790 window .handle_status_label ("No internet connection is available" )
8891 return False
8992
93+ # Seriously, don't ask, yet another windows function
94+ def get_path_to_folder ():
95+ desktop_pidl = shell .SHGetFolderLocation (0 , shellcon .CSIDL_DESKTOP , 0 , 0 )
96+ pidl , display_name , image_list = shell .SHBrowseForFolder (0 , desktop_pidl , "Choose a folder" , 0 , None , None )
97+
98+ return shell .SHGetPathFromIDList (pidl )
99+
100+ def save_image (window ):
101+ date = time .strftime ("%Y-%d-%m-%H-%M" )
102+ image_ext = ".png"
103+ image = "DownloadedImage" + date + image_ext
104+
105+ # Adds the date and time to the downloaded image, before moving it to the Pictures directory
106+ os .rename ("DownloadedImage.png" , image )
107+
108+ cwd = os .getcwd ()
109+ path = os .path .join (cwd , image )
110+
111+ # Copies image to a dir
112+ shutil .copyfile (path , get_path_to_folder ())
113+
90114# Sets downloaded image as wallpaper
91115def set_wallpaper (window ):
92116 cwd = os .getcwd ()
93- path = os .path .join (cwd , "DownloadedImage.png" )
94- url = get_image_download (window )
117+
118+ image_name = "DownloadedImage.png"
119+ path = os .path .join (cwd , image_name )
120+ url = get_image_download (window , image_name )
95121 window .handle_progress_bar (30 )
96122 window .handle_status_label ("Setting image as desktop background..." )
97- if ctypes .windll .user32 .SystemParametersInfoW (20 , 0 , path , 0 ): # Runs on magical pony farts , do not touch
123+ if ctypes .windll .user32 .SystemParametersInfoW (20 , 0 , path , 0 ): # Runs on magic , do not touch
98124 window .handle_progress_bar (30 )
99125 window .handle_status_label ("Desktop background set successfully." )
100126 else :
0 commit comments