(New feature) 0.5.1 - download posters and use them as icons.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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:
|
||||
|
||||
+13
-4
@@ -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__":
|
||||
|
||||
+24
-4
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user