mirror of
https://gitlab.com/SIGBUS/nyaa.git
synced 2025-02-03 16:01:37 +00:00
site-specific changes for new tracker (#453)
This commit is contained in:
parent
7bef642f4e
commit
d7b413e4d7
40
migrations/versions/6cc823948c5a_add_trackerapi.py
Normal file
40
migrations/versions/6cc823948c5a_add_trackerapi.py
Normal 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 ###
|
|
@ -2,8 +2,6 @@ import json
|
|||
import os
|
||||
from datetime import datetime, timedelta
|
||||
from ipaddress import ip_address
|
||||
from urllib.parse import urlencode
|
||||
from urllib.request import urlopen
|
||||
|
||||
import flask
|
||||
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
|
||||
validate_torrent_post_upload(torrent, upload_form)
|
||||
|
||||
# Add to tracker whitelist
|
||||
db.session.add(models.TrackerApi(torrent.info_hash, 'insert'))
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Store the actual torrent file as well
|
||||
|
@ -340,35 +341,6 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False):
|
|||
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):
|
||||
info_dict_path = torrent.info_dict_path
|
||||
if os.path.exists(info_dict_path):
|
||||
|
|
|
@ -755,6 +755,19 @@ class Ban(db.Model):
|
|||
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
|
||||
|
||||
# Torrent
|
||||
|
@ -856,6 +869,15 @@ class SukebeiReport(ReportBase, db.Model):
|
|||
__flavor__ = 'Sukebei'
|
||||
|
||||
|
||||
# TrackerApi
|
||||
class NyaaTrackerApi(TrackerApiBase, db.Model):
|
||||
__flavor__ = 'Nyaa'
|
||||
|
||||
|
||||
class SukebeiTrackerApi(TrackerApiBase, db.Model):
|
||||
__flavor__ = 'Sukebei'
|
||||
|
||||
|
||||
# Choose our defaults for models.Torrent etc
|
||||
if config['SITE_FLAVOR'] == 'nyaa':
|
||||
Torrent = NyaaTorrent
|
||||
|
@ -868,6 +890,7 @@ if config['SITE_FLAVOR'] == 'nyaa':
|
|||
AdminLog = NyaaAdminLog
|
||||
Report = NyaaReport
|
||||
TorrentNameSearch = NyaaTorrentNameSearch
|
||||
TrackerApi = NyaaTrackerApi
|
||||
|
||||
elif config['SITE_FLAVOR'] == 'sukebei':
|
||||
Torrent = SukebeiTorrent
|
||||
|
@ -880,3 +903,4 @@ elif config['SITE_FLAVOR'] == 'sukebei':
|
|||
AdminLog = SukebeiAdminLog
|
||||
Report = SukebeiReport
|
||||
TorrentNameSearch = SukebeiTorrentNameSearch
|
||||
TrackerApi = SukebeiTrackerApi
|
||||
|
|
|
@ -202,7 +202,7 @@ def _delete_torrent(torrent, form, banform):
|
|||
if not torrent.deleted:
|
||||
torrent.deleted = True
|
||||
action = 'deleted and banned'
|
||||
backend.tracker_api([torrent.info_hash], 'ban')
|
||||
db.session.add(models.TrackerApi(torrent.info_hash, 'remove'))
|
||||
db.session.add(torrent)
|
||||
|
||||
elif form.undelete.data and torrent.deleted:
|
||||
|
@ -211,13 +211,13 @@ def _delete_torrent(torrent, form, banform):
|
|||
if torrent.banned:
|
||||
action = 'undeleted and unbanned'
|
||||
torrent.banned = False
|
||||
backend.tracker_api([torrent.info_hash], 'unban')
|
||||
db.session.add(models.TrackerApi(torrent.info_hash, 'insert'))
|
||||
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(models.TrackerApi(torrent.info_hash, 'insert'))
|
||||
db.session.add(torrent)
|
||||
|
||||
if not action and not ban_torrent:
|
||||
|
|
|
@ -9,7 +9,7 @@ from flask_paginate import Pagination
|
|||
|
||||
from itsdangerous import BadSignature, URLSafeSerializer
|
||||
|
||||
from nyaa import backend, forms, models
|
||||
from nyaa import forms, models
|
||||
from nyaa.extensions import db
|
||||
from nyaa.search import (DEFAULT_MAX_SEARCH_RESULT, DEFAULT_PER_PAGE, SERACH_PAGINATE_DISPLAY_MSG,
|
||||
_generate_query_string, search_db, search_elastic)
|
||||
|
@ -107,20 +107,17 @@ def view_user(user_name):
|
|||
if flask.g.user.is_superadmin:
|
||||
nyaa_banned = 0
|
||||
sukebei_banned = 0
|
||||
info_hashes = []
|
||||
for t in chain(user.nyaa_torrents, user.sukebei_torrents):
|
||||
t.deleted = True
|
||||
t.banned = True
|
||||
info_hashes.append([t.info_hash])
|
||||
db.session.add(t)
|
||||
if isinstance(t, models.NyaaTorrent):
|
||||
db.session.add(models.NyaaTrackerApi(t.info_hash, 'remove'))
|
||||
nyaa_banned += 1
|
||||
else:
|
||||
db.session.add(models.SukebeiTrackerApi(t.info_hash, 'remove'))
|
||||
sukebei_banned += 1
|
||||
|
||||
if info_hashes:
|
||||
backend.tracker_api(info_hashes, 'ban')
|
||||
|
||||
for log_flavour, num in ((models.NyaaAdminLog, nyaa_banned),
|
||||
(models.SukebeiAdminLog, sukebei_banned)):
|
||||
if num > 0:
|
||||
|
|
Loading…
Reference in a new issue