comparison bing4desktop/bing4desktop.py @ 11:c01d7edb9cb9

bing4desktop-0.240206-0
author prymula <prymula76@outlook.com>
date Tue, 06 Feb 2024 11:58:47 +0100
parents ed9556d0c5d7
children 73fd89b46da4
comparison
equal deleted inserted replaced
10:ed9556d0c5d7 11:c01d7edb9cb9
18 import os, sys, subprocess 18 import os, sys, subprocess
19 import requests 19 import requests
20 import json 20 import json
21 import tempfile 21 import tempfile
22 import shutil 22 import shutil
23 23 import glob
24 ver = "0.240205-4" 24
25 ver = "0.240206-0"
25 26
26 BING_URI_BASE = "http://www.bing.com" 27 BING_URI_BASE = "http://www.bing.com"
27 BING_WALLPAPER_PATH = "/HPImageArchive.aspx?format=js&idx=50&n=1&mkt=en-US" 28 BING_WALLPAPER_PATH = "/HPImageArchive.aspx?format=js&idx=1&n=1&mkt=en-US"
28 full_path="" 29 full_path=""
29 actualImage = -1 30 actualImage = -1
30 COPYRIGHT_TXT = "" 31 COPYRIGHT_TXT = ""
32 TITLE = ""
31 33
32 tmp_path = tempfile.mkdtemp() 34 tmp_path = tempfile.mkdtemp()
33
34
35 35
36 class WPaper: 36 class WPaper:
37 def get(self): 37 def get(self):
38 global full_path, COPYRIGHT_TXT 38 global full_path, COPYRIGHT_TXT, TITLE
39 # open the Bing HPImageArchive URI and ask for a JSON response 39 # open the Bing HPImageArchive URI and ask for a JSON response
40 resp = requests.get(BING_URI_BASE + BING_WALLPAPER_PATH) 40 resp = requests.get(BING_URI_BASE + BING_WALLPAPER_PATH)
41 41
42 if resp.status_code == 200: 42 if resp.status_code == 200:
43 json_response = json.loads(resp.content) 43 json_response = json.loads(resp.content)
44 wallpaper_path = json_response['images'][0]['url'] 44 wallpaper_path = json_response['images'][0]['url']
45 COPYRIGHT_TXT = json_response['images'][0]['copyright'] 45 COPYRIGHT_TXT = json_response['images'][0]['copyright']
46 TITLE = json_response['images'][0]['title']
47
46 filename = wallpaper_path.split('/')[-1] 48 filename = wallpaper_path.split('/')[-1]
47 wallpaper_uri = BING_URI_BASE + wallpaper_path 49 wallpaper_uri = BING_URI_BASE + wallpaper_path
48 filename_short = filename.split('&') 50 filename_short = filename.split('&')
49 print ("filemname_short", filename_short[0]) 51 print ("filemname_short", filename_short[0])
50 # open the actual wallpaper uri, and write the response as an image on the filesystem 52 # open the actual wallpaper uri, and write the response as an image on the filesystem
171 sys.stderr.write("ERROR: Failed to set wallpaper. There might be a bug.\n") 173 sys.stderr.write("ERROR: Failed to set wallpaper. There might be a bug.\n")
172 return False 174 return False
173 175
174 176
175 class Gui(): 177 class Gui():
176 global COPYRIGHT_TXT 178 global COPYRIGHT_TXT, TITLE
177 179
178 def __init__(self): 180 def __init__(self):
179 self.window = tk.Tk() 181 self.window = tk.Tk()
180 self.image = Image.open("bing.ico") 182 self.image = None
183 for ico in ['./bing.ico', '/usr/share/bing4desktop/bing.ico']:
184 try:
185 self.image = Image.open(ico)
186 except FileNotFoundError as fs:
187 print ('W bieżącym katalogu, brak ikony, ', fs)
188 finally:
189 break
190
191 #if self.image != None:
192 # break
193
194 self.tmp = None
181 self.menu = ( 195 self.menu = (
182 item("Ustaw Tapetę", lambda icon, item: wp.set(full_path, True)), 196 item("Ustaw Tapetę", lambda icon, item: wp.set(full_path, True)),
183 item('Tapeta poprzednia', lambda icon, item: self.change_pic(1)), 197 item('Tapeta poprzednia', lambda icon, item: self.change_pic(1)),
184 item('Tapeta następna', lambda icon, item: self.change_pic(-1)), 198 item('Tapeta następna', lambda icon, item: self.change_pic(-1)),
185 item('Opis zdjęcia', lambda icon, item: icon.notify(COPYRIGHT_TXT)), 199 item('Opis zdjęcia', lambda icon, item: icon.notify(TITLE + ' --- ' + COPYRIGHT_TXT)),
186 item('O Programie', lambda icon, item: self.about()), 200 item('O Programie', lambda icon, item: self.about()),
187 201
188 item('Wyjdź', self.quit_window) 202 item('Wyjdź', self.quit_window)
189 ) 203 )
190 self.frame = tk.Frame(self.window, width=600, height=400) 204 self.frame = tk.Frame(self.window, width=600, height=400)
202 messagebox.showinfo("Bing4Desktop","Inspracja:\npystray.readthedocs.io/en/latest/usage.html\n\nNapisał: Prymula\n\nLuty 2024\n\nLicencja:\nPublic Domain\n\nWersja: "+ver) 216 messagebox.showinfo("Bing4Desktop","Inspracja:\npystray.readthedocs.io/en/latest/usage.html\n\nNapisał: Prymula\n\nLuty 2024\n\nLicencja:\nPublic Domain\n\nWersja: "+ver)
203 # możliwe że nieprawidlowe 217 # możliwe że nieprawidlowe
204 self.window.destroy() 218 self.window.destroy()
205 self.window.update() 219 self.window.update()
206 220
207
208 def change_pic(self, n): 221 def change_pic(self, n):
209 global actualImage, BING_WALLPAPER_PATH 222 global actualImage, BING_WALLPAPER_PATH
210 BASE = "/HPImageArchive.aspx?format=js&idx=" 223 BASE = "/HPImageArchive.aspx?format=js&idx="
211 OFF = "&n=1&mkt=en-US" 224 OFF = "&n=1&mkt=en-US"
212 225
214 if (actualImage < 0): 227 if (actualImage < 0):
215 actualImage = 10 228 actualImage = 10
216 elif (actualImage > 10): 229 elif (actualImage > 10):
217 actualImage = 0 230 actualImage = 0
218 231
219 print ("FULL: ", len(full_path))
220
221 wp=WPaper() 232 wp=WPaper()
222 if wp.get(): 233 if wp.get():
223 BING_WALLPAPER_PATH = BASE+str(actualImage)+OFF 234 BING_WALLPAPER_PATH = BASE+str(actualImage)+OFF
224 if wp.get() == False: 235 if wp.get() == False:
225 cleanup() 236 cleanup()
226 exit 237 exit
227 238
239 # Wszystko pięknie tylko metoda 'show', zmienia nazwę pliku przechowywanego w TMP
240 # Pozatym nie ubija a nie znalazłem nic lepszego
241 print ("GLOB: ", glob.glob( os.path.join('/tmp', '*.PNG')))
242 for infile in glob.glob( os.path.join('/tmp', '*.PNG')):
243 print ("INFILE: ", infile)
244 viewer = subprocess.Popen(['/usr/bin/eom', infile])
245 viewer.terminate()
246 viewer.kill() # make sure the viewer is gone; not needed on Windows
247
228 img = Image.open(full_path) #.convert('RGBA') # PIL solution 248 img = Image.open(full_path) #.convert('RGBA') # PIL solution
229 tmp = img.resize((600, 400), None, None, 3.0) 249 self.tmp = img.resize((600, 400), None, None, 3.0)
230 tmp.show() 250 self.tmp.show()
231 251
232 def quit_window(self): 252 def quit_window(self):
233 self.icon.stop() 253 self.icon.stop()
234 self.window.destroy() 254 self.window.destroy()
235
236 255
237 def show_window(self): 256 def show_window(self):
238 self.icon.stop() 257 self.icon.stop()
239 self.window.protocol('WM_DELETE_WINDOW', self.withdraw_window) 258 self.window.protocol('WM_DELETE_WINDOW', self.withdraw_window)
240 self.window.after(0, self.window.deiconify) 259 self.window.after(0, self.window.deiconify)
241
242 260
243 def withdraw_window(self): 261 def withdraw_window(self):
244 self.window.withdraw() 262 self.window.withdraw()
245 self.icon = pystray.Icon("name", self.image, "title", self.menu) 263 self.icon = pystray.Icon("name", self.image, "title", self.menu)
246 self.icon.run() 264 self.icon.run()