Skip to content

Commit

Permalink
Added SolidTorrent
Browse files Browse the repository at this point in the history
  • Loading branch information
Catta1997 committed Feb 28, 2024
1 parent 10106ef commit 68f09c5
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 173 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.idea/*
__pycache__/*
__pycache__/torrent.cpython-39.pyc
Venv/*
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"torrent_pages": 3,
"torrent_pages": 1,
"autoadd": true,
"sort_by" : "size",
"custom_command": ""
Expand Down
118 changes: 71 additions & 47 deletions gui.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
'''GUI functions'''
"""GUI functions"""

import sys
from random import SystemRandom
import json
from torrent import TorrentDownloader

# text format
bold_text = "\033[1m"
red = "\x1b[31m"
# GUI import
try:
from PySide2.QtCore import QFile, QCoreApplication, Qt, QObject, QEvent
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QLabel, QCheckBox, QPushButton, QLineEdit, QTableWidgetItem, QTableWidget
from PySide2.QtWidgets import (
QApplication,
QLabel,
QCheckBox,
QPushButton,
QLineEdit,
QTableWidgetItem,
QTableWidget,
)
from PySide2.QtGui import QIcon, QPixmap, QKeyEvent
except ModuleNotFoundError:
bold_text = "\033[1m"
Expand All @@ -21,99 +30,113 @@
sys.exit(1)


class TorrentDownloaderGUI():
'''Download Torrent GUI'''
class TorrentDownloaderGUI:
"""Download Torrent GUI"""

def __init__(self) -> None:
'''initialize variables'''
"""initialize variables"""
self.autoadd = None
self.selected_elem = None
self.seleziona = None
self.tabella = None
selfw = self
TorrentDownloader.torrent_list = json.loads(
TorrentDownloader.json_torrent)
TorrentDownloader.torrent_list = json.loads(TorrentDownloader.json_torrent)
TorrentDownloader.setup(TorrentDownloader)

class KeyPressEater(QObject):
'''event filter '''
"""event filter"""

@staticmethod
def eventFilter(widget, event: QKeyEvent) -> bool:
'''catch enter key'''
"""catch enter key"""
if event.type() == QEvent.KeyPress:
key = event.key()
if key == Qt.Key_Return:
TorrentDownloaderGUI.avvia_ricerca(
selfw)
TorrentDownloaderGUI.avvia_ricerca(selfw)
return True
return False

TorrentDownloaderGUI.filtro = KeyPressEater()
TorrentDownloaderGUI.titolo: QLineEdit = TorrentDownloaderGUI.window.findChild(
QLineEdit, "titolo")
QLineEdit, "titolo"
)
TorrentDownloaderGUI.cerca: QPushButton = TorrentDownloaderGUI.window.findChild(
QPushButton, "cerca")
QPushButton, "cerca"
)
TorrentDownloaderGUI.add: QCheckBox = TorrentDownloaderGUI.window.findChild(
QCheckBox, "add")
QCheckBox, "add"
)
TorrentDownloaderGUI.cerca.clicked.connect(
lambda: TorrentDownloaderGUI.avvia_ricerca(self))
lambda: TorrentDownloaderGUI.avvia_ricerca(self)
)
TorrentDownloaderGUI.titolo.installEventFilter(self.filtro)

@staticmethod
def print_elem_gui(elem: dict, torrent: int) -> None:
'''insert torrent element in table'''
title_t = elem['name']
"""insert torrent element in table"""
title_t = elem["name"]
min_pos = 0
max_pos = 70
if max_pos < len(title_t):
TorrentDownloaderGUI.tabella.setItem(torrent, 0, QTableWidgetItem(
(title_t[min_pos:max_pos] + "\n" + title_t[max_pos:140])))
TorrentDownloaderGUI.tabella.setItem(
torrent,
0,
QTableWidgetItem(
(title_t[min_pos:max_pos] + "\n" + title_t[max_pos:140])
),
)
else:
TorrentDownloaderGUI.tabella.setItem(
torrent, 0, QTableWidgetItem((title_t)))
torrent, 0, QTableWidgetItem((title_t))
)
TorrentDownloaderGUI.tabella.setItem(
torrent, 1, QTableWidgetItem(f"{str(elem['size'])} {elem['type']}"))
torrent, 1, QTableWidgetItem(f"{str(elem['size'])} {elem['type']}")
)
TorrentDownloaderGUI.tabella.setItem(torrent, 2, QTableWidgetItem(elem["seed"]))
TorrentDownloaderGUI.tabella.setItem(
torrent, 2, QTableWidgetItem(elem['seed']))
torrent, 3, QTableWidgetItem(elem["leech"])
)
TorrentDownloaderGUI.tabella.setItem(
torrent, 3, QTableWidgetItem(elem['leech']))
TorrentDownloaderGUI.tabella.setItem(
torrent, 4, QTableWidgetItem(elem['movie_type']))
TorrentDownloaderGUI.tabella.setItem(
torrent, 5, QTableWidgetItem(elem['date']))
torrent, 4, QTableWidgetItem(elem["movie_type"])
)
TorrentDownloaderGUI.tabella.setItem(torrent, 5, QTableWidgetItem(elem["date"]))
TorrentDownloaderGUI.tabella.resizeColumnsToContents()
TorrentDownloaderGUI.tabella.resizeRowsToContents()

def avvia_ricerca(self) -> None:
'''avvio ricerca GUI'''
"""avvio ricerca GUI"""
# reset to allow multiple search
TorrentDownloader.json_torrent = '''
TorrentDownloader.json_torrent = """
{
"Torrent": [
]
}
'''
"""
name_input = self.titolo.text()
TorrentDownloader.search1377x_request(
TorrentDownloader, str(name_input))
TorrentDownloader.search1377x_request(TorrentDownloader, str(name_input))
TorrentDownloader.solidtorrents_request(TorrentDownloader, str(name_input))
# populate tabel
torrent = 1
TorrentDownloaderGUI.tabella: QTableWidget = TorrentDownloaderGUI.window.findChild(
QTableWidget, "tableWidget")
TorrentDownloaderGUI.seleziona: QPushButton = TorrentDownloaderGUI.window.findChild(
QPushButton, "select")
TorrentDownloaderGUI.tabella: QTableWidget = (
TorrentDownloaderGUI.window.findChild(QTableWidget, "tableWidget")
)
TorrentDownloaderGUI.seleziona: QPushButton = (
TorrentDownloaderGUI.window.findChild(QPushButton, "select")
)
TorrentDownloaderGUI.seleziona.clicked.connect(
lambda: TorrentDownloaderGUI.get_selected_element(TorrentDownloader))
lambda: TorrentDownloaderGUI.get_selected_element(TorrentDownloader)
)
TorrentDownloaderGUI.tabella.clearContents()
TorrentDownloaderGUI.tabella.setRowCount(0)
QApplication.processEvents()
for elem in TorrentDownloader.torrent_list['Torrent']:
for elem in TorrentDownloader.torrent_list["Torrent"]:
pos = torrent - 1
TorrentDownloaderGUI.tabella.insertRow(pos)
TorrentDownloaderGUI.print_elem_gui(elem, pos)
torrent += 1

def get_selected_element(self) -> None: # this self is TorrentDownloader
'''get list of selected row in GUI'''
"""get list of selected row in GUI"""
# GUI (first time only)
self.autoadd = TorrentDownloaderGUI.add.isChecked()
# get multiple selection
Expand All @@ -122,10 +145,12 @@ def get_selected_element(self) -> None: # this self is TorrentDownloader
# only 1 item in a row
if item.column() == 1:
# start download with each selected row
self.selected_elem = TorrentDownloader.torrent_list[
'Torrent'][item.row()]
self.selected_elem = TorrentDownloader.torrent_list["Torrent"][
item.row()
]
TorrentDownloader.get_magnet(
TorrentDownloader, self.selected_elem['link'], True)
TorrentDownloader, self.selected_elem["link"], True
)


# Winow setup
Expand All @@ -136,14 +161,13 @@ def get_selected_element(self) -> None: # this self is TorrentDownloader
rand = SystemRandom()
icon = rand.randrange(1, 15)
app.setWindowIcon(QIcon(f"Resources/icon_{icon}.png"))
install_ui = 'Resources/install.ui'
install_ui = "Resources/install.ui"
ui_file = QFile(install_ui)
loader = QUiLoader()
TorrentDownloaderGUI.window = loader.load(ui_file)
TorrentDownloaderGUI.logo = TorrentDownloaderGUI.window.findChild(
QLabel, 'logo')
TorrentDownloaderGUI.window.setWindowTitle('TorrentDownloader')
pixmap = QPixmap('Resources/1280px-1337X_logo.svg.png')
TorrentDownloaderGUI.logo = TorrentDownloaderGUI.window.findChild(QLabel, "logo")
TorrentDownloaderGUI.window.setWindowTitle("TorrentDownloader")
pixmap = QPixmap("Resources/1280px-1337X_logo.svg.png")
TorrentDownloaderGUI.logo.setPixmap(pixmap)
ui_file.close()
x = TorrentDownloaderGUI()
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
beautifulsoup4==4.9.1
requests==2.31.0
beautifulsoup4
requests
urllib3
75 changes: 34 additions & 41 deletions terminal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'''CLI functions'''
"""CLI functions"""

import json

# import os
import sys
from torrent import TorrentDownloader
Expand All @@ -19,83 +20,75 @@
white = "\x1b[37m"


class TorrentDownloaderCLI():
'''choose magnet from cli'''
class TorrentDownloaderCLI:
"""choose magnet from cli"""

def __init__(self) -> None:
TorrentDownloader.setup(TorrentDownloader)
if len(sys.argv) == 1:
name_input = input('Nome Film da cercare: ').strip()
name_input = input("Nome Film da cercare: ").strip()
else:
name_input = sys.argv[1]
for elem in sys.argv[2:]:
name_input += '%20' + elem
TorrentDownloader.search1377x_request(
TorrentDownloader, str(name_input))
name_input += "%20" + elem
TorrentDownloader.search1377x_request(TorrentDownloader, str(name_input))
# print list
TorrentDownloader.solidtorrents_request(TorrentDownloader, str(name_input))
torrent = 1
TorrentDownloader.torrent_list = json.loads(
TorrentDownloader.json_torrent)
for elem in TorrentDownloader.torrent_list['Torrent']:
TorrentDownloader.torrent_list = json.loads(TorrentDownloader.json_torrent)
for elem in TorrentDownloader.torrent_list["Torrent"]:
# write _____________
print(underscore + ' ' * 120 + reset_clr+'\n')
print(
f" {bold_text}Torrent {torrent} :{reset_clr}")
print(underscore + " " * 120 + reset_clr + "\n")
print(f" {bold_text}Torrent {torrent} :{reset_clr}")
TorrentDownloaderCLI.print_elem(elem)
torrent += 1
self.choose()

@staticmethod
def print_elem(elem: dict) -> None:
'''Print torrent element'''
title_t = elem['name']
"""Print torrent element"""
title_t = elem["name"]
min_pos = 0
max_pos = 95
print(
f" {cyan}TITLE: {title_t[min_pos:max_pos]}{reset_clr}")
print(f" {cyan}TITLE: {title_t[min_pos:max_pos]}{reset_clr}")
while max_pos < len(title_t):
min_pos += 95
max_pos += 95
print(f" {cyan} {title_t[min_pos:max_pos]}{reset_clr}")
print(
f" {red}DATE: {elem['date']}{reset_clr}")
print(
f" {green}DIM: {str(elem['size'])} {elem['type']}{reset_clr}")
print(
f" {yellow}SEED: {elem['seed']}{reset_clr}")
print(
f" {white}LEECH: {elem['leech']}{reset_clr}")
print(
f" {magenta}TYPE: {elem['movie_type']}{reset_clr}")
print(f" {red}DATE: {elem['date']}{reset_clr}")
print(f" {green}DIM: {str(elem['size'])} {elem['type']}{reset_clr}")
print(f" {yellow}SEED: {elem['seed']}{reset_clr}")
print(f" {white}LEECH: {elem['leech']}{reset_clr}")
print(f" {magenta}TYPE: {elem['movie_type']}{reset_clr}")

def choose(self) -> None:
'''Select torrent'''
"""Select torrent"""
# write _____________
print(underscore+' ' * 120+reset_clr+'\n')
print(underscore + " " * 120 + reset_clr + "\n")
found = 0
while found == 0:
try:
number = int(input('Choose torrent: '))
number = int(input("Choose torrent: "))
except ValueError:
print(
f"\n{red}Not Valid!!{reset_clr}\n")
print(f"\n{red}Not Valid!!{reset_clr}\n")
TorrentDownloaderCLI.choose(self)
found = 1
number -= 1 # indice di un array
self.selected_elem = TorrentDownloader.torrent_list['Torrent'][number]
self.selected_elem = TorrentDownloader.torrent_list["Torrent"][number]
TorrentDownloaderCLI.print_elem(self.selected_elem)
conf = ""
while conf.lower() not in ['y', 'n']:
while conf.lower() not in ["y", "n"]:
conf = input("\ny to confirm, n to repeat: ")
if conf.lower() == 'n':
if conf.lower() == "n":
found = 0
elif conf.lower() == 'y':
elif conf.lower() == "y":
# controllo che number sia una scelta valida:
if 0 <= number < len(TorrentDownloader.torrent_list['Torrent']):
TorrentDownloader.get_magnet(TorrentDownloader,
self.selected_elem['link'], False)
if 0 <= number < len(TorrentDownloader.torrent_list["Torrent"]):
TorrentDownloader.get_magnet(
TorrentDownloader, self.selected_elem["link"], False
)
else:
print(
f"{red}Not Valid{reset_clr}")
print(f"{red}Not Valid{reset_clr}")


x = TorrentDownloaderCLI()
Loading

0 comments on commit 68f09c5

Please sign in to comment.