logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

go.py (12909B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .adobepass import AdobePassIE
  5. from ..compat import compat_str
  6. from ..utils import (
  7. int_or_none,
  8. determine_ext,
  9. parse_age_limit,
  10. try_get,
  11. urlencode_postdata,
  12. ExtractorError,
  13. )
  14. class GoIE(AdobePassIE):
  15. _SITE_INFO = {
  16. 'abc': {
  17. 'brand': '001',
  18. 'requestor_id': 'ABC',
  19. },
  20. 'freeform': {
  21. 'brand': '002',
  22. 'requestor_id': 'ABCFamily',
  23. },
  24. 'watchdisneychannel': {
  25. 'brand': '004',
  26. 'resource_id': 'Disney',
  27. },
  28. 'watchdisneyjunior': {
  29. 'brand': '008',
  30. 'resource_id': 'DisneyJunior',
  31. },
  32. 'watchdisneyxd': {
  33. 'brand': '009',
  34. 'resource_id': 'DisneyXD',
  35. },
  36. 'disneynow': {
  37. 'brand': '011',
  38. 'resource_id': 'Disney',
  39. },
  40. 'fxnow.fxnetworks': {
  41. 'brand': '025',
  42. 'requestor_id': 'dtci',
  43. },
  44. }
  45. _VALID_URL = r'''(?x)
  46. https?://
  47. (?:
  48. (?:(?P<sub_domain>%s)\.)?go|
  49. (?P<sub_domain_2>abc|freeform|disneynow|fxnow\.fxnetworks)
  50. )\.com/
  51. (?:
  52. (?:[^/]+/)*(?P<id>[Vv][Dd][Kk][Aa]\w+)|
  53. (?:[^/]+/)*(?P<display_id>[^/?\#]+)
  54. )
  55. ''' % '|'.join(list(_SITE_INFO.keys()))
  56. _TESTS = [{
  57. 'url': 'http://abc.go.com/shows/designated-survivor/video/most-recent/VDKA3807643',
  58. 'info_dict': {
  59. 'id': 'VDKA3807643',
  60. 'ext': 'mp4',
  61. 'title': 'The Traitor in the White House',
  62. 'description': 'md5:05b009d2d145a1e85d25111bd37222e8',
  63. },
  64. 'params': {
  65. # m3u8 download
  66. 'skip_download': True,
  67. },
  68. 'skip': 'This content is no longer available.',
  69. }, {
  70. 'url': 'http://watchdisneyxd.go.com/doraemon',
  71. 'info_dict': {
  72. 'title': 'Doraemon',
  73. 'id': 'SH55574025',
  74. },
  75. 'playlist_mincount': 51,
  76. }, {
  77. 'url': 'http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood',
  78. 'info_dict': {
  79. 'id': 'VDKA3609139',
  80. 'ext': 'mp4',
  81. 'title': 'This Guilty Blood',
  82. 'description': 'md5:f18e79ad1c613798d95fdabfe96cd292',
  83. 'age_limit': 14,
  84. },
  85. 'params': {
  86. 'geo_bypass_ip_block': '3.244.239.0/24',
  87. # m3u8 download
  88. 'skip_download': True,
  89. },
  90. }, {
  91. 'url': 'https://abc.com/shows/the-rookie/episode-guide/season-02/03-the-bet',
  92. 'info_dict': {
  93. 'id': 'VDKA13435179',
  94. 'ext': 'mp4',
  95. 'title': 'The Bet',
  96. 'description': 'md5:c66de8ba2e92c6c5c113c3ade84ab404',
  97. 'age_limit': 14,
  98. },
  99. 'params': {
  100. 'geo_bypass_ip_block': '3.244.239.0/24',
  101. # m3u8 download
  102. 'skip_download': True,
  103. },
  104. }, {
  105. 'url': 'https://fxnow.fxnetworks.com/shows/better-things/video/vdka12782841',
  106. 'info_dict': {
  107. 'id': 'VDKA12782841',
  108. 'ext': 'mp4',
  109. 'title': 'First Look: Better Things - Season 2',
  110. 'description': 'md5:fa73584a95761c605d9d54904e35b407',
  111. },
  112. 'params': {
  113. 'geo_bypass_ip_block': '3.244.239.0/24',
  114. # m3u8 download
  115. 'skip_download': True,
  116. },
  117. }, {
  118. 'url': 'https://abc.com/shows/modern-family/episode-guide/season-01/101-pilot',
  119. 'info_dict': {
  120. 'id': 'VDKA22600213',
  121. 'ext': 'mp4',
  122. 'title': 'Pilot',
  123. 'description': 'md5:74306df917cfc199d76d061d66bebdb4',
  124. },
  125. 'params': {
  126. # m3u8 download
  127. 'skip_download': True,
  128. },
  129. }, {
  130. 'url': 'http://abc.go.com/shows/the-catch/episode-guide/season-01/10-the-wedding',
  131. 'only_matching': True,
  132. }, {
  133. 'url': 'http://abc.go.com/shows/world-news-tonight/episode-guide/2017-02/17-021717-intense-stand-off-between-man-with-rifle-and-police-in-oakland',
  134. 'only_matching': True,
  135. }, {
  136. # brand 004
  137. 'url': 'http://disneynow.go.com/shows/big-hero-6-the-series/season-01/episode-10-mr-sparkles-loses-his-sparkle/vdka4637915',
  138. 'only_matching': True,
  139. }, {
  140. # brand 008
  141. 'url': 'http://disneynow.go.com/shows/minnies-bow-toons/video/happy-campers/vdka4872013',
  142. 'only_matching': True,
  143. }, {
  144. 'url': 'https://disneynow.com/shows/minnies-bow-toons/video/happy-campers/vdka4872013',
  145. 'only_matching': True,
  146. }]
  147. def _extract_videos(self, brand, video_id='-1', show_id='-1'):
  148. display_id = video_id if video_id != '-1' else show_id
  149. return self._download_json(
  150. 'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/%s/001/-1/%s/-1/%s/-1/-1.json' % (brand, show_id, video_id),
  151. display_id)['video']
  152. def _real_extract(self, url):
  153. mobj = re.match(self._VALID_URL, url)
  154. sub_domain = mobj.group('sub_domain') or mobj.group('sub_domain_2')
  155. video_id, display_id = mobj.group('id', 'display_id')
  156. site_info = self._SITE_INFO.get(sub_domain, {})
  157. brand = site_info.get('brand')
  158. if not video_id or not site_info:
  159. webpage = self._download_webpage(url, display_id or video_id)
  160. data = self._parse_json(
  161. self._search_regex(
  162. r'["\']__abc_com__["\']\s*\]\s*=\s*({.+?})\s*;', webpage,
  163. 'data', default='{}'),
  164. display_id or video_id, fatal=False)
  165. # https://abc.com/shows/modern-family/episode-guide/season-01/101-pilot
  166. layout = try_get(data, lambda x: x['page']['content']['video']['layout'], dict)
  167. video_id = None
  168. if layout:
  169. video_id = try_get(
  170. layout,
  171. (lambda x: x['videoid'], lambda x: x['video']['id']),
  172. compat_str)
  173. if not video_id:
  174. video_id = self._search_regex(
  175. (
  176. # There may be inner quotes, e.g. data-video-id="'VDKA3609139'"
  177. # from http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood
  178. r'data-video-id=["\']*(VDKA\w+)',
  179. # page.analytics.videoIdCode
  180. r'\bvideoIdCode["\']\s*:\s*["\']((?:vdka|VDKA)\w+)',
  181. # https://abc.com/shows/the-rookie/episode-guide/season-02/03-the-bet
  182. r'\b(?:video)?id["\']\s*:\s*["\'](VDKA\w+)'
  183. ), webpage, 'video id', default=video_id)
  184. if not site_info:
  185. brand = self._search_regex(
  186. (r'data-brand=\s*["\']\s*(\d+)',
  187. r'data-page-brand=\s*["\']\s*(\d+)'), webpage, 'brand',
  188. default='004')
  189. site_info = next(
  190. si for _, si in self._SITE_INFO.items()
  191. if si.get('brand') == brand)
  192. if not video_id:
  193. # show extraction works for Disney, DisneyJunior and DisneyXD
  194. # ABC and Freeform has different layout
  195. show_id = self._search_regex(r'data-show-id=["\']*(SH\d+)', webpage, 'show id')
  196. videos = self._extract_videos(brand, show_id=show_id)
  197. show_title = self._search_regex(r'data-show-title="([^"]+)"', webpage, 'show title', fatal=False)
  198. entries = []
  199. for video in videos:
  200. entries.append(self.url_result(
  201. video['url'], 'Go', video.get('id'), video.get('title')))
  202. entries.reverse()
  203. return self.playlist_result(entries, show_id, show_title)
  204. video_data = self._extract_videos(brand, video_id)[0]
  205. video_id = video_data['id']
  206. title = video_data['title']
  207. formats = []
  208. for asset in video_data.get('assets', {}).get('asset', []):
  209. asset_url = asset.get('value')
  210. if not asset_url:
  211. continue
  212. format_id = asset.get('format')
  213. ext = determine_ext(asset_url)
  214. if ext == 'm3u8':
  215. video_type = video_data.get('type')
  216. data = {
  217. 'video_id': video_data['id'],
  218. 'video_type': video_type,
  219. 'brand': brand,
  220. 'device': '001',
  221. }
  222. if video_data.get('accesslevel') == '1':
  223. requestor_id = site_info.get('requestor_id', 'DisneyChannels')
  224. resource = site_info.get('resource_id') or self._get_mvpd_resource(
  225. requestor_id, title, video_id, None)
  226. auth = self._extract_mvpd_auth(
  227. url, video_id, requestor_id, resource)
  228. data.update({
  229. 'token': auth,
  230. 'token_type': 'ap',
  231. 'adobe_requestor_id': requestor_id,
  232. })
  233. else:
  234. self._initialize_geo_bypass({'countries': ['US']})
  235. entitlement = self._download_json(
  236. 'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
  237. video_id, data=urlencode_postdata(data))
  238. errors = entitlement.get('errors', {}).get('errors', [])
  239. if errors:
  240. for error in errors:
  241. if error.get('code') == 1002:
  242. self.raise_geo_restricted(
  243. error['message'], countries=['US'])
  244. error_message = ', '.join([error['message'] for error in errors])
  245. raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
  246. asset_url += '?' + entitlement['uplynkData']['sessionKey']
  247. formats.extend(self._extract_m3u8_formats(
  248. asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
  249. else:
  250. f = {
  251. 'format_id': format_id,
  252. 'url': asset_url,
  253. 'ext': ext,
  254. }
  255. if re.search(r'(?:/mp4/source/|_source\.mp4)', asset_url):
  256. f.update({
  257. 'format_id': ('%s-' % format_id if format_id else '') + 'SOURCE',
  258. 'preference': 1,
  259. })
  260. else:
  261. mobj = re.search(r'/(\d+)x(\d+)/', asset_url)
  262. if mobj:
  263. height = int(mobj.group(2))
  264. f.update({
  265. 'format_id': ('%s-' % format_id if format_id else '') + '%dP' % height,
  266. 'width': int(mobj.group(1)),
  267. 'height': height,
  268. })
  269. formats.append(f)
  270. self._sort_formats(formats)
  271. subtitles = {}
  272. for cc in video_data.get('closedcaption', {}).get('src', []):
  273. cc_url = cc.get('value')
  274. if not cc_url:
  275. continue
  276. ext = determine_ext(cc_url)
  277. if ext == 'xml':
  278. ext = 'ttml'
  279. subtitles.setdefault(cc.get('lang'), []).append({
  280. 'url': cc_url,
  281. 'ext': ext,
  282. })
  283. thumbnails = []
  284. for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
  285. thumbnail_url = thumbnail.get('value')
  286. if not thumbnail_url:
  287. continue
  288. thumbnails.append({
  289. 'url': thumbnail_url,
  290. 'width': int_or_none(thumbnail.get('width')),
  291. 'height': int_or_none(thumbnail.get('height')),
  292. })
  293. return {
  294. 'id': video_id,
  295. 'title': title,
  296. 'description': video_data.get('longdescription') or video_data.get('description'),
  297. 'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
  298. 'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
  299. 'episode_number': int_or_none(video_data.get('episodenumber')),
  300. 'series': video_data.get('show', {}).get('title'),
  301. 'season_number': int_or_none(video_data.get('season', {}).get('num')),
  302. 'thumbnails': thumbnails,
  303. 'formats': formats,
  304. 'subtitles': subtitles,
  305. }