-
Notifications
You must be signed in to change notification settings - Fork 0
/
hulusubs-dl.py
63 lines (59 loc) · 2.29 KB
/
hulusubs-dl.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib3
import re
import os
import sys
import xml.etree.ElementTree as ET
# check command line arguments
is_srt = True
args = sys.argv
if len(args) > 1:
if args[1] == '-v' or args[1] == '--vtt':
is_srt = False
# prompt instruction
print('Copy the text below and paste it to console of your browser to get Content ID:')
print('------------------------------------------------------------------------------')
print('var x=new XMLHttpRequest;x.open("GET","https://discover.hulu.com/content/v3/entity?language=en&schema=4&eab_ids="+window.location.href.split("/").pop(),!1),x.withCredentials=!0,x.send(null),JSON.parse(x.responseText)["items"][0]["content_id"];')
print('------------------------------------------------------------------------------')
content_id = input('Enter Content ID: ')
# process
http = urllib3.PoolManager()
r = http.request('GET', f'https://www.hulu.com/captions.xml?content_id={content_id}')
root = ET.fromstring(r.data.decode('utf-8'))
ens = root.findall('./en')
vtt_url = f'https://assetshuluimcom-a.akamaihd.net/captions_webvtt/{int(content_id[-3:])}/{content_id}_US_en_en.vtt'
if len(ens) > 0:
vtt_url = ens[0].text.replace('captions', 'captions_webvtt').replace('.smi', '.vtt')
else:
print('[Info] Could not find subtitle in this video, but trying...')
r = http.request('GET', vtt_url)
with open(f'{content_id}.vtt', "wb") as vtt_file:
vtt_file.write(r.data)
if is_srt:
with open(f'{content_id}.vtt', 'r+', encoding='utf-8') as read_file:
lines = read_file.readlines()
lines.pop()
line_count = 0
for i, num in enumerate(lines):
if num == '\n':
line_count += 1
if line_count == 1:
lines[i] = f'{line_count}\n'
else:
lines[i] = f'\n{line_count}\n'
read_file.seek(0)
for line in lines:
if ' --> ' in line:
read_file.write(line.replace('.', ','))
else:
read_file.write(line.replace('WEBVTT\n', ''))
try:
os.rename(f'{content_id}.vtt', f'{content_id}.srt')
except Exception as error:
print('[Error] Failed to rename file.')
print(error)
exit
print(f'Succeeded in downloading `{content_id}.srt`.')
else:
print(f'Succeeded in downloading `{content_id}.vtt`.')