From a246ca69fee7e09eb6d9bdab52480597464b091f Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sun, 10 Jul 2022 12:20:35 +0300 Subject: [PATCH] (New feature) 0.5.1 - download posters and use them as icons. --- CHANGELOG.org | 3 +++ src/db.py | 14 +++++++------- src/main.py | 17 +++++++++++++---- src/scraper.py | 28 ++++++++++++++++++++++++---- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.org b/CHANGELOG.org index b151e72..988aee6 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -17,3 +17,6 @@ Little Fix: add update status in quotes ** 0.4.3 <2022-07-09> New small feature: repeating checking the content. +** 0.5.1 <2022-07-10> + New feature: download posters and use them as icons for notifications. + New small feature: showing information about a current task. diff --git a/src/db.py b/src/db.py index d9cb658..0239e3a 100644 --- a/src/db.py +++ b/src/db.py @@ -49,17 +49,17 @@ class DataBase: """Create a table in the DB.""" connect = sqlite3.connect(self.PATH) cursor = connect.cursor() - cursor.execute(f'CREATE TABLE IF NOT EXISTS {table}(id INTEGER PRIMARY KEY, Url TEXT, Title TEXT, Status TEXT);') + cursor.execute(f'CREATE TABLE IF NOT EXISTS {table}(id INTEGER PRIMARY KEY, Url TEXT, Title TEXT, Status TEXT, PosterPath TEXT);') connect.commit() cursor.close() connect.close() - def add_anime(self, url, title, status): + def add_anime(self, url, title, status, poster_path=""): """Add entry of a content.""" connect = sqlite3.connect(self.PATH) cursor = connect.cursor() - data = [url, title, status] - cursor.execute(f'INSERT INTO {self.TABLE}(Url, Title, Status) VALUES(?,?,?)', data) + data = [url, title, status, poster_path] + cursor.execute(f'INSERT INTO {self.TABLE}(Url, Title, Status, PosterPath) VALUES(?,?,?,?)', data) connect.commit() cursor.close() connect.close() @@ -86,7 +86,7 @@ class DataBase: connect.close() return data - def add_anime_if(self, url, title, status): + def add_anime_if(self, url, title, status, poster_path=""): """ Return 0 if not exists. Return 1 if the same as the entry in the DB. @@ -95,10 +95,10 @@ class DataBase: data = self.get_entry(url) # If not exists if data == []: - self.add_anime(url, title, status) + self.add_anime(url, title, status, poster_path) return 0 # If the same - if data[0][1:] == (url, title, status): + if data[0][1:] == (url, title, status, poster_path): return 1 # If the status is newer than the entry status. if data[0][3] != status: diff --git a/src/main.py b/src/main.py index 7bc6ab6..0689013 100644 --- a/src/main.py +++ b/src/main.py @@ -43,6 +43,7 @@ def main(): MESSAGE = "New episode." URL_FILE = "urls" WAITING_PERIOD = 60 # seconds + POSTERS = True # For use poster as icons of notifications # Initialising objects scr = Scraper(HEADERS) @@ -56,15 +57,23 @@ def main(): # If one of page has updated then notifing. # Repeating the checking with the waiting period. while True: + print(f"[DOING] Checking for animes [0/{len(urls)}]") + count = 0 for url in urls: - data = scr.get_anime(url) + data = scr.get_anime(url, POSTERS) if data == None: + print(f"[ERROR] A conections trouble is occured.") continue - url, title, status = data - r = db.add_anime_if(url, title, status) + url, title, status, poster_path = data + print(f"[DOING] Checking for \"{title}\" [{count}/{len(urls)}]") + r = db.add_anime_if(url, title, status, poster_path) if r == -1: - n = Notification(title, MESSAGE) + n = Notification(title, MESSAGE, poster_path) n.send() + print(f"[NOTIFICATION] \"{title}\"") + count+=1 + print(f"[DONE] Checking for \"{title}\" [{count}/{len(urls)}]") + print(f"[WAITING] The next check is after {WAITING_PERIOD} seconds") time.sleep(WAITING_PERIOD) if __name__ == "__main__": diff --git a/src/scraper.py b/src/scraper.py index ef336f9..4a55fce 100644 --- a/src/scraper.py +++ b/src/scraper.py @@ -24,14 +24,25 @@ This module has all for simplify work with scraping. import requests from bs4 import BeautifulSoup +import os class Scraper: """The handler of web connection.""" - def __init__(self, HEADERS): + def __init__(self, HEADERS, POSTER_PATH="posters"): """Initialising the connection information.""" - self.HEADERS = HEADERS + self.HEADERS, self.POSTER_PATH = HEADERS, POSTER_PATH + self.mkdir(self.POSTER_PATH) - def get_anime(self, url): + def mkdir(self, path): + if not os.path.isdir(path): + os.mkdir(path) + + def file_exist(self, path): + if os.path.isfile(path): + return True + return False + + def get_anime(self, url, GETPOSTER = False): """ Return None if response is not 200. Otherwise, return [url, title, status]. @@ -48,4 +59,13 @@ class Scraper: str_current = data.get_text() str_current = str_current[str_current.find(str_find)+len(str_find):] status = str_current[:str_current.find('\n')] - return [url, title, status] + # Poster + poster_url = "https://anitube.in.ua" + soup.find('span', class_="story_post").find('img').get('src') + poster_path = f"{self.POSTER_PATH}/{poster_url.split('/')[-1]}" + if GETPOSTER and not self.file_exist(poster_path): + print(f"[DONWLOADING] The poster for {title}") + img = requests.get(poster_url) + with open(poster_path,'wb') as file: + file.write(img.content) + print(f"[DONWLOADED] The poster for {title}") + return [url, title, status, poster_path]