diff --git a/config.example.py b/config.example.py index c4f1d63..00f88e5 100644 --- a/config.example.py +++ b/config.example.py @@ -43,7 +43,9 @@ MAX_FILES_VIEW = 1000 # Setting to make sure main announce url is present in torrent # ENFORCE_MAIN_ANNOUNCE_URL = False -MAIN_ANNOUNCE_URL = '' +MAIN_ANNOUNCE_URL = 'http://127.0.0.1:6881/announce' +TRACKER_API_URL = 'http://127.0.0.1:6881/api' +TRACKER_API_AUTH = 'topsecret' BACKUP_TORRENT_FOLDER = 'torrents' diff --git a/nyaa/backend.py b/nyaa/backend.py index e777e23..2b8c1f8 100644 --- a/nyaa/backend.py +++ b/nyaa/backend.py @@ -1,6 +1,8 @@ import json import os from ipaddress import ip_address +from urllib.parse import urlencode +from urllib.request import urlopen import flask from werkzeug import secure_filename @@ -211,3 +213,25 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False): torrent_file.close() return torrent + + +def tracker_api(info_hashes, method): + url = app.config.get('TRACKER_API_URL') + if not url: + return False + + qs = [] + qs.append(('auth', app.config.get('TRACKER_API_AUTH'))) + qs.append(('method', method)) + + for infohash in info_hashes: + qs.append(('info_hash', infohash)) + + qs = urlencode(qs) + url += '?' + qs + try: + req = urlopen(url) + except: + return False + + return req.status == 200 diff --git a/nyaa/views/torrents.py b/nyaa/views/torrents.py index 365ad05..7dc2c26 100644 --- a/nyaa/views/torrents.py +++ b/nyaa/views/torrents.py @@ -179,23 +179,27 @@ def delete_torrent(torrent_id): db.session.add(torrent) elif form.ban.data and not torrent.banned and editor.is_moderator: + action = 'banned' torrent.banned = True if not torrent.deleted: torrent.deleted = True action = 'deleted and banned' - else: - action = 'banned' + backend.tracker_api([torrent.info_hash], 'ban') db.session.add(torrent) elif form.undelete.data and torrent.deleted: action = 'undeleted' torrent.deleted = False - torrent.banned = False + if torrent.banned: + action = 'undeleted and unbanned' + torrent.banned = False + backend.tracker_api([torrent.info_hash], 'unban') db.session.add(torrent) elif form.unban.data and torrent.banned: action = 'unbanned' torrent.banned = False + backend.tracker_api([torrent.info_hash], 'unban') db.session.add(torrent) if not action: