list page OK + fix cache

This commit is contained in:
Michel Roux 2019-12-05 18:53:23 +01:00
parent 76ca5549de
commit a4a9e23607
5 changed files with 67 additions and 21 deletions

28
app.py
View File

@ -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')

View File

@ -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': '<a href="%s#comm" target="_blank"><i class="fa fa-comments-o"></i>%s</a>' %
(url['href'], tds[3].string),
'link': '<a href="%s/engine/download_torrent?id=%s">'
@ -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 ''
})

View File

@ -31,7 +31,7 @@
<i>&nbsp;</i>
{{ torrent.lang.value }}
<a href="{{ torrent.href }}" target="_blank">
{{ torrent.name }}
{{ torrent.name|boldify|safe }}
</a>
</td>
<td>
@ -47,7 +47,7 @@
<i>&nbsp;</i>
{{ torrent.lang.value }}
<a href="{{ torrent.href }}" target="_blank">
{{ torrent.name }}
{{ torrent.name|boldify|safe }}
</a>
</td>
<td>

View File

@ -12,6 +12,38 @@
</thead>
<tbody>
{% for title in titles %}
{% for link in title.links %}
<tr>
{% if not loop.index0 %}
<td rowspan="{{ title.links|length }}">
{{ title.name }}
</td>
{% endif %}
{% set instance = connector.get_instance(link.link, '') %}
<td class="{{ instance.color }}">
{{ flags.FR.value if link.vf else flags.JP.value }}
<a href="{{ link.link }}" target="_blank">
{{ link.link|shorten }}
</a>
</td>
<td>
{{ link.season }}
</td>
{% if not loop.index0 %}
<td rowspan="{{ title.links|length }}">
<a href="{{ url_for('search', q=title.keyword) }}" target="_blank">
<i class="fa fa-search"></i>
</a>
</td>
{% endif %}
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
{% endblock %}

View File

@ -34,7 +34,7 @@
<td>
{{ torrent.lang.value }}
<a href="{{ torrent.href }}" target="_blank">
{{ torrent.name|safe }}
{{ torrent.name|boldify|safe }}
</a>
</td>