-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_client.py
More file actions
157 lines (122 loc) · 5.25 KB
/
Copy pathapi_client.py
File metadata and controls
157 lines (122 loc) · 5.25 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import cloudscraper, requests, hashlib, os
api_url = 'https://api.iwara.tv'
file_url = 'https://files.iwara.tv'
class BearerAuth(requests.auth.AuthBase):
"""Bearer Authentication"""
def __init__(self, token):
self.token = token
def __call__(self, r):
r.headers['Authorization'] = 'Bearer ' + self.token
return r
class ApiClient:
def __init__(self, email, password):
self.scraper = cloudscraper.create_scraper()
self.email = email
self.password = password
self.api_url = api_url
self.file_url = file_url
self.token = None
def login(self) -> requests.Response:
url = self.api_url + '/user/login'
json = {'email': self.email, 'password': self.password}
r = self.scraper.post(
url,
json=json,
)
#Debug
print("[DEBUG] login response:", r)
try:
self.token = r.json().get('token')
print('API Login success')
except:
print('API Login failed')
return r
# limit query is not working
def get_videos(self, sort = 'date', rating = 'all', page = 0, limit = 32, subscribed = False) -> requests.Response:
"""# Get new videos from iwara.tv
- sort: date, trending, popularity, views, likes
- rating: all, general, ecchi
"""
url = self.api_url + '/videos'
params = {'sort': sort,
'rating': rating,
'page': page,
'limit': limit,
'subscribed': 'true' if subscribed else 'false',
}
if self.token is None:
r = self.scraper.get(url, params=params)
else:
r = self.scraper.get(url, params=params, auth=BearerAuth(self.token))
#Debug
print("[DEBUG] get_videos response:", r)
return r
def get_video(self, video_id) -> requests.Response:
"""# Get video info from iwara.tv
"""
url = self.api_url + '/video/' + video_id
if self.token is None:
r = self.scraper.get(url)
else:
r = self.scraper.get(url, auth=BearerAuth(self.token))
#Debug
print("[DEBUG] get_video response:", r)
return r
def download_video_thumbnail(self, video_id) -> str:
"""# Download video thumbnail from iwara.tv
"""
video = self.get_video(video_id).json()
file_id = video['file']['id']
thumbnail_id = video['thumbnail']
url = self.file_url + '/image/original/' + file_id + '/thumbnail-{:02d}.jpg'.format(thumbnail_id)
thumbnail_file_name = video_id + '.jpg'
if (os.path.exists(thumbnail_file_name)):
print(f"Video ID {video_id} thumbnail already downloaded, skipped downloading. ")
return thumbnail_file_name
print(f"Downloading thumbnail for video ID: {video_id} ...")
with open(thumbnail_file_name, "wb") as f:
for chunk in self.scraper.get(url).iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
return thumbnail_file_name
def download_video(self, video_id) -> str:
"""# Download video from iwara.tv
"""
try:
video = self.get_video(video_id).json()
except Exception as e:
raise Exception(f"Failed to get video info for video ID: {video_id}, error: {e}")
url = video['fileUrl']
file_id = video['file']['id']
expires = url.split('/')[4].split('?')[1].split('&')[0].split('=')[1]
# IMPORTANT: This might change in the future.
SHA_postfix = "_5nFp9kmbNnHdAFhaqMvt"
SHA_key = file_id + "_" + expires + SHA_postfix
hash = hashlib.sha1(SHA_key.encode('utf-8')).hexdigest()
headers = {"X-Version": hash}
resources = self.scraper.get(url, headers=headers, auth=BearerAuth(self.token)).json()
resources_by_quality = [None for i in range(10)]
for resource in resources:
if resource['name'] == 'Source':
resources_by_quality[0] = resource
for resource in resources_by_quality:
if resource is not None:
download_link = "https:" + resource['src']['download']
file_type = resource['type'].split('/')[1]
video_file_name = video_id + '.' + file_type
if (os.path.exists(video_file_name)):
print(f"Video ID {video_id} Already downloaded, skipped downloading. ")
return video_file_name
print(f"Downloading video ID: {video_id} ...")
try:
with open(video_file_name, "wb") as f:
for chunk in self.scraper.get(download_link).iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
return video_file_name
except Exception as e:
os.remove(video_file_name)
raise Exception(f"Failed to download video ID: {video_id}, error: {e}")
raise Exception("No video with Source quality found")