]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/webofstories.py
Initiate new release.
[youtubedl] / 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 {
17 'url': 'http://www.webofstories.com/play/hans.bethe/71',
18 'md5': '373e4dd915f60cfe3116322642ddf364',
19 'info_dict': {
20 'id': '4536',
21 'ext': 'mp4',
22 'title': 'The temperature of the sun',
23 'thumbnail': 're:^https?://.*\.jpg$',
24 'description': 'Hans Bethe talks about calculating the temperature of the sun',
25 'duration': 238,
26 }
27 },
28 {
29 'url': 'http://www.webofstories.com/play/55908',
30 'md5': '2985a698e1fe3211022422c4b5ed962c',
31 'info_dict': {
32 'id': '55908',
33 'ext': 'mp4',
34 'title': 'The story of Gemmata obscuriglobus',
35 'thumbnail': 're:^https?://.*\.jpg$',
36 'description': 'Planctomycete talks about The story of Gemmata obscuriglobus',
37 'duration': 169,
38 }
39 },
40 ]
41
42 def _real_extract(self, url):
43 video_id = self._match_id(url)
44
45 webpage = self._download_webpage(url, video_id)
46 title = self._og_search_title(webpage)
47 description = self._html_search_meta('description', webpage)
48 thumbnail = self._og_search_thumbnail(webpage)
49
50 embed_params = [s.strip(" \r\n\t'") for s in self._search_regex(
51 r'(?s)\$\("#embedCode"\).html\(getEmbedCode\((.*?)\)',
52 webpage, 'embed params').split(',')]
53
54 (
55 _, speaker_id, story_id, story_duration,
56 speaker_type, great_life, _thumbnail, _has_subtitles,
57 story_filename, _story_order) = embed_params
58
59 is_great_life_series = great_life == 'true'
60 duration = int_or_none(story_duration)
61
62 # URL building, see: http://www.webofstories.com/scripts/player.js
63 ms_prefix = ''
64 if speaker_type.lower() == 'ms':
65 ms_prefix = 'mini_sites/'
66
67 if is_great_life_series:
68 mp4_url = '{0:}lives/{1:}/{2:}.mp4'.format(
69 self._VIDEO_DOMAIN, speaker_id, story_filename)
70 rtmp_ext = 'flv'
71 streamer = self._GREAT_LIFE_STREAMER
72 play_path = 'stories/{0:}/{1:}'.format(
73 speaker_id, story_filename)
74 else:
75 mp4_url = '{0:}{1:}{2:}/{3:}.mp4'.format(
76 self._VIDEO_DOMAIN, ms_prefix, speaker_id, story_filename)
77 rtmp_ext = 'mp4'
78 streamer = self._USER_STREAMER
79 play_path = 'mp4:{0:}{1:}/{2}.mp4'.format(
80 ms_prefix, speaker_id, story_filename)
81
82 formats = [{
83 'format_id': 'mp4_sd',
84 'url': mp4_url,
85 }, {
86 'format_id': 'rtmp_sd',
87 'page_url': url,
88 'url': streamer,
89 'ext': rtmp_ext,
90 'play_path': play_path,
91 }]
92
93 self._sort_formats(formats)
94
95 return {
96 'id': story_id,
97 'title': title,
98 'formats': formats,
99 'thumbnail': thumbnail,
100 'description': description,
101 'duration': duration,
102 }
103
104
105 class WebOfStoriesPlaylistIE(InfoExtractor):
106 _VALID_URL = r'https?://(?:www\.)?webofstories\.com/playAll/(?P<id>[^/]+)'
107 _TEST = {
108 'url': 'http://www.webofstories.com/playAll/donald.knuth',
109 'info_dict': {
110 'id': 'donald.knuth',
111 'title': 'Donald Knuth (Scientist)',
112 },
113 'playlist_mincount': 97,
114 }
115
116 def _real_extract(self, url):
117 playlist_id = self._match_id(url)
118
119 webpage = self._download_webpage(url, playlist_id)
120
121 entries = [
122 self.url_result('http://www.webofstories.com/play/%s' % video_number, 'WebOfStories')
123 for video_number in set(re.findall('href="/playAll/%s\?sId=(\d+)"' % playlist_id, webpage))
124 ]
125
126 title = self._search_regex(
127 r'<div id="speakerName">\s*<span>([^<]+)</span>',
128 webpage, 'speaker', default=None)
129 if title:
130 field = self._search_regex(
131 r'<span id="primaryField">([^<]+)</span>',
132 webpage, 'field', default=None)
133 if field:
134 title += ' (%s)' % field
135
136 if not title:
137 title = self._search_regex(
138 r'<title>Play\s+all\s+stories\s*-\s*([^<]+)\s*-\s*Web\s+of\s+Stories</title>',
139 webpage, 'title')
140
141 return self.playlist_result(entries, playlist_id, title)