From 6e0b50ade19897ae69d78f1b0695dc4c50119fa5 Mon Sep 17 00:00:00 2001 From: Redume Date: Thu, 25 Aug 2022 10:19:32 +0300 Subject: [PATCH] The main actions with the wallpaper were not much rewritten and moved --- src/functions/__init__.py | 0 src/functions/wallpaper.py | 93 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/functions/__init__.py create mode 100644 src/functions/wallpaper.py diff --git a/src/functions/__init__.py b/src/functions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/functions/wallpaper.py b/src/functions/wallpaper.py new file mode 100644 index 0000000..b9dc06e --- /dev/null +++ b/src/functions/wallpaper.py @@ -0,0 +1,93 @@ +from bs4 import BeautifulSoup + +import requests +import urllib +import sys +import ctypes +import os + +from .config import Config + + +class Wallpaper(Config): + def __init__(self): + super().__init__() + self.url = "https://apod.nasa.gov/apod/" + self.config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.ini") + + self.full_page = requests.get(self.url) + self.soup = BeautifulSoup(self.full_page.content, 'html.parser') + self.lnk = None + + def check(self): + config = Config.get_setting(self, path=self.config_path, section="Config", setting="wallpaper-link") + + for link in self.soup.select("img"): + self.lnk = link["src"] + + if self.lnk is None: + return "On the NASA website, they posted not a picture but a video, " \ + "unfortunately, it will not work to install wallpaper" + + if config == self.lnk: + return + else: + Config.update_setting( + self, + path=self.config_path, + section="Config", + setting="wallpaper-link", + value=self.lnk + ) + + def download(self): + try: + img = urllib.request.urlopen(self.url + self.lnk).read() + with open("everyNASA.jpg", "wb") as file: + file.write(img) + except requests.exceptions.ConnectionError: + return "Connection error, please try again later." + + @staticmethod + def set(): + if sys.platform == "win32": + ctypes.windll.user32.SystemParametersInfoW(20, 0, os.path.abspath("everyNASA.jpg"), 0) + elif sys.platform == "linux": + desk_env = os.environ.get("DESKTOP_SESSION") + if desk_env == "gnome": + os.system( + "gsettings set org.gnome.desktop.background " + "picture-uri 'file://{}'" + .format( + os.path.abspath( + "everyNASA.jpg" + ) + ) + ) + elif desk_env == "plasma": + import dbus + jscript = """ + var allDesktops = desktops(); + print (allDesktops); + for (i=0;i