From 35943510c9447516d5f22b85608fb677c725c127 Mon Sep 17 00:00:00 2001 From: Audionut Date: Mon, 6 Jan 2025 20:50:30 +1000 Subject: [PATCH] Banned groups from API Only Aither currently supported. Checks once per week. --- .gitignore | 1 + src/trackers/AITHER.py | 6 +- src/trackersetup.py | 125 ++++++++++++++++++++++++++++++++++------- src/trackerstatus.py | 3 +- 4 files changed, 107 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 4943e5364..e7520b5ff 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ data/tags.json data/cookies/*.txt data/cookies/*.pkl data/cookies/*.pickle +data/banned/*.* *.mkv .vscode/ __pycache__/ diff --git a/src/trackers/AITHER.py b/src/trackers/AITHER.py index 7c08c3e04..3a71e2e69 100644 --- a/src/trackers/AITHER.py +++ b/src/trackers/AITHER.py @@ -29,11 +29,7 @@ def __init__(self, config): self.upload_url = 'https://aither.cc/api/torrents/upload' self.torrent_url = 'https://aither.cc/api/torrents/' self.signature = "\n[center][url=https://github.com/Audionut/Upload-Assistant]Created by Audionut's Upload Assistant[/url][/center]" - self.banned_groups = [ - '4K4U', 'afm72', 'AROMA', 'Bandi', 'BiTOR', 'Bluespots', 'Chivaman', 'd3g', 'edge2020', 'EMBER', 'EVO', 'FGT', 'FreetheFish', 'Garshasp', 'Ghost', 'Grym', 'Hi10', 'HiQVE', 'ImE', 'ION10', - 'iVy', 'Judas', 'LAMA', 'Langbard', 'LION', 'MeGusta', 'MONOLITH', 'Natty', 'nikt0', 'noxxus', 'OEPlus', 'OFT', 'OsC', 'Panda', 'PYC', 'QxR', 'r00t', 'Ralphy', 'RARBG', 'RCVR', 'RetroPeeps', - 'RZeroX', 'SAMPA', 'Sicario', 'Silence', 'SkipTT', 'SM737', 'SPDVD', 'STUTTERSHIT', 'SWTYBLZ', 't3nzin', 'TAoE', 'Telly', 'TGx', 'Tigole', 'TSP', 'TSPxL', 'VXT', 'Vyndros', 'Weasley[HONE]', - 'Will1869', 'x0r', 'YIFY'] + self.banned_groups = [] pass async def upload(self, meta, disctype): diff --git a/src/trackersetup.py b/src/trackersetup.py index 091767315..86fc9424f 100644 --- a/src/trackersetup.py +++ b/src/trackersetup.py @@ -43,8 +43,13 @@ from src.trackers.YOINK import YOINK from src.trackers.HHD import HHD from src.trackers.SP import SP -import cli_ui from src.console import console +import httpx +import aiofiles +import os +import json +from datetime import datetime, timedelta +import asyncio class TRACKER_SETUP: @@ -69,28 +74,106 @@ def trackers_enabled(self, meta): trackers.insert(0, "MANUAL") return trackers - def check_banned_group(self, tracker, banned_group_list, meta): - if meta['tag'] == "": + async def get_banned_groups(self, meta, tracker): + file_path = os.path.join(meta['base_dir'], 'data', 'banned', f'{tracker}_banned_groups.json') + + # Check if we need to update + if not await self.should_update(file_path): + return file_path + + url = f'https://{tracker}.cc/api/blacklists/releasegroups' + headers = { + 'Authorization': f"Bearer {self.config['TRACKERS'][tracker]['api_key'].strip()}", + 'Content-Type': 'application/json', + 'Accept': 'application/json' + } + console.print("headers", headers) + async with httpx.AsyncClient() as client: + try: + response = await client.get(url, headers=headers) + if response.status_code == 200: + data = response.json() + console.print("Response Data:", data) + await self.write_banned_groups_to_file(file_path, data) + return file_path + else: + console.print(f"Error: Received status code {response.status_code}") + return None + except httpx.RequestError as e: + console.print(f"HTTP Request failed: {e}") + return None + + async def write_banned_groups_to_file(self, file_path, json_data): + try: + os.makedirs(os.path.dirname(file_path), exist_ok=True) + names = [item['name'] for item in json_data.get('data', [])] + names_csv = ', '.join(names) + file_content = { + "last_updated": datetime.now().strftime("%Y-%m-%d"), + "banned_groups": names_csv, + "raw_data": json_data + } + async with aiofiles.open(file_path, mode='w') as file: + await file.write(json.dumps(file_content, indent=4)) + console.print(f"File '{file_path}' updated successfully with {len(names)} groups.") + except Exception as e: + console.print(f"An error occurred: {e}") + + async def should_update(self, file_path): + try: + async with aiofiles.open(file_path, mode='r') as file: + content = await file.read() + data = json.loads(content) + last_updated = datetime.strptime(data['last_updated'], "%Y-%m-%d") + return datetime.now() >= last_updated + timedelta(weeks=1) + except FileNotFoundError: + return True + except Exception as e: + console.print(f"Error reading file: {e}") + return True + + async def check_banned_group(self, tracker, banned_group_list, meta): + if not meta['tag']: return False + + if tracker.upper() == "AITHER": + # Dynamically fetch banned groups for AITHER + file_path = await self.get_banned_groups(meta, tracker) + if not file_path: + console.print(f"[bold red]Failed to load banned groups for tracker '{tracker}'.") + return False + + # Load the banned groups from the file + try: + async with aiofiles.open(file_path, mode='r') as file: + content = await file.read() + data = json.loads(content) + banned_group_list.extend(data.get("banned_groups", "").split(", ")) + except FileNotFoundError: + console.print(f"[bold red]Banned group file for tracker '{tracker}' not found.") + return False + except json.JSONDecodeError: + console.print(f"[bold red]Failed to parse banned group file for tracker '{tracker}'.") + return False + + q = False + for tag in banned_group_list: + if isinstance(tag, list): + if meta['tag'][1:].lower() == tag[0].lower(): + console.print(f"[bold yellow]{meta['tag'][1:]}[/bold yellow][bold red] was found on [bold yellow]{tracker}'s[/bold yellow] list of banned groups.") + console.print(f"[bold red]NOTE: [bold yellow]{tag[1]}") + await asyncio.sleep(5) + q = True + else: + if meta['tag'][1:].lower() == tag.lower(): + console.print(f"[bold yellow]{meta['tag'][1:]}[/bold yellow][bold red] was found on [bold yellow]{tracker}'s[/bold yellow] list of banned groups.") + await asyncio.sleep(5) + q = True + + if q: + return True else: - q = False - for tag in banned_group_list: - if isinstance(tag, list): - if meta['tag'][1:].lower() == tag[0].lower(): - console.print(f"[bold yellow]{meta['tag'][1:]}[/bold yellow][bold red] was found on [bold yellow]{tracker}'s[/bold yellow] list of banned groups.") - console.print(f"[bold red]NOTE: [bold yellow]{tag[1]}") - q = True - else: - if meta['tag'][1:].lower() == tag.lower(): - console.print(f"[bold yellow]{meta['tag'][1:]}[/bold yellow][bold red] was found on [bold yellow]{tracker}'s[/bold yellow] list of banned groups.") - q = True - if q: - if not meta['unattended'] or (meta['unattended'] and meta.get('unattended-confirm', False)): - if not cli_ui.ask_yes_no(cli_ui.red, "Upload Anyways?", default=False): - return True - else: - return True - return False + return True tracker_class_map = { diff --git a/src/trackerstatus.py b/src/trackerstatus.py index 60b0644e6..88d68a000 100644 --- a/src/trackerstatus.py +++ b/src/trackerstatus.py @@ -58,8 +58,7 @@ async def process_single_tracker(tracker_name, shared_meta): meta['youtube'] = youtube meta['ptp_groupID'] = groupID - if tracker_setup.check_banned_group(tracker_class.tracker, tracker_class.banned_groups, local_meta): - console.print(f"[red]Tracker '{tracker_name}' is banned. Skipping.[/red]") + if await tracker_setup.check_banned_group(tracker_class.tracker, tracker_class.banned_groups, local_meta): local_tracker_status['banned'] = True if tracker_name not in {"THR", "PTP", "TL"}: