7aea47ed52f7f64032034ab43d51dbe524bff2b3
[youtube-dl] / youtube_dl / extractor / webofstories.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import int_or_none
8
9
10 class WebOfStoriesIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.)?webofstories\.com/play/(?:[^/]+/)?(?P<id>[0-9]+)'
12     _VIDEO_DOMAIN = 'http://eu-mobile.webofstories.com/'
13     _GREAT_LIFE_STREAMER = 'rtmp://eu-cdn1.webofstories.com/cfx/st/'
14     _USER_STREAMER = 'rtmp://eu-users.webofstories.com/cfx/st/'
15     _TESTS = [{
16         'url': 'http://www.webofstories.com/play/hans.bethe/71',
17         'md5': '373e4dd915f60cfe3116322642ddf364',
18         'info_dict': {
19             'id': '4536',
20             'ext': 'mp4',
21             'title': 'The temperature of the sun',
22             'thumbnail': 're:^https?://.*\.jpg$',
23             'description': 'Hans Bethe talks about calculating the temperature of the sun',
24             'duration': 238,
25         }
26     }, {
27         'url': 'http://www.webofstories.com/play/55908',
28         'md5': '2985a698e1fe3211022422c4b5ed962c',
29         'info_dict': {
30             'id': '55908',
31             'ext': 'mp4',
32             'title': 'The story of Gemmata obscuriglobus',
33             'thumbnail': 're:^https?://.*\.jpg$',
34             'description': 'Planctomycete talks about The story of Gemmata obscuriglobus',
35             'duration': 169,
36         },
37         'skip': 'notfound',
38     }, {
39         # malformed og:title meta
40         'url': 'http://www.webofstories.com/play/54215?o=MS',
41         'info_dict': {
42             'id': '54215',
43             'ext': 'mp4',
44             'title': '"A Leg to Stand On"',
45             'thumbnail': 're:^https?://.*\.jpg$',
46             'description': 'Oliver Sacks talks about the death and resurrection of a limb',
47             'duration': 97,
48         },
49         'params': {
50             'skip_download': True,
51         },
52     }]
53
54     def _real_extract(self, url):
55         video_id = self._match_id(url)
56
57         webpage = self._download_webpage(url, video_id)
58         # Sometimes og:title meta is malformed
59         title = self._og_search_title(webpage, default=None) or self._html_search_regex(
60             r'(?s)<strong>Title:\s*</strong>(.+?)<', webpage, 'title')
61         description = self._html_search_meta('description', webpage)
62         thumbnail = self._og_search_thumbnail(webpage)
63
64         embed_params = [s.strip(" \r\n\t'") for s in self._search_regex(
65             r'(?s)\$\("#embedCode"\).html\(getEmbedCode\((.*?)\)',
66             webpage, 'embed params').split(',')]
67
68         (
69             _, speaker_id, story_id, story_duration,
70             speaker_type, great_life, _thumbnail, _has_subtitles,
71             story_filename, _story_order) = embed_params
72
73         is_great_life_series = great_life == 'true'
74         duration = int_or_none(story_duration)
75
76         # URL building, see: http://www.webofstories.com/scripts/player.js
77         ms_prefix = ''
78         if speaker_type.lower() == 'ms':
79             ms_prefix = 'mini_sites/'
80
81         if is_great_life_series:
82             mp4_url = '{0:}lives/{1:}/{2:}.mp4'.format(
83                 self._VIDEO_DOMAIN, speaker_id, story_filename)
84             rtmp_ext = 'flv'
85             streamer = self._GREAT_LIFE_STREAMER
86             play_path = 'stories/{0:}/{1:}'.format(
87                 speaker_id, story_filename)
88         else:
89             mp4_url = '{0:}{1:}{2:}/{3:}.mp4'.format(
90                 self._VIDEO_DOMAIN, ms_prefix, speaker_id, story_filename)
91             rtmp_ext = 'mp4'
92             streamer = self._USER_STREAMER
93             play_path = 'mp4:{0:}{1:}/{2}.mp4'.format(
94                 ms_prefix, speaker_id, story_filename)
95
96         formats = [{
97             'format_id': 'mp4_sd',
98             'url': mp4_url,
99         }, {
100             'format_id': 'rtmp_sd',
101             'page_url': url,
102             'url': streamer,
103             'ext': rtmp_ext,
104             'play_path': play_path,
105         }]
106
107         self._sort_formats(formats)
108
109         return {
110             'id': story_id,
111             'title': title,
112             'formats': formats,
113             'thumbnail': thumbnail,
114             'description': description,
115             'duration': duration,
116         }
117
118
119 class WebOfStoriesPlaylistIE(InfoExtractor):
120     _VALID_URL = r'https?://(?:www\.)?webofstories\.com/playAll/(?P<id>[^/]+)'
121     _TEST = {
122         'url': 'http://www.webofstories.com/playAll/donald.knuth',
123         'info_dict': {
124             'id': 'donald.knuth',
125             'title': 'Donald Knuth (Scientist)',
126         },
127         'playlist_mincount': 97,
128     }
129
130     def _real_extract(self, url):
131         playlist_id = self._match_id(url)
132
133         webpage = self._download_webpage(url, playlist_id)
134
135         entries = [
136             self.url_result('http://www.webofstories.com/play/%s' % video_number, 'WebOfStories')
137             for video_number in set(re.findall('href="/playAll/%s\?sId=(\d+)"' % playlist_id, webpage))
138         ]
139
140         title = self._search_regex(
141             r'<div id="speakerName">\s*<span>([^<]+)</span>',
142             webpage, 'speaker', default=None)
143         if title:
144             field = self._search_regex(
145                 r'<span id="primaryField">([^<]+)</span>',
146                 webpage, 'field', default=None)
147             if field:
148                 title += ' (%s)' % field
149
150         if not title:
151             title = self._search_regex(
152                 r'<title>Play\s+all\s+stories\s*-\s*([^<]+)\s*-\s*Web\s+of\s+Stories</title>',
153                 webpage, 'title')
154
155         return self.playlist_result(entries, playlist_id, title)