[aenetworks] extract history topic playlist title
[youtube-dl] / youtube_dl / extractor / aenetworks.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     smuggle_url,
8     update_url_query,
9     unescapeHTML,
10     extract_attributes,
11     get_element_by_attribute,
12 )
13 from ..compat import (
14     compat_urlparse,
15 )
16
17
18 class AENetworksBaseIE(InfoExtractor):
19     def theplatform_url_result(self, theplatform_url, video_id, query):
20         return {
21             '_type': 'url_transparent',
22             'id': video_id,
23             'url': smuggle_url(
24                 update_url_query(theplatform_url, query),
25                 {
26                     'sig': {
27                         'key': 'crazyjava',
28                         'secret': 's3cr3t'
29                     },
30                     'force_smil_url': True
31                 }),
32             'ie_key': 'ThePlatform',
33         }
34
35
36 class AENetworksIE(AENetworksBaseIE):
37     IE_NAME = 'aenetworks'
38     IE_DESC = 'A+E Networks: A&E, Lifetime, History.com, FYI Network'
39     _VALID_URL = r'https?://(?:www\.)?(?:(?:history|aetv|mylifetime)\.com|fyi\.tv)/shows/(?P<id>[^/]+(?:/[^/]+){0,2})'
40     _TESTS = [{
41         'url': 'http://www.history.com/shows/mountain-men/season-1/episode-1',
42         'md5': '8ff93eb073449f151d6b90c0ae1ef0c7',
43         'info_dict': {
44             'id': '22253814',
45             'ext': 'mp4',
46             'title': 'Winter Is Coming',
47             'description': 'md5:641f424b7a19d8e24f26dea22cf59d74',
48             'timestamp': 1338306241,
49             'upload_date': '20120529',
50             'uploader': 'AENE-NEW',
51         },
52         'add_ie': ['ThePlatform'],
53     }, {
54         'url': 'http://www.history.com/shows/ancient-aliens/season-1',
55         'info_dict': {
56             'id': '71889446852',
57         },
58         'playlist_mincount': 5,
59     }, {
60         'url': 'http://www.mylifetime.com/shows/atlanta-plastic',
61         'info_dict': {
62             'id': 'SERIES4317',
63             'title': 'Atlanta Plastic',
64         },
65         'playlist_mincount': 2,
66     }, {
67         'url': 'http://www.aetv.com/shows/duck-dynasty/season-9/episode-1',
68         'only_matching': True
69     }, {
70         'url': 'http://www.fyi.tv/shows/tiny-house-nation/season-1/episode-8',
71         'only_matching': True
72     }, {
73         'url': 'http://www.mylifetime.com/shows/project-runway-junior/season-1/episode-6',
74         'only_matching': True
75     }]
76
77     def _real_extract(self, url):
78         display_id = self._match_id(url)
79         webpage = self._download_webpage(url, display_id)
80         url_parts = display_id.split('/')
81         url_parts_len = len(url_parts)
82         if url_parts_len == 1:
83             entries = []
84             for season_url_path in re.findall(r'(?s)<li[^>]+data-href="(/shows/%s/season-\d+)"' % url_parts[0], webpage):
85                 entries.append(self.url_result(
86                     compat_urlparse.urljoin(url, season_url_path), 'AENetworks'))
87             return self.playlist_result(
88                 entries, self._html_search_meta('aetn:SeriesId', webpage),
89                 self._html_search_meta('aetn:SeriesTitle', webpage))
90         elif url_parts_len == 2:
91             entries = []
92             for episode_item in re.findall(r'(?s)<div[^>]+class="[^"]*episode-item[^"]*"[^>]*>', webpage):
93                 episode_attributes = extract_attributes(episode_item)
94                 episode_url = compat_urlparse.urljoin(
95                     url, episode_attributes['data-canonical'])
96                 entries.append(self.url_result(
97                     episode_url, 'AENetworks',
98                     episode_attributes['data-videoid']))
99             return self.playlist_result(
100                 entries, self._html_search_meta('aetn:SeasonId', webpage))
101         else:
102             video_id = self._html_search_meta('aetn:VideoID', webpage)
103             media_url = self._search_regex(
104                 r"media_url\s*=\s*'([^']+)'", webpage, 'video url')
105
106             info = self._search_json_ld(webpage, video_id, fatal=False)
107             info.update(self.theplatform_url_result(
108                 media_url, video_id, {
109                     'mbr': 'true',
110                     'assetTypes': 'medium_video_s3'
111                 }))
112             return info
113
114
115 class HistoryTopicIE(AENetworksBaseIE):
116     IE_NAME = 'history:topic'
117     IE_DESC = 'History.com Topic'
118     _VALID_URL = r'https?://(?:www\.)?history\.com/topics/(?:[^/]+/)?(?P<topic_id>[^/]+)/videos(?:/(?P<display_id>[^/?#]+))?'
119     _TESTS = [{
120         'url': 'http://www.history.com/topics/valentines-day/history-of-valentines-day/videos/bet-you-didnt-know-valentines-day?m=528e394da93ae&s=undefined&f=1&free=false',
121         'info_dict': {
122             'id': '40700995724',
123             'ext': 'mp4',
124             'title': "Bet You Didn't Know: Valentine's Day",
125             'description': 'md5:7b57ea4829b391995b405fa60bd7b5f7',
126             'timestamp': 1375819729,
127             'upload_date': '20130806',
128             'uploader': 'AENE-NEW',
129         },
130         'params': {
131             # m3u8 download
132             'skip_download': True,
133         },
134         'add_ie': ['ThePlatform'],
135     }, {
136         'url': 'http://www.history.com/topics/world-war-i/world-war-i-history/videos',
137         'info_dict':
138         {
139             'id': 'world-war-i-history',
140             'title': 'World War I History',
141         },
142         'playlist_mincount': 24,
143     }, {
144         'url': 'http://www.history.com/topics/world-war-i-history/videos',
145         'only_matching': True,
146     }]
147
148     def _real_extract(self, url):
149         topic_id, display_id = re.match(self._VALID_URL, url).groups()
150         if display_id:
151             webpage = self._download_webpage(url, display_id)
152             release_url, video_id = re.search(r"_videoPlayer.play\('([^']+)'\s*,\s*'[^']+'\s*,\s*'(\d+)'\)", webpage).groups()
153             release_url = unescapeHTML(release_url)
154
155             return self.theplatform_url_result(
156                 release_url, video_id, {
157                     'mbr': 'true',
158                     'switch': 'hls'
159                 })
160         else:
161             webpage = self._download_webpage(url, topic_id)
162             entries = []
163             for episode_item in re.findall(r'<a.+?data-release-url="[^"]+"[^>]*>', webpage):
164                 video_attributes = extract_attributes(episode_item)
165                 entries.append(self.theplatform_url_result(
166                     video_attributes['data-release-url'], video_attributes['data-id'], {
167                         'mbr': 'true',
168                         'switch': 'hls'
169                     }))
170             return self.playlist_result(entries, topic_id, get_element_by_attribute('class', 'show-title', webpage))