site-specific changes for new tracker (#453)

This commit is contained in:
A nyaa developer 2018-02-13 00:52:35 +01:00 committed by Arylide
parent 7bef642f4e
commit d7b413e4d7
5 changed files with 73 additions and 40 deletions

View File

@ -0,0 +1,40 @@
"""Add trackerapi table
Revision ID: 6cc823948c5a
Revises: b61e4f6a88cc
Create Date: 2018-02-11 20:57:15.244171
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '6cc823948c5a'
down_revision = 'b61e4f6a88cc'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('nyaa_trackerapi',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('info_hash', sa.BINARY(length=20), nullable=False),
sa.Column('method', sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('sukebei_trackerapi',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('info_hash', sa.BINARY(length=20), nullable=False),
sa.Column('method', sa.String(length=255), nullable=False),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('sukebei_trackerapi')
op.drop_table('nyaa_trackerapi')
# ### end Alembic commands ###

View File

@ -2,8 +2,6 @@ import json
import os import os
from datetime import datetime, timedelta from datetime import datetime, timedelta
from ipaddress import ip_address from ipaddress import ip_address
from urllib.parse import urlencode
from urllib.request import urlopen
import flask import flask
from werkzeug import secure_filename from werkzeug import secure_filename
@ -322,6 +320,9 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False):
# Before final commit, validate the torrent again # Before final commit, validate the torrent again
validate_torrent_post_upload(torrent, upload_form) validate_torrent_post_upload(torrent, upload_form)
# Add to tracker whitelist
db.session.add(models.TrackerApi(torrent.info_hash, 'insert'))
db.session.commit() db.session.commit()
# Store the actual torrent file as well # Store the actual torrent file as well
@ -340,35 +341,6 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False):
return torrent return torrent
def tracker_api(info_hashes, method):
api_url = app.config.get('TRACKER_API_URL')
if not api_url:
return False
# Split list into at most 100 elements
chunk_size = 100
chunk_range = range(0, len(info_hashes), chunk_size)
chunked_info_hashes = (info_hashes[i:i + chunk_size] for i in chunk_range)
for info_hashes_chunk in chunked_info_hashes:
qs = [
('auth', app.config.get('TRACKER_API_AUTH')),
('method', method)
]
qs.extend(('info_hash', info_hash) for info_hash in info_hashes_chunk)
api_url += '?' + urlencode(qs)
try:
req = urlopen(api_url)
except:
return False
if req.status != 200:
return False
return True
def _delete_info_dict(torrent): def _delete_info_dict(torrent):
info_dict_path = torrent.info_dict_path info_dict_path = torrent.info_dict_path
if os.path.exists(info_dict_path): if os.path.exists(info_dict_path):

View File

@ -755,6 +755,19 @@ class Ban(db.Model):
return None return None
class TrackerApiBase(DeclarativeHelperBase):
__tablename_base__ = 'trackerapi'
id = db.Column(db.Integer, primary_key=True)
info_hash = db.Column(BinaryType(length=20), nullable=False)
method = db.Column(db.String(length=255), nullable=False)
# Methods = insert, remove
def __init__(self, info_hash, method):
self.info_hash = info_hash
self.method = method
# Actually declare our site-specific classes # Actually declare our site-specific classes
# Torrent # Torrent
@ -856,6 +869,15 @@ class SukebeiReport(ReportBase, db.Model):
__flavor__ = 'Sukebei' __flavor__ = 'Sukebei'
# TrackerApi
class NyaaTrackerApi(TrackerApiBase, db.Model):
__flavor__ = 'Nyaa'
class SukebeiTrackerApi(TrackerApiBase, db.Model):
__flavor__ = 'Sukebei'
# Choose our defaults for models.Torrent etc # Choose our defaults for models.Torrent etc
if config['SITE_FLAVOR'] == 'nyaa': if config['SITE_FLAVOR'] == 'nyaa':
Torrent = NyaaTorrent Torrent = NyaaTorrent
@ -868,6 +890,7 @@ if config['SITE_FLAVOR'] == 'nyaa':
AdminLog = NyaaAdminLog AdminLog = NyaaAdminLog
Report = NyaaReport Report = NyaaReport
TorrentNameSearch = NyaaTorrentNameSearch TorrentNameSearch = NyaaTorrentNameSearch
TrackerApi = NyaaTrackerApi
elif config['SITE_FLAVOR'] == 'sukebei': elif config['SITE_FLAVOR'] == 'sukebei':
Torrent = SukebeiTorrent Torrent = SukebeiTorrent
@ -880,3 +903,4 @@ elif config['SITE_FLAVOR'] == 'sukebei':
AdminLog = SukebeiAdminLog AdminLog = SukebeiAdminLog
Report = SukebeiReport Report = SukebeiReport
TorrentNameSearch = SukebeiTorrentNameSearch TorrentNameSearch = SukebeiTorrentNameSearch
TrackerApi = SukebeiTrackerApi

View File

@ -202,7 +202,7 @@ def _delete_torrent(torrent, form, banform):
if not torrent.deleted: if not torrent.deleted:
torrent.deleted = True torrent.deleted = True
action = 'deleted and banned' action = 'deleted and banned'
backend.tracker_api([torrent.info_hash], 'ban') db.session.add(models.TrackerApi(torrent.info_hash, 'remove'))
db.session.add(torrent) db.session.add(torrent)
elif form.undelete.data and torrent.deleted: elif form.undelete.data and torrent.deleted:
@ -211,13 +211,13 @@ def _delete_torrent(torrent, form, banform):
if torrent.banned: if torrent.banned:
action = 'undeleted and unbanned' action = 'undeleted and unbanned'
torrent.banned = False torrent.banned = False
backend.tracker_api([torrent.info_hash], 'unban') db.session.add(models.TrackerApi(torrent.info_hash, 'insert'))
db.session.add(torrent) db.session.add(torrent)
elif form.unban.data and torrent.banned: elif form.unban.data and torrent.banned:
action = 'unbanned' action = 'unbanned'
torrent.banned = False torrent.banned = False
backend.tracker_api([torrent.info_hash], 'unban') db.session.add(models.TrackerApi(torrent.info_hash, 'insert'))
db.session.add(torrent) db.session.add(torrent)
if not action and not ban_torrent: if not action and not ban_torrent:

View File

@ -9,7 +9,7 @@ from flask_paginate import Pagination
from itsdangerous import BadSignature, URLSafeSerializer from itsdangerous import BadSignature, URLSafeSerializer
from nyaa import backend, forms, models from nyaa import forms, models
from nyaa.extensions import db from nyaa.extensions import db
from nyaa.search import (DEFAULT_MAX_SEARCH_RESULT, DEFAULT_PER_PAGE, SERACH_PAGINATE_DISPLAY_MSG, from nyaa.search import (DEFAULT_MAX_SEARCH_RESULT, DEFAULT_PER_PAGE, SERACH_PAGINATE_DISPLAY_MSG,
_generate_query_string, search_db, search_elastic) _generate_query_string, search_db, search_elastic)
@ -107,20 +107,17 @@ def view_user(user_name):
if flask.g.user.is_superadmin: if flask.g.user.is_superadmin:
nyaa_banned = 0 nyaa_banned = 0
sukebei_banned = 0 sukebei_banned = 0
info_hashes = []
for t in chain(user.nyaa_torrents, user.sukebei_torrents): for t in chain(user.nyaa_torrents, user.sukebei_torrents):
t.deleted = True t.deleted = True
t.banned = True t.banned = True
info_hashes.append([t.info_hash])
db.session.add(t) db.session.add(t)
if isinstance(t, models.NyaaTorrent): if isinstance(t, models.NyaaTorrent):
db.session.add(models.NyaaTrackerApi(t.info_hash, 'remove'))
nyaa_banned += 1 nyaa_banned += 1
else: else:
db.session.add(models.SukebeiTrackerApi(t.info_hash, 'remove'))
sukebei_banned += 1 sukebei_banned += 1
if info_hashes:
backend.tracker_api(info_hashes, 'ban')
for log_flavour, num in ((models.NyaaAdminLog, nyaa_banned), for log_flavour, num in ((models.NyaaAdminLog, nyaa_banned),
(models.SukebeiAdminLog, sukebei_banned)): (models.SukebeiAdminLog, sukebei_banned)):
if num > 0: if num > 0: