This repository has been archived on 2023-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
PyNyaaTa/app.py

190 lines
6.1 KiB
Python
Raw Normal View History

2020-03-22 16:53:50 +00:00
from logging import getLogger
2019-12-15 19:29:19 +00:00
from operator import attrgetter, itemgetter
2020-03-22 16:53:50 +00:00
from time import sleep
2019-11-25 21:52:22 +00:00
2019-12-07 22:11:14 +00:00
from flask import redirect, render_template, request, url_for
2019-11-25 21:52:22 +00:00
from config import app, auth, db, ADMIN_USERNAME, ADMIN_PASSWORD, APP_PORT
2019-11-30 15:52:13 +00:00
from connectors import *
2019-12-11 18:17:36 +00:00
from models import AnimeFolder, AnimeTitle, DeleteForm, SearchForm, EditForm
2019-11-25 21:52:22 +00:00
2019-11-28 21:20:22 +00:00
@auth.verify_password
def verify_password(username, password):
2019-12-07 22:11:14 +00:00
return username == ADMIN_USERNAME and password == ADMIN_PASSWORD
2019-11-25 21:52:22 +00:00
2019-12-05 17:53:23 +00:00
@app.template_filter('boldify')
def boldify(name):
2019-12-17 14:12:02 +00:00
query = request.args.get('q', '')
2019-12-05 17:53:23 +00:00
name = Connector.boldify(name, query)
2019-12-17 13:55:26 +00:00
for keyword in db.session.query(AnimeTitle.keyword.distinct()).all():
2019-12-17 14:04:58 +00:00
if keyword[0].lower() != query.lower():
2019-12-17 13:55:26 +00:00
name = Connector.boldify(name, keyword[0])
2019-12-05 17:53:23 +00:00
return name
2019-12-07 22:11:14 +00:00
@app.template_filter('flagify')
def flagify(is_vf):
return ConnectorLang.FR.value if is_vf else ConnectorLang.JP.value
@app.template_filter('colorify')
def colorify(model):
return Connector.get_instance(model.link, model.title.keyword).color
2019-12-05 17:53:23 +00:00
2019-11-25 21:52:22 +00:00
@app.route('/')
2019-11-29 14:04:32 +00:00
def home():
2019-12-12 13:37:56 +00:00
return render_template('layout.html', search_form=SearchForm(), title='Animes torrents search engine')
2019-11-29 14:04:32 +00:00
@app.route('/search')
def search():
2019-11-30 15:52:13 +00:00
query = request.args.get('q')
if not query:
return redirect(url_for('home'))
results = [
Nyaa(query).run(),
Pantsu(query).run(),
2019-12-01 17:30:24 +00:00
YggTorrent(query).run(),
YggAnimation(query).run(),
2019-11-30 15:52:13 +00:00
AnimeUltime(query).run(),
]
2019-12-04 18:58:35 +00:00
2019-12-10 21:19:31 +00:00
return render_template('search.html', search_form=SearchForm(), connectors=results)
2019-11-30 15:52:13 +00:00
@app.route('/latest')
2019-12-15 17:53:19 +00:00
@app.route('/latest/<int:page>')
def latest(page=1):
2019-12-01 17:30:24 +00:00
torrents = [
2019-12-03 21:43:53 +00:00
Nyaa('', return_type=ConnectorReturn.HISTORY, page=page).run(),
Pantsu('', return_type=ConnectorReturn.HISTORY, page=page).run(),
YggTorrent('', return_type=ConnectorReturn.HISTORY, page=page).run(),
YggAnimation('', return_type=ConnectorReturn.HISTORY, page=page).run(),
AnimeUltime('', return_type=ConnectorReturn.HISTORY, page=page).run(),
2019-12-01 17:30:24 +00:00
]
results = []
for torrent in torrents:
results = results + torrent.data
2019-12-04 18:58:35 +00:00
for result in results:
result['self'] = Connector.get_instance(result['href'], '')
2019-12-05 17:53:23 +00:00
results.sort(key=itemgetter('date'), reverse=True)
2019-12-04 18:58:35 +00:00
2019-12-10 21:19:31 +00:00
return render_template('latest.html', search_form=SearchForm(), torrents=results, page=page)
2019-11-30 15:52:13 +00:00
@app.route('/list')
2019-12-16 20:10:38 +00:00
@app.route('/list/<url_filters>')
2019-12-16 19:48:17 +00:00
def list_animes(url_filters='nyaa,yggtorrent'):
2019-12-07 09:17:29 +00:00
filters = None
2019-12-16 19:48:17 +00:00
for i, to_filter in enumerate(url_filters.split(',')):
2019-12-07 09:17:29 +00:00
if not i:
filters = AnimeLink.link.contains(to_filter)
else:
filters = filters | AnimeLink.link.contains(to_filter)
2019-12-03 21:43:53 +00:00
2019-12-07 09:17:29 +00:00
titles = db.session.query(AnimeTitle, AnimeLink).join(AnimeLink).filter(filters).order_by(AnimeTitle.name).all()
results = {}
for title, link in titles:
if title.id not in results:
results[title.id] = [link]
else:
results[title.id].append(link)
2019-12-10 21:19:31 +00:00
return render_template('list.html', search_form=SearchForm(), titles=results)
2019-11-30 15:52:13 +00:00
2019-12-15 15:22:39 +00:00
@app.route('/admin', methods=['GET', 'POST'])
2019-11-30 15:52:13 +00:00
@auth.login_required
def admin():
2019-12-07 22:11:14 +00:00
folders = AnimeFolder.query.all()
2019-12-15 19:29:19 +00:00
for folder in folders:
2019-12-15 19:46:13 +00:00
for title in folder.titles:
title.links.sort(key=attrgetter('season'))
2019-12-15 19:29:19 +00:00
folder.titles.sort(key=attrgetter('name'))
2019-12-09 20:24:35 +00:00
form = DeleteForm(request.form)
2019-12-15 15:22:39 +00:00
2019-12-09 20:24:35 +00:00
if form.validate_on_submit():
link = AnimeLink.query.filter_by(id=form.id.data).first()
2019-12-15 15:22:39 +00:00
if link:
title = link.title
2020-03-21 19:17:31 +00:00
folder = title.folder
2019-12-15 15:22:39 +00:00
db.session.delete(link)
2020-03-21 19:17:31 +00:00
db.session.commit()
2019-12-15 15:22:39 +00:00
if not len(title.links):
db.session.delete(title)
2020-03-21 19:17:31 +00:00
db.session.commit()
if not len(folder.titles):
db.session.delete(folder)
db.session.commit()
2019-12-15 16:58:59 +00:00
form.message = '%s (%s) has been successfully deleted' % (title.name, link.season)
2019-12-15 15:22:39 +00:00
else:
form._errors = {'id': ['Id %s was not found in the database' % form.id.data]}
2019-12-10 21:19:31 +00:00
2019-12-15 15:22:39 +00:00
return render_template('admin/list.html', search_form=SearchForm(), folders=folders, action_form=form)
2019-12-10 21:19:31 +00:00
2020-03-21 19:17:31 +00:00
def clean_model(obj):
for attr in dir(obj):
if not attr.startswith('_') and getattr(obj, attr) is None:
try:
setattr(obj, attr, '')
except Exception:
pass
return obj
2019-12-15 15:22:39 +00:00
@app.route('/admin/edit', methods=['GET', 'POST'])
@app.route('/admin/edit/<int:link_id>', methods=['GET', 'POST'])
2019-12-10 21:19:31 +00:00
@auth.login_required
2019-12-15 15:22:39 +00:00
def admin_edit(link_id=None):
2020-03-21 19:17:31 +00:00
folders = AnimeFolder.query.all()
2019-12-11 13:45:55 +00:00
form = EditForm(request.form)
2019-12-15 15:22:39 +00:00
2019-12-11 13:45:55 +00:00
if form.validate_on_submit():
2020-03-21 19:17:31 +00:00
folder = AnimeFolder.query.filter_by(name=form.folder.data).first()
folder = folder if folder else AnimeFolder()
folder.name = form.folder.data
db.session.add(folder)
2019-12-13 15:49:11 +00:00
title = AnimeTitle.query.filter_by(name=form.name.data).first()
title = title if title else AnimeTitle()
2020-03-21 19:17:31 +00:00
title.folder_id = folder.id
2019-12-13 15:49:11 +00:00
title.name = form.name.data
title.keyword = form.keyword.data.lower() if form.keyword.data else title.keyword
2019-12-11 13:45:55 +00:00
db.session.add(title)
2019-12-13 15:49:11 +00:00
link = AnimeLink.query.filter_by(id=form.id.data).first()
link = link if link else AnimeLink()
link.title_id = title.id
link.link = form.link.data
link.season = form.season.data
link.comment = form.comment.data
link.vf = form.is_vf.data
2019-12-11 13:45:55 +00:00
db.session.add(link)
db.session.commit()
2019-12-15 15:22:39 +00:00
return redirect(url_for('admin'))
if link_id:
link = AnimeLink.query.filter_by(id=link_id).first()
2019-12-15 18:06:37 +00:00
form.folder.data = link.title.folder.id
2019-12-11 13:45:55 +00:00
else:
2020-03-21 19:17:31 +00:00
link = clean_model(AnimeLink())
link.title = clean_model(AnimeTitle())
2019-12-26 17:42:53 +00:00
link.vf = False
2019-12-15 15:22:39 +00:00
2020-03-21 19:17:31 +00:00
return render_template('admin/edit.html', search_form=SearchForm(), link=link, folders=folders, action_form=form)
2019-12-11 13:45:55 +00:00
2019-11-28 21:20:22 +00:00
if __name__ == '__main__':
2020-03-02 15:16:50 +00:00
while True:
try:
app.run('0.0.0.0', APP_PORT, IS_DEBUG)
2020-03-22 16:53:50 +00:00
except Exception as e:
getLogger().exception(e)
sleep(10)
2020-03-02 15:16:50 +00:00
pass