From 37ea16c95a176c3fa8b4e42b765f764580295abd Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Wed, 1 Sep 2021 15:57:54 +0000 Subject: [PATCH] Prettifying the code a little bit + redundant href on A-U --- get404.py | 13 ++++++++++-- pynyaata/__init__.py | 34 ++++++++++++++++++++++++------ pynyaata/connectors/animeultime.py | 13 ++++++++---- pynyaata/connectors/core.py | 25 ++++++++++++++++++---- pynyaata/connectors/nyaa.py | 7 +++++- pynyaata/connectors/pantsu.py | 7 +++++- pynyaata/connectors/yggtorrent.py | 7 ++++-- pynyaata/models.py | 26 ++++++++++++++++++----- 8 files changed, 106 insertions(+), 26 deletions(-) diff --git a/get404.py b/get404.py index 0a8ab80..7eda786 100644 --- a/get404.py +++ b/get404.py @@ -7,6 +7,15 @@ for link in links: html = curl_content(link.link, debug=False) if html['http_code'] != 200 and html['http_code'] != 500: - print('(%d) %s %s : %s' % (html['http_code'], link.title.name, link.season, link.link)) + print('(%d) %s %s : %s' % ( + html['http_code'], + link.title.name, + link.season, + link.link + )) elif 'darkgray' in str(html['output']): - print('(darkgray) %s %s : %s' % (link.title.name, link.season, link.link)) + print('(darkgray) %s %s : %s' % ( + link.title.name, + link.season, + link.link + )) diff --git a/pynyaata/__init__.py b/pynyaata/__init__.py index 69834be..9065fad 100644 --- a/pynyaata/__init__.py +++ b/pynyaata/__init__.py @@ -92,7 +92,9 @@ def search(): @app.route('/latest/') def latest(page=1): set_event_loop(SelectorEventLoop()) - torrents = get_event_loop().run_until_complete(run_all('', return_type=ConnectorReturn.HISTORY, page=page)) + torrents = get_event_loop().run_until_complete( + run_all('', return_type=ConnectorReturn.HISTORY, page=page) + ) results = [] for torrent in torrents: @@ -136,7 +138,10 @@ def admin(): if form.validate_on_submit(): link = AnimeLink.query.filter_by(id=form.id.data).first() if link: - form.message = '%s (%s) has been successfully deleted' % (link.title.name, link.season) + form.message = '%s (%s) has been successfully deleted' % ( + link.title.name, + link.season + ) db.session.delete(link) db.session.commit() @@ -145,7 +150,9 @@ def admin(): db.session.delete(title) db.session.commit() else: - form._errors = {'id': ['Id %s was not found in the database' % form.id.data]} + form._errors = { + 'id': ['Id %s was not found in the database' % form.id.data] + } folders = AnimeFolder.query.all() for folder in folders: @@ -169,7 +176,9 @@ def folder_list(): db.session.delete(folder) db.session.commit() else: - form._errors = {'id': ['Id %s was not found in the database' % form.id.data]} + form._errors = { + 'id': ['Id %s was not found in the database' % form.id.data] + } folders = AnimeFolder.query.all() @@ -183,7 +192,12 @@ def folder_list(): def folder_edit(folder_id=None): folder = AnimeFolder.query.filter_by(id=folder_id).first() folder = folder if folder else AnimeFolder() - form = FolderEditForm(request.form, id=folder.id, name=folder.name, path=folder.path) + form = FolderEditForm( + request.form, + id=folder.id, + name=folder.name, + path=folder.path + ) if form.validate_on_submit(): # Folder @@ -246,9 +260,15 @@ def admin_edit(link_id=None): # Transmission if TRANSMISSION_ENABLED and isinstance(instance, Nyaa): if title.folder.path is not None and title.folder.path != '': - download_url = link.link.replace('/view/', '/download/') + '.torrent' + download_url = link.link.replace( + '/view/', + '/download/' + ) + '.torrent' torrent_path = '%s/%s' % (title.folder.path, title.name) - torrent = transmission.add_torrent(download_url, download_dir=torrent_path) + torrent = transmission.add_torrent( + download_url, + download_dir=torrent_path + ) transmission.move_torrent_data(torrent.id, torrent_path) transmission.start_torrent(torrent.id) diff --git a/pynyaata/connectors/animeultime.py b/pynyaata/connectors/animeultime.py index ee04645..9c34343 100644 --- a/pynyaata/connectors/animeultime.py +++ b/pynyaata/connectors/animeultime.py @@ -30,7 +30,9 @@ class AnimeUltime(ConnectorCore): @ConnectorCache.cache_data def search(self): - response = curl_content(self.get_full_search_url(), {'search': self.query}) + response = curl_content(self.get_full_search_url(), { + 'search': self.query + }) if response['http_code'] == 200: html = BeautifulSoup(response['output'], 'html.parser') @@ -63,11 +65,14 @@ class AnimeUltime(ConnectorCore): elif len(player) > 0: name = html.select('h1') ani_type = html.select('div.titre') - href = '%s/file-0-1/%s' % (self.base_url, player[0]['data-serie']) + href = '%s/file-0-1/%s' % ( + self.base_url, + player[0]['data-serie'] + ) self.data.append({ 'vf': self.is_vf(), - 'href': '%s/file-0-1/%s' % (self.base_url, player[0]['data-serie']), + 'href': href, 'name': name[0].get_text(), 'type': ani_type[0].get_text().replace(':', ''), 'class': self.color if link_exist_in_db(href) else '' @@ -95,7 +100,7 @@ class AnimeUltime(ConnectorCore): self.data.append({ 'vf': self.is_vf(), - 'href': '%s/%s' % (self.base_url, link['href']), + 'href': href, 'name': link.get_text(), 'type': tds[4].get_text(), 'date': parse_date(h3s[i].string[:-3], '%A %d %B %Y'), diff --git a/pynyaata/connectors/core.py b/pynyaata/connectors/core.py index bcaecaf..9f2899e 100644 --- a/pynyaata/connectors/core.py +++ b/pynyaata/connectors/core.py @@ -31,7 +31,12 @@ class Cache: @wraps(f) def wrapper(*args, **kwds): connector = args[0] - key = 'pynyaata.%s.%s.%s.%s' % (connector.__class__.__name__, f.__name__, connector.query, connector.page) + key = 'pynyaata.%s.%s.%s.%s' % ( + connector.__class__.__name__, + f.__name__, + connector.query, + connector.page + ) if REDIS_ENABLED: json = None @@ -82,9 +87,18 @@ def curl_content(url, params=None, ajax=False, debug=True): try: if not instance.is_behind_cloudflare: if method == 'post': - response = requests.post(url, params, timeout=REQUESTS_TIMEOUT, headers=headers) + response = requests.post( + url, + params, + timeout=REQUESTS_TIMEOUT, + headers=headers + ) else: - response = requests.get(url, timeout=REQUESTS_TIMEOUT, headers=headers) + response = requests.get( + url, + timeout=REQUESTS_TIMEOUT, + headers=headers + ) output = response.text http_code = response.status_code @@ -97,7 +111,10 @@ def curl_content(url, params=None, ajax=False, debug=True): response_session = loads(json_session.text) cloudproxy_session = response_session['session'] - headers['Content-Type'] = 'application/x-www-form-urlencoded' if (method == 'post') else 'application/json' + if method == 'post': + headers['Content-Type'] = 'application/x-www-form-urlencoded' + else: + headers['Content-Type'] = 'application/json' json_response = requests.post(CLOUDPROXY_ENDPOINT, headers=headers, data=dumps({ 'cmd': 'request.%s' % method, diff --git a/pynyaata/connectors/nyaa.py b/pynyaata/connectors/nyaa.py index f4cfb79..5e37d43 100644 --- a/pynyaata/connectors/nyaa.py +++ b/pynyaata/connectors/nyaa.py @@ -17,7 +17,12 @@ class Nyaa(ConnectorCore): if self.return_type is ConnectorReturn.HISTORY: sort_type = 'id' - to_query = '(%s vf)|(%s vostfr)|(%s multi)|(%s french)' % (self.query, self.query, self.query, self.query) + to_query = '(%s vf)|(%s vostfr)|(%s multi)|(%s french)' % ( + self.query, + self.query, + self.query, + self.query + ) return '%s/?f=0&c=1_3&s=%s&o=desc&q=%s&p=%s' % (self.base_url, sort_type, to_query, self.page) def get_history(self): diff --git a/pynyaata/connectors/pantsu.py b/pynyaata/connectors/pantsu.py index 0a15c5e..a34c3ca 100644 --- a/pynyaata/connectors/pantsu.py +++ b/pynyaata/connectors/pantsu.py @@ -17,7 +17,12 @@ class Pantsu(ConnectorCore): if self.return_type is ConnectorReturn.HISTORY: sort_type = 2 - to_query = '(%s vf)|(%s vostfr)|(%s multi)|(%s french)' % (self.query, self.query, self.query, self.query) + to_query = '(%s vf)|(%s vostfr)|(%s multi)|(%s french)' % ( + self.query, + self.query, + self.query, + self.query + ) return '%s/search/%s?c=3_13&order=false&q=%s&sort=%s' % (self.base_url, self.page, to_query, sort_type) def get_history(self): diff --git a/pynyaata/connectors/yggtorrent.py b/pynyaata/connectors/yggtorrent.py index d90dd58..1f3b0c5 100644 --- a/pynyaata/connectors/yggtorrent.py +++ b/pynyaata/connectors/yggtorrent.py @@ -21,7 +21,9 @@ class YggTorrent(ConnectorCore): sort_type = 'size' if self.return_type is ConnectorReturn.HISTORY: sort_type = 'publish_date' - sort_page = '&page=%s' % ((self.page - 1) * 50) if self.page > 1 else '' + sort_page = '&page=%s' % ( + (self.page - 1) * 50 + ) if self.page > 1 else '' return '%s/engine/search?name=%s&category=2145&sub_category=%s&do=search&order=desc&sort=%s%s' % ( self.base_url, self.query, self.category, sort_type, sort_page @@ -65,7 +67,8 @@ class YggTorrent(ConnectorCore): (url['href'], tds[3].decode_contents()), 'link': '' '' - '' % (self.base_url, re.search(r'/(\d+)', url['href']).group(1)), + '' % (self.base_url, + re.search(r'/(\d+)', url['href']).group(1)), 'size': tds[5].get_text(), 'date': parse_date(datetime.fromtimestamp(int(tds[4].div.get_text()))), 'seeds': check_seeds, diff --git a/pynyaata/models.py b/pynyaata/models.py index 654d728..4e19641 100644 --- a/pynyaata/models.py +++ b/pynyaata/models.py @@ -3,17 +3,33 @@ from .config import db class AnimeFolder(db.Model): id = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String(length=100, collation='utf8mb4_general_ci'), unique=True, nullable=False) + name = db.Column(db.String( + length=100, + collation='utf8mb4_general_ci' + ), unique=True, nullable=False) path = db.Column(db.String(length=100, collation='utf8mb4_general_ci')) - titles = db.relationship("AnimeTitle", backref="folder", cascade='all,delete-orphan') + titles = db.relationship( + "AnimeTitle", + backref="folder", + cascade='all,delete-orphan' + ) class AnimeTitle(db.Model): id = db.Column(db.Integer, primary_key=True) - name = db.Column(db.String(length=100, collation='utf8mb4_general_ci'), unique=True, nullable=False) - keyword = db.Column(db.Text(collation='utf8mb4_general_ci'), nullable=False) + name = db.Column(db.String( + length=100, + collation='utf8mb4_general_ci' + ), unique=True, nullable=False) + keyword = db.Column(db.Text( + collation='utf8mb4_general_ci' + ), nullable=False) folder_id = db.Column(db.Integer, db.ForeignKey('anime_folder.id')) - links = db.relationship('AnimeLink', backref="title", cascade='all,delete-orphan') + links = db.relationship( + 'AnimeLink', + backref="title", + cascade='all,delete-orphan' + ) class AnimeLink(db.Model):