Last active 1749870372

Scripts to manage pack torrents

aly's Avatar aly revised this gist 1749870372. Go to revision

2 files changed, 64 insertions

libdl.py(file created)

@@ -0,0 +1,36 @@
1 + #!/usr/bin/env -S python3 -i
2 + from requests import post
3 + from tabulate import tabulate
4 + from sys import argv
5 + lib = argv[1]
6 + ROOT = "/mnt/storage/_live/{}library.db"
7 + QBT = "http://localhost:8080"
8 +
9 + import sqlite3
10 + con = sqlite3.connect(ROOT.format(lib))
11 + cur = con.cursor()
12 +
13 + class SORT:
14 + filename=0
15 + size=1
16 + pack=2
17 +
18 + def 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 +
24 + def 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(file created)

@@ -0,0 +1,28 @@
1 + #!/usr/bin/env python3
2 + import sqlite3
3 + from sys import argv
4 + from requests import get,post
5 + lib = argv[1]
6 + ROOT = "/mnt/storage/_live/{}library.db"
7 + QBT = "http://localhost:8080"
8 +
9 + con = sqlite3.connect(ROOT.format(lib))
10 + cur = con.cursor()
11 + cur.execute("CREATE TABLE IF NOT EXISTS file(hash,'index',size,availability,priority,progress,name)")
12 +
13 + j = get(f"{QBT}/api/v2/sync/maindata?rid=0").json()
14 + torrents = {t:v for t,v in j["torrents"].items() if v["category"] == f"{lib}library" and "attn" in v["tags"]}
15 +
16 + for 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 +
21 + inp = []
22 + for 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 +
26 + cur.executemany("INSERT INTO file VALUES(?,?,?,?,?,?,?)",inp)
27 + con.commit()
28 + con.close()
Newer Older