tgx/main.py
2024-01-06 16:53:03 +01:00

139 lines
3.7 KiB
Python

import subprocess
import sys
import requests
from bs4 import BeautifulSoup, ResultSet, Tag
class Movie:
def __init__(
self,
number: int,
category: str,
name: str,
uploader: str,
trusted: bool,
size: str,
comments: str,
views: str,
seeders_leechers: str,
magnet: str,
):
self.number = number
self.category = category
self.name = name
self.uploader = uploader
self.trusted = trusted
self.size = size
self.comments = comments
self.views = views
self.seeders_leechers = seeders_leechers
self.magnet = magnet
def print(self):
print(self.number, end=" | ")
print(self.category, end=" ; ")
print(self.name, end=" ; ")
print(self.uploader, end="")
if self.trusted:
print("/Trusted", end=" ; ")
else:
print(" ; ", end="")
print(self.size, end=" ; ")
print(self.comments, end=" ; ")
print(self.views, end=" ; ")
print(self.seeders_leechers)
def get_name():
return input("Enter movie/tv show name: ")
def get_response(name: str) -> requests.Response:
name.replace(" ", "++")
url = f"https://torrentgalaxy.to/torrents.php?search={name}#results"
return requests.get(url)
def get_torrents(response: requests.Response) -> ResultSet[Tag]:
soup = BeautifulSoup(response.text, "lxml")
return soup.select("div.tgxtablerow")
def get_movies(torrents: ResultSet[Tag]) -> list[Movie]:
movies: list[Movie] = []
number: int = 0
for torrent in torrents:
category: str = torrent.select_one("a:has(small)").text.strip()
name: str = torrent.select_one("a.txlight:has(b)").text.strip()
uploader: str = torrent.select_one("a.username").text.strip()
size: str = torrent.select_one("span.badge-secondary").text.strip()
comments: str = torrent.select_one("font:has(a.badge-secondary)").text.strip()
views: str = torrent.select_one(
"span.badge-warning:has(font:has(b))"
).text.strip()
seeders_leechers: str = torrent.find(
"span", {"title": "Seeders/Leechers"}
).text.strip()
magnet: str = str(torrent.select_one("a:has(i.glyphicon-magnet)").get("href"))
trusted: bool = False
if torrent.select_one("a.username").get("title") == "Trusted Uploader":
trusted = True
else:
trusted = False
number += 1
movies.append(
Movie(
number,
category,
name,
uploader,
trusted,
size,
comments,
views,
seeders_leechers,
magnet,
)
)
return movies
def print_movies(movies: list[Movie]):
for movie in movies:
movie.print()
def get_input(movies: list[Movie]) -> int:
choosen = input(f"Enter the number of wanted torrent. [1-{len(movies)}]: ")
try:
return int(choosen)
except Exception:
print("Not a number!!")
sys.exit(1)
def add_torrent(choosen: int, movies: list[Movie]):
if choosen >= 1 and choosen <= len(movies):
magnet = movies[choosen - 1].magnet
subprocess.call(["transmission-remote", "-a", magnet])
else:
print("Not choosen a correct number")
def main():
name: str = get_name()
response: requests.Response = get_response(name)
torrents: ResultSet[Tag] = get_torrents(response)
movies: list[Movie] = get_movies(torrents)
print_movies(movies)
input: int = get_input(movies)
add_torrent(input, movies)
sys.exit(0)
if __name__ == "__main__":
main()