forked from zoexmh99/wvd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site_disneyplus.py
193 lines (173 loc) · 11.6 KB
/
site_disneyplus.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
import os, sys, traceback, re, json, threading, time, shutil, subprocess, psutil, requests
from urllib import parse
from datetime import datetime
from .site_base import SiteBase, d, logger, package_name, ModelSetting, Utility, P, path_data, ToolBaseFile, webdriver, WebDriverWait, EC, By, Keys
class SiteDisney(SiteBase):
name = 'disney'
name_on_filename = 'DP'
url_regex = request_url_regex = re.compile(r'www\.disneyplus\.com\/ko-kr\/video\/(?P<code>.*?)$')
lic_url = 'https://disney.playback.edge.bamgrid.com/widevine/v1/obtain-license'
pssh_find_str = '4250k'
streaming_protocol = 'hls'
def __init__(self, db_id, json_filepath):
super(SiteDisney, self).__init__(db_id, json_filepath)
def prepare(self):
try:
self.meta['content_type'] = 'show'
self.meta['title'] = self.code
self.meta['episode_number'] = 1
self.meta['season_number'] = 1
for item in self.data['har']['log']['entries']:
if item['request']['method'] == 'GET' and item['request']['url'].find(f'contentId/{self.code}') != -1:
self.meta['source'] = self.get_response(item).json()
Utility.write_json(os.path.join(self.temp_dir, f'{self.code}.meta.json'), self.meta['source'])
break
if self.meta['source']['data']['DmcVideo']['video']['episodeSequenceNumber'] != None:
self.meta['content_type'] = 'show'
self.meta['season_number'] = self.meta['source']['data']['DmcVideo']['video']['seasonSequenceNumber']
self.meta['episode_number'] = self.meta['source']['data']['DmcVideo']['video']['episodeSequenceNumber']
else:
self.meta['content_type'] = 'movie'
for item in self.meta['source']['data']['DmcVideo']['video']['texts']:
if item['field'] == 'title' and item['type'] == 'full':
if (self.meta['content_type'] == 'show' and item['sourceEntity'] == 'series') or (self.meta['content_type'] == 'movie' and item['sourceEntity'] == 'program'):
self.meta['title'] = item['content']
break
except Exception as e:
P.logger.error('Exception:%s', e)
P.logger.error(traceback.format_exc())
def download_m3u8(self):
try:
m3u8_base_url = None
request_list = self.data['har']['log']['entries']
m3u8_data = {'video':None, 'audio':None, 'text':None}
for item in reversed(request_list):
if item['request']['method'] == 'GET' and item['request']['url'].find('.m3u8') != -1:
logger.warning(item['request']['url'])
if item['request']['url'].find('4250k_CENC') != -1 and m3u8_data['video'] == None:
m3u8_data['video'] = {'bandwidth':'1', 'lang':None}
m3u8_data['video']['url'] = item['request']['url']
#m3u8_data['video']['url'] = 'http://vod-ftc-ap-north-2.media.dssott.com/ps01/disney/2cd95643-36c3-4e5c-ab67-9e7c51567839/r/composite_8500k_CENC_CTR_FHD_SDR_a5794f70-b9ff-46e6-8c05-27e2f2f7c4e0_374ae605-3509-4966-9203-410920769578.m3u8'
#item['request']['url'] = m3u8_data['video']['url']
m3u8_data['video']['data'] = self.get_response(item).text
Utility.write_file(os.path.join(self.temp_dir, f"{self.code}.video.m3u8"), m3u8_data['video']['data'])
m3u8_base_url = item['request']['url'][:item['request']['url'].rfind('/')+1]
elif item['request']['url'].find('_mp4a') != -1 and m3u8_data['audio'] == None:
m3u8_data['audio'] = {'bandwidth':'2'}
m3u8_data['audio']['url'] = item['request']['url']
m3u8_data['audio']['data'] = self.get_response(item).text
Utility.write_file(os.path.join(self.temp_dir, f"{self.code}.audio.m3u8"), m3u8_data['audio']['data'])
m3u8_data['audio']['lang'] = item['request']['url'].split('/')[-1].split('_')[3]
elif item['request']['url'].find('_ko_') != -1 and m3u8_data['text'] == None:
#logger.warning(item['request']['url'])
m3u8_data['text'] = {'lang':'ko', 'mimeType':'text/vtt'}
m3u8_data['audio']['url'] = item['request']['url']
m3u8_data['text']['data'] = self.get_response(item).text
Utility.write_file(os.path.join(self.temp_dir, f"{self.code}.text.m3u8"), m3u8_data['text']['data'])
"""
if item['request']['url'].find('_en_') != -1 and m3u8_data['text'] == None:
m3u8_data['text'] = {'lang':'en', 'mimeType':'text/vtt'}
m3u8_data['audio']['url'] = item['request']['url']
m3u8_data['text']['data'] = self.get_response(item).text
Utility.write_file(os.path.join(self.temp_dir, f"{self.code}.text.m3u8"), m3u8_data['text']['data'])
"""
for ct in ['video', 'audio', 'text']:
if m3u8_data[ct] == None:
continue
#logger.debug(d(m3u8_data[ct]['data']))
m3u8_data[ct]['url_list'] = []
source_list = {}
for line in m3u8_data[ct]['data'].split('\n'):
if line.startswith('#') == False:
key = line.split('/')[0]
if key not in source_list:
source_list[key] = []
source_list[key].append(line)
max_key = None
max_urls = 0
for key, value in source_list.items():
if len(value) > max_urls:
max_key = key
max_urls = len(value)
m3u8_data[ct]['url_list'] = source_list[max_key]
self.filepath_mkv = os.path.join(self.temp_dir, f"{self.code}.mkv")
merge_option = ['-o', f'"{self.filepath_mkv}"']
#logger.debug(d(m3u8_data))
for ct in ['video', 'audio', 'text']:
##if m3u8_data[ct]['contentType'] == None:
# continue
m3u8_data[ct]['contentType'] = ct
self.make_filepath(m3u8_data[ct])
if ct in ['video', 'audio']:
#m3u8_data[ct]['filepath_merge2'] = m3u8_data[ct]['filepath_merge'].replace('decrypt', 'ffmpeg')
url = f"{m3u8_base_url}{m3u8_data[ct]['url_list'][0].replace('00/00/00_000.mp4', 'map.mp4')}"
init_filepath = os.path.join(self.temp_dir, f"{self.code}_{ct}_init.mp4")
Utility.aria2c_download(url, init_filepath)
for idx, line in enumerate(m3u8_data[ct]['url_list']):
url = f"{m3u8_base_url}{line}"
filepath = os.path.join(self.temp_dir, f"{self.code}_{ct}_{str(idx).zfill(5)}.m4s")
Utility.aria2c_download(url, filepath)
Utility.concat(init_filepath, os.path.join(self.temp_dir, f"{self.code}_{ct}_0*.m4s"), m3u8_data[ct]['filepath_download'])
if os.path.exists(m3u8_data[ct]['filepath_download']) and os.path.exists(m3u8_data[ct]['filepath_dump']) == False:
Utility.mp4dump(m3u8_data[ct]['filepath_download'], m3u8_data[ct]['filepath_dump'])
if os.path.exists(m3u8_data[ct]['filepath_merge']) == False:
text = Utility.read_file(m3u8_data[ct]['filepath_dump'])
if text.find('default_KID = [') == -1:
shutil.copy(m3u8_data[ct]['filepath_download'], m3u8_data[ct]['filepath_merge'])
else:
kid = text.split('default_KID = [')[1].split(']')[0].replace(' ', '')
key = self.find_key(kid)
logger.debug(self.data['key'])
logger.debug('%s:%s', kid, key)
Utility.mp4decrypt(m3u8_data[ct]['filepath_download'], m3u8_data[ct]['filepath_merge'], kid, key)
logger.debug(os.path.exists(m3u8_data[ct]['filepath_merge']))
#Utility.ffmpeg_copy(m3u8_data[ct]['filepath_merge'], m3u8_data[ct]['filepath_merge2'])
if ct == 'audio':
merge_option += ['--language', '0:%s' % m3u8_data[ct]['lang']]
merge_option += ['"%s"' % m3u8_data[ct]['filepath_merge']]
else:
sub = ''
last_time = None
#logger.warning(d(m3u8_data[ct]['url_list']))
for idx, line in enumerate(m3u8_data[ct]['url_list']):
url = f"{m3u8_base_url}{line}"
filepath = os.path.join(self.temp_dir, f"{self.code}_{ct}_{str(idx).zfill(5)}.vtt")
#logger.debug(url)
#logger.debug(filepath)
Utility.aria2c_download(url, filepath)
data = Utility.read_file(filepath)
#logger.debug(data)
flag_append = False
for line_idx, tmp in enumerate(data.split('\n')):
if re.match('\d{2}:\d{2}:\d{2}', tmp):
break
sub += '\n'.join(data.split('\n')[line_idx:])
#Utility.write_file(m3u8_data[ct]['filepath_download']+str(idx)+'.txt', sub)
#logger.debug(sub)
Utility.write_file(m3u8_data[ct]['filepath_download'], sub)
Utility.vtt2srt(m3u8_data[ct]['filepath_download'], m3u8_data[ct]['filepath_merge'])
merge_option += ['--language', '"0:ko"']
merge_option += ['--default-track', '"0:yes"']
merge_option += ['"%s"' % m3u8_data[ct]['filepath_merge']]
if self.meta['content_type'] == 'show':
self.output_filename = u'{title}.S{season_number}E{episode_number}.720p.WEB-DL.AAC.H.264.SW{site}.mkv'.format(
title = ToolBaseFile.text_for_filename(self.meta['title']).strip(),
season_number = str(self.meta['season_number']).zfill(2),
episode_number = str(self.meta['episode_number']).zfill(2),
site = self.name_on_filename,
)
else:
self.output_filename = u'{title}.720p.WEB-DL.AAC.H.264.SW{site}.mkv'.format(
title = ToolBaseFile.text_for_filename(self.meta['title']).strip(),
site = self.name_on_filename,
)
logger.warning(self.output_filename)
self.filepath_output = os.path.join(Utility.output_dir, self.output_filename)
if os.path.exists(self.filepath_output) == False:
Utility.mkvmerge(merge_option)
shutil.move(self.filepath_mkv, self.filepath_output)
self.add_log(f'파일 생성: {self.output_filename}')
return True
except Exception as e:
P.logger.error('Exception:%s', e)
P.logger.error(traceback.format_exc())