mirror of
https://github.com/KeksPirates/SoftwareManager.git
synced 2026-08-03 17:39:42 +02:00
Update everything to work with new API + Reorganize file structurem functions and common logic
This commit is contained in:
@@ -6,10 +6,7 @@ def scrape_rutracker(search_text):
|
||||
search = requests.get(f"{state.api_url}/search/{search_text}")
|
||||
if search:
|
||||
try:
|
||||
data = search.json()
|
||||
resulttitles = data["titles"]
|
||||
resultlinks = data["links"]
|
||||
return resulttitles, resultlinks
|
||||
return search.text
|
||||
except Exception:
|
||||
if state.debug:
|
||||
print("No results found / No response from server")
|
||||
|
||||
+14
-20
@@ -25,6 +25,8 @@ from core.data.scrapers.rutracker import scrape_rutracker
|
||||
from core.utils.wrappers import run_thread
|
||||
from core.utils.settings import save_settings
|
||||
from core.utils.data.state import state
|
||||
from core.utils.network.jsonhandler import split_data, format_data
|
||||
from core.utils.network.download import download_selected
|
||||
from core.network.aria2_integration import dlprogress
|
||||
|
||||
|
||||
@@ -71,7 +73,7 @@ class MainWindow(QtWidgets.QMainWindow, QWidget):
|
||||
self.softwareList.addItems(searchresults)
|
||||
|
||||
# download button triggers
|
||||
self.dlbutton.clicked.connect(lambda: run_thread(threading.Thread(target=self.download_selected)))
|
||||
self.dlbutton.clicked.connect(lambda: run_thread(threading.Thread(target=download_selected, args=(self.softwareList.currentItem(), state.posts, state.post_titles))))
|
||||
|
||||
container.setLayout(containerLayout)
|
||||
self.setCentralWidget(container)
|
||||
@@ -179,7 +181,6 @@ class MainWindow(QtWidgets.QMainWindow, QWidget):
|
||||
text_edit.setText(state.api_url)
|
||||
dialog.layout().addWidget(api_url_container)
|
||||
|
||||
# dialog.layout().addWidget(QtWidgets.QPushButton("Close", clicked=dialog.close))
|
||||
|
||||
layout = QHBoxLayout() # layout for buttons
|
||||
|
||||
@@ -199,17 +200,6 @@ class MainWindow(QtWidgets.QMainWindow, QWidget):
|
||||
|
||||
dialog.exec()
|
||||
|
||||
# def download_selected(self):
|
||||
# item = self.softwareList.currentItem()
|
||||
# if item is not None:
|
||||
# if state.debug:
|
||||
# print(f"network {self.softwareList.currentItem().text()}")
|
||||
# self.run_thread(threading.Thread(target=get_item_index, args=(self.softwareList.currentItem().text(), self.postnames, self.postlinks, state.debug)))
|
||||
# else:
|
||||
# if state.debug:
|
||||
# print("No item selected for download.")
|
||||
# # add gui notification for no item selected
|
||||
|
||||
|
||||
# this needs a cleanup
|
||||
def return_pressed(self):
|
||||
@@ -223,19 +213,23 @@ class MainWindow(QtWidgets.QMainWindow, QWidget):
|
||||
if state.tracker == "uztracker":
|
||||
response = asyncio.run(scrape_uztracker(search_text))
|
||||
if state.tracker == "rutracker":
|
||||
response = asyncio.run(scrape_rutracker(search_text))
|
||||
response = scrape_rutracker(search_text)
|
||||
if response:
|
||||
if state.tracker == "uztracker":
|
||||
self.postnames, self.postlinks = response
|
||||
state.post_titles, state.post_urls, = response
|
||||
self.softwareList.clear()
|
||||
if self.postnames:
|
||||
self.softwareList.addItems(self.postnames)
|
||||
if state.post_titles:
|
||||
self.softwareList.addItems(state.post_titles)
|
||||
if state.tracker == "rutracker":
|
||||
pass
|
||||
_, state.posts, _, _ = split_data(response)
|
||||
state.post_titles, _ = format_data(state.posts)
|
||||
self.softwareList.clear()
|
||||
self.softwareList.addItems(state.post_titles)
|
||||
if not response:
|
||||
print(f"No Results found for \"{search_text}\"")
|
||||
if state.debug:
|
||||
print(f"No Results found for \"{search_text}\"")
|
||||
self.softwareList.clear()
|
||||
self.softwareList.addItem("No Results")
|
||||
self.softwareList.addItem("No Results") # replace with no results text in center
|
||||
|
||||
|
||||
def update_progress(self):
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Any
|
||||
|
||||
from typing import Optional, Any, List, Dict
|
||||
|
||||
@dataclass
|
||||
class AppState:
|
||||
posts: List[Dict[str, Any]]
|
||||
post_titles: List[str]
|
||||
post_urls: List[str]
|
||||
debug: bool = False
|
||||
tracker: str = "rutracker"
|
||||
api_url: str = "https://api.michijackson.xyz"
|
||||
aria2process: Optional[Any] = None
|
||||
aria2_threads: int = 4
|
||||
|
||||
|
||||
state = AppState()
|
||||
state = AppState(posts=[], post_titles=[], post_urls=[])
|
||||
+14
-18
@@ -1,43 +1,39 @@
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from core.network.aria2_wrapper import start_client
|
||||
from core.network.aria2_wrapper import add_magnet
|
||||
from core.utils.data.state import state
|
||||
from core.utils.network.jsonhandler import format_data
|
||||
|
||||
|
||||
|
||||
def get_item_index(item, list, listlinks, debug):
|
||||
position = list.index(item)
|
||||
if 0 <= position < len(listlinks): # note for myself: py starts counting at 0; check if number is not negative, check if number is not more than list length.
|
||||
def get_item_url(item, posts, post_titles): # oftwarelist currentitem, post list (dict), post titles list
|
||||
post_index = post_titles.index(item)
|
||||
if 0 <= post_index < len(post_titles):
|
||||
if state.tracker == "uztracker":
|
||||
selected = "https://uztracker.net/" + listlinks[position].lstrip("./")
|
||||
item = "https://uztracker.net/" + state.post_urls[post_index].lstrip("./")
|
||||
return item
|
||||
if state.tracker == "rutracker":
|
||||
selected = listlinks[position]
|
||||
|
||||
if debug:
|
||||
print("Selected URL: ", selected)
|
||||
selected_magnet = get_magnet_link(selected, debug)
|
||||
start_client()
|
||||
add_magnet(selected_magnet)
|
||||
item_dict = posts[post_index]
|
||||
_, post_links = format_data([item_dict])
|
||||
return post_links[0]
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
def get_magnet_link(post_url, debug):
|
||||
def get_magnet_link(post_url):
|
||||
try:
|
||||
response = requests.get(post_url)
|
||||
response.raise_for_status()
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
magnet_link = soup.find('a', href=lambda x: x and x.startswith('magnet:'))
|
||||
if magnet_link:
|
||||
if debug:
|
||||
if state.debug:
|
||||
print("Magnet Link Retrieved: ", magnet_link['href'])
|
||||
return magnet_link['href']
|
||||
else:
|
||||
if debug:
|
||||
if state.debug:
|
||||
print("Magnet Link not Found!")
|
||||
except requests.RequestException as e:
|
||||
if debug:
|
||||
if state.debug:
|
||||
print(f"Failed to fetch {post_url}: {e}")
|
||||
return None
|
||||
|
||||
|
||||
@@ -1,17 +1,30 @@
|
||||
from core.utils.data.state import state
|
||||
from core.utils.data.tracker import get_item_index
|
||||
from core.utils.data.tracker import get_item_url
|
||||
from core.utils.data.tracker import get_magnet_link
|
||||
from core.network.aria2_wrapper import start_client
|
||||
from core.network.aria2_wrapper import add_magnet
|
||||
from core.utils.wrappers import run_thread
|
||||
import threading
|
||||
|
||||
|
||||
def download_selected(item, ):
|
||||
def download_selected(item, posts, post_titles):
|
||||
if item is not None:
|
||||
if state.debug:
|
||||
print(f"network {item.text()}")
|
||||
run_thread(threading.Thread(target=get_item_index, args=(item.text(), self.postnames, self.postlinks, state.debug)))
|
||||
run_thread(threading.Thread(target=run_download, args=(item.text(), posts, post_titles)))
|
||||
|
||||
|
||||
else:
|
||||
if state.debug:
|
||||
print("No item selected for download.")
|
||||
|
||||
def run_download(item, posts, post_titles):
|
||||
post_url = get_item_url(item, posts, post_titles)
|
||||
if state.debug:
|
||||
print("Selected URL: ", post_url)
|
||||
magnet_uri = get_magnet_link(post_url)
|
||||
start_client()
|
||||
add_magnet(magnet_uri)
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
def split_data(data):
|
||||
json = json.loads(data)
|
||||
import json
|
||||
|
||||
count = json["count"]
|
||||
posts = json["data"]
|
||||
query = json["query"]
|
||||
success = json["success"]
|
||||
def split_data(data):
|
||||
p_json = json.loads(data)
|
||||
|
||||
count = p_json["count"]
|
||||
posts = p_json["data"]
|
||||
query = p_json["query"]
|
||||
success = p_json["success"]
|
||||
|
||||
return count, posts, query, success
|
||||
|
||||
def format_data(data):
|
||||
post_titles = [post["title"] for post in data]
|
||||
post_links = [post["url"] for post in data]
|
||||
|
||||
return post_titles, post_links
|
||||
|
||||
|
||||
Reference in New Issue
Block a user