annotate bing4desktop/bing4desktop.py @ 12:74d743cf58cb

bing4desktop-0.240206-0_clean
author prymula <prymula76@outlook.com>
date Tue, 06 Feb 2024 12:01:38 +0100
parents c01d7edb9cb9
children 73fd89b46da4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
1 #!/usr/bin/env python3
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
2 # bing4desktop.py - skrypt tray dla Cinnamon oraz Matte i innych WM
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
3 # inspiracja: https://pystray.readthedocs.io/en/latest/usage.html
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
4 # oraz 'zasłyszane' w internecie :)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
5 # licencja: Public Domain
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
6 # edytor: Geany
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
7 # data: 05-II-2024
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
8 # napisał: Prymula
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
9
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
10 # python3-pillow-tk
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
11
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
12 import pystray
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
13 from pystray import Icon as icon, Menu as menu, MenuItem as item
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
14 import tkinter as tk
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
15 from tkinter import messagebox
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
16 from PIL import ImageTk, Image
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
17
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
18 import os, sys, subprocess
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
19 import requests
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
20 import json
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
21 import tempfile
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
22 import shutil
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
23 import glob
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
24
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
25 ver = "0.240206-0"
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
26
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
27 BING_URI_BASE = "http://www.bing.com"
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
28 BING_WALLPAPER_PATH = "/HPImageArchive.aspx?format=js&idx=1&n=1&mkt=en-US"
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
29 full_path=""
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
30 actualImage = -1
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
31 COPYRIGHT_TXT = ""
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
32 TITLE = ""
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
33
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
34 tmp_path = tempfile.mkdtemp()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
35
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
36 class WPaper:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
37 def get(self):
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
38 global full_path, COPYRIGHT_TXT, TITLE
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
39 # open the Bing HPImageArchive URI and ask for a JSON response
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
40 resp = requests.get(BING_URI_BASE + BING_WALLPAPER_PATH)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
41
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
42 if resp.status_code == 200:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
43 json_response = json.loads(resp.content)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
44 wallpaper_path = json_response['images'][0]['url']
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
45 COPYRIGHT_TXT = json_response['images'][0]['copyright']
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
46 TITLE = json_response['images'][0]['title']
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
47
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
48 filename = wallpaper_path.split('/')[-1]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
49 wallpaper_uri = BING_URI_BASE + wallpaper_path
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
50 filename_short = filename.split('&')
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
51 print ("filemname_short", filename_short[0])
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
52 # open the actual wallpaper uri, and write the response as an image on the filesystem
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
53 response = requests.get(wallpaper_uri)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
54 if resp.status_code == 200:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
55 desktop_env = os.environ.get('DESKTOP_SESSION')
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
56 if desktop_env=="windows":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
57 messagebox.showinfo("Błąd","Brak wsparcia dla Windows i Mac !")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
58 cleanup()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
59 exit
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
60 else:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
61 full_path = tmp_path + filename_short[0]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
62 with open(full_path, 'wb') as f:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
63 f.write(response.content)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
64 else:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
65 raise ValueError("[ERROR] non-200 response from Bing server for '{}'".format(wallpaper_uri))
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
66 COPYRIGHT_TXT = "[ERROR] non-200 response from Bing server for " + str(format(wallpaper_uri))
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
67 return False
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
68 else:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
69 raise ValueError("[ERROR] non-200 response from Bing server for '{}'".format(BING_URI_BASE + BING_WALLPAPER_PATH))
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
70 COPYRIGHT_TXT = "[ERROR] non-200 response from Bing server for " + str(format(BING_URI_BASE + BING_WALLPAPER_PATH))
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
71 return False
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
72 return True
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
73
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
74 def set(self,file_loc, first_run):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
75 global full_path
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
76 desktop_env = os.environ.get('DESKTOP_SESSION')
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
77
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
78 if len(full_path) == 0:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
79 messagebox.showinfo("Błąd","Niewybrano obrazu, najpierw wybierz obraz")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
80 return
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
81 try:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
82 if desktop_env in ["gnome-classic", "gnome", "unity", "default"]:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
83 uri = "'file://%s'" % file_loc
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
84 try:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
85 SCHEMA = "org.gnome.desktop.background"
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
86 KEY = "picture-uri"
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
87 gsettings = Gio.Settings.new(SCHEMA)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
88 gsettings.set_string(KEY, uri)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
89 except:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
90 args = ["gsettings", "set", "org.gnome.desktop.background", "picture-uri", uri]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
91 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
92 elif desktop_env in ["cinnamon"]:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
93 uri = "'file://%s'" % file_loc
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
94 try:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
95 SCHEMA = "org.cinnamon.desktop.background"
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
96 KEY = "picture-uri"
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
97 gsettings = Gio.Settings.new(SCHEMA)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
98 gsettings.set_string(KEY, uri)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
99 except:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
100 args = ["gsettings", "set", "org.cinnamon.desktop.background", "picture-uri", uri]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
101 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
102 elif desktop_env=="mate":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
103 try: # MATE >= 1.6
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
104 # info from http://wiki.mate-desktop.org/docs:gsettings
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
105 args = ["gsettings", "set", "org.mate.background", "picture-filename", "'%s'" % file_loc]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
106 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
107 except: # MATE < 1.6
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
108 # From https://bugs.launchpad.net/variety/+bug/1033918
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
109 args = ["mateconftool-2","-t","string","--set","/desktop/mate/background/picture_filename",'"%s"' %file_loc]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
110 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
111 elif desktop_env=="gnome2": # Not tested
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
112 # From https://bugs.launchpad.net/variety/+bug/1033918
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
113 args = ["gconftool-2","-t","string","--set","/desktop/gnome/background/picture_filename", '"%s"' %file_loc]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
114 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
115 ## KDE4 is difficult
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
116 ## see http://blog.zx2c4.com/699 for a solution that might work
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
117 elif desktop_env in ["kde3", "trinity"]:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
118 # From http://ubuntuforums.org/archive/index.php/t-803417.html
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
119 args = 'dcop kdesktop KBackgroundIface setWallpaper 0 "%s" 6' % file_loc
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
120 subprocess.Popen(args,shell=True)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
121
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
122 elif desktop_env=="xfce4":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
123 #From http://www.commandlinefu.com/commands/view/2055/change-wallpaper-for-xfce4-4.6.0
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
124 if first_run:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
125 args0 = ["xfconf-query", "-c", "xfce4-desktop", "-p", "/backdrop/screen0/monitor0/image-path", "-s", file_loc]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
126 args1 = ["xfconf-query", "-c", "xfce4-desktop", "-p", "/backdrop/screen0/monitor0/image-style", "-s", "3"]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
127 args2 = ["xfconf-query", "-c", "xfce4-desktop", "-p", "/backdrop/screen0/monitor0/image-show", "-s", "true"]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
128 subprocess.Popen(args0)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
129 subprocess.Popen(args1)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
130 subprocess.Popen(args2)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
131 args = ["xfdesktop","--reload"]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
132 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
133
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
134 elif desktop_env in ["fluxbox","jwm","openbox","afterstep"]:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
135 #http://fluxbox-wiki.org/index.php/Howto_set_the_background
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
136 # used fbsetbg on jwm too since I am too lazy to edit the XML configuration
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
137 # now where fbsetbg does the job excellent anyway.
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
138 # and I have not figured out how else it can be set on Openbox and AfterSTep
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
139 # but fbsetbg works excellent here too.
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
140 try:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
141 args = ["fbsetbg", file_loc]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
142 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
143 except:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
144 sys.stderr.write("ERROR: Failed to set wallpaper with fbsetbg!\n")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
145 sys.stderr.write("Please make sre that You have fbsetbg installed.\n")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
146 elif desktop_env=="icewm":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
147 # command found at http://urukrama.wordpress.com/2007/12/05/desktop-backgrounds-in-window-managers/
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
148 args = ["icewmbg", file_loc]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
149 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
150 elif desktop_env=="blackbox":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
151 # command found at http://blackboxwm.sourceforge.net/BlackboxDocumentation/BlackboxBackground
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
152 args = ["bsetbg", "-full", file_loc]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
153 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
154 elif desktop_env=="lxde":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
155 args = "pcmanfm --set-wallpaper %s --wallpaper-mode=scaled" % file_loc
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
156 subprocess.Popen(args,shell=True)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
157 elif desktop_env=="WindowMaker":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
158 # From http://www.commandlinefu.com/commands/view/3857/set-wallpaper-on-windowmaker-in-one-line
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
159 args = "wmsetbg -s -u %s" % file_loc
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
160 subprocess.Popen(args,shell=True)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
161 elif desktop_env=="windows": #Not tested since I do not run this on Windows
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
162 #From https://stackoverflow.com/questions/1977694/change-desktop-background
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
163 import ctypes
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
164 SPI_SETDESKWALLPAPER = 20
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
165 ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, file_loc , 0)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
166 else:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
167 if first_run: #don't spam the user with the same message over and over again
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
168 sys.stderr.write("Warning: Failed to set wallpaper. Your desktop environment is not supported.")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
169 sys.stderr.write("You can try manually to set Your wallpaper to %s" % file_loc)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
170 return False
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
171 return True
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
172 except:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
173 sys.stderr.write("ERROR: Failed to set wallpaper. There might be a bug.\n")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
174 return False
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
175
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
176
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
177 class Gui():
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
178 global COPYRIGHT_TXT, TITLE
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
179
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
180 def __init__(self):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
181 self.window = tk.Tk()
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
182 self.image = None
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
183 for ico in ['./bing.ico', '/usr/share/bing4desktop/bing.ico']:
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
184 try:
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
185 self.image = Image.open(ico)
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
186 except FileNotFoundError as fs:
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
187 print ('W bieżącym katalogu, brak ikony, ', fs)
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
188 finally:
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
189 break
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
190
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
191 #if self.image != None:
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
192 # break
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
193
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
194 self.tmp = None
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
195 self.menu = (
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
196 item("Ustaw Tapetę", lambda icon, item: wp.set(full_path, True)),
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
197 item('Tapeta poprzednia', lambda icon, item: self.change_pic(1)),
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
198 item('Tapeta następna', lambda icon, item: self.change_pic(-1)),
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
199 item('Opis zdjęcia', lambda icon, item: icon.notify(TITLE + ' --- ' + COPYRIGHT_TXT)),
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
200 item('O Programie', lambda icon, item: self.about()),
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
201
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
202 item('Wyjdź', self.quit_window)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
203 )
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
204 self.frame = tk.Frame(self.window, width=600, height=400)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
205 self.frame.pack()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
206 self.frame.place(anchor='center', relx=0.5, rely=5)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
207
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
208 #self.window.title("Bing4Desktop")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
209 #self.window.geometry('800x600')
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
210 #self.window.protocol('WM_DELETE_WINDOW', self.withdraw_window)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
211 self.withdraw_window()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
212 self.window.mainloop()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
213
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
214 def about(self):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
215 global ver
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
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)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
217 # możliwe że nieprawidlowe
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
218 self.window.destroy()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
219 self.window.update()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
220
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
221 def change_pic(self, n):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
222 global actualImage, BING_WALLPAPER_PATH
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
223 BASE = "/HPImageArchive.aspx?format=js&idx="
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
224 OFF = "&n=1&mkt=en-US"
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
225
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
226 actualImage = actualImage + n;
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
227 if (actualImage < 0):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
228 actualImage = 10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
229 elif (actualImage > 10):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
230 actualImage = 0
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
231
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
232 wp=WPaper()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
233 if wp.get():
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
234 BING_WALLPAPER_PATH = BASE+str(actualImage)+OFF
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
235 if wp.get() == False:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
236 cleanup()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
237 exit
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
238
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
239 # Wszystko pięknie tylko metoda 'show', zmienia nazwę pliku przechowywanego w TMP
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
240 # Pozatym nie ubija a nie znalazłem nic lepszego
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
241 print ("GLOB: ", glob.glob( os.path.join('/tmp', '*.PNG')))
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
242 for infile in glob.glob( os.path.join('/tmp', '*.PNG')):
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
243 print ("INFILE: ", infile)
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
244 viewer = subprocess.Popen(['/usr/bin/eom', infile])
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
245 viewer.terminate()
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
246 viewer.kill() # make sure the viewer is gone; not needed on Windows
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
247
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
248 img = Image.open(full_path) #.convert('RGBA') # PIL solution
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
249 self.tmp = img.resize((600, 400), None, None, 3.0)
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
250 self.tmp.show()
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
251
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
252 def quit_window(self):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
253 self.icon.stop()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
254 self.window.destroy()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
255
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
256 def show_window(self):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
257 self.icon.stop()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
258 self.window.protocol('WM_DELETE_WINDOW', self.withdraw_window)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
259 self.window.after(0, self.window.deiconify)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
260
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
261 def withdraw_window(self):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
262 self.window.withdraw()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
263 self.icon = pystray.Icon("name", self.image, "title", self.menu)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
264 self.icon.run()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
265
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
266 def cleanup():
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
267 # NIEBEZPIECZNA ! lepiej niczym innym nie nadpisywać tych zmiennych
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
268 shutil.rmtree(tmp_path)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
269
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
270 if __name__ in '__main__':
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
271 wp=WPaper()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
272 if wp.get():
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
273 wp.set(full_path, True)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
274 g = Gui()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
275 cleanup()