Custom decorator + cache /o/

This commit is contained in:
Michel Roux 2019-12-04 19:58:35 +01:00
parent 931df1225f
commit 76ca5549de
2 changed files with 227 additions and 170 deletions

4
app.py
View File

@ -33,6 +33,7 @@ def search():
YggAnimation(query).run(),
AnimeUltime(query).run(),
]
return render_template('search.html', form=SearchForm(), connectors=results)
@ -57,6 +58,9 @@ def latest():
for result in results:
result['name'] = Connector.boldify(result['name'], keyword)
for result in results:
result['self'] = Connector.get_instance(result['href'], '')
return render_template('latest.html', form=SearchForm(), torrents=results, page=page)

View File

@ -3,6 +3,7 @@ import re
from abc import ABC, abstractmethod
from datetime import datetime, timedelta
from enum import Enum
from functools import wraps
from subprocess import run
from sys import platform
@ -22,6 +23,46 @@ class ConnectorLang(Enum):
JP = '🇯🇵'
class Cache:
CACHE_TIMEOUT = 60 * 60
CACHE_DATA = {}
def cache_data(self, f):
@wraps(f)
def wrapper(*args, **kwds):
connector = args[0]
timestamp = datetime.now().timestamp()
if connector.__class__.__name__ not in self.CACHE_DATA:
self.CACHE_DATA[connector.__class__.__name__] = {}
if f.__name__ not in self.CACHE_DATA[connector.__class__.__name__]:
self.CACHE_DATA[connector.__class__.__name__][f.__name__] = {}
if connector.query not in self.CACHE_DATA[connector.__class__.__name__][f.__name__]:
self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query] = {}
if connector.page not in self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query]:
self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query][connector.page] = {
'data': {},
'timeout': 0
}
cached_data = self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query][connector.page]
if cached_data['timeout'] > timestamp:
connector.data = cached_data['data']
return
ret = f(*args, **kwds)
self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query][connector.page] = {
'data': connector.data,
'timeout': timestamp + self.CACHE_TIMEOUT
}
return ret
return wrapper
ConnectorCache = Cache()
class Connector(ABC):
blacklist_words = ['Chris44', 'Vol.']
@ -71,6 +112,7 @@ class Connector(ABC):
def search(self):
pass
@abstractmethod
def get_history(self):
pass
@ -110,6 +152,17 @@ class Connector(ABC):
return {'http_code': http_code, 'output': output}
@staticmethod
def abord_on_success(f):
@wraps(f)
def wrapper(*args, **kwds):
connector = args[0]
if not connector.on_error:
return
return f(*args, **kwds)
return wrapper
@staticmethod
def get_instance(url, query):
if 'nyaa.si' in url:
@ -157,8 +210,9 @@ class Nyaa(Connector):
def get_history(self):
self.search()
@ConnectorCache.cache_data
@Connector.abord_on_success
def search(self):
if self.on_error:
response = self.curl_content(self.get_full_search_url())
if response['http_code'] is 200:
@ -191,7 +245,6 @@ class Nyaa(Connector):
href = '%s%s' % (self.base_url, url['href'])
self.data.append({
'self': self,
'lang': self.get_lang(url.string),
'href': href,
'name': self.boldify(url.string, self.query),
@ -231,8 +284,9 @@ class Pantsu(Connector):
def get_history(self):
self.search()
@ConnectorCache.cache_data
@Connector.abord_on_success
def search(self):
if self.on_error:
response = self.curl_content(self.get_full_search_url())
if response['http_code'] is 200:
@ -274,7 +328,6 @@ class Pantsu(Connector):
locale.setlocale(locale.LC_ALL, current_locale)
self.data.append({
'self': self,
'lang': self.get_lang(url.string),
'href': href,
'name': self.boldify(url.string, self.query),
@ -316,8 +369,10 @@ class YggTorrent(Connector):
def get_history(self):
self.search()
@ConnectorCache.cache_data
@Connector.abord_on_success
def search(self):
if self.category and self.on_error:
if self.category:
response = self.curl_content(self.get_full_search_url())
if response['http_code'] is 200:
@ -342,7 +397,6 @@ class YggTorrent(Connector):
valid_trs = valid_trs + 1
self.data.append({
'self': self,
'lang': self.get_lang(url.string),
'href': url['href'],
'name': self.boldify(url.string, self.query),
@ -392,8 +446,9 @@ class AnimeUltime(Connector):
return '%s/%s-0-1/%s' % (self.base_url, sort_type, from_date)
@ConnectorCache.cache_data
@Connector.abord_on_success
def search(self):
if self.on_error:
response = self.curl_content(self.get_full_search_url(), {'search': self.query})
if response['http_code'] is 200:
@ -416,7 +471,6 @@ class AnimeUltime(Connector):
href = '%s/%s' % (self.base_url, url['href'])
self.data.append({
'self': self,
'lang': ConnectorLang.JP,
'href': '%s/%s' % (self.base_url, url['href']),
'name': url.decode_contents(),
@ -430,7 +484,6 @@ class AnimeUltime(Connector):
href = '%s/file-0-1/%s' % (self.base_url, player[0]['data-serie'])
self.data.append({
'self': self,
'lang': ConnectorLang.JP,
'href': '%s/file-0-1/%s' % (self.base_url, player[0]['data-serie']),
'name': self.boldify(name[0].string, self.query),
@ -440,8 +493,9 @@ class AnimeUltime(Connector):
self.on_error = False
@ConnectorCache.cache_data
@Connector.abord_on_success
def get_history(self):
if self.on_error:
response = self.curl_content(self.get_full_search_url())
if response['http_code'] is 200:
@ -464,7 +518,6 @@ class AnimeUltime(Connector):
href = '%s/%s' % (self.base_url, link['href'])
self.data.append({
'self': self,
'lang': ConnectorLang.JP,
'href': '%s/%s' % (self.base_url, link['href']),
'name': link.string,