aenetworks.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. from .theplatform import ThePlatformIE
  2. from ..utils import (
  3. ExtractorError,
  4. GeoRestrictedError,
  5. int_or_none,
  6. remove_start,
  7. traverse_obj,
  8. update_url_query,
  9. urlencode_postdata,
  10. )
  11. class AENetworksBaseIE(ThePlatformIE): # XXX: Do not subclass from concrete IE
  12. _BASE_URL_REGEX = r'''(?x)https?://
  13. (?:(?:www|play|watch)\.)?
  14. (?P<domain>
  15. (?:history(?:vault)?|aetv|mylifetime|lifetimemovieclub)\.com|
  16. fyi\.tv
  17. )/'''
  18. _THEPLATFORM_KEY = '43jXaGRQud'
  19. _THEPLATFORM_SECRET = 'S10BPXHMlb'
  20. _DOMAIN_MAP = {
  21. 'history.com': ('HISTORY', 'history'),
  22. 'aetv.com': ('AETV', 'aetv'),
  23. 'mylifetime.com': ('LIFETIME', 'lifetime'),
  24. 'lifetimemovieclub.com': ('LIFETIMEMOVIECLUB', 'lmc'),
  25. 'fyi.tv': ('FYI', 'fyi'),
  26. 'historyvault.com': (None, 'historyvault'),
  27. 'biography.com': (None, 'biography'),
  28. }
  29. def _extract_aen_smil(self, smil_url, video_id, auth=None):
  30. query = {
  31. 'mbr': 'true',
  32. 'formats': 'M3U+none,MPEG-DASH+none,MPEG4,MP3',
  33. }
  34. if auth:
  35. query['auth'] = auth
  36. TP_SMIL_QUERY = [{
  37. 'assetTypes': 'high_video_ak',
  38. 'switch': 'hls_high_ak',
  39. }, {
  40. 'assetTypes': 'high_video_s3',
  41. }, {
  42. 'assetTypes': 'high_video_s3',
  43. 'switch': 'hls_high_fastly',
  44. }]
  45. formats = []
  46. subtitles = {}
  47. last_e = None
  48. for q in TP_SMIL_QUERY:
  49. q.update(query)
  50. m_url = update_url_query(smil_url, q)
  51. m_url = self._sign_url(m_url, self._THEPLATFORM_KEY, self._THEPLATFORM_SECRET)
  52. try:
  53. tp_formats, tp_subtitles = self._extract_theplatform_smil(
  54. m_url, video_id, 'Downloading %s SMIL data' % (q.get('switch') or q['assetTypes']))
  55. except ExtractorError as e:
  56. if isinstance(e, GeoRestrictedError):
  57. raise
  58. last_e = e
  59. continue
  60. formats.extend(tp_formats)
  61. subtitles = self._merge_subtitles(subtitles, tp_subtitles)
  62. if last_e and not formats:
  63. raise last_e
  64. return {
  65. 'id': video_id,
  66. 'formats': formats,
  67. 'subtitles': subtitles,
  68. }
  69. def _extract_aetn_info(self, domain, filter_key, filter_value, url):
  70. requestor_id, brand = self._DOMAIN_MAP[domain]
  71. result = self._download_json(
  72. f'https://feeds.video.aetnd.com/api/v2/{brand}/videos',
  73. filter_value, query={f'filter[{filter_key}]': filter_value})
  74. result = traverse_obj(
  75. result, ('results',
  76. lambda k, v: k == 0 and v[filter_key] == filter_value),
  77. get_all=False)
  78. if not result:
  79. raise ExtractorError('Show not found in A&E feed (too new?)', expected=True,
  80. video_id=remove_start(filter_value, '/'))
  81. title = result['title']
  82. video_id = result['id']
  83. media_url = result['publicUrl']
  84. theplatform_metadata = self._download_theplatform_metadata(self._search_regex(
  85. r'https?://link\.theplatform\.com/s/([^?]+)', media_url, 'theplatform_path'), video_id)
  86. info = self._parse_theplatform_metadata(theplatform_metadata)
  87. auth = None
  88. if theplatform_metadata.get('AETN$isBehindWall'):
  89. resource = self._get_mvpd_resource(
  90. requestor_id, theplatform_metadata['title'],
  91. theplatform_metadata.get('AETN$PPL_pplProgramId') or theplatform_metadata.get('AETN$PPL_pplProgramId_OLD'),
  92. traverse_obj(theplatform_metadata, ('ratings', 0, 'rating')))
  93. auth = self._extract_mvpd_auth(
  94. url, video_id, requestor_id, resource)
  95. info.update(self._extract_aen_smil(media_url, video_id, auth))
  96. info.update({
  97. 'title': title,
  98. 'series': result.get('seriesName'),
  99. 'season_number': int_or_none(result.get('tvSeasonNumber')),
  100. 'episode_number': int_or_none(result.get('tvSeasonEpisodeNumber')),
  101. })
  102. return info
  103. class AENetworksIE(AENetworksBaseIE):
  104. IE_NAME = 'aenetworks'
  105. IE_DESC = 'A+E Networks: A&E, Lifetime, History.com, FYI Network and History Vault'
  106. _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'''(?P<id>
  107. shows/[^/]+/season-\d+/episode-\d+|
  108. (?:
  109. (?:movie|special)s/[^/]+|
  110. (?:shows/[^/]+/)?videos
  111. )/[^/?#&]+
  112. )'''
  113. _TESTS = [{
  114. 'url': 'http://www.history.com/shows/mountain-men/season-1/episode-1',
  115. 'info_dict': {
  116. 'id': '22253814',
  117. 'ext': 'mp4',
  118. 'title': 'Winter Is Coming',
  119. 'description': 'md5:a40e370925074260b1c8a633c632c63a',
  120. 'timestamp': 1338306241,
  121. 'upload_date': '20120529',
  122. 'uploader': 'AENE-NEW',
  123. 'duration': 2592.0,
  124. 'thumbnail': r're:^https?://.*\.jpe?g$',
  125. 'chapters': 'count:5',
  126. 'tags': 'count:14',
  127. 'categories': ['Mountain Men'],
  128. 'episode_number': 1,
  129. 'episode': 'Episode 1',
  130. 'season': 'Season 1',
  131. 'season_number': 1,
  132. 'series': 'Mountain Men',
  133. },
  134. 'params': {
  135. # m3u8 download
  136. 'skip_download': True,
  137. },
  138. 'add_ie': ['ThePlatform'],
  139. 'skip': 'Geo-restricted - This content is not available in your location.',
  140. }, {
  141. 'url': 'http://www.aetv.com/shows/duck-dynasty/season-9/episode-1',
  142. 'info_dict': {
  143. 'id': '600587331957',
  144. 'ext': 'mp4',
  145. 'title': 'Inlawful Entry',
  146. 'description': 'md5:57c12115a2b384d883fe64ca50529e08',
  147. 'timestamp': 1452634428,
  148. 'upload_date': '20160112',
  149. 'uploader': 'AENE-NEW',
  150. 'duration': 1277.695,
  151. 'thumbnail': r're:^https?://.*\.jpe?g$',
  152. 'chapters': 'count:4',
  153. 'tags': 'count:23',
  154. 'episode': 'Episode 1',
  155. 'episode_number': 1,
  156. 'season': 'Season 9',
  157. 'season_number': 9,
  158. 'series': 'Duck Dynasty',
  159. },
  160. 'params': {
  161. # m3u8 download
  162. 'skip_download': True,
  163. },
  164. 'add_ie': ['ThePlatform'],
  165. 'skip': 'This video is only available for users of participating TV providers.',
  166. }, {
  167. 'url': 'http://www.fyi.tv/shows/tiny-house-nation/season-1/episode-8',
  168. 'only_matching': True,
  169. }, {
  170. 'url': 'http://www.mylifetime.com/shows/project-runway-junior/season-1/episode-6',
  171. 'only_matching': True,
  172. }, {
  173. 'url': 'http://www.mylifetime.com/movies/center-stage-on-pointe/full-movie',
  174. 'only_matching': True,
  175. }, {
  176. 'url': 'https://watch.lifetimemovieclub.com/movies/10-year-reunion/full-movie',
  177. 'only_matching': True,
  178. }, {
  179. 'url': 'http://www.history.com/specials/sniper-into-the-kill-zone/full-special',
  180. 'only_matching': True,
  181. }, {
  182. 'url': 'https://www.aetv.com/specials/hunting-jonbenets-killer-the-untold-story/preview-hunting-jonbenets-killer-the-untold-story',
  183. 'only_matching': True,
  184. }, {
  185. 'url': 'http://www.history.com/videos/history-of-valentines-day',
  186. 'only_matching': True,
  187. }, {
  188. 'url': 'https://play.aetv.com/shows/duck-dynasty/videos/best-of-duck-dynasty-getting-quack-in-shape',
  189. 'only_matching': True,
  190. }]
  191. def _real_extract(self, url):
  192. domain, canonical = self._match_valid_url(url).groups()
  193. return self._extract_aetn_info(domain, 'canonical', '/' + canonical, url)
  194. class AENetworksListBaseIE(AENetworksBaseIE):
  195. def _call_api(self, resource, slug, brand, fields):
  196. return self._download_json(
  197. 'https://yoga.appsvcs.aetnd.com/graphql',
  198. slug, query={'brand': brand}, data=urlencode_postdata({
  199. 'query': '''{
  200. %s(slug: "%s") {
  201. %s
  202. }
  203. }''' % (resource, slug, fields), # noqa: UP031
  204. }))['data'][resource]
  205. def _real_extract(self, url):
  206. domain, slug = self._match_valid_url(url).groups()
  207. _, brand = self._DOMAIN_MAP[domain]
  208. playlist = self._call_api(self._RESOURCE, slug, brand, self._FIELDS)
  209. base_url = f'http://watch.{domain}'
  210. entries = []
  211. for item in (playlist.get(self._ITEMS_KEY) or []):
  212. doc = self._get_doc(item)
  213. canonical = doc.get('canonical')
  214. if not canonical:
  215. continue
  216. entries.append(self.url_result(
  217. base_url + canonical, AENetworksIE.ie_key(), doc.get('id')))
  218. description = None
  219. if self._PLAYLIST_DESCRIPTION_KEY:
  220. description = playlist.get(self._PLAYLIST_DESCRIPTION_KEY)
  221. return self.playlist_result(
  222. entries, playlist.get('id'),
  223. playlist.get(self._PLAYLIST_TITLE_KEY), description)
  224. class AENetworksCollectionIE(AENetworksListBaseIE):
  225. IE_NAME = 'aenetworks:collection'
  226. _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'(?:[^/]+/)*(?:list|collections)/(?P<id>[^/?#&]+)/?(?:[?#&]|$)'
  227. _TESTS = [{
  228. 'url': 'https://watch.historyvault.com/list/america-the-story-of-us',
  229. 'info_dict': {
  230. 'id': '282',
  231. 'title': 'America The Story of Us',
  232. },
  233. 'playlist_mincount': 12,
  234. }, {
  235. 'url': 'https://watch.historyvault.com/shows/america-the-story-of-us-2/season-1/list/america-the-story-of-us',
  236. 'only_matching': True,
  237. }, {
  238. 'url': 'https://www.historyvault.com/collections/mysteryquest',
  239. 'only_matching': True,
  240. }]
  241. _RESOURCE = 'list'
  242. _ITEMS_KEY = 'items'
  243. _PLAYLIST_TITLE_KEY = 'display_title'
  244. _PLAYLIST_DESCRIPTION_KEY = None
  245. _FIELDS = '''id
  246. display_title
  247. items {
  248. ... on ListVideoItem {
  249. doc {
  250. canonical
  251. id
  252. }
  253. }
  254. }'''
  255. def _get_doc(self, item):
  256. return item.get('doc') or {}
  257. class AENetworksShowIE(AENetworksListBaseIE):
  258. IE_NAME = 'aenetworks:show'
  259. _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'shows/(?P<id>[^/?#&]+)/?(?:[?#&]|$)'
  260. _TESTS = [{
  261. 'url': 'http://www.history.com/shows/ancient-aliens',
  262. 'info_dict': {
  263. 'id': 'SERIES1574',
  264. 'title': 'Ancient Aliens',
  265. 'description': 'md5:3f6d74daf2672ff3ae29ed732e37ea7f',
  266. },
  267. 'playlist_mincount': 150,
  268. }]
  269. _RESOURCE = 'series'
  270. _ITEMS_KEY = 'episodes'
  271. _PLAYLIST_TITLE_KEY = 'title'
  272. _PLAYLIST_DESCRIPTION_KEY = 'description'
  273. _FIELDS = '''description
  274. id
  275. title
  276. episodes {
  277. canonical
  278. id
  279. }'''
  280. def _get_doc(self, item):
  281. return item
  282. class HistoryTopicIE(AENetworksBaseIE):
  283. IE_NAME = 'history:topic'
  284. IE_DESC = 'History.com Topic'
  285. _VALID_URL = r'https?://(?:www\.)?history\.com/topics/[^/]+/(?P<id>[\w+-]+?)-video'
  286. _TESTS = [{
  287. 'url': 'https://www.history.com/topics/valentines-day/history-of-valentines-day-video',
  288. 'info_dict': {
  289. 'id': '40700995724',
  290. 'ext': 'mp4',
  291. 'title': 'History of Valentine’s Day',
  292. 'description': 'md5:7b57ea4829b391995b405fa60bd7b5f7',
  293. 'timestamp': 1375819729,
  294. 'upload_date': '20130806',
  295. 'uploader': 'AENE-NEW',
  296. },
  297. 'params': {
  298. # m3u8 download
  299. 'skip_download': True,
  300. },
  301. 'add_ie': ['ThePlatform'],
  302. }]
  303. def _real_extract(self, url):
  304. display_id = self._match_id(url)
  305. return self.url_result(
  306. 'http://www.history.com/videos/' + display_id,
  307. AENetworksIE.ie_key())
  308. class HistoryPlayerIE(AENetworksBaseIE):
  309. IE_NAME = 'history:player'
  310. _VALID_URL = r'https?://(?:www\.)?(?P<domain>(?:history|biography)\.com)/player/(?P<id>\d+)'
  311. _TESTS = []
  312. def _real_extract(self, url):
  313. domain, video_id = self._match_valid_url(url).groups()
  314. return self._extract_aetn_info(domain, 'id', video_id, url)
  315. class BiographyIE(AENetworksBaseIE):
  316. _VALID_URL = r'https?://(?:www\.)?biography\.com/video/(?P<id>[^/?#&]+)'
  317. _TESTS = [{
  318. 'url': 'https://www.biography.com/video/vincent-van-gogh-full-episode-2075049808',
  319. 'info_dict': {
  320. 'id': '30322987',
  321. 'ext': 'mp4',
  322. 'title': 'Vincent Van Gogh - Full Episode',
  323. 'description': 'A full biography about the most influential 20th century painter, Vincent Van Gogh.',
  324. 'timestamp': 1311970571,
  325. 'upload_date': '20110729',
  326. 'uploader': 'AENE-NEW',
  327. },
  328. 'params': {
  329. # m3u8 download
  330. 'skip_download': True,
  331. },
  332. 'add_ie': ['ThePlatform'],
  333. 'skip': '404 Not Found',
  334. }]
  335. def _real_extract(self, url):
  336. display_id = self._match_id(url)
  337. webpage = self._download_webpage(url, display_id)
  338. player_url = self._search_regex(
  339. rf'<phoenix-iframe[^>]+src="({HistoryPlayerIE._VALID_URL})',
  340. webpage, 'player URL')
  341. return self.url_result(player_url, HistoryPlayerIE.ie_key())