(New feature) 0.5.1 - download posters and use them as icons.

main
KKlochko 3 years ago
parent 2b121c3e89
commit a246ca69fe

@ -17,3 +17,6 @@
Little Fix: add update status in quotes Little Fix: add update status in quotes
** 0.4.3 <2022-07-09> ** 0.4.3 <2022-07-09>
New small feature: repeating checking the content. 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.

@ -49,17 +49,17 @@ class DataBase:
"""Create a table in the DB.""" """Create a table in the DB."""
connect = sqlite3.connect(self.PATH) connect = sqlite3.connect(self.PATH)
cursor = connect.cursor() 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() connect.commit()
cursor.close() cursor.close()
connect.close() connect.close()
def add_anime(self, url, title, status): def add_anime(self, url, title, status, poster_path=""):
"""Add entry of a content.""" """Add entry of a content."""
connect = sqlite3.connect(self.PATH) connect = sqlite3.connect(self.PATH)
cursor = connect.cursor() cursor = connect.cursor()
data = [url, title, status] data = [url, title, status, poster_path]
cursor.execute(f'INSERT INTO {self.TABLE}(Url, Title, Status) VALUES(?,?,?)', data) cursor.execute(f'INSERT INTO {self.TABLE}(Url, Title, Status, PosterPath) VALUES(?,?,?,?)', data)
connect.commit() connect.commit()
cursor.close() cursor.close()
connect.close() connect.close()
@ -86,7 +86,7 @@ class DataBase:
connect.close() connect.close()
return data 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 0 if not exists.
Return 1 if the same as the entry in the DB. Return 1 if the same as the entry in the DB.
@ -95,10 +95,10 @@ class DataBase:
data = self.get_entry(url) data = self.get_entry(url)
# If not exists # If not exists
if data == []: if data == []:
self.add_anime(url, title, status) self.add_anime(url, title, status, poster_path)
return 0 return 0
# If the same # If the same
if data[0][1:] == (url, title, status): if data[0][1:] == (url, title, status, poster_path):
return 1 return 1
# If the status is newer than the entry status. # If the status is newer than the entry status.
if data[0][3] != status: if data[0][3] != status:

@ -43,6 +43,7 @@ def main():
MESSAGE = "New episode." MESSAGE = "New episode."
URL_FILE = "urls" URL_FILE = "urls"
WAITING_PERIOD = 60 # seconds WAITING_PERIOD = 60 # seconds
POSTERS = True # For use poster as icons of notifications
# Initialising objects # Initialising objects
scr = Scraper(HEADERS) scr = Scraper(HEADERS)
@ -56,15 +57,23 @@ def main():
# If one of page has updated then notifing. # If one of page has updated then notifing.
# Repeating the checking with the waiting period. # Repeating the checking with the waiting period.
while True: while True:
print(f"[DOING] Checking for animes [0/{len(urls)}]")
count = 0
for url in urls: for url in urls:
data = scr.get_anime(url) data = scr.get_anime(url, POSTERS)
if data == None: if data == None:
print(f"[ERROR] A conections trouble is occured.")
continue continue
url, title, status = data url, title, status, poster_path = data
r = db.add_anime_if(url, title, status) print(f"[DOING] Checking for \"{title}\" [{count}/{len(urls)}]")
r = db.add_anime_if(url, title, status, poster_path)
if r == -1: if r == -1:
n = Notification(title, MESSAGE) n = Notification(title, MESSAGE, poster_path)
n.send() 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) time.sleep(WAITING_PERIOD)
if __name__ == "__main__": if __name__ == "__main__":

@ -24,14 +24,25 @@ This module has all for simplify work with scraping.
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import os
class Scraper: class Scraper:
"""The handler of web connection.""" """The handler of web connection."""
def __init__(self, HEADERS): def __init__(self, HEADERS, POSTER_PATH="posters"):
"""Initialising the connection information.""" """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. Return None if response is not 200.
Otherwise, return [url, title, status]. Otherwise, return [url, title, status].
@ -48,4 +59,13 @@ class Scraper:
str_current = data.get_text() str_current = data.get_text()
str_current = str_current[str_current.find(str_find)+len(str_find):] str_current = str_current[str_current.find(str_find)+len(str_find):]
status = str_current[:str_current.find('\n')] 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]

Loading…
Cancel
Save