This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugins.py
112 lines (95 loc) · 3.78 KB
/
plugins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from pythonopensubtitles.opensubtitles import OpenSubtitles
from opensubtitles_v2 import OpenSubtitlesV2
from rarbgapi import RarbgAPI
import subprocess
import json
class SubtitlesSearch:
def __init__(self, user, password):
self.api = OpenSubtitles()
self.api.login(user, password)
def query(self, query, lang="fre", max_results=5):
subtitles = self.api.search_subtitles([{"query": query, "sublanguageid": lang}])
if subtitles:
remap = lambda sub: dict(
id=sub["IDSubtitleFile"],
name=sub["MovieReleaseName"],
nb_downloads=int(sub["SubDownloadsCnt"]),
lang=sub["SubLanguageID"][:2],
ext=sub["SubFormat"],
)
remapped = list(map(remap, subtitles))
return sorted(remapped, key=lambda sub: sub["nb_downloads"], reverse=True)[
:max_results
]
def download(self, sub, name, path):
id, name = [sub["id"]], {sub["id"]: f"{name}.{sub['ext']}"}
return self.api.download_subtitles(id, name, path, extension=sub["ext"])
class SubtitlesSearchV2:
def __init__(self, user, password, apikey):
self.api = OpenSubtitlesV2()
self.api.login(user, password, apikey)
def query(self, query, lang="fre", max_results=5):
lang = lang[:2]
subtitles = self.api.search_subtitles(query, lang)
if subtitles:
remap = lambda sub: dict(
id=sub["attributes"]["files"][0]["file_id"],
name=sub["attributes"]["files"][0]["file_name"]
if "file_name" in sub["attributes"]["files"][0]
else sub["attributes"]["release"],
nb_downloads=sub["attributes"]["download_count"],
lang=sub["attributes"]["language"],
ext="srt",
)
return list(map(remap, subtitles[:max_results]))
def download(self, sub, name, path):
id, name = sub["id"], f"{name}.{sub['ext']}"
return self.api.download_subtitle(id, name, path)
class TorrentSearch:
CATEGORIES = [14, 48, 17, 44, 45, 50, 51, 52, 54, 42, 46, 18, 41, 49]
def __init__(self):
self.api = RarbgAPI()
def query(self, query, min_seeders=5, max_results=5):
torrents = self.api.search(
search_string=query,
extended_response=True,
sort="seeders",
categories=self.CATEGORIES,
limit=10,
)
filt = lambda t: t.seeders > min_seeders
remap = lambda t: dict(
name=t.filename,
seeders=t.seeders,
leechers=t.leechers,
size=round(t.size / 2**30, 2),
category=t.category,
date=t.pubdate[:-6],
magnet=t.download,
)
filtered = filter(filt, torrents)
remapped = list(map(remap, filtered))
return remapped[:max_results]
def get_public_ip():
try:
ipv4 = json.loads(
subprocess.run(
["curl", "ipinfo.io"], capture_output=True, text=True, check=True
).stdout.strip()
).get("ip")
ipv6 = subprocess.run(
["curl", "ifconfig.me"], capture_output=True, text=True, check=True
).stdout.strip()
return f"IPv4: {ipv4}\nIPv6: {ipv6}"
except:
pass
if __name__ == "__main__":
from rich import print
from os import getenv
from dotenv import load_dotenv
load_dotenv()
ost_user = getenv("OST_USER")
ost_pass = getenv("OST_PASS")
ost_apikey = getenv("OST_API_KEY")
ss = SubtitlesSearchV2(ost_user, ost_pass, ost_apikey)
print(ss.query("The last of us s01e03", lang="eng", max_results=5))