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
/
opensubtitles_v2.py
115 lines (108 loc) · 4.77 KB
/
opensubtitles_v2.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
113
114
115
import os
import requests
import json
from urllib.parse import urlencode
class OpenSubtitlesV2:
def __init__(self):
self.login_token = None
self.user_downloads_remaining = None
def login(self, user, password, apikey):
self.user = user
self.password = password
self.apikey = apikey
login_url = "https://api.opensubtitles.com/api/v1/login"
login_headers = {
"api-key": self.apikey,
"content-type": "application/json",
"user-agent": "Mediagram v1",
}
login_body = {"username": self.user, "password": self.password}
try:
login_response = requests.post(
login_url, data=json.dumps(login_body), headers=login_headers
)
login_response.raise_for_status()
login_json_response = login_response.json()
self.login_token = login_json_response["token"]
except requests.exceptions.HTTPError as httperr:
raise Exception(httperr)
except requests.exceptions.RequestException as reqerr:
raise Exception("Failed to login: " + reqerr)
except ValueError as e:
raise Exception("Failed to parse login JSON response: " + e)
user_url = "https://api.opensubtitles.com/api/v1/infos/user"
user_headers = {
"api-key": self.apikey,
"authorization": self.login_token,
"user-agent": "Mediagram v1",
}
try:
user_response = requests.get(user_url, headers=user_headers)
user_response.raise_for_status()
user_json_response = user_response.json()
self.user_downloads_remaining = user_json_response["data"][
"remaining_downloads"
]
except requests.exceptions.HTTPError as httperr:
raise Exception(httperr)
except requests.exceptions.RequestException as reqerr:
raise Exception("Failed to login: " + reqerr)
except ValueError as e:
raise Exception("Failed to parse user JSON response: " + e)
def search_subtitles(self, filename, sublanguage):
try:
query_params = {
"foreign_parts_only": "exclude",
"languages": sublanguage.lower(),
"order_by": "download_count",
"order_direction": "desc",
"query": filename.lower(),
}
query_params = urlencode(query_params)
query_url = "https://api.opensubtitles.com/api/v1/subtitles"
query_headers = {"api-key": self.apikey, "user-agent": "Mediagram v1"}
query_response = requests.get(
query_url, params=query_params, headers=query_headers
)
query_response.raise_for_status()
query_json_response = query_response.json()
if "data" in query_json_response:
return query_json_response["data"]
except requests.exceptions.HTTPError as httperr:
raise Exception(httperr)
except requests.exceptions.RequestException as reqerr:
raise Exception("Failed to login: " + reqerr)
except ValueError as e:
raise Exception("Failed to parse search_subtitle JSON response: " + e)
def download_subtitle(self, id, name, path):
download_url = "https://api.opensubtitles.com/api/v1/download"
download_headers = {
"api-key": self.apikey,
"authorization": self.login_token,
"content-type": "application/json",
"user-agent": "Mediagram v1",
}
download_body = {"file_id": id}
if self.user_downloads_remaining > 0:
try:
download_response = requests.post(
download_url,
data=json.dumps(download_body),
headers=download_headers,
)
download_json_response = download_response.json()
self.user_downloads_remaining = download_json_response["remaining"]
download_link = download_json_response["link"]
download_remote_file = requests.get(download_link)
file = os.path.join(path, name)
with open(file, "wb") as f:
f.write(download_remote_file.content)
return {id: file}
except requests.exceptions.HTTPError as httperr:
raise Exception(httperr)
except requests.exceptions.RequestException as reqerr:
raise Exception("Failed to login: " + reqerr)
except ValueError as e:
raise Exception("Failed to parse search_subtitle JSON response: " + e)
else:
print("Download limit reached. Wait for your quota to reset (~24hrs)")