annotate bing4desktop/bing4desktop.py @ 17:3b8fd6a7f2d0

bing4desktop-0.240207-2
author prymula <prymula76@outlook.com>
date Wed, 07 Feb 2024 12:11:34 +0100
parents 2dea5bff9c0c
children e7a6488c02b7
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
17
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
25 ver = "0.240207-2"
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=""
17
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
30 actualImage = 1
10
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 = ""
17
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
33 TMP_COPYRIGHT = ""
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
34 TMP_TITLE = ""
10
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 tmp_path = tempfile.mkdtemp()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
37
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
38 class WPaper:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
39 def get(self):
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
40 global full_path, COPYRIGHT_TXT, TITLE
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
41 # open the Bing HPImageArchive URI and ask for a JSON response
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
42 resp = requests.get(BING_URI_BASE + BING_WALLPAPER_PATH)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
43
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
44 if resp.status_code == 200:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
45 json_response = json.loads(resp.content)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
46 wallpaper_path = json_response['images'][0]['url']
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
47 COPYRIGHT_TXT = json_response['images'][0]['copyright']
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
48 TITLE = json_response['images'][0]['title']
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
49
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
50 filename = wallpaper_path.split('/')[-1]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
51 wallpaper_uri = BING_URI_BASE + wallpaper_path
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
52 filename_short = filename.split('&')
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
53 print ("filemname_short", filename_short[0])
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
54 # 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
55 response = requests.get(wallpaper_uri)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
56 if resp.status_code == 200:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
57 desktop_env = os.environ.get('DESKTOP_SESSION')
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
58 if desktop_env=="windows":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
59 messagebox.showinfo("Błąd","Brak wsparcia dla Windows i Mac !")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
60 cleanup()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
61 exit
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
62 else:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
63 full_path = tmp_path + filename_short[0]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
64 with open(full_path, 'wb') as f:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
65 f.write(response.content)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
66 else:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
67 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
68 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
69 return False
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
70 else:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
71 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
72 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
73 return False
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
74 return True
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
75
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
76 def set(self,file_loc, first_run):
17
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
77 global full_path, TMP_COPYRIGHT, COPYRIGHT_TXT, TMP_TITLE, TITLE
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
78 desktop_env = os.environ.get('DESKTOP_SESSION')
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
79
17
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
80 TMP_COPYRIGHT = COPYRIGHT_TXT
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
81 TMP_TITLE = TITLE
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
82
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
83 if len(full_path) == 0:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
84 messagebox.showinfo("Błąd","Niewybrano obrazu, najpierw wybierz obraz")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
85 return
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
86 try:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
87 if desktop_env in ["gnome-classic", "gnome", "unity", "default"]:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
88 uri = "'file://%s'" % file_loc
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
89 try:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
90 SCHEMA = "org.gnome.desktop.background"
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
91 KEY = "picture-uri"
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
92 gsettings = Gio.Settings.new(SCHEMA)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
93 gsettings.set_string(KEY, uri)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
94 except:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
95 args = ["gsettings", "set", "org.gnome.desktop.background", "picture-uri", uri]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
96 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
97 elif desktop_env in ["cinnamon"]:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
98 uri = "'file://%s'" % file_loc
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
99 try:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
100 SCHEMA = "org.cinnamon.desktop.background"
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
101 KEY = "picture-uri"
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
102 gsettings = Gio.Settings.new(SCHEMA)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
103 gsettings.set_string(KEY, uri)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
104 except:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
105 args = ["gsettings", "set", "org.cinnamon.desktop.background", "picture-uri", uri]
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 elif desktop_env=="mate":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
108 try: # MATE >= 1.6
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
109 # info from http://wiki.mate-desktop.org/docs:gsettings
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
110 args = ["gsettings", "set", "org.mate.background", "picture-filename", "'%s'" % file_loc]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
111 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
112 except: # MATE < 1.6
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
113 # From https://bugs.launchpad.net/variety/+bug/1033918
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
114 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
115 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
116 elif desktop_env=="gnome2": # Not tested
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
117 # From https://bugs.launchpad.net/variety/+bug/1033918
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
118 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
119 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
120 ## KDE4 is difficult
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
121 ## see http://blog.zx2c4.com/699 for a solution that might work
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
122 elif desktop_env in ["kde3", "trinity"]:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
123 # From http://ubuntuforums.org/archive/index.php/t-803417.html
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
124 args = 'dcop kdesktop KBackgroundIface setWallpaper 0 "%s" 6' % file_loc
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
125 subprocess.Popen(args,shell=True)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
126
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
127 elif desktop_env=="xfce4":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
128 #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
129 if first_run:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
130 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
131 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
132 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
133 subprocess.Popen(args0)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
134 subprocess.Popen(args1)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
135 subprocess.Popen(args2)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
136 args = ["xfdesktop","--reload"]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
137 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
138
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
139 elif desktop_env in ["fluxbox","jwm","openbox","afterstep"]:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
140 #http://fluxbox-wiki.org/index.php/Howto_set_the_background
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
141 # 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
142 # now where fbsetbg does the job excellent anyway.
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
143 # 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
144 # but fbsetbg works excellent here too.
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
145 try:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
146 args = ["fbsetbg", file_loc]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
147 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
148 except:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
149 sys.stderr.write("ERROR: Failed to set wallpaper with fbsetbg!\n")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
150 sys.stderr.write("Please make sre that You have fbsetbg installed.\n")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
151 elif desktop_env=="icewm":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
152 # 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
153 args = ["icewmbg", file_loc]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
154 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
155 elif desktop_env=="blackbox":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
156 # command found at http://blackboxwm.sourceforge.net/BlackboxDocumentation/BlackboxBackground
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
157 args = ["bsetbg", "-full", file_loc]
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
158 subprocess.Popen(args)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
159 elif desktop_env=="lxde":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
160 args = "pcmanfm --set-wallpaper %s --wallpaper-mode=scaled" % file_loc
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
161 subprocess.Popen(args,shell=True)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
162 elif desktop_env=="WindowMaker":
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
163 # 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
164 args = "wmsetbg -s -u %s" % file_loc
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
165 subprocess.Popen(args,shell=True)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
166 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
167 #From https://stackoverflow.com/questions/1977694/change-desktop-background
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
168 import ctypes
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
169 SPI_SETDESKWALLPAPER = 20
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
170 ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, file_loc , 0)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
171 else:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
172 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
173 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
174 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
175 return False
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
176 return True
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
177 except:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
178 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
179 return False
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
180
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
181
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
182 class Gui():
17
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
183 global TMP_COPYRIGHT, TMP_TITLE
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
184
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
185 def __init__(self):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
186 self.window = tk.Tk()
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
187 self.image = None
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
188 for ico in ['./bing.ico', '/usr/share/bing4desktop/bing.ico']:
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
189 try:
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
190 self.image = Image.open(ico)
13
73fd89b46da4 bing4desktop-0.240206-3
prymula <prymula76@outlook.com>
parents: 11
diff changeset
191 except:
73fd89b46da4 bing4desktop-0.240206-3
prymula <prymula76@outlook.com>
parents: 11
diff changeset
192 print ('W bieżącym katalogu, brak ikony ' + ico)
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
193 finally:
13
73fd89b46da4 bing4desktop-0.240206-3
prymula <prymula76@outlook.com>
parents: 11
diff changeset
194 if self.image != None:
73fd89b46da4 bing4desktop-0.240206-3
prymula <prymula76@outlook.com>
parents: 11
diff changeset
195 break
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
196
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
197 self.tmp = None
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
198 self.menu = (
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
199 item("Ustaw Tapetę", lambda icon, item: wp.set(full_path, True)),
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
200 item('Tapeta poprzednia', lambda icon, item: self.change_pic(1)),
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
201 item('Tapeta następna', lambda icon, item: self.change_pic(-1)),
17
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
202 item('Opis zdjęcia', lambda icon, item: icon.notify(TMP_TITLE + ' --- ' + TMP_COPYRIGHT)),
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
203 item('O Programie', lambda icon, item: self.about()),
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
204
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
205 item('Wyjdź', self.quit_window)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
206 )
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
207 self.frame = tk.Frame(self.window, width=600, height=400)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
208 self.frame.pack()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
209 self.frame.place(anchor='center', relx=0.5, rely=5)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
210
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
211 #self.window.title("Bing4Desktop")
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
212 #self.window.geometry('800x600')
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
213 #self.window.protocol('WM_DELETE_WINDOW', self.withdraw_window)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
214 self.withdraw_window()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
215 self.window.mainloop()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
216
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
217 def about(self):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
218 global ver
15
18816a0c1fc2 bing4desktop-0.240206-4
prymula <prymula76@outlook.com>
parents: 13
diff changeset
219 messagebox.showinfo("Bing4Desktop","Inspiracja:\npystray.readthedocs.io/en/latest/usage.html\n\nNapisał: Prymula\n\nLuty 2024\n\nLicencja:\nPublic Domain\n\nWersja: "+ver)
13
73fd89b46da4 bing4desktop-0.240206-3
prymula <prymula76@outlook.com>
parents: 11
diff changeset
220 # możliwe że nieprawidlowe po wyjściu z aplikacji zwróci bląd :(
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
221 self.window.destroy()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
222 self.window.update()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
223
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
224 def change_pic(self, n):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
225 global actualImage, BING_WALLPAPER_PATH
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
226 BASE = "/HPImageArchive.aspx?format=js&idx="
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
227 OFF = "&n=1&mkt=en-US"
17
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
228
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
229 actualImage = actualImage + n;
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
230 if (actualImage < 0):
17
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
231 actualImage = 7
3b8fd6a7f2d0 bing4desktop-0.240207-2
prymula <prymula76@outlook.com>
parents: 16
diff changeset
232 elif (actualImage > 7):
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
233 actualImage = 0
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
234
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
235 wp=WPaper()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
236 if wp.get():
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
237 BING_WALLPAPER_PATH = BASE+str(actualImage)+OFF
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
238 if wp.get() == False:
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
239 cleanup()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
240 exit
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
241
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
242 # 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
243 # Pozatym nie ubija a nie znalazłem nic lepszego
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
244 print ("GLOB: ", glob.glob( os.path.join('/tmp', '*.PNG')))
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
245 for infile in glob.glob( os.path.join('/tmp', '*.PNG')):
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
246 print ("INFILE: ", infile)
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
247 viewer = subprocess.Popen(['/usr/bin/eom', infile])
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
248 viewer.terminate()
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
249 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
250
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
251 img = Image.open(full_path) #.convert('RGBA') # PIL solution
11
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
252 self.tmp = img.resize((600, 400), None, None, 3.0)
c01d7edb9cb9 bing4desktop-0.240206-0
prymula <prymula76@outlook.com>
parents: 10
diff changeset
253 self.tmp.show()
10
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
254
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
255 def quit_window(self):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
256 self.icon.stop()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
257 self.window.destroy()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
258
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
259 def show_window(self):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
260 self.icon.stop()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
261 self.window.protocol('WM_DELETE_WINDOW', self.withdraw_window)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
262 self.window.after(0, self.window.deiconify)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
263
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
264 def withdraw_window(self):
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
265 self.window.withdraw()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
266 self.icon = pystray.Icon("name", self.image, "title", self.menu)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
267 self.icon.run()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
268
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
269 def cleanup():
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
270 # NIEBEZPIECZNA ! lepiej niczym innym nie nadpisywać tych zmiennych
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
271 shutil.rmtree(tmp_path)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
272
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
273 if __name__ in '__main__':
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
274 wp=WPaper()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
275 if wp.get():
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
276 wp.set(full_path, True)
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
277 g = Gui()
ed9556d0c5d7 bing4desktop-0.240205-4
prymula <prymula76@outlook.com>
parents:
diff changeset
278 cleanup()