Ensimmäinen commit
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import requests
|
||||
import json
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
SKRIPTIN_KANSIO = os.path.dirname(os.path.abspath(__file__))
|
||||
LOKI_TIEDOSTO = os.path.join(SKRIPTIN_KANSIO, "ladatut_torrentit.txt")
|
||||
JSON_TIEDOSTO = os.path.join(SKRIPTIN_KANSIO, "hakulista.json")
|
||||
|
||||
def lataa_kaikki_data():
|
||||
oletus_data = {
|
||||
"asetukset": {
|
||||
"jackett_url": "http://192.168.1.40:9117",
|
||||
"jackett_api_key": "",
|
||||
"qbit_url": "http://172.16.0.1:8080",
|
||||
"qbit_user": "admin",
|
||||
"qbit_pass": ""
|
||||
},
|
||||
"indeksorit": ["knaben", "torrentgalaxyclone", "therarbg", "uindex"],
|
||||
"hakukohteet": []
|
||||
}
|
||||
|
||||
if os.path.exists(JSON_TIEDOSTO):
|
||||
try:
|
||||
with open(JSON_TIEDOSTO, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
if "asetukset" not in data:
|
||||
data["asetukset"] = oletus_data["asetukset"]
|
||||
return data
|
||||
except Exception as e:
|
||||
print(f"Virhe JSON-luvussa: {e}")
|
||||
|
||||
tallenna_kaikki_data(oletus_data)
|
||||
return oletus_data
|
||||
|
||||
def tallenna_kaikki_data(data):
|
||||
with open(JSON_TIEDOSTO, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=4, ensure_ascii=False)
|
||||
|
||||
def lataa_jo_ladatut():
|
||||
if os.path.exists(LOKI_TIEDOSTO):
|
||||
with open(LOKI_TIEDOSTO, "r", encoding="utf-8") as f:
|
||||
return set(line.strip() for line in f if line.strip())
|
||||
return set()
|
||||
|
||||
def tallenna_lokiin(linkki):
|
||||
with open(LOKI_TIEDOSTO, "a", encoding="utf-8") as f:
|
||||
f.write(linkki + "\n")
|
||||
|
||||
def hae_jackettista(jackett_url, api_key, indeksori, hakusana):
|
||||
url = f"{jackett_url}/api/v2.0/indexers/{indeksori}/results/torznab/api"
|
||||
params = {"apikey": api_key, "t": "search", "q": hakusana}
|
||||
try:
|
||||
res = requests.get(url, params=params, timeout=15)
|
||||
res.raise_for_status()
|
||||
soup = BeautifulSoup(res.text, 'xml')
|
||||
|
||||
loydetyt = []
|
||||
for item in soup.find_all('item'):
|
||||
title = item.find('title')
|
||||
title_text = title.text if title else ""
|
||||
|
||||
enclosure = item.find('enclosure', url=True)
|
||||
linkki = enclosure['url'] if enclosure and enclosure['url'].startswith('magnet:') else None
|
||||
if not linkki:
|
||||
link_tag = item.find('link')
|
||||
if link_tag and link_tag.text.startswith('magnet:'):
|
||||
linkki = link_tag.text
|
||||
|
||||
if linkki and title_text:
|
||||
loydetyt.append((title_text, linkki))
|
||||
return loydetyt
|
||||
except Exception as e:
|
||||
print(f" [Jackett Virhe sivustolla {indeksori}] {e}")
|
||||
return []
|
||||
|
||||
def qbit_lisaa_torrent_api_keylla(qbit_url, api_key, magnet_linkki, kategoria):
|
||||
pohja_url = qbit_url.rstrip('/')
|
||||
add_url = f"{pohja_url}/api/v2/torrents/add"
|
||||
|
||||
headers = {
|
||||
"X-API-Key": api_key,
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Referer": f"{pohja_url}/"
|
||||
}
|
||||
|
||||
data = {
|
||||
"urls": magnet_linkki,
|
||||
"category": kategoria
|
||||
}
|
||||
|
||||
try:
|
||||
res = requests.post(add_url, data=data, headers=headers, timeout=10)
|
||||
return res.status_code
|
||||
except Exception as e:
|
||||
print(f" [qBittorrent] Yhteysvirhe: {e}")
|
||||
return 500
|
||||
|
||||
def tarkista_ja_lataa():
|
||||
print(f"--- ALOITETAAN ERITELTY JACKETT & QBITTORRENT API-HAKU ({time.strftime('%Y-%m-%d %H:%M:%S')}) ---")
|
||||
data = lataa_kaikki_data()
|
||||
ladatut = lataa_jo_ladatut()
|
||||
|
||||
jackett_url = data["asetukset"].get("jackett_url", "")
|
||||
api_key = data["asetukset"].get("jackett_api_key", "")
|
||||
qbit_url = data["asetukset"].get("qbit_url", "")
|
||||
qbit_api_key = data["asetukset"].get("qbit_pass", "")
|
||||
|
||||
indeksori_lista = data["indeksorit"]
|
||||
hakulista = data["hakukohteet"]
|
||||
|
||||
if not api_key or not qbit_url or not qbit_api_key:
|
||||
print(" [Virhe] Asetuksia puuttuu!")
|
||||
return
|
||||
|
||||
for kohde in hakulista:
|
||||
haku = kohde["jackett_haku"]
|
||||
hakusanat = haku.lower().split()
|
||||
kategoria = kohde["kohdekansio"].strip()
|
||||
|
||||
kaikki_tulokset = []
|
||||
for idx in indeksori_lista:
|
||||
tulokset = hae_jackettista(jackett_url, api_key, idx, haku)
|
||||
kaikki_tulokset.extend(tulokset)
|
||||
|
||||
uudet_kohteet = []
|
||||
for nimi, linkki in list(set(kaikki_tulokset)):
|
||||
if linkki in ladatut: continue
|
||||
|
||||
nimi_lower = nimi.lower()
|
||||
viimeisin_indeksi = -1
|
||||
loytyy_kaikki = True
|
||||
|
||||
for sana in hakusanat:
|
||||
indeksi = nimi_lower.find(sana, viimeisin_indeksi + 1)
|
||||
if indeksi == -1:
|
||||
loytyy_kaikki = False
|
||||
break
|
||||
viimeisin_indeksi = indeksi
|
||||
|
||||
if loytyy_kaikki:
|
||||
uudet_kohteet.append((nimi, linkki))
|
||||
|
||||
for nimi, linkki in uudet_kohteet:
|
||||
status = qbit_lisaa_torrent_api_keylla(qbit_url, qbit_api_key, linkki, kategoria)
|
||||
if status == 200 or status == 409:
|
||||
tallenna_lokiin(linkki)
|
||||
print(f" -> KÄSITELTY: {nimi}")
|
||||
|
||||
print("--- HAKUKIERROS VALMIS ---\n")
|
||||
|
||||
if __name__ == "__main__":
|
||||
tarkista_ja_lataa()
|
||||
Reference in New Issue
Block a user