Last active 1749870372

Scripts to manage pack torrents

libdl.py Raw
1#!/usr/bin/env -S python3 -i
2from requests import post
3from tabulate import tabulate
4from sys import argv
5lib = argv[1]
6ROOT = "/mnt/storage/_live/{}library.db"
7QBT = "http://localhost:8080"
8
9import sqlite3
10con = sqlite3.connect(ROOT.format(lib))
11cur = con.cursor()
12
13class SORT:
14 filename=0
15 size=1
16 pack=2
17
18def add(hash,_id):
19 post(f"{QBT}/api/v2/torrents/filePrio", data={"hash": hash, "id": _id, "priority": 1})
20 post(f"{QBT}/api/v2/torrents/addTags", data={"hashes": hash, "tags": "attn"})
21 post(f"{QBT}/api/v2/torrents/start", data={"hashes": hash})
22 print(f"added {hash}:{_id}")
23
24def search(query,*n,sort=SORT.pack):
25 rows = []
26 data = []
27 for a,b,c,d in cur.execute("select hash,`index`,name,size from file where name like ? order by name", (f"%{query}%",)):
28 _ = c.split("/")
29 rows.append([_[-1],f"{d/1024/1024:.2f}M",_[0],a,b])
30 outp = [[n, *row] for n,row in enumerate(sorted(rows,key=lambda row: row[sort]))]
31 for pick in n:
32 print(f"adding {outp[pick][1]}...")
33 add(*outp[pick][-2:])
34# if x in n or "*" in n:
35# add(a,b)
36 if not n: print(tabulate([row[:-2] for row in outp], headers=["n",f"filename{' ^' if sort==SORT.filename else ''}",f"size{' ^' if sort==SORT.size else ''}",f"pack{' ^' if sort==SORT.pack else ''}"]))
libupdate.py Raw
1#!/usr/bin/env python3
2import sqlite3
3from sys import argv
4from requests import get,post
5lib = argv[1]
6ROOT = "/mnt/storage/_live/{}library.db"
7QBT = "http://localhost:8080"
8
9con = sqlite3.connect(ROOT.format(lib))
10cur = con.cursor()
11cur.execute("CREATE TABLE IF NOT EXISTS file(hash,'index',size,availability,priority,progress,name)")
12
13j = get(f"{QBT}/api/v2/sync/maindata?rid=0").json()
14torrents = {t:v for t,v in j["torrents"].items() if v["category"] == f"{lib}library" and "attn" in v["tags"]}
15
16for n,hash in enumerate(torrents):
17 if "files" not in torrents[hash]:
18 torrents[hash]["files"] = get(f"{QBT}/api/v2/torrents/files",params={"hash": hash}).json()
19 print(n,len(torrents[hash]["files"]))
20
21inp = []
22for hash in torrents:
23 for file in torrents[hash]["files"]:
24 inp.append((hash,file['index'],file['size'],file['availability'],file['priority'],file['progress'],file['name']))
25
26cur.executemany("INSERT INTO file VALUES(?,?,?,?,?,?,?)",inp)
27con.commit()
28con.close()