From fbf42ece8e26913b9544a37dfdb686b70a5d2328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?X=C3=A9fir=20Destiny?= Date: Sat, 30 Nov 2019 23:42:35 +0100 Subject: [PATCH] Search page OK --- .env.dist | 10 ++++++++ .gitignore | 1 + config.py | 2 +- connectors.py | 48 +++++++++++++++++++++++++++---------- docker-compose.yml | 19 +++++---------- models.py | 13 +++++----- templates/search.html | 56 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 116 insertions(+), 33 deletions(-) create mode 100644 .env.dist diff --git a/.env.dist b/.env.dist new file mode 100644 index 0000000..aa86d5c --- /dev/null +++ b/.env.dist @@ -0,0 +1,10 @@ +FLASK_APP=app.py +FLASK_ENV=development +FLASK_PORT=5000 +MYSQL_USER=root +MYSQL_PASSWORD=root +MYSQL_DATABASE=www +MYSQL_SERVER=db +ADMIN_USERNAME=admin +ADMIN_PASSWORD=secret +MYSQL_ROOT_PASSWORD=root diff --git a/.gitignore b/.gitignore index 79eb8be..21432d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea/ .venv/ .db/ +.env __pycache__/ test.py diff --git a/config.py b/config.py index 94fbabd..d04f32d 100644 --- a/config.py +++ b/config.py @@ -12,7 +12,7 @@ modules["MySQLdb"] = pymysql db_user = environ.get('MYSQL_USER') db_password = environ.get('MYSQL_PASSWORD') db_name = environ.get('MYSQL_DATABASE') -db_host = environ.get('MYSQL_HOST') +db_host = environ.get('MYSQL_SERVER') if not db_host or not db_user or not db_password or not db_name: print('Missing connection environment variables') exit() diff --git a/connectors.py b/connectors.py index bf6f132..19ab514 100644 --- a/connectors.py +++ b/connectors.py @@ -9,12 +9,19 @@ from sys import platform import requests from bs4 import BeautifulSoup +from models import AnimeLink + class ConnectorReturn(Enum): SEARCH = 1 HISTORY = 2 +class ConnectorLang(Enum): + FR = '🇫🇷' + JP = '🇯🇵' + + class Connector(ABC): blacklist_words = ['Chris44', 'Vol.'] @@ -90,9 +97,9 @@ class Connector(ABC): @staticmethod def get_lang(str_to_test): if re.search('(vf|multi|french)', str_to_test, re.IGNORECASE): - return 'fr' + return ConnectorLang.FR else: - return 'jp' + return ConnectorLang.JP def boldify(self, str_to_replace): if self.query: @@ -106,6 +113,7 @@ class Nyaa(Connector): title = 'Nyaa' favicon = 'nyaa.png' base_url = 'https://nyaa.si' + is_light = False def get_full_search_url(self): sort_type = 'size' @@ -149,10 +157,11 @@ class Nyaa(Connector): continue valid_trs = valid_trs + 1 + href = '%s%s' % (self.base_url, url['href']) self.data.append({ 'lang': self.get_lang(url.string), - 'href': '%s%s' % (self.base_url, url['href']), + 'href': href, 'name': self.boldify(url.string), 'comment': str(urls[0]).replace('/view/', '%s%s' % (self.base_url, '/view/')) if has_comment else '', @@ -163,7 +172,8 @@ class Nyaa(Connector): 'seeds': check_seeds, 'leechs': tds[6].string, 'downloads': check_downloads, - 'class': 'is-%s' % tr['class'][0] + 'class': self.color if AnimeLink.query.filter_by(link=href).first() else 'is-%s' % + tr['class'][0] }) self.on_error = False @@ -175,6 +185,7 @@ class Pantsu(Connector): title = 'Pantsu' favicon = 'pantsu.png' base_url = 'https://nyaa.net' + is_light = False def get_full_search_url(self): sort_type = 4 @@ -211,10 +222,11 @@ class Pantsu(Connector): continue valid_trs = valid_trs + 1 + href = '%s%s' % (self.base_url, url['href']) self.data.append({ 'lang': self.get_lang(url.string), - 'href': '%s%s' % (self.base_url, url['href']), + 'href': href, 'name': self.boldify(url.string), 'comment': '', 'link': tds[2].decode_contents() @@ -227,7 +239,8 @@ class Pantsu(Connector): 'seeds': check_seeds, 'leechs': tds[5].string, 'downloads': check_downloads, - 'class': 'is-%s' % tr['class'][0] + 'class': self.color if AnimeLink.query.filter_by(link=href).first() else 'is-%s' % + tr['class'][0] }) self.on_error = False @@ -239,6 +252,7 @@ class YggTorrent(Connector): title = 'YggTorrent' favicon = 'yggtorrent.png' base_url = 'https://www2.yggtorrent.pe' + is_light = False def get_full_search_url(self): sort_type = 'size' @@ -291,7 +305,7 @@ class YggTorrent(Connector): 'seeds': check_seeds, 'leechs': tds[8].string, 'downloads': check_downloads, - 'class': '' + 'class': self.color if AnimeLink.query.filter_by(link=url['href']).first() else '' }) self.on_error = False @@ -303,6 +317,7 @@ class AnimeUltime(Connector): title = 'Anime-Ultime' favicon = 'animeultime.png' base_url = 'http://www.anime-ultime.net' + is_light = True def get_full_search_url(self): from_date = '' @@ -336,23 +351,27 @@ class AnimeUltime(Connector): continue url = tds[0].a + href = '%s/%s' % (self.base_url, url['href']) self.data.append({ - 'lang': 'jp', + 'lang': ConnectorLang.JP, 'href': '%s/%s' % (self.base_url, url['href']), 'name': url.decode_contents(), - 'type': tds[1].string + 'type': tds[1].string, + 'class': self.color if AnimeLink.query.filter_by(link=href).first() else '' }) else: player = html.select('div.AUVideoPlayer') name = html.select('h1') ani_type = html.select('div.titre') + href = '%s/file-0-1/%s' % (self.base_url, player[0]['data-serie']) self.data.append({ - 'lang': 'jp', + 'lang': ConnectorLang.JP, 'href': '%s/file-0-1/%s' % (self.base_url, player[0]['data-serie']), 'name': self.boldify(name[0].string), - 'type': ani_type[0].string.replace(':', '') + 'type': ani_type[0].string.replace(':', ''), + 'class': self.color if AnimeLink.query.filter_by(link=href).first() else '' }) self.on_error = False @@ -378,13 +397,15 @@ class AnimeUltime(Connector): locale.setlocale(locale.LC_ALL, ('fr_FR', 'UTF-8')) release_date = datetime.strptime(h3s[i].string, '%A %d %B %Y : ').strftime('%Y-%m-%d %H:%M:%S') locale.setlocale(locale.LC_ALL, current_locale) + href = '%s/%s' % (self.base_url, link['href']) self.data.append({ - 'lang': 'jp', + 'lang': ConnectorLang.JP, 'href': '%s/%s' % (self.base_url, link['href']), 'name': link.string, 'type': tds[4].string, - 'date': release_date + 'date': release_date, + 'class': self.color if AnimeLink.query.filter_by(link=href).first() else '' }) self.on_error = False @@ -394,6 +415,7 @@ class Other(Connector): color = 'is-danger' title = 'Other' favicon = 'blank.png' + is_light = True def get_full_search_url(self): pass diff --git a/docker-compose.yml b/docker-compose.yml index a8c850e..d2fdb44 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,16 +7,9 @@ services: - "5000:5000" entrypoint: python3 app.py working_dir: /app - environment: - FLASK_APP: app.py - FLASK_ENV: development - FLASK_PORT: 5000 - MYSQL_USER: root - MYSQL_PASSWORD: root - MYSQL_DATABASE: www - MYSQL_HOST: db - ADMIN_USERNAME: admin - ADMIN_PASSWORD: secret + env_file: + - .env.dist + - .env volumes: - .:/app @@ -24,8 +17,8 @@ services: image: mariadb ports: - "3306:3306" - environment: - MYSQL_ROOT_PASSWORD: root - MYSQL_DATABASE: www + env_file: + - .env.dist + - .env volumes: - ./.db:/var/lib/mysql diff --git a/models.py b/models.py index 29b8852..6481184 100644 --- a/models.py +++ b/models.py @@ -8,16 +8,15 @@ from config import db class AnimeFolder(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text, unique=True, nullable=False) - titles = db.relationship("AnimeTitle", back_populates="folder") + titles = db.relationship("AnimeTitle", backref="folder") class AnimeTitle(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text, unique=True, nullable=False) keyword = db.Column(db.Text, nullable=False) - folder_id = db.Column(db.Integer, db.ForeignKey('folder.id')) - folder = db.relationship('AnimeFolder', back_populates="titles") - links = db.relationship('AnimeLink', back_populates="title") + folder_id = db.Column(db.Integer, db.ForeignKey('anime_folder.id')) + links = db.relationship('AnimeLink', backref="title") class AnimeLink(db.Model): @@ -26,9 +25,11 @@ class AnimeLink(db.Model): season = db.Column(db.Text, nullable=False) comment = db.Column(db.Text) vf = db.Column(db.Boolean, nullable=False) - title_id = db.Column(db.Integer, db.ForeignKey('title.id')) - title = db.relationship('AnimeTitle', back_populates="links") + title_id = db.Column(db.Integer, db.ForeignKey('anime_title.id')) class SearchForm(FlaskForm): q = SearchField('search', validators=[DataRequired]) + + +db.create_all() diff --git a/templates/search.html b/templates/search.html index 997af76..f3b5c6a 100644 --- a/templates/search.html +++ b/templates/search.html @@ -26,8 +26,64 @@ {% for connector in connectors %} {% for torrent in connector.data %} + {% if not loop.index0 %} + {{ connector.title }} + {% endif %} + + + {{ torrent.lang.value }} + + {{ torrent.name|safe }} + + + + {% if connector.is_light %} + + {{ torrent.type }} + + {% else %} + + {{ torrent.comment|safe }} + + + {{ torrent.link|safe }} + + + {{ torrent.size }} + + + {{ torrent.date }} + + + {{ torrent.seeds }} + + + {{ torrent.leechs }} + + + {{ torrent.downloads }} + + {% endif %} + {% endfor %} + + {% if connector.is_more %} + + + More ... + + + {% endif %} + + {% if connector.on_error %} + + + Error, can't grab data from {{ connector.title }} + Go to the website -> + + + {% endif %} {% endfor %}