mirror of
https://github.com/KeksPirates/SoftwareManager.git
synced 2026-08-03 17:39:42 +02:00
Compare commits
62 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c4685dc2de | |||
| f34d02d3d0 | |||
| 91b5c12afa | |||
| b77867b111 | |||
| bf20625832 | |||
| 8d218443bf | |||
| a55a049583 | |||
| 4e7e08c136 | |||
| c3efa39b09 | |||
| d3d7a9d2ac | |||
| 74e7fcfd70 | |||
| adecc78aec | |||
| f6b852309b | |||
| 86ec6daca2 | |||
| 8d6d02a1af | |||
| 2e2e526d3a | |||
| 11b8f3f265 | |||
| 5369578194 | |||
| c5e6608ae2 | |||
| 214d9a0e23 | |||
| e0da811bfe | |||
| ef538fc83d | |||
| c5e72332dc | |||
| 39522a4068 | |||
| a9f229926b | |||
| ec950b751b | |||
| 56735c4344 | |||
| 8db9f3d9e2 | |||
| 80d3ad208d | |||
| 87886a5cf9 | |||
| 7cdae312ce | |||
| 66217ff9b6 | |||
| 9ba11bfa78 | |||
| 43cbfe056c | |||
| 12df32f72a | |||
| 9e0ee2a36d | |||
| 5b9bdd2a2a | |||
| ff0e1a0798 | |||
| 891c5a86c1 | |||
| ffdd656b9c | |||
| d8fec74de1 | |||
| a5190f151c | |||
| 95f6640212 | |||
| 78a47e89d3 | |||
| e47efab901 | |||
| 75e4da33eb | |||
| 003dd0e3aa | |||
| c76d179daa | |||
| eba4c01fca | |||
| 577a1637e4 | |||
| 889c50e201 | |||
| e4d1c189b7 | |||
| 7d55af6ac0 | |||
| b9dc6cc18d | |||
| 29bac99359 | |||
| d6a388652b | |||
| 1c3fb95e40 | |||
| e354ef50cf | |||
| 2066c86001 | |||
| 7e2b4cb46b | |||
| bef019cf8c | |||
| 886ee8f0f0 |
@@ -27,24 +27,30 @@
|
|||||||
AppId={{8F2E4B6A-1C3D-4E5F-9A7B-0D8E6F2C4A1B}
|
AppId={{8F2E4B6A-1C3D-4E5F-9A7B-0D8E6F2C4A1B}
|
||||||
AppName={#MyAppName}
|
AppName={#MyAppName}
|
||||||
AppVersion={#MyAppVersion}
|
AppVersion={#MyAppVersion}
|
||||||
|
AppVerName={#MyAppName} {#MyAppVersion}
|
||||||
AppPublisher={#MyAppPublisher}
|
AppPublisher={#MyAppPublisher}
|
||||||
AppPublisherURL={#MyAppURL}
|
AppPublisherURL={#MyAppURL}
|
||||||
AppSupportURL={#MyAppURL}/issues
|
AppSupportURL={#MyAppURL}/issues
|
||||||
|
AppUpdatesURL={#MyAppURL}/releases
|
||||||
DefaultDirName={autopf}\{#MyAppName}
|
DefaultDirName={autopf}\{#MyAppName}
|
||||||
DefaultGroupName={#MyAppName}
|
DefaultGroupName={#MyAppName}
|
||||||
AllowNoIcons=yes
|
AllowNoIcons=yes
|
||||||
|
LicenseFile=LICENSE
|
||||||
OutputDir={#MyOutputDir}
|
OutputDir={#MyOutputDir}
|
||||||
OutputBaseFilename={#MyOutputFilename}
|
OutputBaseFilename={#MyOutputFilename}
|
||||||
Compression=lzma2
|
Compression=lzma2
|
||||||
SolidCompression=yes
|
SolidCompression=yes
|
||||||
WizardStyle=modern
|
WizardStyle=modern
|
||||||
|
DisableWelcomePage=no
|
||||||
PrivilegesRequired=lowest
|
PrivilegesRequired=lowest
|
||||||
PrivilegesRequiredOverridesAllowed=dialog
|
PrivilegesRequiredOverridesAllowed=dialog
|
||||||
UninstallDisplayIcon={app}\{#MyAppExeName}
|
UninstallDisplayIcon={app}\{#MyAppExeName}
|
||||||
|
UninstallDisplayName={#MyAppName}
|
||||||
ArchitecturesAllowed=x64compatible
|
ArchitecturesAllowed=x64compatible
|
||||||
ArchitecturesInstallIn64BitMode=x64compatible
|
ArchitecturesInstallIn64BitMode=x64compatible
|
||||||
CloseApplications=yes
|
CloseApplications=yes
|
||||||
RestartApplications=yes
|
RestartApplications=yes
|
||||||
|
ShowLanguageDialog=auto
|
||||||
|
|
||||||
[Languages]
|
[Languages]
|
||||||
Name: "english"; MessagesFile: "compiler:Default.isl"
|
Name: "english"; MessagesFile: "compiler:Default.isl"
|
||||||
@@ -62,3 +68,4 @@ Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: de
|
|||||||
|
|
||||||
[Run]
|
[Run]
|
||||||
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
|
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
|
||||||
|
Filename: "{app}\{#MyAppExeName}"; Flags: nowait skipifnotsilent
|
||||||
|
|||||||
+1
-1
@@ -8,4 +8,4 @@ aiohttp==3.13.0
|
|||||||
plyer==2.1.0
|
plyer==2.1.0
|
||||||
psutil==7.1.0
|
psutil==7.1.0
|
||||||
libtorrent==2.0.11
|
libtorrent==2.0.11
|
||||||
libtorrent-windows-dll==0.0.3
|
libtorrent-windows-dll==0.0.3
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
|
from core.utils.data.state import state
|
||||||
|
from core.utils.data.tracker import get_magnet_link
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
from typing import Dict
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -31,7 +34,7 @@ def _get_telegram_posts():
|
|||||||
|
|
||||||
for bubble in bubbles:
|
for bubble in bubbles:
|
||||||
post_txt = bubble.find("div", class_="tgme_widget_message_text js-message_text")
|
post_txt = bubble.find("div", class_="tgme_widget_message_text js-message_text")
|
||||||
if not post_txt or not post_txt.b:
|
if post_txt is None or post_txt.b is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
title = post_txt.b.text
|
title = post_txt.b.text
|
||||||
@@ -42,9 +45,9 @@ def _get_telegram_posts():
|
|||||||
if post_url not in added:
|
if post_url not in added:
|
||||||
added.add(post_url)
|
added.add(post_url)
|
||||||
posts.append(dict(
|
posts.append(dict(
|
||||||
|
title=title,
|
||||||
author="m0nkrus",
|
author="m0nkrus",
|
||||||
id=len(posts) + 1,
|
id=len(posts) + 1,
|
||||||
title=title,
|
|
||||||
url=post_url
|
url=post_url
|
||||||
))
|
))
|
||||||
|
|
||||||
@@ -67,4 +70,18 @@ def scrape_m0nkrus(query):
|
|||||||
filtered_post["id"] = len(filtered_posts) + 1
|
filtered_post["id"] = len(filtered_posts) + 1
|
||||||
filtered_posts.append(filtered_post)
|
filtered_posts.append(filtered_post)
|
||||||
|
|
||||||
return filtered_posts
|
return filtered_posts
|
||||||
|
|
||||||
|
def get_magnet(post: Dict):
|
||||||
|
return get_magnet_link(post["url"])
|
||||||
|
|
||||||
|
Metadata = {
|
||||||
|
"name" : "m0nkrus",
|
||||||
|
"headers" : ["Post Title", "Author"],
|
||||||
|
"scrapeFunc" : scrape_m0nkrus,
|
||||||
|
"linkFunc" : get_magnet,
|
||||||
|
"isMagnet" : True,
|
||||||
|
}
|
||||||
|
|
||||||
|
def init_m0nkrus():
|
||||||
|
state.trackers.update({Metadata["name"] : Metadata})
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
def scrape_buzzheavier(url):
|
||||||
|
|
||||||
|
response = requests.get(url)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
download_url = url + '/download'
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
'hx-current-url': url,
|
||||||
|
'hx-request': 'true',
|
||||||
|
'referer': url
|
||||||
|
}
|
||||||
|
|
||||||
|
head_response = requests.head(download_url, headers=headers, allow_redirects=False)
|
||||||
|
hx_redirect = head_response.headers.get('hx-redirect')
|
||||||
|
return hx_redirect
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
from core.utils.logging.loghandler import consoleLog
|
||||||
|
import requests
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def scrape_gofile(url):
|
||||||
|
|
||||||
|
filetoken = re.findall(r"(?<=https...gofile.io\/d\/).*", url)[0]
|
||||||
|
session = requests.Session()
|
||||||
|
|
||||||
|
acc = session.post("https://api.gofile.io/accounts")
|
||||||
|
token = acc.json()["data"]["token"]
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"Authorization": f"Bearer {token}",
|
||||||
|
"X-Website-Token": "4fd6sg89d7s6", # Maybe make this dynamic in the future
|
||||||
|
}
|
||||||
|
|
||||||
|
r = session.get(
|
||||||
|
f"https://api.gofile.io/contents/{filetoken}",
|
||||||
|
headers=headers,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
|
||||||
|
temp: dict = r.json()["data"]["children"]
|
||||||
|
child = [key for key in temp.keys()]
|
||||||
|
|
||||||
|
return temp[child[0]]["link"]
|
||||||
|
except:
|
||||||
|
consoleLog("gofile didnt auth you, either the api has changed\n or you sent to many requests, please try again later.\n If it still doesnt work please open an issue on Github.")
|
||||||
|
|
||||||
@@ -1,19 +1,52 @@
|
|||||||
import requests
|
import requests
|
||||||
from core.utils.data.state import state
|
from core.utils.data.state import state
|
||||||
from core.utils.logging.logs import consoleLog
|
from core.utils.logging.logs import consoleLog
|
||||||
|
from core.utils.data.tracker import get_magnet_link
|
||||||
|
from core.utils.network.jsonhandler import split_data, format_data
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
def scrape_rutracker(query):
|
def scrape_rutracker(query):
|
||||||
search = requests.get(f"{state.api_url}/search?q={query}")
|
search = requests.get(f"{state.api_url}/search?q={query}", timeout=15)
|
||||||
consoleLog("Sent request to server")
|
consoleLog("Sent request to server")
|
||||||
if search:
|
if search:
|
||||||
try:
|
_, data, _, success, cached = split_data(search.text)
|
||||||
return search.text
|
if cached:
|
||||||
except Exception:
|
consoleLog("Server response cached")
|
||||||
consoleLog("No results found / No response from server")
|
if success:
|
||||||
return None
|
sorted_data = []
|
||||||
|
|
||||||
|
for entry in data:
|
||||||
|
sorted_data.append(
|
||||||
|
{
|
||||||
|
"title" : entry["title"],
|
||||||
|
"author" : entry["author"],
|
||||||
|
"seeders" : entry["seeders"],
|
||||||
|
"leechers" : entry["leechers"],
|
||||||
|
"url" : entry["url"],
|
||||||
|
"id" : entry["id"],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return sorted_data
|
||||||
|
else:
|
||||||
|
consoleLog("Scraping failed server-side, unable to fetch posts from rutracker")
|
||||||
|
return []
|
||||||
else:
|
else:
|
||||||
return None
|
consoleLog("No response from server, returning nothing")
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
# This function utilizes the SoftwareManager server - source code can be found under the SoftwareManager-Server repository.
|
def get_magnet(post: Dict):
|
||||||
|
_, post_links, _, _, _ = format_data([post])
|
||||||
|
return get_magnet_link(post_links[0])
|
||||||
|
|
||||||
|
Metadata = {
|
||||||
|
"name" : "rutracker",
|
||||||
|
"headers" : ["Post Title", "Author", "Seeders", "Leechers"],
|
||||||
|
"scrapeFunc" : scrape_rutracker,
|
||||||
|
"linkFunc" : get_magnet,
|
||||||
|
"isMagnet" : True,
|
||||||
|
}
|
||||||
|
|
||||||
|
def init_rutracker():
|
||||||
|
state.trackers.update({Metadata["name"] : Metadata})
|
||||||
|
|||||||
@@ -0,0 +1,123 @@
|
|||||||
|
from core.data.scrapers.provider.buzzheavier import scrape_buzzheavier
|
||||||
|
from core.data.scrapers.provider.gofile import scrape_gofile
|
||||||
|
from core.utils.logging.logs import consoleLog
|
||||||
|
from core.utils.data.state import state
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
from typing import Dict
|
||||||
|
import webbrowser
|
||||||
|
import requests
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
|
||||||
|
cache = {
|
||||||
|
"data": [],
|
||||||
|
"last_fetched": 0
|
||||||
|
}
|
||||||
|
|
||||||
|
cache_expiry = 300
|
||||||
|
|
||||||
|
def get_Metadata():
|
||||||
|
return Metadata
|
||||||
|
|
||||||
|
def scrape_steamrip_links():
|
||||||
|
|
||||||
|
current_time = time.time()
|
||||||
|
|
||||||
|
if cache["data"] != [] and (current_time - cache["last_fetched"] < cache_expiry):
|
||||||
|
return cache["data"]
|
||||||
|
|
||||||
|
url = f"https://steamrip.com/games-list-page/"
|
||||||
|
response = requests.get(url)
|
||||||
|
text = response.text
|
||||||
|
|
||||||
|
soup = BeautifulSoup(text, "html.parser")
|
||||||
|
games = soup.find_all("li", class_="az-list-item")
|
||||||
|
|
||||||
|
if len(games) == 0:
|
||||||
|
return []
|
||||||
|
|
||||||
|
links = []
|
||||||
|
names = []
|
||||||
|
for gamehtml in games:
|
||||||
|
link = gamehtml.find("a", href=lambda x: x and x.startswith("/"))
|
||||||
|
links += re.findall(r'(?<=href=")[^"]*', link.__str__())
|
||||||
|
name = gamehtml.find("a", href=lambda x: x and x.startswith("/"))
|
||||||
|
names += re.findall(r'(?<=\/">)[^<]*', name.__str__())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# construct the list[dict[str,str]]
|
||||||
|
|
||||||
|
ret = []
|
||||||
|
|
||||||
|
for i in range(len(names)):
|
||||||
|
ret.append({"title" : names[i], "url" : links[i]})
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def scrape_steamrip_game_downloads(gamelink):
|
||||||
|
url = "https://steamrip.com" + gamelink
|
||||||
|
response = requests.get(url)
|
||||||
|
soup = BeautifulSoup(response.text, "html.parser")
|
||||||
|
download_link_elements = soup.find_all("a",class_="shortc-button")
|
||||||
|
download_links = ["buzzheavier", "gofile"]
|
||||||
|
|
||||||
|
for download_link in download_link_elements:
|
||||||
|
pure = download_link.attrs.get("href")
|
||||||
|
if pure[2] == "b":
|
||||||
|
download_links[0] = "https:" + pure
|
||||||
|
if pure[2] == "g":
|
||||||
|
download_links[1] = "https:" + pure
|
||||||
|
if pure[2] == "v":
|
||||||
|
download_links[2] = "https:" + pure
|
||||||
|
if pure[2] == "m":
|
||||||
|
download_links[3] = "https:" + pure
|
||||||
|
|
||||||
|
ret = []
|
||||||
|
|
||||||
|
for link in download_links:
|
||||||
|
if len(link) != 1:
|
||||||
|
ret.append(link)
|
||||||
|
|
||||||
|
cache["data"] = ret
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
def get_download_link(post: Dict):
|
||||||
|
url = post["url"]
|
||||||
|
links = scrape_steamrip_game_downloads(url)
|
||||||
|
best = ""
|
||||||
|
for link in links:
|
||||||
|
if link[0] == "h":
|
||||||
|
best = link
|
||||||
|
break
|
||||||
|
|
||||||
|
if links.index(best) == 0:
|
||||||
|
return scrape_buzzheavier(best)
|
||||||
|
elif links.index(best) == 1:
|
||||||
|
return scrape_gofile(best)
|
||||||
|
else:
|
||||||
|
consoleLog("Unable to retrieve download link due to captcha, launching browser...")
|
||||||
|
webbrowser.open(best)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def filter_steamrip(query: str):
|
||||||
|
games = scrape_steamrip_links()
|
||||||
|
|
||||||
|
filtered_games = [
|
||||||
|
game for game in games
|
||||||
|
if query.lower() in game["title"].lower()
|
||||||
|
]
|
||||||
|
|
||||||
|
return filtered_games
|
||||||
|
|
||||||
|
Metadata = {
|
||||||
|
"name" : "steamrip",
|
||||||
|
"headers" : ["Game"],
|
||||||
|
"scrapeFunc" : filter_steamrip,
|
||||||
|
"linkFunc" : get_download_link,
|
||||||
|
"isMagnet" : False,
|
||||||
|
}
|
||||||
|
|
||||||
|
def init_steamrip():
|
||||||
|
state.trackers.update({Metadata["name"] : Metadata})
|
||||||
@@ -2,6 +2,10 @@ import requests
|
|||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
from core.utils.logging.logs import consoleLog
|
from core.utils.logging.logs import consoleLog
|
||||||
|
from core.utils.data.state import state
|
||||||
|
from core.utils.data.tracker import get_magnet_link
|
||||||
|
from typing import Dict
|
||||||
|
|
||||||
|
|
||||||
def scrape_uztracker(query):
|
def scrape_uztracker(query):
|
||||||
base_url="https://uztracker.net/"
|
base_url="https://uztracker.net/"
|
||||||
@@ -16,7 +20,9 @@ def scrape_uztracker(query):
|
|||||||
links = soup.find_all('tr', class_="tCenter hl-tr", id=lambda x: x and x.startswith('tor_'))
|
links = soup.find_all('tr', class_="tCenter hl-tr", id=lambda x: x and x.startswith('tor_'))
|
||||||
for link in links:
|
for link in links:
|
||||||
|
|
||||||
theme_link = link.find('a', class_="genmed tLink", href=lambda x: x and x.startswith('./viewtopic'))
|
theme_link = link.find('a', class_="genmed tLink", href=lambda x: x and x.startswith('./viewtopic'))
|
||||||
|
if not theme_link or not theme_link.b:
|
||||||
|
continue
|
||||||
url = urljoin(base_url, theme_link['href'])
|
url = urljoin(base_url, theme_link['href'])
|
||||||
title = theme_link.b.text
|
title = theme_link.b.text
|
||||||
author_link = link.find('a', class_="med")
|
author_link = link.find('a', class_="med")
|
||||||
@@ -24,12 +30,26 @@ def scrape_uztracker(query):
|
|||||||
|
|
||||||
posts.append(dict(
|
posts.append(dict(
|
||||||
title=title,
|
title=title,
|
||||||
|
author=author,
|
||||||
url=url,
|
url=url,
|
||||||
author=author
|
|
||||||
))
|
))
|
||||||
|
|
||||||
return posts
|
return posts
|
||||||
|
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
consoleLog(f"Failed to fetch {search_url}: {e}")
|
consoleLog(f"Failed to fetch {search_url}: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def get_magnet(post: Dict):
|
||||||
|
return get_magnet_link(post["url"])
|
||||||
|
|
||||||
|
Metadata = {
|
||||||
|
"name" : "uztracker",
|
||||||
|
"headers" : ["Post Title", "Author"],
|
||||||
|
"scrapeFunc" : scrape_uztracker,
|
||||||
|
"linkFunc" : get_magnet,
|
||||||
|
"isMagnet" : True,
|
||||||
|
}
|
||||||
|
|
||||||
|
def init_uztracker():
|
||||||
|
state.trackers.update({Metadata["name"] : Metadata})
|
||||||
|
|||||||
@@ -4,8 +4,9 @@ from core.utils.logging.logs import consoleLog
|
|||||||
from core.interface.utils.tabhelper import general_tab
|
from core.interface.utils.tabhelper import general_tab
|
||||||
from core.interface.utils.tabhelper import paths_tab
|
from core.interface.utils.tabhelper import paths_tab
|
||||||
from core.interface.utils.tabhelper import network_tab
|
from core.interface.utils.tabhelper import network_tab
|
||||||
|
from core.interface.utils.svghelper import svg_icon
|
||||||
from PySide6 import QtWidgets
|
from PySide6 import QtWidgets
|
||||||
from PySide6.QtCore import Qt
|
from PySide6.QtCore import Qt, QSize
|
||||||
from PySide6.QtWidgets import (
|
from PySide6.QtWidgets import (
|
||||||
QLineEdit,
|
QLineEdit,
|
||||||
QPushButton,
|
QPushButton,
|
||||||
@@ -22,6 +23,8 @@ from PySide6.QtWidgets import (
|
|||||||
)
|
)
|
||||||
import platform
|
import platform
|
||||||
|
|
||||||
|
SVG_FOLDER = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M2 6c0-1.1.9-2 2-2h5l2 2h7c1.1 0 2 .9 2 2v10c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6z" fill="{color}"/></svg>'
|
||||||
|
|
||||||
|
|
||||||
def settings_dialog(self):
|
def settings_dialog(self):
|
||||||
|
|
||||||
@@ -51,7 +54,6 @@ def settings_dialog(self):
|
|||||||
update_checkbox.setChecked(state.ignore_updates)
|
update_checkbox.setChecked(state.ignore_updates)
|
||||||
update_checkbox.toggled.connect(lambda checked: setattr(state, 'ignore_updates', checked))
|
update_checkbox.toggled.connect(lambda checked: setattr(state, 'ignore_updates', checked))
|
||||||
update_checkbox_layout.addWidget(update_checkbox)
|
update_checkbox_layout.addWidget(update_checkbox)
|
||||||
dialog.layout().addWidget(update_checkbox_container)
|
|
||||||
|
|
||||||
# auto-resume downloads checkbox
|
# auto-resume downloads checkbox
|
||||||
|
|
||||||
@@ -113,7 +115,19 @@ def settings_dialog(self):
|
|||||||
if dir_path:
|
if dir_path:
|
||||||
download_path.setText(dir_path)
|
download_path.setText(dir_path)
|
||||||
|
|
||||||
browse_button = QPushButton("📁")
|
browse_button = QPushButton()
|
||||||
|
browse_button.setFixedSize(36, 36)
|
||||||
|
browse_button.setIconSize(QSize(24, 24))
|
||||||
|
browse_button.setIcon(svg_icon(SVG_FOLDER, 24))
|
||||||
|
browse_button.setCursor(Qt.CursorShape.PointingHandCursor)
|
||||||
|
browse_button.setStyleSheet("""
|
||||||
|
QPushButton {
|
||||||
|
border: none;
|
||||||
|
background: transparent;
|
||||||
|
padding: 0px;
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
|
||||||
download_path_layout.addWidget(browse_button)
|
download_path_layout.addWidget(browse_button)
|
||||||
browse_button.clicked.connect(browse_download_path)
|
browse_button.clicked.connect(browse_download_path)
|
||||||
|
|
||||||
@@ -125,7 +139,7 @@ def settings_dialog(self):
|
|||||||
image_path_layout = QHBoxLayout()
|
image_path_layout = QHBoxLayout()
|
||||||
|
|
||||||
image_path = QLineEdit()
|
image_path = QLineEdit()
|
||||||
image_path_layout.addWidget(QLabel("Image Path (requires restart):"))
|
image_path_layout.addWidget(QLabel("Image Path (requires restart, experimental):"))
|
||||||
image_path_layout.addWidget(image_path)
|
image_path_layout.addWidget(image_path)
|
||||||
image_path_container.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
|
image_path_container.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
|
||||||
image_path_container.setLayout(image_path_layout)
|
image_path_container.setLayout(image_path_layout)
|
||||||
@@ -136,7 +150,18 @@ def settings_dialog(self):
|
|||||||
if file_path:
|
if file_path:
|
||||||
image_path.setText(file_path)
|
image_path.setText(file_path)
|
||||||
|
|
||||||
browse_button = QPushButton("📁")
|
browse_button = QPushButton()
|
||||||
|
browse_button.setFixedSize(36, 36)
|
||||||
|
browse_button.setIconSize(QSize(24, 24))
|
||||||
|
browse_button.setIcon(svg_icon(SVG_FOLDER, 24))
|
||||||
|
browse_button.setCursor(Qt.CursorShape.PointingHandCursor)
|
||||||
|
browse_button.setStyleSheet("""
|
||||||
|
QPushButton {
|
||||||
|
border: none;
|
||||||
|
background: transparent;
|
||||||
|
padding: 0px;
|
||||||
|
}
|
||||||
|
""")
|
||||||
image_path_layout.addWidget(browse_button)
|
image_path_layout.addWidget(browse_button)
|
||||||
browse_button.clicked.connect(browse_image_path)
|
browse_button.clicked.connect(browse_image_path)
|
||||||
|
|
||||||
@@ -224,8 +249,6 @@ def settings_dialog(self):
|
|||||||
else:
|
else:
|
||||||
interface_select.setCurrentIndex(0)
|
interface_select.setCurrentIndex(0)
|
||||||
|
|
||||||
interface_select.setFixedWidth(180)
|
|
||||||
interface_select.setFixedHeight(30)
|
|
||||||
interface_select.setFixedWidth(180)
|
interface_select.setFixedWidth(180)
|
||||||
interface_select.setFixedHeight(30)
|
interface_select.setFixedHeight(30)
|
||||||
interface_layout.addWidget(interface_select)
|
interface_layout.addWidget(interface_select)
|
||||||
|
|||||||
+467
-346
File diff suppressed because it is too large
Load Diff
@@ -1,66 +1,27 @@
|
|||||||
from PySide6.QtWidgets import QTableWidgetItem
|
from PySide6.QtWidgets import QTableWidgetItem, QHeaderView
|
||||||
from core.utils.logging.logs import consoleLog
|
from core.utils.logging.logs import consoleLog
|
||||||
from core.data.scrapers.uztracker import scrape_uztracker
|
from core.data.scrapers.rutracker import init_rutracker
|
||||||
from core.data.scrapers.rutracker import scrape_rutracker
|
from core.data.scrapers.uztracker import init_uztracker
|
||||||
from core.data.scrapers.monkrus import scrape_m0nkrus
|
from core.data.scrapers.monkrus import init_m0nkrus
|
||||||
from core.utils.network.jsonhandler import split_data, format_data, format_data_minimal
|
from core.data.scrapers.steamrip import init_steamrip
|
||||||
from core.utils.data.state import state
|
from core.utils.data.state import state
|
||||||
|
|
||||||
scrapers = {
|
init_rutracker()
|
||||||
"uztracker": scrape_uztracker,
|
init_uztracker()
|
||||||
"rutracker": scrape_rutracker,
|
init_m0nkrus()
|
||||||
"m0nkrus": scrape_m0nkrus
|
init_steamrip()
|
||||||
}
|
|
||||||
|
|
||||||
def return_pressed(self):
|
def return_pressed(self):
|
||||||
|
self.show_empty_results(False)
|
||||||
search_text = self.searchbar.text()
|
search_text = self.searchbar.text()
|
||||||
if search_text == "":
|
if search_text == "":
|
||||||
consoleLog("Error: Can't search for nothing")
|
consoleLog("Error: Can't search for nothing")
|
||||||
return
|
return
|
||||||
consoleLog(f"User searched for: {search_text}")
|
consoleLog(f"User searched for: {search_text}")
|
||||||
|
|
||||||
if state.tracker == "rutracker":
|
tracker = state.trackers[state.currenttracker]
|
||||||
response = scrape_rutracker(search_text)
|
scrapefunc = tracker["scrapeFunc"]
|
||||||
if response:
|
state.posts = scrapefunc(search_text)
|
||||||
_, state.posts, _, _, cached = split_data(response)
|
|
||||||
if state.posts == []:
|
|
||||||
consoleLog(f"No Results for {search_text}")
|
|
||||||
state.tracker_list[state.tracker].clear()
|
|
||||||
self.show_empty_results(True)
|
|
||||||
else:
|
|
||||||
state.post_titles, _, state.post_author, state.post_seeders, state.post_leechers = format_data(state.posts)
|
|
||||||
self.show_empty_results(False)
|
|
||||||
state.tracker_list[state.tracker].clear()
|
|
||||||
state.tracker_list[state.tracker].setHorizontalHeaderLabels(["Post Title", "Author", "Seeders", "Leechers"])
|
|
||||||
state.tracker_list[state.tracker].setRowCount(len(state.post_titles))
|
|
||||||
for i, author in enumerate(state.post_author):
|
|
||||||
state.tracker_list[state.tracker].setItem(i, 1, QTableWidgetItem(author))
|
|
||||||
for i, title in enumerate(state.post_titles):
|
|
||||||
state.tracker_list[state.tracker].setItem(i, 0, QTableWidgetItem(title))
|
|
||||||
for i, seeders in enumerate(state.post_seeders):
|
|
||||||
state.tracker_list[state.tracker].setItem(i, 2, QTableWidgetItem(seeders))
|
|
||||||
for i, leechers in enumerate(state.post_leechers):
|
|
||||||
state.tracker_list[state.tracker].setItem(i, 3, QTableWidgetItem(leechers))
|
|
||||||
if state.debug == True:
|
|
||||||
consoleLog(f"Response Cached: {cached}")
|
|
||||||
else:
|
|
||||||
consoleLog(f"No response from rutracker")
|
|
||||||
state.tracker_list[state.tracker].clear()
|
|
||||||
self.show_empty_results(True)
|
|
||||||
|
|
||||||
elif state.tracker is not None:
|
from core.interface.gui import MainWindow
|
||||||
state.posts = scrapers[state.tracker](search_text)
|
MainWindow._instance.search_results_signal.emit(tracker["headers"])
|
||||||
if state.posts == []:
|
|
||||||
consoleLog(f"No Results for {search_text}")
|
|
||||||
state.tracker_list[state.tracker].clear()
|
|
||||||
self.show_empty_results(True)
|
|
||||||
else:
|
|
||||||
state.post_author, state.post_titles, state.post_urls = format_data_minimal(state.posts)
|
|
||||||
self.show_empty_results(False)
|
|
||||||
state.tracker_list[state.tracker].clear()
|
|
||||||
state.tracker_list[state.tracker].setHorizontalHeaderLabels(["Post Title", "Author"])
|
|
||||||
state.tracker_list[state.tracker].setRowCount(len(state.post_titles))
|
|
||||||
for i, author in enumerate(state.post_author):
|
|
||||||
state.tracker_list[state.tracker].setItem(i, 1, QTableWidgetItem(author))
|
|
||||||
for i, title in enumerate(state.post_titles):
|
|
||||||
state.tracker_list[state.tracker].setItem(i, 0, QTableWidgetItem(title))
|
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
|
||||||
|
from PySide6 import QtWidgets
|
||||||
|
from PySide6.QtCore import Qt, QByteArray
|
||||||
|
from PySide6.QtGui import QIcon, QPixmap, QPainter
|
||||||
|
from PySide6.QtSvg import QSvgRenderer
|
||||||
|
import darkdetect
|
||||||
|
|
||||||
|
|
||||||
|
def _is_dark_mode():
|
||||||
|
return darkdetect.isDark()
|
||||||
|
|
||||||
|
|
||||||
|
def svg_icon(svg_str, size=20):
|
||||||
|
app = QtWidgets.QApplication.instance()
|
||||||
|
if app:
|
||||||
|
if _is_dark_mode():
|
||||||
|
color = "white"
|
||||||
|
else:
|
||||||
|
color = "#555555"
|
||||||
|
else:
|
||||||
|
color = "white"
|
||||||
|
svg = svg_str.replace("{color}", color)
|
||||||
|
renderer = QSvgRenderer(QByteArray(svg.encode()))
|
||||||
|
pixmap = QPixmap(size, size)
|
||||||
|
pixmap.fill(Qt.GlobalColor.transparent)
|
||||||
|
painter = QPainter(pixmap)
|
||||||
|
renderer.render(painter)
|
||||||
|
painter.end()
|
||||||
|
return QIcon(pixmap)
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
from typing import Optional
|
||||||
|
from core.utils.data.state import state
|
||||||
|
from core.utils.logging.logs import consoleLog, add_download_log
|
||||||
|
|
||||||
|
from .handle import DirectDownloadHandle
|
||||||
|
from .utils import (
|
||||||
|
sanitize_filename,
|
||||||
|
extract_filename_from_url,
|
||||||
|
detect_filename_from_headers,
|
||||||
|
)
|
||||||
|
|
||||||
|
def add_direct_download(url: str, title: str, dl_path: Optional[str] = None):
|
||||||
|
|
||||||
|
if dl_path is None:
|
||||||
|
dl_path = state.download_path
|
||||||
|
|
||||||
|
if url in state.active_downloads:
|
||||||
|
consoleLog(f"Download already active: {title}")
|
||||||
|
return
|
||||||
|
|
||||||
|
filename = (
|
||||||
|
detect_filename_from_headers(url, DirectDownloadHandle.USER_AGENT)
|
||||||
|
or extract_filename_from_url(url)
|
||||||
|
or sanitize_filename(title) + ".zip"
|
||||||
|
)
|
||||||
|
|
||||||
|
handle = DirectDownloadHandle(url, filename, dl_path)
|
||||||
|
state.active_downloads[url] = handle
|
||||||
|
add_download_log(title, url, "", False)
|
||||||
|
handle.start()
|
||||||
|
consoleLog(f"Started direct download: {filename}")
|
||||||
@@ -0,0 +1,443 @@
|
|||||||
|
import os
|
||||||
|
import threading
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import hashlib
|
||||||
|
import time
|
||||||
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
|
from typing import Optional
|
||||||
|
import libtorrent as lt
|
||||||
|
|
||||||
|
from core.utils.logging.logs import consoleLog, update_download_completed
|
||||||
|
from core.utils.data.state import state
|
||||||
|
from .status import DirectDownloadStatus, ChunkSpec
|
||||||
|
from .utils import format_size
|
||||||
|
|
||||||
|
class SimpleThrottler:
|
||||||
|
def __init__(self):
|
||||||
|
self._lock = threading.Lock()
|
||||||
|
self._last_time = time.monotonic()
|
||||||
|
self._allowance = 0.0
|
||||||
|
|
||||||
|
def throttle(self, bytes_count: int, limit_kbps: int):
|
||||||
|
if limit_kbps <= 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
limit_bps = limit_kbps * 1024
|
||||||
|
wait_time = 0
|
||||||
|
with self._lock:
|
||||||
|
now = time.monotonic()
|
||||||
|
elapsed = now - self._last_time
|
||||||
|
self._last_time = now
|
||||||
|
|
||||||
|
self._allowance += elapsed * limit_bps
|
||||||
|
if self._allowance > 2 * limit_bps:
|
||||||
|
self._allowance = 2 * limit_bps
|
||||||
|
|
||||||
|
self._allowance -= bytes_count
|
||||||
|
|
||||||
|
if self._allowance < 0:
|
||||||
|
wait_time = -self._allowance / limit_bps
|
||||||
|
self._allowance = 0
|
||||||
|
|
||||||
|
if wait_time > 0:
|
||||||
|
time.sleep(min(wait_time, 1.0))
|
||||||
|
|
||||||
|
_throttler = SimpleThrottler()
|
||||||
|
|
||||||
|
class DirectDownloadHandle:
|
||||||
|
STREAM_BLOCK_SIZE = 1 << 17 # 128 KiB per read
|
||||||
|
MAX_RETRIES = 5
|
||||||
|
RETRY_BACKOFF_BASE = 2.0
|
||||||
|
NUM_THREADS = 16
|
||||||
|
MIN_CHUNK_SIZE = 1 << 20 # 1 MiB minimum per chunk
|
||||||
|
REQUEST_TIMEOUT = (15, 60) # (connect, read) timeouts
|
||||||
|
USER_AGENT = (
|
||||||
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
||||||
|
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||||
|
"Chrome/125.0.0.0 Safari/537.36"
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, url: str, name: str, save_path: str):
|
||||||
|
self.url = url
|
||||||
|
self._name = name
|
||||||
|
self._save_path = save_path
|
||||||
|
self._file_path = os.path.join(save_path, name)
|
||||||
|
self._status = DirectDownloadStatus(name, save_path, 0)
|
||||||
|
self._stop_event = threading.Event()
|
||||||
|
self._pause_event = threading.Event() # SET = paused
|
||||||
|
self._thread: Optional[threading.Thread] = None
|
||||||
|
self._session: Optional[requests.Session] = None
|
||||||
|
self._supports_range = False
|
||||||
|
|
||||||
|
state_dir = os.path.join(state.settings_path, "direct_downloads")
|
||||||
|
os.makedirs(state_dir, exist_ok=True)
|
||||||
|
url_hash = hashlib.sha256(url.encode()).hexdigest()
|
||||||
|
self._state_file = os.path.join(state_dir, f"{url_hash}.json")
|
||||||
|
|
||||||
|
# Load state to initialize progress
|
||||||
|
state_data = self._load_state()
|
||||||
|
if state_data:
|
||||||
|
total_wanted = state_data.get("total_wanted", 0)
|
||||||
|
chunks_progress = state_data.get("chunks", {})
|
||||||
|
chunks_progress = {int(k): int(v) for k, v in chunks_progress.items()}
|
||||||
|
self._status.initialize_progress(total_wanted, chunks_progress)
|
||||||
|
|
||||||
|
|
||||||
|
def status(self) -> DirectDownloadStatus:
|
||||||
|
return self._status
|
||||||
|
|
||||||
|
def pause(self):
|
||||||
|
self._status.paused = True
|
||||||
|
self._pause_event.set()
|
||||||
|
|
||||||
|
def resume(self):
|
||||||
|
self._status.paused = False
|
||||||
|
self._pause_event.clear()
|
||||||
|
|
||||||
|
def set_flags(self, flags):
|
||||||
|
if flags & lt.torrent_flags.auto_managed:
|
||||||
|
self._status.auto_managed = True
|
||||||
|
|
||||||
|
def unset_flags(self, flags):
|
||||||
|
if flags & lt.torrent_flags.auto_managed:
|
||||||
|
self._status.auto_managed = False
|
||||||
|
|
||||||
|
def save_path(self) -> str:
|
||||||
|
return self._save_path
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self._stop_event.set()
|
||||||
|
if self._thread and self._thread.is_alive():
|
||||||
|
self._thread.join(timeout=10)
|
||||||
|
|
||||||
|
|
||||||
|
def _load_state(self) -> dict:
|
||||||
|
if os.path.exists(self._state_file):
|
||||||
|
try:
|
||||||
|
with open(self._state_file, "r") as f:
|
||||||
|
return json.load(f)
|
||||||
|
except Exception as e:
|
||||||
|
consoleLog(f"Failed to load state for {self._name}: {e}")
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def _save_state(self, chunks_done: dict[int, int]):
|
||||||
|
try:
|
||||||
|
with open(self._state_file, "w") as f:
|
||||||
|
json.dump({
|
||||||
|
"url": self.url,
|
||||||
|
"name": self._name,
|
||||||
|
"save_path": self._save_path,
|
||||||
|
"total_wanted": self._status.total_wanted,
|
||||||
|
"chunks": chunks_done
|
||||||
|
}, f)
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _clear_state(self):
|
||||||
|
if os.path.exists(self._state_file):
|
||||||
|
try:
|
||||||
|
os.remove(self._state_file)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self._thread = threading.Thread(
|
||||||
|
target=self._download_orchestrator, name=f"dl-{self._name}", daemon=True
|
||||||
|
)
|
||||||
|
self._thread.start()
|
||||||
|
|
||||||
|
def _build_session(self) -> requests.Session:
|
||||||
|
session = requests.Session()
|
||||||
|
session.headers.update({"User-Agent": self.USER_AGENT})
|
||||||
|
adapter = requests.adapters.HTTPAdapter(
|
||||||
|
max_retries=0,
|
||||||
|
pool_connections=self.NUM_THREADS,
|
||||||
|
pool_maxsize=self.NUM_THREADS + 2,
|
||||||
|
)
|
||||||
|
session.mount("https://", adapter)
|
||||||
|
session.mount("http://", adapter)
|
||||||
|
return session
|
||||||
|
|
||||||
|
def _probe_url(self, session: requests.Session) -> tuple[int, bool]:
|
||||||
|
resp = session.head(self.url, allow_redirects=True, timeout=self.REQUEST_TIMEOUT)
|
||||||
|
resp.raise_for_status()
|
||||||
|
|
||||||
|
total_size = int(resp.headers.get("content-length", 0))
|
||||||
|
supports_range = (total_size > 0)
|
||||||
|
|
||||||
|
# Double-check range support with a small range request
|
||||||
|
if supports_range:
|
||||||
|
try:
|
||||||
|
test = session.get(
|
||||||
|
self.url,
|
||||||
|
headers={"Range": "bytes=0-0"},
|
||||||
|
timeout=self.REQUEST_TIMEOUT,
|
||||||
|
stream=True,
|
||||||
|
)
|
||||||
|
supports_range = test.status_code == 206
|
||||||
|
test.close()
|
||||||
|
except Exception:
|
||||||
|
supports_range = False
|
||||||
|
|
||||||
|
return total_size, supports_range
|
||||||
|
|
||||||
|
def _download_orchestrator(self):
|
||||||
|
try:
|
||||||
|
self._session = self._build_session()
|
||||||
|
total_size, self._supports_range = self._probe_url(self._session)
|
||||||
|
self._status.total_wanted = total_size
|
||||||
|
|
||||||
|
os.makedirs(self._save_path, exist_ok=True)
|
||||||
|
|
||||||
|
use_multithreaded = (
|
||||||
|
self._supports_range
|
||||||
|
and total_size > self.MIN_CHUNK_SIZE * 2
|
||||||
|
)
|
||||||
|
|
||||||
|
if use_multithreaded:
|
||||||
|
consoleLog(
|
||||||
|
f"Multi-threaded download ({self.NUM_THREADS} threads): {self._name} "
|
||||||
|
f"({format_size(total_size)})"
|
||||||
|
)
|
||||||
|
self._preallocate_file(total_size)
|
||||||
|
self._multithreaded_download(total_size)
|
||||||
|
else:
|
||||||
|
reason = "no range support" if not self._supports_range else "file too small"
|
||||||
|
consoleLog(f"Single-threaded download ({reason}): {self._name}")
|
||||||
|
self._single_threaded_download()
|
||||||
|
|
||||||
|
if not self._stop_event.is_set() and self._status.error is None:
|
||||||
|
self._verify_download(total_size)
|
||||||
|
self._status.mark_completed()
|
||||||
|
update_download_completed(self.url, True)
|
||||||
|
self._clear_state()
|
||||||
|
consoleLog(f"Finished downloading {self._name}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self._status.mark_error(str(e))
|
||||||
|
consoleLog(f"Download failed for {self._name}: {e}")
|
||||||
|
finally:
|
||||||
|
if self._session:
|
||||||
|
self._session.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _preallocate_file(self, total_size: int):
|
||||||
|
if os.path.exists(self._file_path) and os.path.getsize(self._file_path) == total_size:
|
||||||
|
return
|
||||||
|
with open(self._file_path, "wb") as f:
|
||||||
|
f.truncate(total_size)
|
||||||
|
|
||||||
|
def _compute_chunks(self, total_size: int) -> list[ChunkSpec]:
|
||||||
|
num_chunks = min(self.NUM_THREADS, max(1, total_size // self.MIN_CHUNK_SIZE))
|
||||||
|
chunk_size = total_size // num_chunks
|
||||||
|
chunks = []
|
||||||
|
for i in range(num_chunks):
|
||||||
|
start = i * chunk_size
|
||||||
|
end = (i + 1) * chunk_size - 1 if i < num_chunks - 1 else total_size - 1
|
||||||
|
chunks.append(ChunkSpec(chunk_id=i, start=start, end=end))
|
||||||
|
return chunks
|
||||||
|
|
||||||
|
def _multithreaded_download(self, total_size: int):
|
||||||
|
chunks = self._compute_chunks(total_size)
|
||||||
|
state_data = self._load_state()
|
||||||
|
chunks_progress = state_data.get("chunks", {})
|
||||||
|
|
||||||
|
chunks_progress = {int(k): int(v) for k, v in chunks_progress.items()}
|
||||||
|
|
||||||
|
# Initialize progress in status
|
||||||
|
for chunk_id, bytes_done in chunks_progress.items():
|
||||||
|
self._status.update_chunk_progress(chunk_id, bytes_done)
|
||||||
|
|
||||||
|
with ThreadPoolExecutor(
|
||||||
|
max_workers=len(chunks), thread_name_prefix="dl-chunk"
|
||||||
|
) as executor:
|
||||||
|
futures = {
|
||||||
|
executor.submit(self._download_chunk_with_retry, chunk, chunks_progress.get(chunk.chunk_id, 0)): chunk
|
||||||
|
for chunk in chunks
|
||||||
|
}
|
||||||
|
|
||||||
|
for future in as_completed(futures):
|
||||||
|
chunk = futures[future]
|
||||||
|
try:
|
||||||
|
future.result()
|
||||||
|
except Exception as e:
|
||||||
|
consoleLog(
|
||||||
|
f"Chunk {chunk.chunk_id} ({chunk.start}-{chunk.end}) "
|
||||||
|
f"failed permanently: {e}"
|
||||||
|
)
|
||||||
|
self._status.mark_error(
|
||||||
|
f"Chunk {chunk.chunk_id} failed: {e}"
|
||||||
|
)
|
||||||
|
self._stop_event.set()
|
||||||
|
|
||||||
|
def _download_chunk_with_retry(self, chunk: ChunkSpec, initial_bytes: int):
|
||||||
|
bytes_written = initial_bytes
|
||||||
|
|
||||||
|
for attempt in range(1, self.MAX_RETRIES + 1):
|
||||||
|
if self._stop_event.is_set():
|
||||||
|
return
|
||||||
|
|
||||||
|
current_start = chunk.start + bytes_written
|
||||||
|
if current_start > chunk.end:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
# _download_range returns new bytes written
|
||||||
|
bytes_written += self._download_range(
|
||||||
|
chunk.chunk_id, current_start, chunk.end, bytes_written
|
||||||
|
)
|
||||||
|
return
|
||||||
|
except Exception as e:
|
||||||
|
if self._stop_event.is_set():
|
||||||
|
return
|
||||||
|
if attempt < self.MAX_RETRIES:
|
||||||
|
wait = self.RETRY_BACKOFF_BASE ** attempt
|
||||||
|
consoleLog(
|
||||||
|
f"Chunk {chunk.chunk_id} attempt {attempt} failed: {e}. "
|
||||||
|
f"Retrying in {wait:.0f}s (resuming from byte {current_start})..."
|
||||||
|
)
|
||||||
|
if self._stop_event.wait(timeout=wait):
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Chunk {chunk.chunk_id} failed after {self.MAX_RETRIES} attempts: {e}"
|
||||||
|
) from e
|
||||||
|
|
||||||
|
def _download_range(
|
||||||
|
self, chunk_id: int, start: int, end: int, prior_bytes: int
|
||||||
|
) -> int:
|
||||||
|
|
||||||
|
headers = {"Range": f"bytes={start}-{end}"}
|
||||||
|
new_bytes = 0
|
||||||
|
|
||||||
|
with self._session.get(
|
||||||
|
self.url, headers=headers, stream=True, timeout=self.REQUEST_TIMEOUT
|
||||||
|
) as resp:
|
||||||
|
resp.raise_for_status()
|
||||||
|
if resp.status_code not in (200, 206):
|
||||||
|
raise RuntimeError(f"Unexpected status {resp.status_code}")
|
||||||
|
|
||||||
|
with open(self._file_path, "r+b") as f:
|
||||||
|
f.seek(start)
|
||||||
|
for block in resp.iter_content(chunk_size=self.STREAM_BLOCK_SIZE):
|
||||||
|
if self._stop_event.is_set():
|
||||||
|
return new_bytes
|
||||||
|
|
||||||
|
self._wait_if_paused()
|
||||||
|
if self._stop_event.is_set():
|
||||||
|
return new_bytes
|
||||||
|
|
||||||
|
_throttler.throttle(len(block), state.down_speed_limit)
|
||||||
|
|
||||||
|
f.write(block)
|
||||||
|
new_bytes += len(block)
|
||||||
|
self._status.update_chunk_progress(
|
||||||
|
chunk_id, prior_bytes + new_bytes
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if new_bytes % (1024 * 1024) < len(block):
|
||||||
|
|
||||||
|
with self._status._lock:
|
||||||
|
self._save_state(self._status._chunk_bytes)
|
||||||
|
|
||||||
|
with self._status._lock:
|
||||||
|
self._save_state(self._status._chunk_bytes)
|
||||||
|
|
||||||
|
return new_bytes
|
||||||
|
|
||||||
|
|
||||||
|
def _single_threaded_download(self):
|
||||||
|
chunk_id = 0
|
||||||
|
bytes_written = 0
|
||||||
|
|
||||||
|
if os.path.exists(self._file_path):
|
||||||
|
bytes_written = os.path.getsize(self._file_path)
|
||||||
|
if bytes_written > 0:
|
||||||
|
self._status.update_chunk_progress(chunk_id, bytes_written)
|
||||||
|
|
||||||
|
for attempt in range(1, self.MAX_RETRIES + 1):
|
||||||
|
if self._stop_event.is_set():
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
headers = {}
|
||||||
|
mode = "wb"
|
||||||
|
|
||||||
|
if bytes_written > 0 and self._supports_range:
|
||||||
|
headers["Range"] = f"bytes={bytes_written}-"
|
||||||
|
mode = "r+b"
|
||||||
|
elif bytes_written > 0:
|
||||||
|
bytes_written = 0
|
||||||
|
mode = "wb"
|
||||||
|
|
||||||
|
with self._session.get(
|
||||||
|
self.url, headers=headers, stream=True, timeout=self.REQUEST_TIMEOUT
|
||||||
|
) as resp:
|
||||||
|
resp.raise_for_status()
|
||||||
|
|
||||||
|
if self._status.total_wanted == 0:
|
||||||
|
content_length = int(resp.headers.get("content-length", 0))
|
||||||
|
self._status.total_wanted = content_length + bytes_written
|
||||||
|
|
||||||
|
with open(self._file_path, mode) as f:
|
||||||
|
if mode == "r+b":
|
||||||
|
f.seek(bytes_written)
|
||||||
|
|
||||||
|
for block in resp.iter_content(
|
||||||
|
chunk_size=self.STREAM_BLOCK_SIZE
|
||||||
|
):
|
||||||
|
if self._stop_event.is_set():
|
||||||
|
return
|
||||||
|
|
||||||
|
self._wait_if_paused()
|
||||||
|
if self._stop_event.is_set():
|
||||||
|
return
|
||||||
|
|
||||||
|
_throttler.throttle(len(block), state.down_speed_limit)
|
||||||
|
|
||||||
|
f.write(block)
|
||||||
|
bytes_written += len(block)
|
||||||
|
self._status.update_chunk_progress(chunk_id, bytes_written)
|
||||||
|
|
||||||
|
# Periodic state save (single thread, chunk_id=0)
|
||||||
|
if bytes_written % (1024 * 1024) < len(block):
|
||||||
|
self._save_state({0: bytes_written})
|
||||||
|
|
||||||
|
self._save_state({0: bytes_written})
|
||||||
|
return
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
if self._stop_event.is_set():
|
||||||
|
return
|
||||||
|
if attempt < self.MAX_RETRIES:
|
||||||
|
wait = self.RETRY_BACKOFF_BASE ** attempt
|
||||||
|
consoleLog(
|
||||||
|
f"Download attempt {attempt} failed: {e}. "
|
||||||
|
f"Retrying in {wait:.0f}s..."
|
||||||
|
)
|
||||||
|
if self._stop_event.wait(timeout=wait):
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Download failed after {self.MAX_RETRIES} attempts: {e}"
|
||||||
|
) from e
|
||||||
|
|
||||||
|
|
||||||
|
def _wait_if_paused(self):
|
||||||
|
while self._pause_event.is_set():
|
||||||
|
if self._stop_event.wait(timeout=0.5):
|
||||||
|
return
|
||||||
|
|
||||||
|
def _verify_download(self, expected_size: int):
|
||||||
|
if expected_size <= 0:
|
||||||
|
return
|
||||||
|
actual_size = os.path.getsize(self._file_path)
|
||||||
|
if actual_size != expected_size:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Size mismatch: expected {format_size(expected_size)}, "
|
||||||
|
f"got {format_size(actual_size)}"
|
||||||
|
)
|
||||||
|
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
import threading
|
||||||
|
import time
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional
|
||||||
|
import libtorrent as lt
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ChunkSpec:
|
||||||
|
chunk_id: int
|
||||||
|
start: int
|
||||||
|
end: int # inclusive
|
||||||
|
|
||||||
|
|
||||||
|
class DirectDownloadStatus:
|
||||||
|
|
||||||
|
def __init__(self, name: str, save_path: str, total_size: int = 0):
|
||||||
|
self.name = name
|
||||||
|
self.save_path = save_path
|
||||||
|
self.total_wanted = total_size
|
||||||
|
self.paused = False
|
||||||
|
self.auto_managed = True
|
||||||
|
self.has_metadata = True
|
||||||
|
self.state = lt.torrent_status.downloading
|
||||||
|
self.error: Optional[str] = None
|
||||||
|
|
||||||
|
self._lock = threading.Lock()
|
||||||
|
self._total_wanted_done = 0
|
||||||
|
self._progress = 0.0
|
||||||
|
self._download_rate = 0
|
||||||
|
self._upload_rate = 0
|
||||||
|
|
||||||
|
self._chunk_bytes: dict[int, int] = {}
|
||||||
|
self._speed_window: list[tuple[float, int]] = []
|
||||||
|
self._speed_window_size = 3.0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def total_wanted_done(self) -> int:
|
||||||
|
with self._lock:
|
||||||
|
return self._total_wanted_done
|
||||||
|
|
||||||
|
@property
|
||||||
|
def progress(self) -> float:
|
||||||
|
with self._lock:
|
||||||
|
return self._progress
|
||||||
|
|
||||||
|
@property
|
||||||
|
def download_rate(self) -> int:
|
||||||
|
with self._lock:
|
||||||
|
return self._download_rate
|
||||||
|
|
||||||
|
@property
|
||||||
|
def upload_rate(self) -> int:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def update_chunk_progress(self, chunk_id: int, bytes_downloaded: int):
|
||||||
|
with self._lock:
|
||||||
|
self._chunk_bytes[chunk_id] = bytes_downloaded
|
||||||
|
self._total_wanted_done = sum(self._chunk_bytes.values())
|
||||||
|
if self.total_wanted > 0:
|
||||||
|
self._progress = min(self._total_wanted_done / self.total_wanted, 1.0)
|
||||||
|
|
||||||
|
|
||||||
|
now = time.monotonic()
|
||||||
|
self._speed_window.append((now, self._total_wanted_done))
|
||||||
|
|
||||||
|
cutoff = now - self._speed_window_size
|
||||||
|
self._speed_window = [
|
||||||
|
(t, b) for t, b in self._speed_window if t >= cutoff
|
||||||
|
]
|
||||||
|
if len(self._speed_window) >= 2:
|
||||||
|
oldest_time, oldest_bytes = self._speed_window[0]
|
||||||
|
dt = now - oldest_time
|
||||||
|
if dt > 0:
|
||||||
|
self._download_rate = int(
|
||||||
|
(self._total_wanted_done - oldest_bytes) / dt
|
||||||
|
)
|
||||||
|
|
||||||
|
def initialize_progress(self, total_wanted: int, chunk_bytes: dict[int, int]):
|
||||||
|
with self._lock:
|
||||||
|
self.total_wanted = total_wanted
|
||||||
|
self._chunk_bytes = chunk_bytes.copy()
|
||||||
|
self._total_wanted_done = sum(self._chunk_bytes.values())
|
||||||
|
if self.total_wanted > 0:
|
||||||
|
self._progress = min(self._total_wanted_done / self.total_wanted, 1.0)
|
||||||
|
|
||||||
|
def mark_completed(self):
|
||||||
|
with self._lock:
|
||||||
|
self.state = lt.torrent_status.seeding
|
||||||
|
self._progress = 1.0
|
||||||
|
self._download_rate = 0
|
||||||
|
|
||||||
|
def mark_error(self, error: str):
|
||||||
|
with self._lock:
|
||||||
|
self.error = error
|
||||||
|
self._download_rate = 0
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
import os
|
||||||
|
import requests
|
||||||
|
from typing import Optional
|
||||||
|
from urllib.parse import urlparse, unquote
|
||||||
|
|
||||||
|
def sanitize_filename(name: str) -> str:
|
||||||
|
# Remove or replace dangerous characters
|
||||||
|
keepchars = (" ", ".", "_", "-")
|
||||||
|
cleaned = "".join(c for c in name if c.isalnum() or c in keepchars).strip()
|
||||||
|
while " " in cleaned:
|
||||||
|
cleaned = cleaned.replace(" ", " ")
|
||||||
|
return cleaned or "download"
|
||||||
|
|
||||||
|
|
||||||
|
def extract_filename_from_url(url: str) -> Optional[str]:
|
||||||
|
parsed = urlparse(url)
|
||||||
|
path = unquote(parsed.path)
|
||||||
|
basename = os.path.basename(path)
|
||||||
|
if basename and "." in basename and len(basename) < 256:
|
||||||
|
return basename
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def detect_filename_from_headers(url: str, user_agent: str) -> Optional[str]:
|
||||||
|
try:
|
||||||
|
resp = requests.head(
|
||||||
|
url,
|
||||||
|
headers={"User-Agent": user_agent},
|
||||||
|
allow_redirects=True,
|
||||||
|
timeout=15,
|
||||||
|
)
|
||||||
|
cd = resp.headers.get("content-disposition", "")
|
||||||
|
if "filename=" in cd:
|
||||||
|
parts = cd.split("filename=")
|
||||||
|
if len(parts) > 1:
|
||||||
|
fname = parts[-1].strip().strip('"').strip("'")
|
||||||
|
if fname:
|
||||||
|
return fname
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return None
|
||||||
|
|
||||||
|
def format_size(size_bytes: int) -> str:
|
||||||
|
for unit in ("B", "KiB", "MiB", "GiB", "TiB"):
|
||||||
|
if abs(size_bytes) < 1024:
|
||||||
|
return f"{size_bytes:.1f} {unit}"
|
||||||
|
size_bytes /= 1024
|
||||||
|
return f"{size_bytes:.1f} PiB"
|
||||||
@@ -2,19 +2,21 @@ from core.utils.logging.logs import consoleLog
|
|||||||
from core.utils.data.state import state
|
from core.utils.data.state import state
|
||||||
import psutil
|
import psutil
|
||||||
|
|
||||||
addrs = psutil.net_if_addrs()
|
|
||||||
stats = psutil.net_if_stats()
|
|
||||||
|
|
||||||
def get_net_interfaces():
|
def get_net_interfaces():
|
||||||
|
addrs = psutil.net_if_addrs()
|
||||||
for interface in addrs.keys():
|
for interface in addrs.keys():
|
||||||
consoleLog(f"Found Interface: {interface}")
|
consoleLog(f"Found Interface: {interface}")
|
||||||
return addrs.keys()
|
return addrs.keys()
|
||||||
|
|
||||||
|
|
||||||
def get_active_interfaces():
|
def get_active_interfaces():
|
||||||
|
addrs = psutil.net_if_addrs()
|
||||||
|
stats = psutil.net_if_stats()
|
||||||
active = []
|
active = []
|
||||||
|
|
||||||
for interface, addr_list in addrs.items():
|
for interface, addr_list in addrs.items():
|
||||||
|
if interface not in stats:
|
||||||
|
continue
|
||||||
up = stats[interface].isup
|
up = stats[interface].isup
|
||||||
for addr in addr_list:
|
for addr in addr_list:
|
||||||
if addr.family == 2: # ipv4
|
if addr.family == 2: # ipv4
|
||||||
@@ -27,26 +29,31 @@ def get_active_interfaces():
|
|||||||
|
|
||||||
|
|
||||||
def list_interfaces() -> None:
|
def list_interfaces() -> None:
|
||||||
|
addrs = psutil.net_if_addrs()
|
||||||
|
stats = psutil.net_if_stats()
|
||||||
|
|
||||||
for interface, addr_list in addrs.items():
|
for interface, addr_list in addrs.items():
|
||||||
|
if interface not in stats:
|
||||||
|
continue
|
||||||
up = stats[interface].isup
|
up = stats[interface].isup
|
||||||
|
status = "INACTIVE"
|
||||||
for addr in addr_list:
|
for addr in addr_list:
|
||||||
if addr.family == 2: # ipv4
|
if addr.family == 2: # ipv4
|
||||||
ipv4 = addr.address
|
ipv4 = addr.address
|
||||||
if not ipv4.startswith("127.") and not ipv4.startswith("169.254") and up:
|
if not ipv4.startswith("127.") and not ipv4.startswith("169.254") and up:
|
||||||
status = "ACTIVE"
|
status = "ACTIVE"
|
||||||
else:
|
|
||||||
status = "INACTIVE"
|
|
||||||
consoleLog(f"Found Interface: {interface} [{status}]")
|
consoleLog(f"Found Interface: {interface} [{status}]")
|
||||||
|
|
||||||
def init_interfaces():
|
def init_interfaces():
|
||||||
|
addrs = psutil.net_if_addrs()
|
||||||
state.interfaces = list(addrs.keys())
|
state.interfaces = list(addrs.keys())
|
||||||
state.active_interfaces = get_active_interfaces()
|
state.active_interfaces = get_active_interfaces()
|
||||||
|
|
||||||
def get_interface_ip(interface_name):
|
def get_interface_ip(interface_name):
|
||||||
|
addrs = psutil.net_if_addrs()
|
||||||
if interface_name in addrs:
|
if interface_name in addrs:
|
||||||
for addr in addrs[interface_name]:
|
for addr in addrs[interface_name]:
|
||||||
if addr.family == 2: # ipv4
|
if addr.family == 2: # ipv4
|
||||||
if not addr.address.startswith("127.") and not addr.address.startswith("169.254"):
|
if not addr.address.startswith("127.") and not addr.address.startswith("169.254"):
|
||||||
return addr.address
|
return addr.address
|
||||||
return None
|
return None
|
||||||
|
|||||||
@@ -1,17 +1,27 @@
|
|||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
import platform
|
||||||
|
import ctypes
|
||||||
from core.utils.general.wrappers import run_thread
|
from core.utils.general.wrappers import run_thread
|
||||||
from core.network.interface import get_interface_ip
|
from core.network.interface import get_interface_ip
|
||||||
import threading
|
import threading
|
||||||
import libtorrent as lt
|
import libtorrent as lt
|
||||||
from core.utils.data.state import state
|
from core.utils.data.state import state
|
||||||
from core.utils.logging.logs import consoleLog
|
from core.utils.logging.logs import consoleLog
|
||||||
from core.utils.general.shutdown import shutdown_event
|
|
||||||
|
|
||||||
|
|
||||||
global loop_running
|
global loop_running
|
||||||
loop_running = False
|
loop_running = False
|
||||||
|
|
||||||
|
def get_free_space_mb(dirname):
|
||||||
|
if platform.system() == 'Windows':
|
||||||
|
free_bytes = ctypes.c_ulonglong(0)
|
||||||
|
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(dirname), None, None, ctypes.pointer(free_bytes))
|
||||||
|
return free_bytes.value
|
||||||
|
else:
|
||||||
|
st = os.statvfs(dirname)
|
||||||
|
return st.f_bavail * st.f_frsize
|
||||||
|
|
||||||
def init_session():
|
def init_session():
|
||||||
if state.dl_session is not None:
|
if state.dl_session is not None:
|
||||||
return
|
return
|
||||||
@@ -20,8 +30,8 @@ def init_session():
|
|||||||
|
|
||||||
|
|
||||||
settings = {
|
settings = {
|
||||||
"upload_rate_limit": state.up_speed_limit,
|
"upload_rate_limit": state.up_speed_limit * 1024,
|
||||||
"download_rate_limit": state.down_speed_limit,
|
"download_rate_limit": state.down_speed_limit * 1024,
|
||||||
"enable_dht": True,
|
"enable_dht": True,
|
||||||
"enable_lsd": True,
|
"enable_lsd": True,
|
||||||
"enable_upnp": True,
|
"enable_upnp": True,
|
||||||
@@ -45,40 +55,64 @@ def init_session():
|
|||||||
consoleLog("Initialized Session")
|
consoleLog("Initialized Session")
|
||||||
|
|
||||||
|
|
||||||
def add_download(magnet_uri, dl_path=state.download_path):
|
def add_download(magnet_uri):
|
||||||
|
|
||||||
if state.active_downloads is None:
|
if state.active_downloads is None:
|
||||||
state.active_downloads = {}
|
state.active_downloads = {}
|
||||||
|
|
||||||
init_session()
|
init_session()
|
||||||
|
|
||||||
|
free_space = get_free_space_mb(state.download_path)
|
||||||
|
|
||||||
if magnet_uri in state.active_downloads:
|
if magnet_uri in state.active_downloads:
|
||||||
try:
|
try:
|
||||||
handle = state.active_downloads[magnet_uri]
|
handle = state.active_downloads[magnet_uri]
|
||||||
status = handle.status()
|
status = handle.status()
|
||||||
|
|
||||||
|
if status.has_metadata:
|
||||||
|
filepath = os.path.join(status.save_path, status.name)
|
||||||
|
|
||||||
|
if not os.path.exists(filepath):
|
||||||
|
|
||||||
|
consoleLog(f"File Deleted, redownloading: {status.name}")
|
||||||
|
state.dl_session.remove_torrent(handle)
|
||||||
|
del state.active_downloads[magnet_uri]
|
||||||
|
else:
|
||||||
|
consoleLog("Skipping Downloading, download already running...")
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
consoleLog("Skipping Downloading, download already running... ")
|
||||||
|
return False
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
consoleLog(f"Error in LibTorrent Handle: {e}")
|
consoleLog(f"Error in LibTorrent Handle: {e}")
|
||||||
|
del state.active_downloads[magnet_uri]
|
||||||
|
|
||||||
if status.has_metadata:
|
try:
|
||||||
filepath = os.path.join(status.save_path, status.name)
|
params = lt.parse_magnet_uri(magnet_uri)
|
||||||
|
params.save_path = state.download_path
|
||||||
|
|
||||||
if not os.path.exists(filepath):
|
handle = state.dl_session.add_torrent(params)
|
||||||
|
|
||||||
consoleLog(f"File Deleted, redownloading: {status.name}")
|
while not handle.has_metadata():
|
||||||
state.dl_session.remove_torrent(handle)
|
time.sleep(1)
|
||||||
del state.active_downloads[magnet_uri]
|
|
||||||
else:
|
total_size = handle.get_torrent_info().total_size()
|
||||||
consoleLog("Skipping Downloading, download already running...")
|
|
||||||
return False
|
if free_space > total_size:
|
||||||
|
magnetdl = lt.parse_magnet_uri(magnet_uri)
|
||||||
|
magnetdl.save_path = state.download_path
|
||||||
|
download = state.dl_session.add_torrent(magnetdl)
|
||||||
else:
|
else:
|
||||||
consoleLog("Skipping Downloading, download already running... ")
|
download = None
|
||||||
|
state.dl_session.remove_torrent(handle)
|
||||||
|
consoleLog("Not enough free space to download this item.")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
magnetdl = lt.parse_magnet_uri(magnet_uri)
|
except Exception as e:
|
||||||
magnetdl.save_path = dl_path
|
consoleLog(f"Failed to add torrent or fetch info: {e}")
|
||||||
|
return False
|
||||||
download = state.dl_session.add_torrent(magnetdl)
|
if download:
|
||||||
state.active_downloads[magnet_uri] = download
|
state.active_downloads[magnet_uri] = download
|
||||||
consoleLog(f"Added {magnet_uri} to downloads")
|
consoleLog(f"Added {magnet_uri} to downloads")
|
||||||
|
|
||||||
run_thread(threading.Thread(target=dl_status_loop))
|
run_thread(threading.Thread(target=dl_status_loop))
|
||||||
@@ -95,10 +129,13 @@ def add_seed(magnet_uri, file_path):
|
|||||||
consoleLog("Already seeding this torrent")
|
consoleLog("Already seeding this torrent")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
magnetdl = lt.parse_magnet_uri(magnet_uri)
|
try:
|
||||||
magnetdl.save_path = os.path.dirname(file_path)
|
magnetdl = lt.parse_magnet_uri(magnet_uri)
|
||||||
|
magnetdl.save_path = os.path.dirname(file_path)
|
||||||
handle = state.dl_session.add_torrent(magnetdl)
|
handle = state.dl_session.add_torrent(magnetdl)
|
||||||
|
except Exception as e:
|
||||||
|
consoleLog(f"Failed to add seed: {e}")
|
||||||
|
return False
|
||||||
state.active_downloads[magnet_uri] = handle
|
state.active_downloads[magnet_uri] = handle
|
||||||
state.seeded_magnets.add(magnet_uri)
|
state.seeded_magnets.add(magnet_uri)
|
||||||
return True
|
return True
|
||||||
@@ -117,7 +154,7 @@ def dl_status_loop():
|
|||||||
loop_running = False
|
loop_running = False
|
||||||
return
|
return
|
||||||
|
|
||||||
while state.active_downloads and not shutdown_event.is_set():
|
while state.active_downloads and not state.shutdown_event.is_set():
|
||||||
for magnet_uri, magnetdl in list(state.active_downloads.items()):
|
for magnet_uri, magnetdl in list(state.active_downloads.items()):
|
||||||
try:
|
try:
|
||||||
status = magnetdl.status()
|
status = magnetdl.status()
|
||||||
@@ -143,8 +180,8 @@ def update_settings():
|
|||||||
return
|
return
|
||||||
|
|
||||||
settings = {
|
settings = {
|
||||||
"upload_rate_limit": state.up_speed_limit,
|
"upload_rate_limit": state.up_speed_limit * 1024,
|
||||||
"download_rate_limit": state.down_speed_limit,
|
"download_rate_limit": state.down_speed_limit * 1024,
|
||||||
"connections_limit": state.max_connections,
|
"connections_limit": state.max_connections,
|
||||||
"active_downloads": state.max_downloads
|
"active_downloads": state.max_downloads
|
||||||
}
|
}
|
||||||
@@ -165,4 +202,3 @@ def update_bound_interface():
|
|||||||
settings = { "outgoing_interfaces": state.bound_interface }
|
settings = { "outgoing_interfaces": state.bound_interface }
|
||||||
|
|
||||||
state.dl_session.apply_settings(settings)
|
state.dl_session.apply_settings(settings)
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,10 @@ def update_log(shutdown_event):
|
|||||||
|
|
||||||
if status.state == lt.torrent_status.seeding and magnet_uri not in updated and magnet_uri not in state.seeded_magnets:
|
if status.state == lt.torrent_status.seeding and magnet_uri not in updated and magnet_uri not in state.seeded_magnets:
|
||||||
consoleLog(f"Marking {status.name} as completed")
|
consoleLog(f"Marking {status.name} as completed")
|
||||||
info_hash = str(status.info_hash)
|
if hasattr(status, 'info_hashes'):
|
||||||
|
info_hash = str(status.info_hashes.v1)
|
||||||
|
else:
|
||||||
|
info_hash = str(status.info_hash)
|
||||||
update_download_completed_by_hash(info_hash, True)
|
update_download_completed_by_hash(info_hash, True)
|
||||||
updated.add(magnet_uri)
|
updated.add(magnet_uri)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import os
|
|
||||||
import platform
|
|
||||||
import configparser
|
|
||||||
from core.utils.data.state import state
|
from core.utils.data.state import state
|
||||||
|
import configparser
|
||||||
|
import platform
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
def create_config():
|
def create_config():
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
@@ -15,7 +16,7 @@ def create_config():
|
|||||||
|
|
||||||
config["Network"] = {
|
config["Network"] = {
|
||||||
"api_url": f"{state.api_url}",
|
"api_url": f"{state.api_url}",
|
||||||
"download_path": f"{state.download_path}",
|
"bound_interface": f"{state.bound_interface}" if state.bound_interface is not None else "None",
|
||||||
"download_speed_limit": f"{state.down_speed_limit}",
|
"download_speed_limit": f"{state.down_speed_limit}",
|
||||||
"upload_speed_limit": f"{state.up_speed_limit}",
|
"upload_speed_limit": f"{state.up_speed_limit}",
|
||||||
"max_connections": f"{state.max_connections}",
|
"max_connections": f"{state.max_connections}",
|
||||||
@@ -23,7 +24,7 @@ def create_config():
|
|||||||
}
|
}
|
||||||
|
|
||||||
config["Paths"] = {
|
config["Paths"] = {
|
||||||
"bound_interface": f"{state.bound_interface}",
|
"download_path": f"{state.download_path}",
|
||||||
"image_path": f"{state.image_path}"
|
"image_path": f"{state.image_path}"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,14 +65,16 @@ def read_config():
|
|||||||
|
|
||||||
# Network
|
# Network
|
||||||
state.api_url = config.get("Network", "api_url", fallback=state.api_url)
|
state.api_url = config.get("Network", "api_url", fallback=state.api_url)
|
||||||
state.download_path = config.get("Network", "download_path", fallback=state.download_path)
|
state.bound_interface = config.get("Network", "bound_interface", fallback=state.bound_interface)
|
||||||
|
if state.bound_interface == "None":
|
||||||
|
state.bound_interface = None
|
||||||
state.down_speed_limit = config.getint("Network", "download_speed_limit", fallback=state.down_speed_limit)
|
state.down_speed_limit = config.getint("Network", "download_speed_limit", fallback=state.down_speed_limit)
|
||||||
state.up_speed_limit = config.getint("Network", "upload_speed_limit", fallback=state.up_speed_limit)
|
state.up_speed_limit = config.getint("Network", "upload_speed_limit", fallback=state.up_speed_limit)
|
||||||
state.max_connections = config.getint("Network", "max_connections", fallback=state.max_connections)
|
state.max_connections = config.getint("Network", "max_connections", fallback=state.max_connections)
|
||||||
state.max_downloads = config.getint("Network", "max_downloads", fallback=state.max_downloads)
|
state.max_downloads = config.getint("Network", "max_downloads", fallback=state.max_downloads)
|
||||||
|
|
||||||
# Paths
|
# Paths
|
||||||
state.bound_interface = config.get("Paths", "bound_interface", fallback=state.bound_interface)
|
state.download_path = config.get("Paths", "download_path", fallback=state.download_path)
|
||||||
state.image_path = config.get("Paths", "image_path", fallback=state.image_path)
|
state.image_path = config.get("Paths", "image_path", fallback=state.image_path)
|
||||||
|
|
||||||
create_config()
|
create_config()
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ def save_settings(close=lambda: None, apiurl=None, download_path=None, down_spee
|
|||||||
state.max_downloads = max_downloads
|
state.max_downloads = max_downloads
|
||||||
if bound_interface is not None:
|
if bound_interface is not None:
|
||||||
state.bound_interface = None if bound_interface == "None" else bound_interface
|
state.bound_interface = None if bound_interface == "None" else bound_interface
|
||||||
|
|
||||||
|
|
||||||
update_settings()
|
update_settings()
|
||||||
consoleLog("Saved Settings")
|
consoleLog("Saved Settings")
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
|
import threading
|
||||||
from PySide6.QtCore import QObject, Signal
|
from PySide6.QtCore import QObject, Signal
|
||||||
from PySide6.QtWidgets import QTableWidget
|
from PySide6.QtWidgets import QTableWidget
|
||||||
from typing import Optional, Any, List, Dict
|
from typing import Any, List, Dict
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
class AppState(QObject):
|
class AppState(QObject):
|
||||||
@@ -8,20 +9,26 @@ class AppState(QObject):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.posts: list[Any] | None = None
|
self.posts: list[Dict[str,str]] | None = None # titles, urls, author, seederm leecher
|
||||||
self.post_titles: Optional[List] = None
|
|
||||||
self.post_urls: Optional[List] = None
|
|
||||||
self.post_author: List[str] = []
|
|
||||||
self.post_seeders: List[str] = []
|
|
||||||
self.post_leechers: List[str] = []
|
|
||||||
self.version: str = "dev"
|
self.version: str = "dev"
|
||||||
self._image_path: str = ""
|
self._image_path: str = ""
|
||||||
self.ignore_updates: bool = False
|
self.ignore_updates: bool = False
|
||||||
self.debug: bool = False
|
self.debug: bool = False
|
||||||
self.autoresume: bool = True
|
self.autoresume: bool = True
|
||||||
self.tracker: str = "rutracker"
|
|
||||||
self.tracker_list: dict[str, QTableWidget] = {}
|
self.currenttracker: str = "rutracker"
|
||||||
|
self.trackertable: QTableWidget
|
||||||
|
self.trackers: Dict[str,Dict[str,Any]] = {} # each tracker should add itself here
|
||||||
|
'''
|
||||||
|
an example:
|
||||||
|
"rutracker" : {
|
||||||
|
"name" : "rutracker", # name of the tracker
|
||||||
|
"headers" : ["author", "title"], # keys shown in the table
|
||||||
|
"scrapeFunc" : function,
|
||||||
|
}
|
||||||
|
'''
|
||||||
self.api_url: str = "https://api.michijackson.xyz"
|
self.api_url: str = "https://api.michijackson.xyz"
|
||||||
|
self.seeded_magnets: set = set()
|
||||||
self.download_path: str = str(Path.home() / "Downloads")
|
self.download_path: str = str(Path.home() / "Downloads")
|
||||||
self.up_speed_limit: int = 0
|
self.up_speed_limit: int = 0
|
||||||
self.down_speed_limit: int = 0
|
self.down_speed_limit: int = 0
|
||||||
@@ -30,11 +37,15 @@ class AppState(QObject):
|
|||||||
self.settings_path: str = ""
|
self.settings_path: str = ""
|
||||||
self.dl_session: Any = None
|
self.dl_session: Any = None
|
||||||
self.active_downloads: Dict = {}
|
self.active_downloads: Dict = {}
|
||||||
self.seeded_magnets: set = set()
|
|
||||||
self.window_transparency: bool = False
|
self.window_transparency: bool = False
|
||||||
self.interfaces: List = []
|
self.interfaces: List = []
|
||||||
self.active_interfaces: List = []
|
self.active_interfaces: List = []
|
||||||
self.bound_interface: Any = None
|
self.bound_interface: Any = None
|
||||||
|
self.log_buffer: List[str] = []
|
||||||
|
self.downloads_lock = threading.RLock()
|
||||||
|
self.main_window: Any = None
|
||||||
|
self.loop_running: bool = False
|
||||||
|
self.shutdown_event = threading.Event()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def image_path(self) -> str:
|
def image_path(self) -> str:
|
||||||
|
|||||||
@@ -1,36 +1,11 @@
|
|||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from core.utils.data.state import state
|
|
||||||
from core.utils.logging.logs import consoleLog
|
from core.utils.logging.logs import consoleLog
|
||||||
from core.utils.network.jsonhandler import format_data
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_item_url(item, posts, post_titles): # softwarelist currentitem, post list (dict), post titles list
|
|
||||||
post_index = post_titles.index(item)
|
|
||||||
if 0 <= post_index < len(post_titles):
|
|
||||||
if state.tracker == "uztracker":
|
|
||||||
item = "https://uztracker.net/" + state.post_urls[post_index].lstrip("./")
|
|
||||||
consoleLog(f"Found post URL: {item}")
|
|
||||||
return item
|
|
||||||
if state.tracker == "rutracker":
|
|
||||||
item_dict = posts[post_index]
|
|
||||||
_, post_links, _, _, _ = format_data([item_dict])
|
|
||||||
consoleLog(f"Found post URL: {post_links[0]}")
|
|
||||||
return post_links[0]
|
|
||||||
if state.tracker == "m0nkrus":
|
|
||||||
item_dict = posts[post_index]
|
|
||||||
_, post_links, _, _, _,= format_data([item_dict])
|
|
||||||
consoleLog(f"Found post URL: {post_links[0]}")
|
|
||||||
return post_links[0]
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_magnet_link(post_url):
|
def get_magnet_link(post_url):
|
||||||
try:
|
try:
|
||||||
response = requests.get(post_url) # eventually impl. cloudscraper
|
response = requests.get(post_url, timeout=15) # eventually impl. cloudscraper
|
||||||
consoleLog("Sent Request to retrieve Magnet Link...")
|
consoleLog("Sent Request to retrieve Magnet Link...")
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
soup = BeautifulSoup(response.text, 'html.parser')
|
soup = BeautifulSoup(response.text, 'html.parser')
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import threading
|
from core.utils.data.state import state
|
||||||
import os
|
import os
|
||||||
|
|
||||||
shutdown_event = threading.Event()
|
|
||||||
|
|
||||||
def closehelper():
|
def closehelper():
|
||||||
shutdown_event.set()
|
state.shutdown_event.set()
|
||||||
|
try:
|
||||||
|
from core.network.libtorrent_misc import cleanup_session
|
||||||
|
cleanup_session()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
def force_exit():
|
def force_exit():
|
||||||
os._exit(0)
|
os._exit(0)
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
from core.utils.logging.logs import consoleLog, remove_download_log
|
from core.utils.logging.logs import consoleLog, remove_download_log
|
||||||
from core.utils.network.download import run_download_direct, seed_magnet
|
from core.utils.network.download import run_download_direct, seed_magnet
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
def split_data(data):
|
def split_data(data):
|
||||||
@@ -15,15 +14,23 @@ def check_completed(downloads, resume):
|
|||||||
if download.completed == False:
|
if download.completed == False:
|
||||||
consoleLog(f"Found unfinished download: {download.title}")
|
consoleLog(f"Found unfinished download: {download.title}")
|
||||||
if resume == True:
|
if resume == True:
|
||||||
run_download_direct(download.magnet_uri)
|
if download.magnet_uri:
|
||||||
consoleLog(f"Resuming {download.title}")
|
run_download_direct(download.magnet_uri, download.title)
|
||||||
|
consoleLog(f"Resuming Magnet: {download.title}")
|
||||||
|
elif download.url:
|
||||||
|
from core.network.direct_download import add_direct_download
|
||||||
|
add_direct_download(download.url, download.title)
|
||||||
|
consoleLog(f"Resuming Direct Download: {download.title}")
|
||||||
|
|
||||||
def check_downloads(downloads):
|
def check_downloads(downloads):
|
||||||
for download in downloads:
|
for download in downloads:
|
||||||
if os.path.exists(download.path):
|
if download.completed == True and os.path.exists(download.path):
|
||||||
consoleLog(f"Existing Download: {download.title}")
|
consoleLog(f"Existing Download: {download.title}")
|
||||||
seed_magnet(download.magnet_uri, download.path)
|
try:
|
||||||
else:
|
seed_magnet(download.magnet_uri, download.path)
|
||||||
|
except Exception as e:
|
||||||
|
consoleLog(f"Failed to seed {download.title}: {e}")
|
||||||
|
elif download.completed == True and not os.path.exists(download.path):
|
||||||
consoleLog(f"Inexistent Download: {download.title}")
|
consoleLog(f"Inexistent Download: {download.title}")
|
||||||
remove_download_log(download.magnet_uri)
|
remove_download_log(download.magnet_uri)
|
||||||
|
|
||||||
@@ -8,10 +8,6 @@ import re
|
|||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
_log_buffer = []
|
|
||||||
_downloads_lock = threading.RLock()
|
|
||||||
|
|
||||||
|
|
||||||
def add_download_log(title, url, magnet_uri, completed) -> DownloadList:
|
def add_download_log(title, url, magnet_uri, completed) -> DownloadList:
|
||||||
# wait for metadata outside the lock to avoid blocking other threads
|
# wait for metadata outside the lock to avoid blocking other threads
|
||||||
magnetdl = state.active_downloads.get(magnet_uri)
|
magnetdl = state.active_downloads.get(magnet_uri)
|
||||||
@@ -28,7 +24,7 @@ def add_download_log(title, url, magnet_uri, completed) -> DownloadList:
|
|||||||
path = os.path.join(save_path, torrent_name)
|
path = os.path.join(save_path, torrent_name)
|
||||||
else:
|
else:
|
||||||
path = os.path.join(state.download_path, title)
|
path = os.path.join(state.download_path, title)
|
||||||
with _downloads_lock:
|
with state.downloads_lock:
|
||||||
return _add_download_log_inner(title, url, magnet_uri, completed, path)
|
return _add_download_log_inner(title, url, magnet_uri, completed, path)
|
||||||
|
|
||||||
def _add_download_log_inner(title, url, magnet_uri, completed, path) -> DownloadList:
|
def _add_download_log_inner(title, url, magnet_uri, completed, path) -> DownloadList:
|
||||||
@@ -44,14 +40,15 @@ def _add_download_log_inner(title, url, magnet_uri, completed, path) -> Download
|
|||||||
else:
|
else:
|
||||||
downloads = []
|
downloads = []
|
||||||
|
|
||||||
if any(d.magnet_uri == magnet_uri or d.url == url for d in downloads):
|
if any((magnet_uri and d.magnet_uri == magnet_uri) or (url and d.url == url) for d in downloads):
|
||||||
if magnet_uri in state.active_downloads:
|
if magnet_uri and magnet_uri in state.active_downloads:
|
||||||
consoleLog("Skipping Logging, download already running...")
|
consoleLog("Skipping Logging, download already running...")
|
||||||
return
|
return DownloadList(data=downloads, count=len(downloads))
|
||||||
consoleLog("File already in Log, updating Download State...")
|
consoleLog("File already in Log, updating Download State...")
|
||||||
hash = extract_hash_from_magnet(magnet_uri)
|
hash = extract_hash_from_magnet(magnet_uri)
|
||||||
update_download_completed_by_hash(hash, False)
|
if hash:
|
||||||
return DownloadList(data=downloads, count=len(downloads)) # thanks again claude (im stupid)
|
update_download_completed_by_hash(hash, False)
|
||||||
|
return DownloadList(data=downloads, count=len(downloads))
|
||||||
|
|
||||||
|
|
||||||
downloads.append(Download(
|
downloads.append(Download(
|
||||||
@@ -71,7 +68,7 @@ def _add_download_log_inner(title, url, magnet_uri, completed, path) -> Download
|
|||||||
return download_list
|
return download_list
|
||||||
|
|
||||||
def remove_download_log(magnet_uri) -> DownloadList:
|
def remove_download_log(magnet_uri) -> DownloadList:
|
||||||
with _downloads_lock:
|
with state.downloads_lock:
|
||||||
return _remove_download_log_inner(magnet_uri)
|
return _remove_download_log_inner(magnet_uri)
|
||||||
|
|
||||||
def _remove_download_log_inner(magnet_uri) -> DownloadList:
|
def _remove_download_log_inner(magnet_uri) -> DownloadList:
|
||||||
@@ -89,7 +86,10 @@ def _remove_download_log_inner(magnet_uri) -> DownloadList:
|
|||||||
|
|
||||||
|
|
||||||
magnet_link = (magnet_uri or "").strip()
|
magnet_link = (magnet_uri or "").strip()
|
||||||
title = next((getattr(d, 'title', 'Unknown') for d in downloads if (getattr(d, 'magnet_uri', None) or '').strip() == magnet_link or (getattr(d, 'url', None) or '').strip() == magnet_link))
|
if not magnet_link:
|
||||||
|
return DownloadList(data=downloads, count=len(downloads))
|
||||||
|
|
||||||
|
title = next((getattr(d, 'title', 'Unknown') for d in downloads if (getattr(d, 'magnet_uri', None) or '').strip() == magnet_link or (getattr(d, 'url', None) or '').strip() == magnet_link), 'Unknown')
|
||||||
downloads = [d for d in downloads if (getattr(d, 'magnet_uri', None) or "").strip() != magnet_link and (getattr(d, 'url', None) or "").strip() != magnet_link]
|
downloads = [d for d in downloads if (getattr(d, 'magnet_uri', None) or "").strip() != magnet_link and (getattr(d, 'url', None) or "").strip() != magnet_link]
|
||||||
|
|
||||||
consoleLog(f"Removed {title} from Log File")
|
consoleLog(f"Removed {title} from Log File")
|
||||||
@@ -101,7 +101,7 @@ def _remove_download_log_inner(magnet_uri) -> DownloadList:
|
|||||||
return download_list
|
return download_list
|
||||||
|
|
||||||
def update_download_completed(magnet_uri, completed) -> DownloadList:
|
def update_download_completed(magnet_uri, completed) -> DownloadList:
|
||||||
with _downloads_lock:
|
with state.downloads_lock:
|
||||||
return _update_download_completed_inner(magnet_uri, completed)
|
return _update_download_completed_inner(magnet_uri, completed)
|
||||||
|
|
||||||
def _update_download_completed_inner(magnet_uri, completed) -> DownloadList:
|
def _update_download_completed_inner(magnet_uri, completed) -> DownloadList:
|
||||||
@@ -135,9 +135,7 @@ def _update_download_completed_inner(magnet_uri, completed) -> DownloadList:
|
|||||||
try:
|
try:
|
||||||
stored_magnet = (getattr(download, 'magnet_uri', None) or "").strip()
|
stored_magnet = (getattr(download, 'magnet_uri', None) or "").strip()
|
||||||
stored_url = (getattr(download, 'url', None) or "").strip()
|
stored_url = (getattr(download, 'url', None) or "").strip()
|
||||||
consoleLog(f"Comparing with magnet: {stored_magnet[:50] if stored_magnet else 'None'}...")
|
|
||||||
if identifier and (stored_magnet == identifier or stored_url == identifier):
|
if identifier and (stored_magnet == identifier or stored_url == identifier):
|
||||||
consoleLog(f"Match found! Setting completed={completed}")
|
|
||||||
download.completed = completed
|
download.completed = completed
|
||||||
found = True
|
found = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -157,7 +155,7 @@ def _update_download_completed_inner(magnet_uri, completed) -> DownloadList:
|
|||||||
|
|
||||||
|
|
||||||
def get_download_logs() -> DownloadList:
|
def get_download_logs() -> DownloadList:
|
||||||
with _downloads_lock:
|
with state.downloads_lock:
|
||||||
return _get_download_logs_inner()
|
return _get_download_logs_inner()
|
||||||
|
|
||||||
def _get_download_logs_inner() -> DownloadList:
|
def _get_download_logs_inner() -> DownloadList:
|
||||||
@@ -176,15 +174,24 @@ def _get_download_logs_inner() -> DownloadList:
|
|||||||
return DownloadList(data=downloads, count=len(downloads))
|
return DownloadList(data=downloads, count=len(downloads))
|
||||||
|
|
||||||
|
|
||||||
def extract_hash_from_magnet(magnet_uri): # full credits to claude for this
|
def extract_hash_from_magnet(magnet_uri):
|
||||||
match = re.search(r'urn:btih:([A-F0-9]+)', magnet_uri, re.IGNORECASE)
|
if not magnet_uri:
|
||||||
if match:
|
return None
|
||||||
return match.group(1).upper()
|
try:
|
||||||
return None
|
import libtorrent as lt
|
||||||
|
params = lt.parse_magnet_uri(magnet_uri)
|
||||||
|
if hasattr(params, 'info_hashes'): # lt 2.0+
|
||||||
|
return str(params.info_hashes.v1).upper()
|
||||||
|
return str(params.info_hash).upper()
|
||||||
|
except Exception:
|
||||||
|
match = re.search(r'urn:btih:([a-zA-Z0-9]+)', magnet_uri)
|
||||||
|
if match:
|
||||||
|
return match.group(1).upper()
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def update_download_completed_by_hash(info_hash, completed) -> DownloadList:
|
def update_download_completed_by_hash(info_hash, completed) -> DownloadList:
|
||||||
with _downloads_lock:
|
with state.downloads_lock:
|
||||||
return _update_download_completed_by_hash_inner(info_hash, completed)
|
return _update_download_completed_by_hash_inner(info_hash, completed)
|
||||||
|
|
||||||
def _update_download_completed_by_hash_inner(info_hash, completed) -> DownloadList:
|
def _update_download_completed_by_hash_inner(info_hash, completed) -> DownloadList:
|
||||||
@@ -230,20 +237,16 @@ def _update_download_completed_by_hash_inner(info_hash, completed) -> DownloadLi
|
|||||||
return download_list
|
return download_list
|
||||||
|
|
||||||
|
|
||||||
_main_window = None
|
|
||||||
|
|
||||||
def set_main_window(window):
|
def set_main_window(window):
|
||||||
global _main_window
|
state.main_window = window
|
||||||
_main_window = window
|
|
||||||
|
|
||||||
def flush_log_buffer(): # credits to claude
|
def flush_log_buffer(): # credits to claude
|
||||||
global _log_buffer
|
if state.log_buffer:
|
||||||
if _log_buffer:
|
|
||||||
try:
|
try:
|
||||||
from core.interface.gui import MainWindow
|
from core.interface.gui import MainWindow
|
||||||
for log_entry in _log_buffer:
|
for log_entry in state.log_buffer:
|
||||||
MainWindow.add_log(log_entry)
|
MainWindow.add_log(log_entry)
|
||||||
_log_buffer = []
|
state.log_buffer = []
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -254,10 +257,10 @@ def consoleLog(text, printAnyways = False):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
from core.interface.gui import MainWindow
|
from core.interface.gui import MainWindow
|
||||||
MainWindow.add_log(formatted_text)
|
if not MainWindow.add_log(formatted_text):
|
||||||
|
state.log_buffer.append(formatted_text)
|
||||||
except Exception:
|
except Exception:
|
||||||
global _log_buffer
|
state.log_buffer.append(formatted_text)
|
||||||
_log_buffer.append(formatted_text)
|
|
||||||
|
|
||||||
if state.debug or printAnyways:
|
if state.debug or printAnyways:
|
||||||
print(formatted_text)
|
print(formatted_text)
|
||||||
|
|||||||
@@ -1,14 +1,18 @@
|
|||||||
|
from PySide6.QtWidgets import QTableWidgetItem
|
||||||
|
from PySide6.QtCore import Qt
|
||||||
from core.utils.logging.logs import consoleLog
|
from core.utils.logging.logs import consoleLog
|
||||||
from core.utils.data.tracker import get_item_url
|
from core.network.libtorrent_wrapper import add_magnet
|
||||||
from core.utils.data.tracker import get_magnet_link
|
|
||||||
from core.network.libtorrent_wrapper import add_download
|
|
||||||
from core.utils.general.wrappers import run_thread
|
from core.utils.general.wrappers import run_thread
|
||||||
from core.utils.logging.logs import add_download_log
|
from core.utils.logging.logs import add_download_log
|
||||||
from core.network.libtorrent_int import add_seed
|
from core.network.libtorrent_int import add_seed
|
||||||
|
from core.utils.data.state import state
|
||||||
|
from core.network.direct_download import add_direct_download
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
|
|
||||||
def download_selected(items, posts, post_titles):
|
|
||||||
|
|
||||||
|
def download_selected(items: list[QTableWidgetItem]):
|
||||||
if not items:
|
if not items:
|
||||||
consoleLog("No item selected for download.")
|
consoleLog("No item selected for download.")
|
||||||
return
|
return
|
||||||
@@ -16,25 +20,32 @@ def download_selected(items, posts, post_titles):
|
|||||||
for item in items:
|
for item in items:
|
||||||
if item.column() != 0:
|
if item.column() != 0:
|
||||||
continue
|
continue
|
||||||
text = item.text()
|
post_idx = item.data(Qt.ItemDataRole.UserRole)
|
||||||
if text and text not in seen:
|
if post_idx is None:
|
||||||
seen.add(text)
|
post_idx = item.row()
|
||||||
consoleLog(f"Downloading {text}")
|
|
||||||
run_thread(threading.Thread(target=run_download, args=(text, posts, post_titles)))
|
if post_idx not in seen:
|
||||||
|
seen.add(post_idx)
|
||||||
|
post = state.posts[post_idx]
|
||||||
|
consoleLog(f"Downloading {post.get('title', 'Unknown')}")
|
||||||
|
run_thread(threading.Thread(target=run_download, args=(post,)))
|
||||||
|
|
||||||
def run_download(item, posts, post_titles):
|
def run_download(post):
|
||||||
post_url = get_item_url(item, posts, post_titles)
|
linkfunc = state.trackers[state.currenttracker]["linkFunc"]
|
||||||
consoleLog(f"Selected URL: {post_url}")
|
ismagnet = state.trackers[state.currenttracker]["isMagnet"]
|
||||||
magnet_uri = get_magnet_link(post_url)
|
link = linkfunc(post)
|
||||||
|
|
||||||
if add_download(magnet_uri):
|
if ismagnet:
|
||||||
add_download_log(item, post_url, magnet_uri, False)
|
add_magnet(link)
|
||||||
|
add_download_log(post.get("title", "Unknown"), "", link, False)
|
||||||
|
else:
|
||||||
|
add_direct_download(link, post.get("title", "Unknown"))
|
||||||
|
|
||||||
def run_download_direct(magnet_uri):
|
def run_download_direct(magnet_uri, title="Direct Download"):
|
||||||
consoleLog(f"Direct download: {magnet_uri[:60]}")
|
consoleLog(f"Magnet: {title}")
|
||||||
add_download(magnet_uri)
|
add_magnet(magnet_uri)
|
||||||
add_download_log("Direct Download", "", magnet_uri, False)
|
add_download_log(title, "", magnet_uri, False)
|
||||||
|
|
||||||
def seed_magnet(magnet_uri, file_path):
|
def seed_magnet(magnet_uri, file_path):
|
||||||
consoleLog(f"Seeding: {magnet_uri[:60]}")
|
consoleLog(f"Seeding: {magnet_uri[:60]}")
|
||||||
add_seed(magnet_uri, file_path)
|
add_seed(magnet_uri, file_path)
|
||||||
|
|||||||
@@ -2,29 +2,38 @@ import requests
|
|||||||
from core.utils.data.state import state
|
from core.utils.data.state import state
|
||||||
from core.utils.logging.logs import consoleLog
|
from core.utils.logging.logs import consoleLog
|
||||||
|
|
||||||
def check_for_updates():
|
def get_updates():
|
||||||
url = f"https://api.github.com/repos/KeksPirates/SoftwareManager/releases"
|
url = f"https://api.github.com/repos/KeksPirates/SoftwareManager/releases/latest"
|
||||||
response = requests.get(url)
|
try:
|
||||||
|
response = requests.get(url, timeout=15)
|
||||||
|
except requests.RequestException as e:
|
||||||
|
consoleLog(f"Failed to fetch releases: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
consoleLog(f"Failed to fetch releases: {response.status_code}")
|
consoleLog(f"Failed to fetch releases: {response.status_code}")
|
||||||
exit(1)
|
return None
|
||||||
|
|
||||||
releases = response.json()
|
release = response.json()
|
||||||
releases.sort(key=lambda r: r["published_at"], reverse=True)
|
|
||||||
|
|
||||||
latest_release = releases[0]
|
latest_version = release.get("name") or release.get("tag_name")
|
||||||
latest_version = latest_release["name"]
|
assets = release.get("assets", [])
|
||||||
assets = latest_release["assets"]
|
|
||||||
|
release_assets = []
|
||||||
|
|
||||||
if latest_version != state.version:
|
if latest_version != state.version:
|
||||||
consoleLog(f"New release available: {latest_version}")
|
consoleLog(f"New release available: {latest_version}")
|
||||||
if assets:
|
if assets:
|
||||||
consoleLog("Assets:")
|
consoleLog("Assets:")
|
||||||
for asset in assets:
|
for asset in assets:
|
||||||
consoleLog(f" - {asset['name']}: {asset['browser_download_url']}")
|
consoleLog(f"{asset['name']}")
|
||||||
return assets, latest_version
|
release_assets.append(dict(
|
||||||
else:
|
name=asset['name'],
|
||||||
|
url=asset['browser_download_url'],
|
||||||
|
hash=asset.get('digest')
|
||||||
|
))
|
||||||
|
return release_assets, latest_version
|
||||||
|
else:
|
||||||
return None, None
|
return None, None
|
||||||
else:
|
else:
|
||||||
consoleLog("Already up-to-date.")
|
consoleLog("Already up-to-date.")
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
from core.network.direct_download.handle import DirectDownloadHandle
|
||||||
|
from core.utils.logging.logs import consoleLog
|
||||||
|
from core.utils.data.state import state
|
||||||
|
from PySide6.QtCore import Qt
|
||||||
|
from PySide6 import QtWidgets
|
||||||
|
import libtorrent as lt
|
||||||
|
import subprocess
|
||||||
|
import tempfile
|
||||||
|
import hashlib
|
||||||
|
import time
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def _verify_hash(file_path: str, expected_hash: str) -> bool:
|
||||||
|
sha256 = hashlib.sha256()
|
||||||
|
with open(file_path, "rb") as f:
|
||||||
|
for chunk in iter(lambda: f.read(8192), b""):
|
||||||
|
sha256.update(chunk)
|
||||||
|
return f"sha256:{sha256.hexdigest()}" == expected_hash
|
||||||
|
|
||||||
|
|
||||||
|
def download_update(assets: list):
|
||||||
|
filename = None
|
||||||
|
setup_hash = None
|
||||||
|
url = None
|
||||||
|
|
||||||
|
for asset in assets:
|
||||||
|
if "-windows-setup.exe" in asset["name"]:
|
||||||
|
filename = asset["name"]
|
||||||
|
setup_hash = asset["hash"]
|
||||||
|
url = asset["url"]
|
||||||
|
break
|
||||||
|
|
||||||
|
if not filename:
|
||||||
|
consoleLog("Error: No Windows installer found in release assets")
|
||||||
|
return
|
||||||
|
|
||||||
|
installer_path = os.path.join(tempfile.gettempdir(), filename)
|
||||||
|
|
||||||
|
progress = QtWidgets.QProgressDialog("Downloading update... (0.0 MB/s)", None, 0, 100)
|
||||||
|
progress.setWindowTitle("Updating")
|
||||||
|
progress.setWindowModality(Qt.WindowModality.ApplicationModal)
|
||||||
|
progress.setCancelButton(None)
|
||||||
|
progress.setMinimumDuration(0)
|
||||||
|
progress.setAutoClose(False)
|
||||||
|
progress.setAutoReset(False)
|
||||||
|
progress.setValue(0)
|
||||||
|
progress.show()
|
||||||
|
QtWidgets.QApplication.processEvents()
|
||||||
|
|
||||||
|
original_limit = state.down_speed_limit
|
||||||
|
state.down_speed_limit = 0
|
||||||
|
handle = DirectDownloadHandle(url, filename, tempfile.gettempdir())
|
||||||
|
handle.start()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
QtWidgets.QApplication.processEvents()
|
||||||
|
status = handle.status()
|
||||||
|
|
||||||
|
if status.error:
|
||||||
|
state.down_speed_limit = original_limit
|
||||||
|
progress.close()
|
||||||
|
consoleLog(f"Update download failed: {status.error}")
|
||||||
|
return
|
||||||
|
|
||||||
|
if status.total_wanted > 0:
|
||||||
|
pct = int(status.total_wanted_done * 100 / status.total_wanted)
|
||||||
|
speed_mb = status.download_rate / (1024 * 1024)
|
||||||
|
progress.setValue(pct)
|
||||||
|
progress.setLabelText(f"Downloading update... ({speed_mb:.1f} MB/s)")
|
||||||
|
|
||||||
|
if status.state == lt.torrent_status.seeding:
|
||||||
|
break
|
||||||
|
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
state.down_speed_limit = original_limit
|
||||||
|
|
||||||
|
if setup_hash:
|
||||||
|
if _verify_hash(installer_path, setup_hash):
|
||||||
|
consoleLog(f"Successfully validated installer hash ({setup_hash})")
|
||||||
|
else:
|
||||||
|
progress.close()
|
||||||
|
consoleLog("Error: Invalid file hash, file may be corrupted")
|
||||||
|
sys.exit(0)
|
||||||
|
else:
|
||||||
|
consoleLog("Skipping hash verification (no hash found for release)")
|
||||||
|
|
||||||
|
progress.setLabelText("Installing update...")
|
||||||
|
progress.setValue(100)
|
||||||
|
QtWidgets.QApplication.processEvents()
|
||||||
|
|
||||||
|
subprocess.Popen([installer_path, "/VERYSILENT", "/SUPPRESSMSGBOXES", "/SP-", "/CLOSEAPPLICATIONS"])
|
||||||
|
time.sleep(1)
|
||||||
|
sys.exit(0)
|
||||||
+8
-7
@@ -1,9 +1,9 @@
|
|||||||
from core.interface.gui import MainWindow, windowCloseHelper
|
from core.interface.gui import MainWindow
|
||||||
from core.utils.data.state import state
|
from core.utils.data.state import state
|
||||||
from core.utils.logging.logs import consoleLog
|
from core.utils.logging.logs import consoleLog
|
||||||
from core.network.libtorrent_misc import send_notification, update_log, check_deleted_files
|
from core.network.libtorrent_misc import send_notification, update_log, check_deleted_files
|
||||||
from core.utils.logging.logs import get_download_logs
|
from core.utils.logging.logs import get_download_logs
|
||||||
from core.utils.general.shutdown import closehelper, shutdown_event
|
from core.utils.general.shutdown import closehelper
|
||||||
from core.utils.general.wrappers import run_thread
|
from core.utils.general.wrappers import run_thread
|
||||||
from core.utils.logging.loghandler import split_data, check_completed, check_downloads
|
from core.utils.logging.loghandler import split_data, check_completed, check_downloads
|
||||||
from core.network.interface import list_interfaces, init_interfaces
|
from core.network.interface import list_interfaces, init_interfaces
|
||||||
@@ -48,18 +48,19 @@ def main():
|
|||||||
consoleLog("Initializing Interface variables...")
|
consoleLog("Initializing Interface variables...")
|
||||||
init_interfaces()
|
init_interfaces()
|
||||||
consoleLog(f"Current Bound: {state.bound_interface}")
|
consoleLog(f"Current Bound: {state.bound_interface}")
|
||||||
run_thread(threading.Thread(target=send_notification, args=(shutdown_event,), daemon=True))
|
run_thread(threading.Thread(target=send_notification, args=(state.shutdown_event,), daemon=True))
|
||||||
consoleLog("Started Thread: send_notification")
|
consoleLog("Started Thread: send_notification")
|
||||||
run_thread(threading.Thread(target=update_log, args=(shutdown_event,), daemon=True))
|
run_thread(threading.Thread(target=update_log, args=(state.shutdown_event,), daemon=True))
|
||||||
consoleLog("Started Thread: update_log")
|
consoleLog("Started Thread: update_log")
|
||||||
run_thread(threading.Thread(target=check_completed, args=(downloads, state.autoresume)))
|
run_thread(threading.Thread(target=check_completed, args=(downloads, state.autoresume)))
|
||||||
consoleLog("Started Thread: check_completed")
|
consoleLog("Started Thread: check_completed")
|
||||||
run_thread(threading.Thread(target=check_deleted_files, args=(shutdown_event,), daemon=True))
|
run_thread(threading.Thread(target=check_deleted_files, args=(state.shutdown_event,), daemon=True))
|
||||||
consoleLog("Started Thread: check_deleted_files")
|
consoleLog("Started Thread: check_deleted_files")
|
||||||
check_downloads(downloads)
|
run_thread(threading.Thread(target=check_downloads, args=(downloads,)))
|
||||||
|
consoleLog("Started Thread: check_downloads")
|
||||||
elapsed = time.perf_counter() - start_time
|
elapsed = time.perf_counter() - start_time
|
||||||
consoleLog(f"Initialization completed in {elapsed:.2f}s. Launching GUI")
|
consoleLog(f"Initialization completed in {elapsed:.2f}s. Launching GUI")
|
||||||
run_gui()
|
run_gui()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user