mirror of
https://github.com/KeksPirates/SoftwareManager.git
synced 2026-08-04 09:59:41 +02:00
refactor: streamline scraper functions to utilize centralized state management
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import requests
|
||||
from ..utils.state import state
|
||||
|
||||
|
||||
def scrape_rutracker(search_text, debug, server):
|
||||
search = requests.get(f"{server}/search/{search_text}")
|
||||
def scrape_rutracker(search_text):
|
||||
search = requests.get(f"{state.api_url}/search/{search_text}")
|
||||
if search:
|
||||
try:
|
||||
data = search.json()
|
||||
@@ -10,7 +11,7 @@ def scrape_rutracker(search_text, debug, server):
|
||||
resultlinks = data["links"]
|
||||
return resulttitles, resultlinks
|
||||
except Exception:
|
||||
if debug:
|
||||
if state.debug:
|
||||
print("No results found / No response from server")
|
||||
return None
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ import requests
|
||||
import asyncio
|
||||
import aiohttp
|
||||
from bs4 import BeautifulSoup
|
||||
from ..utils.state import state
|
||||
|
||||
|
||||
global url_uztracker
|
||||
url_uztracker = "https://uztracker.net/tracker.php?nm="
|
||||
@@ -24,7 +26,7 @@ async def init_uztracker():
|
||||
|
||||
asyncio.run(init_uztracker())
|
||||
|
||||
async def scrape_uztracker(search, debug, max_results=450):
|
||||
async def scrape_uztracker(search, max_results=450):
|
||||
if up:
|
||||
result = False
|
||||
global results
|
||||
@@ -36,7 +38,7 @@ async def scrape_uztracker(search, debug, max_results=450):
|
||||
|
||||
for start in range(0, max_results, per_page):
|
||||
search_url = url_uztracker + search + f"&start={start}"
|
||||
if debug:
|
||||
if state.debug:
|
||||
print(search_url)
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
@@ -72,7 +74,7 @@ async def scrape_uztracker(search, debug, max_results=450):
|
||||
|
||||
return resulttitles, results
|
||||
|
||||
def get_post_title(post_url, debug):
|
||||
def get_post_title(post_url):
|
||||
if up:
|
||||
response = requests.get(post_url)
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
@@ -80,7 +82,7 @@ def get_post_title(post_url, debug):
|
||||
if maintitle:
|
||||
return maintitle.text
|
||||
else:
|
||||
if debug:
|
||||
if state.debug:
|
||||
print("Program not found")
|
||||
else:
|
||||
print("Error: Uztracker down")
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppState:
|
||||
debug: bool = False
|
||||
tracker: str = "rutracker"
|
||||
api_url: str = "https://api.michijackson.xyz"
|
||||
|
||||
|
||||
state = AppState()
|
||||
Reference in New Issue
Block a user