From b13b03657e1de3e4acf869bd4f069bb01f630b75 Mon Sep 17 00:00:00 2001 From: Redume Date: Thu, 7 Jul 2022 20:38:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=BE=20?= =?UTF-8?q?=D0=BA=D0=B0=D1=80=D0=B4=D1=80=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=84=D0=BE=D1=82=D0=BE=D0=B3=D1=80=D0=B0?= =?UTF-8?q?=D1=84=D0=B8=D0=B8,=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=B4=20=D0=BA=D0=B0=D0=B6=D0=B4=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=B4=D0=B5=D0=BD=D1=8C=20=D0=BC=D0=B5=D0=BD=D1=8F=D0=B5=D1=82?= =?UTF-8?q?=20=D0=BE=D0=B1=D0=BE=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 45 +++++++-------------------------------------- requirements.txt | 5 ++--- 2 files changed, 9 insertions(+), 41 deletions(-) diff --git a/main.py b/main.py index 263a0c8..f2428c0 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,10 @@ -import math from bs4 import BeautifulSoup import requests import ctypes import os import urllib - -from PIL import Image -from screeninfo import get_monitors +import schedule class Nasa: @@ -17,59 +14,31 @@ class Nasa: self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) ' 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'} self.photoName = "everydayphotonasa.jpg" - self.screen_size = (self.get_size_screen()) def download_photo(self): full_page = requests.get(self.url, headers=self.headers) soup = BeautifulSoup(full_page.content, 'html.parser') + lnk = str for link in soup.select("img"): lnk = link["src"] - print(f"Сохарняю картинку — {self.url + lnk}") + print(f"Скачиваю картинку — {self.url + lnk}") img = urllib.request.urlopen(self.url + lnk).read() out = open(self.photoName, "wb") out.write(img) out.close() - def get_size_photo(self): - image = Image.open(self.photoName) - width, height = image.size - return width, height - - def get_size_screen(self): - for m in get_monitors(): - width = m.width - height = m.height - return width, height - def set_wallpaper(self): path = os.path.abspath(self.photoName) ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 0) - def aspect_ratio(self, width, height): - k = math.gcd(width, height) - return width // k, height // k - - def valid_image(self): - w_image, h_image = self.get_size_photo() - w_image, h_image = self.aspect_ratio(w_image, h_image) - w_screen, h_screen = self.aspect_ratio(self.screen_size[0], self.screen_size[1]) - return w_image == w_screen and h_image == h_screen - - def crop_image(self): - size_screen = self.get_size_screen() - img = Image.open(self.photoName) - img_crop = img.crop((0, 0, size_screen[0], size_screen[1])) - img_crop.save(self.photoName) - def start(self): self.download_photo() - if not self.valid_image(): - self.crop_image() - self.set_wallpaper() if __name__ == "__main__": - nasa = Nasa() - nasa.start() + schedule.every(1).days.do(Nasa().start) + while True: + schedule.run_pending() + diff --git a/requirements.txt b/requirements.txt index 8bc4ef1..4f42f6b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ -beautifulsoup4~=4.11.1 +beautifulsoup4==4.11.1 requests~=2.28.0 -Pillow~=9.2.0 -screeninfo~=0.8 +schedule~=1.1.0 \ No newline at end of file