forked from Santoshkurmi/hls_tts_bypass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
495 lines (370 loc) · 15.6 KB
/
main.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
import requests as req
import os
import tqdm
from Crypto.Cipher import AES #pip install pycryptodome
from Crypto.Util.Padding import unpad
import base64
import re
import time
import os
import base64
import hashlib
from base64 import b64decode
from Crypto.Cipher import AES
import m3u8
import requests
import os
import threading
import re
import json
import mmap
import sys
user_id = "74312"
authorization = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6Ijc0MzEyIiwiZW1haWwiOiJraHVtYXBva2hhcmVsMjA3OEBnbWFpbC5jb20iLCJ0aW1lc3RhbXAiOjE3MzI2NDIyNzR9.Wz2iUOpyMzmuo_bi0PV-eu7JgnVVHFXj3PS4SagtmYQ"
host = "https://harkiratapi.classx.co.in"
headers = {
"Authorization":authorization,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.86 Safari/537.36",
"Origin": "https://harkirat.classx.co.in",
"Host": "harkiratapi.classx.co.in",
"Sec-Ch-Ua-Platform": "Linux",
"Referer": "https://harkirat.classx.co.in/",
"Auth-Key": "appxapi"
}
def get_all_purchases():
res = req.get(host+f"/get/get_all_purchases?userid={user_id}&item_type=10",headers=headers).json()
res = res["data"]
return res
def get_titles(id,pid=-1):
res = req.get(host+f"/get/folder_contentsv2?course_id={id}&parent_id={pid}",headers=headers).json()
res = res["data"]
return res
def get_video_token(cid,vid):
res = req.get(host+f"/get/fetchVideoDetailsById?course_id={cid}&video_id={vid}&ytflag=0&folder_wise_course=1",headers=headers).json()
token = res["data"]["video_player_token"]
# cookie = res["data"]["cookie_value"]
return token
def get_video_enc_links(cid,vid):
res = req.get(host+f"/get/fetchVideoDetailsById?course_id={cid}&video_id={vid}&ytflag=0&folder_wise_course=1",headers=headers).json()
res = res["data"]["encrypted_links"]
# cookie = res["data"]["cookie_value"]
return res
def get_video_html(token):
res = req.get(f"https://player.akamai.net.in/secure-player?token={token}&watermark=").text
return res
def extract_key(url):
# Regular expression to match "encrypted-xxxxx"
match = re.search(r'encrypted-[^/]+', url)
if match:
return match.group(0).split("-")[1] # Return the matched string
return None
def watch_video(cid,vid):
headers2 = {
"Authorization":authorization,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.86 Safari/537.36",
"Origin": "https://harkirat.classx.co.in",
"Host": "harkiratapi.classx.co.in",
"Sec-Ch-Ua-Platform": "Linux",
"Referer": "https://harkirat.classx.co.in/",
"Auth-Key": "appxapi"
}
headers2["content-type"]="application/x-www-form-urlencoded"
data = f"user_id={user_id}&course_id={cid}&live_course_id={vid}&ytFlag=0&folder_wise_course=1"
try:
res = req.post(host+f"/post/watch_videov2",data=data,headers=headers2).json()
# print(res)
except Exception as e:
print(e)
# for downloading video in real one
def get_data_enc_key(time_val,token):
# Extract parts of the string
n = time_val[-4:] # Last 4 characters of time_val
r = int(n[0]) # First character of n as an integer
i = int(n[1:3]) # Next two characters of n as an integer
o = int(n[3]) # Last character of n as an integer
# Create the new string
a = time_val + token[r:i]
# Create SHA-256 hash
s = hashlib.sha256()
s.update(a.encode('utf-8'))
c = s.digest()
# Determine the sign based on the value of o
if o == 6:
sign = c[:16] # First 16 bytes
elif o == 7:
sign = c[:24] # First 24 bytes
else:
sign = c # Entire hash
key = base64.b64encode(sign).decode('utf-8')
# Log values for debugging (optional)
return key
def decrypt_data(data,key,ivb):
i = b64decode(key) # Key
o = b64decode(ivb) # Initialization Vector (IV)
a = b64decode(data) # Encrypted data
# Create AES Cipher object
cipher = AES.new(i, AES.MODE_CBC, o)
# Decrypt the data
l = cipher.decrypt(a)
# Remove padding (PKCS7) if necessary
# padding_length = l[-1]
# if padding_length < 16: # PKCS7 padding uses byte value equal to padding length
# l = l[:-padding_length]
# Convert decrypted data to a UTF-8 string
dec = l.decode('utf-8')
# Return the decrypted string
return dec
def decode_video_tsa(input_string):
shift_value = 0xa * 0x2 # 3 in decimal
result = ''
for char in input_string:
char_code = ord(char) # Get the Unicode code point of the character
xor_result = char_code -shift_value # Perform XOR with the constant
result += chr(xor_result) # Convert back to a character and append to the result
binary_data = base64.b64decode(result)
return binary_data
def decode_video_tsb(input_string):
xor_value = 0x3 # 42 in decimal
shift_value = 0x2a # 3 in decimal
result = ''
for char in input_string:
char_code = ord(char) # Get the Unicode code point of the character
xor_result = char_code >> xor_value # Perform XOR with the constant
shifted_result = xor_result ^ shift_value # Right shift the result by 3
result += chr(shifted_result) # Convert back to a character and append to the result
binary_data = base64.b64decode(result)
return binary_data
def decode_video_tsc(input_string):
shift_value = 0xa # 3 in decimal
result = ''
for char in input_string:
char_code = ord(char) # Get the Unicode code point of the character
xor_result = char_code - shift_value # Perform XOR with the constant
result += chr(xor_result) # Convert back to a character and append to the result
binary_data = base64.b64decode(result)
return binary_data
def decode_video_tsd(input_string):
shift_value = 0x2 # 3 in decimal
result = ''
for char in input_string:
char_code = ord(char) # Get the Unicode code point of the character
# xor_result = char_code ^ shift_value # Perform XOR with the constant
shifted_result = char_code >> shift_value # Right shift the result by 3
result += chr(shifted_result) # Convert back to a character and append to the result
binary_data = base64.b64decode(result)
return binary_data
def decode_video_tse(input_string):
xor_value = 0x3 # 42 in decimal
shift_value = 0x2a # 3 in decimal
result = ''
for char in input_string:
char_code = ord(char) # Get the Unicode code point of the character
xor_result = char_code ^ shift_value # Perform XOR with the constant
shifted_result = xor_result >> xor_value # Right shift the result by 3
result += chr(shifted_result) # Convert back to a character and append to the result
binary_data = base64.b64decode(result)
return binary_data
def get_file_extension(url):
# Regex to match a dot followed by word characters (\w+) at the end of the string ($)
match = re.search(r'\.\w+$', url)
if match:
# Remove the leading dot and return the extension
return match.group(0)[1:]
return None
total = 0
current = 0
def download_and_decrypt_segment(segment_url, key=None, iv=None, output_path=None,bit=7):
# Download the segment
global current
if os.path.exists(output_path):
current = current + 1
print(f"Downloaded and decrypted segments: {current}/{total}",end="\r")
return
attempt = 0
segment_data = None
while attempt <=5:
try:
response = requests.get(segment_url, stream=True,timeout=15)
response.raise_for_status()
segment_data = response.content
break
except requests.exceptions.Timeout:
attempt = attempt + 1
except Exception as e:
attempt = attempt + 1
if not segment_data:
return
ext = get_file_extension(segment_url)
if ext =="tsa":
segment_data = decode_video_tsa(segment_data.decode("utf-8"))
elif ext == "tsb":
segment_data = decode_video_tsb(segment_data.decode("utf-8"))
elif ext == "tsc":
segment_data = decode_video_tsc(segment_data.decode("utf-8"))
elif ext == "tsd":
segment_data = decode_video_tsd(segment_data.decode("utf-8"))
elif ext == "tse":
segment_data = decode_video_tse(segment_data.decode("utf-8"))
# Decrypt the segment if a key is provided
cipher = AES.new(key, AES.MODE_CBC, iv)
segment_data = cipher.decrypt(segment_data)
# Save the segment to a file
with open(output_path+".bak", "wb") as f:
f.write(segment_data)
os.rename(output_path+".bak",output_path)
current = current + 1
print(f"Downloaded and decrypted: {current}/{total}",end="\r")
def download_m3u8_playlist(playlist, output_file,key,directory,max_thread=1,max_segment=0):
# Load the m3u8 playlist
os.makedirs(directory, exist_ok=True)
print(f"Downloading video with max segment {max_segment} "+output_file)
# print("Downloading video "+output_file)
if not playlist.segments:
raise ValueError("No segments found in the playlist")
# Download and decrypt segments
segment_files = []
global total,current
current = 0
total = len(playlist.segments)
for i in range(0,len(playlist.segments),max_thread):
threads = []
batch = playlist.segments[i:i + max_thread]
for j, segment in enumerate(batch):
# print(i+j)
# print(max_segment)
if not max_segment == 0 and max_segment < i+j:
break
segment_url = segment.uri
segment_file = f"segment_{i+j}.ts"
segment_files.append(segment_file)
# print(segment.key.method)
# exit()
# Get the AES key and IV if encrypted
iv = None
if segment.key:
if segment.key.method == "AES-128":
key_url = segment.key.uri
iv = bytes.fromhex(segment.key.iv[2:]) if segment.key.iv else None
thread = threading.Thread(target=download_and_decrypt_segment,args=(segment_url, key, iv, directory+segment_file))
# download_and_decrypt_segment(segment_url, key, iv, directory+segment_file)
threads.append(thread)
thread.start()
for t in threads:
t.join()
# Combine segments into a single file
if current != len(segment_files):
print("All files are not downloaded")
exit()
with open(output_file+".bak", "wb") as output:
for segment_file in segment_files:
with open(directory+segment_file, "rb") as segment:
output.write(segment.read())
os.remove(directory+segment_file) # Clean up segment file
os.rename(output_file+".bak",output_file)
print(f"Video saved as {output_file}")
def handle_download_start(html,isFile=False,output_file="",max_thread=1,max_segment=0):
pattern = r'<script(.*?) id="__NEXT_DATA__"(.*?)>(.*?)</script>'
if isFile:
with open(html,"r") as f:
html = f.read()
match = re.search(pattern, html, re.DOTALL)
if match:
# Extract the JSON content from the match
json_content = match.group(3).strip()
decoded = json.loads(json_content)["props"]["pageProps"]
datetime = decoded["datetime"]
bit = datetime[-1]
token = decoded["token"]
iv = decoded["ivb6"]
urls = decoded["urls"]
data_dec_key = get_data_enc_key(datetime,token)
one = urls[0]
quality = one["quality"]
# print(quality)
kstr = one["kstr"]
jstr = one["jstr"]
output_file = output_file +" "+quality+".mp4"
if os.path.exists(output_file):
print(f"This video {output_file} is already downloaded")
return
video_dec_key = decrypt_data(kstr,data_dec_key,iv)
# print(video_dec_key)
video_dec_key = base64.b64decode(video_dec_key)
# video_dec_key = video_dec_key.ljust(24, b'\x00')
# print( len(video_dec_key))
# exit()
video_m3u8 = decrypt_data(jstr,data_dec_key,iv)
playlist = m3u8.loads(video_m3u8)
download_m3u8_playlist(playlist, output_file,video_dec_key,".temp/",max_thread,max_segment)
# print(video_m3u8)
# end of this
def start():
courses = get_all_purchases()
print("\n\n")
c = 1
for course in courses:
name = course["coursedt"][0]["course_name"]
# id = course["itemid"]
print(f"{c}. {name}")
c = c + 1
choice = input("\n\nEnter the course: ")
c_title = "courses/"+courses[int(choice)-1]["coursedt"][0]["course_name"]
cid = courses[int(choice)-1]["itemid"]
os.makedirs(c_title, exist_ok=True)
titles = get_titles(cid)
c=1
if len(titles) !=1:
for title in titles:
print(f"{c}. {title['Title']} | {title['material_type']}")
c = c + 1
choice = input("\nEnter the choice: ")
else:
choice = "1"
pid = titles[int(choice)-1]["id"]
titles = get_titles(cid,pid)
choice= input("Choose Download options: \n1. Links only\n2. Download Links and Videos both\n=>")
if True:
if sys.argv[1]:
segement_size = int( sys.argv[1] )
else:
segement_size = 40
if sys.argv[2]:
thread_size = int( sys.argv[2] )
else:
thread_size = 50
c=1
for title in titles:
print(f"\n{c}. Downloading link of '{title['Title']} | {title['material_type']}'")
vid = title["id"]
if title['material_type'] !="VIDEO":
print("Ignoring this file as it is not video")
continue
c = c + 1
if os.path.exists(c_title+"/"+title["Title"]+".html"):
print("Already downloaded link for "+"'"+title["Title"]+"'\n")
handle_download_start(c_title+"/"+title["Title"]+".html",True,c_title+"/"+title["Title"],thread_size,segement_size)
continue
# exit()
# choice = input("Enter the choice: ")
# vid = titles[int(choice)-1]["id"]
vtoken = get_video_token(cid,vid)
watch_video(cid,vid)
# print(vtoken)
# print(cookie)
html = get_video_html(vtoken)
html = html.replace('src="/','src="https://player.akamai.net.in/')
html = html.replace('href="/','href="https://player.akamai.net.in/')
html = html.replace('"quality":"360p","isPremier":','"quality":"720p","isPremier":')
# print(c_title)
if "Token Expired" in html:
print("This one is expired...\n")
print("Waiting for 30 seconds to prevent rate limiting\n")
time.sleep(30)
continue
with open(c_title+"/"+title["Title"]+".html","w") as e:
e.write(html)
if choice=="2":
handle_download_start(html,False,c_title+"/"+title["Title"],thread_size,segement_size)
print("Waiting for 30 seconds to prevent rate limiting\n")
time.sleep(30)
start()