aenetworks.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .theplatform import ThePlatformIE
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. update_url_query,
  9. urlencode_postdata,
  10. )
  11. class AENetworksBaseIE(ThePlatformIE):
  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 = 'crazyjava'
  19. _THEPLATFORM_SECRET = 's3cr3t'
  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. }
  28. def _extract_aen_smil(self, smil_url, video_id, auth=None):
  29. query = {'mbr': 'true'}
  30. if auth:
  31. query['auth'] = auth
  32. TP_SMIL_QUERY = [{
  33. 'assetTypes': 'high_video_ak',
  34. 'switch': 'hls_high_ak'
  35. }, {
  36. 'assetTypes': 'high_video_s3'
  37. }, {
  38. 'assetTypes': 'high_video_s3',
  39. 'switch': 'hls_high_fastly',
  40. }]
  41. formats = []
  42. subtitles = {}
  43. last_e = None
  44. for q in TP_SMIL_QUERY:
  45. q.update(query)
  46. m_url = update_url_query(smil_url, q)
  47. m_url = self._sign_url(m_url, self._THEPLATFORM_KEY, self._THEPLATFORM_SECRET)
  48. try:
  49. tp_formats, tp_subtitles = self._extract_theplatform_smil(
  50. m_url, video_id, 'Downloading %s SMIL data' % (q.get('switch') or q['assetTypes']))
  51. except ExtractorError as e:
  52. last_e = e
  53. continue
  54. formats.extend(tp_formats)
  55. subtitles = self._merge_subtitles(subtitles, tp_subtitles)
  56. if last_e and not formats:
  57. raise last_e
  58. self._sort_formats(formats)
  59. return {
  60. 'id': video_id,
  61. 'formats': formats,
  62. 'subtitles': subtitles,
  63. }
  64. class AENetworksIE(AENetworksBaseIE):
  65. IE_NAME = 'aenetworks'
  66. IE_DESC = 'A+E Networks: A&E, Lifetime, History.com, FYI Network and History Vault'
  67. _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'''(?P<id>
  68. shows/[^/]+/season-\d+/episode-\d+|
  69. (?:
  70. (?:movie|special)s/[^/]+|
  71. (?:shows/[^/]+/)?videos
  72. )/[^/?#&]+
  73. )'''
  74. _TESTS = [{
  75. 'url': 'http://www.history.com/shows/mountain-men/season-1/episode-1',
  76. 'info_dict': {
  77. 'id': '22253814',
  78. 'ext': 'mp4',
  79. 'title': 'Winter is Coming',
  80. 'description': 'md5:641f424b7a19d8e24f26dea22cf59d74',
  81. 'timestamp': 1338306241,
  82. 'upload_date': '20120529',
  83. 'uploader': 'AENE-NEW',
  84. },
  85. 'params': {
  86. # m3u8 download
  87. 'skip_download': True,
  88. },
  89. 'add_ie': ['ThePlatform'],
  90. 'skip': 'This video is only available for users of participating TV providers.',
  91. }, {
  92. 'url': 'http://www.aetv.com/shows/duck-dynasty/season-9/episode-1',
  93. 'info_dict': {
  94. 'id': '600587331957',
  95. 'ext': 'mp4',
  96. 'title': 'Inlawful Entry',
  97. 'description': 'md5:57c12115a2b384d883fe64ca50529e08',
  98. 'timestamp': 1452634428,
  99. 'upload_date': '20160112',
  100. 'uploader': 'AENE-NEW',
  101. },
  102. 'params': {
  103. # m3u8 download
  104. 'skip_download': True,
  105. },
  106. 'add_ie': ['ThePlatform'],
  107. }, {
  108. 'url': 'http://www.fyi.tv/shows/tiny-house-nation/season-1/episode-8',
  109. 'only_matching': True
  110. }, {
  111. 'url': 'http://www.mylifetime.com/shows/project-runway-junior/season-1/episode-6',
  112. 'only_matching': True
  113. }, {
  114. 'url': 'http://www.mylifetime.com/movies/center-stage-on-pointe/full-movie',
  115. 'only_matching': True
  116. }, {
  117. 'url': 'https://watch.lifetimemovieclub.com/movies/10-year-reunion/full-movie',
  118. 'only_matching': True
  119. }, {
  120. 'url': 'http://www.history.com/specials/sniper-into-the-kill-zone/full-special',
  121. 'only_matching': True
  122. }, {
  123. 'url': 'https://www.aetv.com/specials/hunting-jonbenets-killer-the-untold-story/preview-hunting-jonbenets-killer-the-untold-story',
  124. 'only_matching': True
  125. }, {
  126. 'url': 'http://www.history.com/videos/history-of-valentines-day',
  127. 'only_matching': True
  128. }, {
  129. 'url': 'https://play.aetv.com/shows/duck-dynasty/videos/best-of-duck-dynasty-getting-quack-in-shape',
  130. 'only_matching': True
  131. }]
  132. def _real_extract(self, url):
  133. domain, canonical = re.match(self._VALID_URL, url).groups()
  134. requestor_id, brand = self._DOMAIN_MAP[domain]
  135. result = self._download_json(
  136. 'https://feeds.video.aetnd.com/api/v2/%s/videos' % brand,
  137. canonical, query={'filter[canonical]': '/' + canonical})['results'][0]
  138. title = result['title']
  139. video_id = result['id']
  140. media_url = result['publicUrl']
  141. theplatform_metadata = self._download_theplatform_metadata(self._search_regex(
  142. r'https?://link\.theplatform\.com/s/([^?]+)', media_url, 'theplatform_path'), video_id)
  143. info = self._parse_theplatform_metadata(theplatform_metadata)
  144. auth = None
  145. if theplatform_metadata.get('AETN$isBehindWall'):
  146. resource = self._get_mvpd_resource(
  147. requestor_id, theplatform_metadata['title'],
  148. theplatform_metadata.get('AETN$PPL_pplProgramId') or theplatform_metadata.get('AETN$PPL_pplProgramId_OLD'),
  149. theplatform_metadata['ratings'][0]['rating'])
  150. auth = self._extract_mvpd_auth(
  151. url, video_id, requestor_id, resource)
  152. info.update(self._extract_aen_smil(media_url, video_id, auth))
  153. info.update({
  154. 'title': title,
  155. 'series': result.get('seriesName'),
  156. 'season_number': int_or_none(result.get('tvSeasonNumber')),
  157. 'episode_number': int_or_none(result.get('tvSeasonEpisodeNumber')),
  158. })
  159. return info
  160. class AENetworksListBaseIE(AENetworksBaseIE):
  161. def _call_api(self, resource, slug, brand, fields):
  162. return self._download_json(
  163. 'https://yoga.appsvcs.aetnd.com/graphql',
  164. slug, query={'brand': brand}, data=urlencode_postdata({
  165. 'query': '''{
  166. %s(slug: "%s") {
  167. %s
  168. }
  169. }''' % (resource, slug, fields),
  170. }))['data'][resource]
  171. def _real_extract(self, url):
  172. domain, slug = re.match(self._VALID_URL, url).groups()
  173. _, brand = self._DOMAIN_MAP[domain]
  174. playlist = self._call_api(self._RESOURCE, slug, brand, self._FIELDS)
  175. base_url = 'http://watch.%s' % domain
  176. entries = []
  177. for item in (playlist.get(self._ITEMS_KEY) or []):
  178. doc = self._get_doc(item)
  179. canonical = doc.get('canonical')
  180. if not canonical:
  181. continue
  182. entries.append(self.url_result(
  183. base_url + canonical, AENetworksIE.ie_key(), doc.get('id')))
  184. description = None
  185. if self._PLAYLIST_DESCRIPTION_KEY:
  186. description = playlist.get(self._PLAYLIST_DESCRIPTION_KEY)
  187. return self.playlist_result(
  188. entries, playlist.get('id'),
  189. playlist.get(self._PLAYLIST_TITLE_KEY), description)
  190. class AENetworksCollectionIE(AENetworksListBaseIE):
  191. IE_NAME = 'aenetworks:collection'
  192. _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'(?:[^/]+/)*(?:list|collections)/(?P<id>[^/?#&]+)/?(?:[?#&]|$)'
  193. _TESTS = [{
  194. 'url': 'https://watch.historyvault.com/list/america-the-story-of-us',
  195. 'info_dict': {
  196. 'id': '282',
  197. 'title': 'America The Story of Us',
  198. },
  199. 'playlist_mincount': 12,
  200. }, {
  201. 'url': 'https://watch.historyvault.com/shows/america-the-story-of-us-2/season-1/list/america-the-story-of-us',
  202. 'only_matching': True
  203. }, {
  204. 'url': 'https://www.historyvault.com/collections/mysteryquest',
  205. 'only_matching': True
  206. }]
  207. _RESOURCE = 'list'
  208. _ITEMS_KEY = 'items'
  209. _PLAYLIST_TITLE_KEY = 'display_title'
  210. _PLAYLIST_DESCRIPTION_KEY = None
  211. _FIELDS = '''id
  212. display_title
  213. items {
  214. ... on ListVideoItem {
  215. doc {
  216. canonical
  217. id
  218. }
  219. }
  220. }'''
  221. def _get_doc(self, item):
  222. return item.get('doc') or {}
  223. class AENetworksShowIE(AENetworksListBaseIE):
  224. IE_NAME = 'aenetworks:show'
  225. _VALID_URL = AENetworksBaseIE._BASE_URL_REGEX + r'shows/(?P<id>[^/?#&]+)/?(?:[?#&]|$)'
  226. _TESTS = [{
  227. 'url': 'http://www.history.com/shows/ancient-aliens',
  228. 'info_dict': {
  229. 'id': 'SH012427480000',
  230. 'title': 'Ancient Aliens',
  231. 'description': 'md5:3f6d74daf2672ff3ae29ed732e37ea7f',
  232. },
  233. 'playlist_mincount': 168,
  234. }]
  235. _RESOURCE = 'series'
  236. _ITEMS_KEY = 'episodes'
  237. _PLAYLIST_TITLE_KEY = 'title'
  238. _PLAYLIST_DESCRIPTION_KEY = 'description'
  239. _FIELDS = '''description
  240. id
  241. title
  242. episodes {
  243. canonical
  244. id
  245. }'''
  246. def _get_doc(self, item):
  247. return item
  248. class HistoryTopicIE(AENetworksBaseIE):
  249. IE_NAME = 'history:topic'
  250. IE_DESC = 'History.com Topic'
  251. _VALID_URL = r'https?://(?:www\.)?history\.com/topics/[^/]+/(?P<id>[\w+-]+?)-video'
  252. _TESTS = [{
  253. 'url': 'https://www.history.com/topics/valentines-day/history-of-valentines-day-video',
  254. 'info_dict': {
  255. 'id': '40700995724',
  256. 'ext': 'mp4',
  257. 'title': "History of Valentine’s Day",
  258. 'description': 'md5:7b57ea4829b391995b405fa60bd7b5f7',
  259. 'timestamp': 1375819729,
  260. 'upload_date': '20130806',
  261. 'uploader': 'AENE-NEW',
  262. },
  263. 'params': {
  264. # m3u8 download
  265. 'skip_download': True,
  266. },
  267. 'add_ie': ['ThePlatform'],
  268. }]
  269. def _real_extract(self, url):
  270. display_id = self._match_id(url)
  271. return self.url_result(
  272. 'http://www.history.com/videos/' + display_id,
  273. AENetworksIE.ie_key())