From a4a9e236072486cf31b82ff40f3378b9a6f2c291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?X=C3=A9fir=20Destiny?= Date: Thu, 5 Dec 2019 18:53:23 +0100 Subject: [PATCH] list page OK + fix cache --- app.py | 28 ++++++++++++++++++++-------- connectors.py | 22 ++++++++++++---------- templates/latest.html | 4 ++-- templates/list.html | 32 ++++++++++++++++++++++++++++++++ templates/search.html | 2 +- 5 files changed, 67 insertions(+), 21 deletions(-) diff --git a/app.py b/app.py index 6058062..c83530f 100644 --- a/app.py +++ b/app.py @@ -15,6 +15,21 @@ def verify_password(username, password): return username is admin_username and password is admin_password +@app.template_filter('boldify') +def boldify(name): + query = request.args.get('q') + name = Connector.boldify(name, query) + for title in AnimeTitle.query.all(): + if title.keyword is not query: + name = Connector.boldify(name, title.keyword) + return name + + +@app.template_filter('shorten') +def shorten(str): + return str[:30] + '...' if len(str) > 30 else str + + @app.route('/') def home(): return render_template('layout.html', form=SearchForm()) @@ -52,23 +67,20 @@ def latest(): results = [] for torrent in torrents: results = results + torrent.data - results.sort(key=itemgetter('date'), reverse=True) - - for keyword in AnimeTitle.query.all(): - for result in results: - result['name'] = Connector.boldify(result['name'], keyword) - for result in results: result['self'] = Connector.get_instance(result['href'], '') + results.sort(key=itemgetter('date'), reverse=True) return render_template('latest.html', form=SearchForm(), torrents=results, page=page) @app.route('/list') def list_animes(): - results = [] + filters = request.args.get('s', 'nyaa,yggtorrent').split(',') + titles = AnimeTitle.query.order_by(AnimeTitle.name).all() - return render_template('list.html', form=SearchForm(), torrents=results) + return render_template('list.html', form=SearchForm(), titles=titles, filters=filters, connector=Connector, + flags=ConnectorLang) @app.route('/admin') diff --git a/connectors.py b/connectors.py index 05dc40a..be80ff0 100644 --- a/connectors.py +++ b/connectors.py @@ -41,20 +41,22 @@ class Cache: 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'] + connector.is_more = cached_data['is_more'] 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 - } + if not connector.on_error: + self.CACHE_DATA[connector.__class__.__name__][f.__name__][connector.query][connector.page] = { + 'data': connector.data, + 'timeout': timestamp + self.CACHE_TIMEOUT, + 'is_more': connector.is_more + } return ret return wrapper @@ -247,7 +249,7 @@ class Nyaa(Connector): self.data.append({ 'lang': self.get_lang(url.string), 'href': href, - 'name': self.boldify(url.string, self.query), + 'name': url.string, 'comment': str(urls[0]).replace('/view/', '%s%s' % (self.base_url, '/view/')) if has_comment else '', 'link': tds[2].decode_contents().replace('/download/', @@ -330,7 +332,7 @@ class Pantsu(Connector): self.data.append({ 'lang': self.get_lang(url.string), 'href': href, - 'name': self.boldify(url.string, self.query), + 'name': url.string, 'comment': '', 'link': tds[2].decode_contents() .replace('icon-magnet', 'fa fa-fw fa-magnet') @@ -399,7 +401,7 @@ class YggTorrent(Connector): self.data.append({ 'lang': self.get_lang(url.string), 'href': url['href'], - 'name': self.boldify(url.string, self.query), + 'name': url.string, 'comment': '%s' % (url['href'], tds[3].string), 'link': '' @@ -473,7 +475,7 @@ class AnimeUltime(Connector): self.data.append({ 'lang': ConnectorLang.JP, 'href': '%s/%s' % (self.base_url, url['href']), - 'name': url.decode_contents(), + 'name': url.get_text(), 'type': tds[1].string, 'class': self.color if AnimeLink.query.filter_by(link=href).first() else '' }) @@ -486,7 +488,7 @@ class AnimeUltime(Connector): self.data.append({ 'lang': ConnectorLang.JP, 'href': '%s/file-0-1/%s' % (self.base_url, player[0]['data-serie']), - 'name': self.boldify(name[0].string, self.query), + 'name': name[0].string, 'type': ani_type[0].string.replace(':', ''), 'class': self.color if AnimeLink.query.filter_by(link=href).first() else '' }) diff --git a/templates/latest.html b/templates/latest.html index 49fec88..3ce1135 100644 --- a/templates/latest.html +++ b/templates/latest.html @@ -31,7 +31,7 @@   {{ torrent.lang.value }} - {{ torrent.name }} + {{ torrent.name|boldify|safe }} @@ -47,7 +47,7 @@   {{ torrent.lang.value }} - {{ torrent.name }} + {{ torrent.name|boldify|safe }} diff --git a/templates/list.html b/templates/list.html index 1c7ca16..76978c5 100644 --- a/templates/list.html +++ b/templates/list.html @@ -12,6 +12,38 @@ + {% for title in titles %} + {% for link in title.links %} + + {% if not loop.index0 %} + + {{ title.name }} + + {% endif %} + + {% set instance = connector.get_instance(link.link, '') %} + + {{ flags.FR.value if link.vf else flags.JP.value }} + + {{ link.link|shorten }} + + + + + {{ link.season }} + + + {% if not loop.index0 %} + + + + + + {% endif %} + + {% endfor %} + {% endfor %} + {% endblock %} diff --git a/templates/search.html b/templates/search.html index 470618d..9d4eb5c 100644 --- a/templates/search.html +++ b/templates/search.html @@ -34,7 +34,7 @@ {{ torrent.lang.value }} - {{ torrent.name|safe }} + {{ torrent.name|boldify|safe }}