217 lines
8.0 KiB
Python
217 lines
8.0 KiB
Python
from flask import Blueprint, render_template, session, redirect, url_for, jsonify, request
|
|
import requests
|
|
import sqlite3
|
|
|
|
site_bp = Blueprint('site', __name__)
|
|
|
|
def get_db_connection():
|
|
conn = sqlite3.connect('pt_manager.db')
|
|
conn.row_factory = sqlite3.Row
|
|
return conn
|
|
|
|
def get_nexusphp_config(site_id=None):
|
|
conn = get_db_connection()
|
|
|
|
if site_id:
|
|
site = conn.execute("SELECT * FROM sites WHERE id = ?", (site_id,)).fetchone()
|
|
conn.close()
|
|
if site:
|
|
return {
|
|
'site_url': site['url'],
|
|
'api_token': site['api_token'] if site['api_token'] else ''
|
|
}
|
|
else:
|
|
# Get the first enabled site
|
|
site = conn.execute("SELECT * FROM sites WHERE enabled = 1 LIMIT 1").fetchone()
|
|
conn.close()
|
|
if site:
|
|
return {
|
|
'site_url': site['url'],
|
|
'api_token': site['api_token'] if site['api_token'] else ''
|
|
}
|
|
|
|
# Fallback to default settings
|
|
site_url = conn.execute("SELECT value FROM settings WHERE key = 'nexusphp_site_url'").fetchone()
|
|
api_token = conn.execute("SELECT value FROM settings WHERE key = 'nexusphp_api_token'").fetchone()
|
|
conn.close()
|
|
|
|
return {
|
|
'site_url': site_url['value'] if site_url else 'https://www.ptskit.org',
|
|
'api_token': api_token['value'] if api_token else ''
|
|
}
|
|
|
|
def get_all_sites():
|
|
conn = get_db_connection()
|
|
sites = conn.execute("SELECT * FROM sites WHERE enabled = 1").fetchall()
|
|
conn.close()
|
|
return sites
|
|
|
|
@site_bp.route('/site')
|
|
def site_index():
|
|
if 'user_id' not in session:
|
|
return redirect(url_for('auth.login'))
|
|
|
|
sites = get_all_sites()
|
|
return render_template('site/index.html', sites=sites)
|
|
|
|
@site_bp.route('/site/<int:site_id>/profile')
|
|
def profile(site_id):
|
|
if 'user_id' not in session:
|
|
return redirect(url_for('auth.login'))
|
|
|
|
sites = get_all_sites()
|
|
config = get_nexusphp_config(site_id)
|
|
headers = {
|
|
'Accept': 'application/json',
|
|
# 'Authorization': f'Bearer {config["api_token"]}'
|
|
'Authorization': f'Bearer 262|wEGbxaqybJ6ZfLZAAtxX0oiQTymFcelHpc6YHims27a70898'
|
|
|
|
}
|
|
|
|
try:
|
|
response = requests.get(f'{config["site_url"]}/api/v1/profile', headers=headers)
|
|
if response.status_code == 200:
|
|
profile_data = response.json()
|
|
return render_template('site/profile.html', profile=profile_data, sites=sites, current_site_id=site_id)
|
|
else:
|
|
return render_template('site/profile.html', error='Failed to fetch profile data', sites=sites, current_site_id=site_id)
|
|
except Exception as e:
|
|
return render_template('site/profile.html', error=str(e), sites=sites, current_site_id=site_id)
|
|
|
|
@site_bp.route('/site/<int:site_id>/torrents')
|
|
def torrents(site_id):
|
|
if 'user_id' not in session:
|
|
return redirect(url_for('auth.login'))
|
|
|
|
sites = get_all_sites()
|
|
config = get_nexusphp_config(site_id)
|
|
headers = {
|
|
'Accept': 'application/json',
|
|
# 'Authorization': f'Bearer {config["api_token"]}'
|
|
'Authorization': f'Bearer 262|wEGbxaqybJ6ZfLZAAtxX0oiQTymFcelHpc6YHims27a70898'
|
|
}
|
|
|
|
# Get query parameters
|
|
page = request.args.get('page', 1, type=int)
|
|
per_page = request.args.get('per_page', 20, type=int)
|
|
sort = request.args.get('sort', '-seeders')
|
|
title_filter = request.args.get('title', '')
|
|
|
|
params = {
|
|
'page': page,
|
|
'per_page': per_page,
|
|
'sorts': sort
|
|
}
|
|
|
|
if title_filter:
|
|
params['filter[title]'] = title_filter
|
|
|
|
try:
|
|
response = requests.get(f'{config["site_url"]}/api/v1/torrents',
|
|
headers=headers, params=params)
|
|
if response.status_code == 200:
|
|
torrents_data = response.json()
|
|
return render_template('site/torrents.html', torrents=torrents_data,
|
|
page=page, per_page=per_page, sort=sort, title_filter=title_filter,
|
|
sites=sites, current_site_id=site_id)
|
|
else:
|
|
return render_template('site/torrents.html', error='Failed to fetch torrents data',
|
|
sites=sites, current_site_id=site_id)
|
|
except Exception as e:
|
|
return render_template('site/torrents.html', error=str(e), sites=sites, current_site_id=site_id)
|
|
|
|
@site_bp.route('/site/<int:site_id>/torrent/<int:torrent_id>')
|
|
def torrent_detail(site_id, torrent_id):
|
|
if 'user_id' not in session:
|
|
return jsonify({'error': 'Authentication required'}), 401
|
|
|
|
config = get_nexusphp_config(site_id)
|
|
headers = {
|
|
'Accept': 'application/json',
|
|
# 'Authorization': f'Bearer {config["api_token"]}'
|
|
'Authorization': f'Bearer 262|wEGbxaqybJ6ZfLZAAtxX0oiQTymFcelHpc6YHims27a70898'
|
|
}
|
|
|
|
try:
|
|
response = requests.get(f'{config["site_url"]}/api/v1/detail/{torrent_id}?includes=user,tags&include_fields[torrent]=description,download_url', headers=headers)
|
|
if response.status_code == 200:
|
|
torrent_data = response.json()
|
|
return render_template('site/torrent_detail.html', torrent=torrent_data)
|
|
else:
|
|
return jsonify({'error': 'Failed to fetch torrent data'}), response.status_code
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@site_bp.route('/site/<int:site_id>/bookmarks')
|
|
def bookmarks(site_id):
|
|
if 'user_id' not in session:
|
|
return redirect(url_for('auth.login'))
|
|
|
|
sites = get_all_sites()
|
|
config = get_nexusphp_config(site_id)
|
|
headers = {
|
|
'Accept': 'application/json',
|
|
'Authorization': f'Bearer {config["api_token"]}'
|
|
}
|
|
|
|
try:
|
|
response = requests.get(f'{config["site_url"]}/api/v1/bookmarks', headers=headers)
|
|
if response.status_code == 200:
|
|
bookmarks_data = response.json()
|
|
return render_template('site/bookmarks.html', bookmarks=bookmarks_data,
|
|
sites=sites, current_site_id=site_id)
|
|
else:
|
|
return render_template('site/bookmarks.html', error='Failed to fetch bookmarks data',
|
|
sites=sites, current_site_id=site_id)
|
|
except Exception as e:
|
|
return render_template('site/bookmarks.html', error=str(e), sites=sites, current_site_id=site_id)
|
|
|
|
@site_bp.route('/site/<int:site_id>/bookmark/add/<int:torrent_id>', methods=['POST'])
|
|
def add_bookmark(site_id, torrent_id):
|
|
if 'user_id' not in session:
|
|
return jsonify({'error': 'Authentication required'}), 401
|
|
|
|
config = get_nexusphp_config(site_id)
|
|
headers = {
|
|
'Accept': 'application/json',
|
|
'Authorization': f'Bearer {config["api_token"]}'
|
|
}
|
|
|
|
data = {
|
|
'torrent_id': torrent_id
|
|
}
|
|
|
|
try:
|
|
response = requests.post(f'{config["site_url"]}/api/v1/bookmarks',
|
|
headers=headers, data=data)
|
|
if response.status_code == 200:
|
|
return jsonify({'success': True})
|
|
else:
|
|
return jsonify({'error': 'Failed to add bookmark'}), response.status_code
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
@site_bp.route('/site/<int:site_id>/bookmark/remove/<int:torrent_id>', methods=['POST'])
|
|
def remove_bookmark(site_id, torrent_id):
|
|
if 'user_id' not in session:
|
|
return jsonify({'error': 'Authentication required'}), 401
|
|
|
|
config = get_nexusphp_config(site_id)
|
|
headers = {
|
|
'Accept': 'application/json',
|
|
'Authorization': f'Bearer {config["api_token"]}'
|
|
}
|
|
|
|
data = {
|
|
'torrent_id': torrent_id
|
|
}
|
|
|
|
try:
|
|
response = requests.post(f'{config["site_url"]}/api/v1/bookmarks/delete',
|
|
headers=headers, data=data)
|
|
if response.status_code == 200:
|
|
return jsonify({'success': True})
|
|
else:
|
|
return jsonify({'error': 'Failed to remove bookmark'}), response.status_code
|
|
except Exception as e:
|
|
return jsonify({'error': str(e)}), 500 |