forked from stabbedbybrick/freevine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroku.py
504 lines (396 loc) · 13.9 KB
/
roku.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
"""
Rokuchannel downloader
Version: 0.0.1 - 18/08/23
Author: stabbedbybrick
Info:
This program will grab higher 1080p bitrate and Dolby Digital audio (if available)
Default settings are set to local CDM and best available quality with all subtitles
Place blob and key file in pywidevine/L3/cdm/devices/android_generic to use local CDM
Requirements:
N_m3u8DL-RE
ffmpeg OR mkvmerge (default: mkvmerge)
mp4decrypt OR shaka-packager (default: mp4decrypt)
Necessary libraries:
pip install -r requirements.txt
Usage:
python roku.py --help
"""
import base64
import datetime
import re
import subprocess
import urllib
import json
import asyncio
import shutil
from urllib.parse import urlparse
from pathlib import Path
from abc import ABC
from collections import Counter
import click
import httpx
from bs4 import BeautifulSoup
from sortedcontainers import SortedKeyList
from rich.console import Console
from pywidevine.L3.decrypt.wvdecryptcustom import WvDecrypt
from pywidevine.L3.cdm import deviceconfig
CONTENT = (
f"https://therokuchannel.roku.com/api/v2/homescreen/content/"
"https%3A%2F%2Fcontent.sr.roku.com%2Fcontent%2Fv1%2Froku-trc%2F"
)
class Episode:
def __init__(
self,
service: str,
title: str,
season: str,
number: str,
name: str,
year: str,
data: str,
) -> None:
title = title.strip()
if name is not None:
name = name.strip()
self.service = service
self.title = title
self.season = season
self.number = number
self.name = name
self.year = year
self.data = data
def __str__(self) -> str:
return "{title} S{season:02}E{number:02} {name}".format(
title=self.title,
season=self.season,
number=self.number,
name=self.name or "",
).strip()
def get_filename(self) -> str:
name = "{title} S{season:02}E{number:02} {name}".format(
title=self.title.replace("$", "S"),
season=self.season,
number=self.number,
name=self.name or "",
).strip()
return string_cleaning(name)
class Series(SortedKeyList, ABC):
def __init__(self, iterable=None):
super().__init__(iterable, key=lambda x: (x.season, x.number, x.year or 0))
def __str__(self) -> str:
if not self:
return super().__str__()
return self[0].title + (f" ({self[0].year})" if self[0].year else "")
class Movie:
def __init__(
self,
service: str,
title: str,
year: int,
name: str,
data: str,
) -> None:
name = name.strip()
self.service = service
self.title = title
self.year = year
self.name = name
self.data = data
def __str__(self) -> str:
if self.year:
return f"{self.name} ({self.year})"
return self.name
def get_filename(self) -> str:
name = str(self).replace("$", "S")
return string_cleaning(name)
class Movies(SortedKeyList, ABC):
def __init__(self, iterable=None):
super().__init__(iterable, key=lambda x: x.year or 0)
def __str__(self) -> str:
if not self:
return super().__str__()
return self[0].title + (f" ({self[0].year})" if self[0].year else "")
def stamp(text: str) -> str:
time = datetime.datetime.now().strftime("%H:%M:%S.%f")[:-3]
stamp = click.style(f"{time}")
info = click.style(f"INFO", fg="green", underline=True)
message = click.style(f" : {text}")
return f"{stamp} {info}{message}"
def local_cdm(pssh: str, lic_url: str, cert_b64=None) -> str:
wvdecrypt = WvDecrypt(
init_data_b64=pssh,
cert_data_b64=cert_b64,
device=deviceconfig.device_android_generic,
)
response = client.post(url=lic_url, data=wvdecrypt.get_challenge())
license_b64 = base64.b64encode(response.content)
wvdecrypt.update_license(license_b64)
status, content = wvdecrypt.start_process()
if status:
return content[0]
else:
raise ValueError("Unable to fetch decryption keys")
def remote_cdm(pssh: str, lic_url: str) -> str:
headers = {
"accept": "application/json, text/plain, */*",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
}
payload = {
"password": "password",
"license": lic_url,
"headers": "connection: keep-alive",
"pssh": pssh,
"buildInfo": "",
"proxy": "",
"cache": False,
}
response = client.post("https://wvclone.fly.dev/wv", headers=headers, json=payload)
soup = BeautifulSoup(response.text, "html.parser")
return soup.select_one("ol li").text
def get_data(url: str) -> json:
video_id = urlparse(url).path.split("/")[2]
try:
return client.get(f"{CONTENT}{video_id}").json()
except:
raise KeyError(
"Request failed. IP-address is either blocked or content is premium"
)
async def fetch_titles(async_client: httpx.AsyncClient, id: str) -> json:
response = await async_client.get(f"{CONTENT}{id}")
return response.json()
async def get_titles(data: dict) -> list:
async with httpx.AsyncClient() as async_client:
tasks = [fetch_titles(async_client, x["meta"]["id"]) for x in data["episodes"]]
return await asyncio.gather(*tasks)
def get_episodes(url: str) -> Series:
data = get_data(url)
episodes = asyncio.run(get_titles(data))
return Series(
[
Episode(
service="ROKU",
title=data["title"],
season=int(episode["seasonNumber"]),
number=int(episode["episodeNumber"]),
name=episode["title"],
year=data["releaseYear"],
data=episode["meta"]["id"],
)
for episode in episodes
]
)
def get_movies(url: str) -> Movies:
data = get_data(url)
return Movies(
[
Movie(
service="ROKU",
title=data["title"],
year=data["releaseYear"],
name=data["title"],
data=data["meta"]["id"],
)
]
)
def get_playlist(id: str) -> tuple:
response = client.get("https://therokuchannel.roku.com/api/v1/csrf").json()
token = response["csrf"]
headers = {
"csrf-token": token,
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
}
payload = {
"rokuId": id,
"mediaFormat": "mpeg-dash",
"drmType": "widevine",
"quality": "fhd",
"providerId": "rokuavod",
}
url = f"https://therokuchannel.roku.com/api/v3/playback"
response = client.post(
url, headers=headers, cookies=client.cookies, json=payload
).json()
try:
videos = response["playbackMedia"]["videos"]
except:
raise KeyError(
"Request failed. IP-address is either blocked or content is premium"
)
lic_url = [
x["drmParams"]["licenseServerURL"]
for x in videos
if x["drmParams"]["keySystem"] == "Widevine"
][0]
mpd = [x["url"] for x in videos if x["streamFormat"] == "dash"][0]
manifest = urllib.parse.unquote(mpd).split("=")[1].split("?")[0]
return lic_url, manifest
def get_mediainfo(manifest: str, quality: str) -> str:
soup = BeautifulSoup(client.get(manifest), "xml")
elements = soup.find_all("Representation")
codecs = [x.attrs["codecs"] for x in elements if x.attrs.get("codecs")]
heights = sorted(
[int(x.attrs["height"]) for x in elements if x.attrs.get("height")],
reverse=True,
)
audio = "DD5.1" if "ac-3" in codecs else "AAC2.0"
if quality is not None:
if quality in heights:
return quality, audio
else:
closest_match = min(heights, key=lambda x: abs(int(x) - int(quality)))
click.echo(stamp(f"Resolution not available. Getting closest match:"))
return closest_match, audio
return heights[0], audio
def string_cleaning(filename: str) -> str:
filename = re.sub(r"[:; ]", ".", filename)
filename = re.sub(r"[\\*!?¿,'\"()<>|$#`]", "", filename)
filename = re.sub(rf"[{'.'}]{{2,}}", ".", filename)
return filename
def list_titles(url: str) -> None:
with console.status("Fetching titles..."):
series = get_episodes(url)
seasons = Counter(x.season for x in series)
num_seasons = len(seasons)
num_episodes = sum(seasons.values())
click.echo(
stamp((f"{str(series)}: {num_seasons} Season(s), {num_episodes} Episode(s)\n"))
)
for episode in series:
print(episode.get_filename())
def download_episode(quality: str, url: str, remote: bool, requested: str) -> None:
with console.status("Fetching titles..."):
series = get_episodes(url)
seasons = Counter(x.season for x in series)
num_seasons = len(seasons)
num_episodes = sum(seasons.values())
click.echo(
stamp((f"{str(series)}: {num_seasons} Season(s), {num_episodes} Episode(s)\n"))
)
if "-" in requested:
download_range(series, requested, quality, remote)
for episode in series:
episode.name = episode.get_filename()
if requested in episode.name:
download_stream(episode, quality, remote, str(series))
def download_range(series: object, episode: str, quality: str, remote: str) -> None:
start, end = episode.split("-")
start_season, start_episode = start.split("E")
end_season, end_episode = end.split("E")
start_season = int(start_season[1:])
start_episode = int(start_episode)
end_season = int(end_season[1:])
end_episode = int(end_episode)
episode_range = [
f"S{season:02d}E{episode:02d}"
for season in range(start_season, end_season + 1)
for episode in range(start_episode, end_episode + 1)
]
for episode in series:
episode.name = episode.get_filename()
if any(i in episode.name for i in episode_range):
download_stream(episode, quality, remote, str(series))
def download_season(quality: str, url: str, remote: bool, requested: str) -> None:
with console.status("Fetching titles..."):
series = get_episodes(url)
seasons = Counter(x.season for x in series)
num_seasons = len(seasons)
num_episodes = sum(seasons.values())
click.echo(
stamp((f"{str(series)}: {num_seasons} Season(s), {num_episodes} Episode(s)\n"))
)
for episode in series:
episode.name = episode.get_filename()
if requested in episode.name:
download_stream(episode, quality, remote, str(series))
def download_movie(quality: str, url: str, remote: bool) -> None:
with console.status("Fetching titles..."):
movies = get_movies(url)
click.echo(stamp(f"{str(movies)}\n"))
for movie in movies:
movie.name = movie.get_filename()
download_stream(movie, quality, remote, str(movies))
def download_stream(stream: object, quality: str, remote: bool, title: str) -> None:
pssh = "AAAAKXBzc2gAAAAA7e+LqXnWSs6jyCfc1R0h7QAAAAkiASpI49yVmwY="
title = string_cleaning(title)
lic_url, manifest = get_playlist(stream.data)
resolution, audio = get_mediainfo(manifest, quality)
downloads = Path("downloads")
save_path = downloads.joinpath(title)
save_path.mkdir(parents=True, exist_ok=True)
filename = f"{stream.name}.{resolution}p.{stream.service}.WEB-DL.{audio}.H.264"
with console.status("Getting decryption keys..."):
key = remote_cdm(pssh, lic_url) if remote else local_cdm(pssh, lic_url)
click.echo(stamp(f"{filename}"))
click.echo(stamp(f"{key}"))
click.echo("")
m3u8dl = shutil.which("N_m3u8DL-RE") or shutil.which("n-m3u8dl-re")
args = [
m3u8dl,
"--key",
key,
manifest,
"-sv",
f"res='{resolution}'",
"-sa",
"for=best",
"-ss",
"all",
"-mt",
"-M",
"format=mkv:muxer=mkvmerge",
"--thread-count",
"6",
"--save-name",
f"{filename}",
"--tmp-dir",
"tmp",
"--save-dir",
f"{save_path}",
"--no-log",
# "--log-level",
# "OFF",
]
try:
subprocess.run(args, check=True)
except:
raise ValueError(
"Download failed. Install necessary binaries before downloading"
)
@click.command()
@click.option("-q", "--quality", type=str, help="Specify resolution")
@click.option("-e", "--episode", type=str, help="Download episode(s)")
@click.option("-s", "--season", type=str, help="Download season")
@click.option("-m", "--movie", is_flag=True, help="Download a movie")
@click.option("-t", "--titles", is_flag=True, default=False, help="List all titles")
@click.option("-r", "--remote", is_flag=True, default=False, help="Use remote CDM")
@click.argument("url", type=str, required=True)
def main(
quality: str,
episode: str,
season: str,
movie: bool,
titles: bool,
url: str,
remote: bool,
) -> None:
"""
Examples:\n
*Use S01E01-S01E10 to download a range of episodes (within the same season)
\b
python roku.py --episode S01E01 URL
python roku.py --episode S01E01-S01E10 URL
python roku.py --remote --episode S01E01 URL
python roku.py --quality 720 --season S01 URL
python roku.py --movie URL
python roku.py --titles URL
"""
list_titles(url) if titles else None
download_episode(quality, url, remote, episode.upper()) if episode else None
download_season(quality, url, remote, season.upper()) if season else None
download_movie(quality, url, remote) if movie else None
if __name__ == "__main__":
console = Console()
client = httpx.Client(headers={"user-agent": "Chrome/113.0.0.0 Safari/537.36"})
main()