[dplay] Add extractor (closes #7515)
[youtube-dl] / youtube_dl / extractor / dplay.py
1 from __future__ import unicode_literals
2
3 import time
4
5 from .common import InfoExtractor
6 from ..utils import int_or_none
7
8
9 class DPlayIE(InfoExtractor):
10     _VALID_URL = r'http://www\.dplay\.se/[^/]+/(?P<id>[^/?#]+)'
11
12     _TEST = {
13         'url': 'http://www.dplay.se/nugammalt-77-handelser-som-format-sverige/season-1-svensken-lar-sig-njuta-av-livet/',
14         'info_dict': {
15             'id': '3172',
16             'ext': 'mp4',
17             'display_id': 'season-1-svensken-lar-sig-njuta-av-livet',
18             'title': 'Svensken lär sig njuta av livet',
19             'duration': 2650,
20         },
21     }
22
23     def _real_extract(self, url):
24         display_id = self._match_id(url)
25         webpage = self._download_webpage(url, display_id)
26         video_id = self._search_regex(
27             r'data-video-id="(\d+)"', webpage, 'video id')
28
29         info = self._download_json(
30             'http://www.dplay.se/api/v2/ajax/videos?video_id=' + video_id,
31             video_id)['data'][0]
32
33         self._set_cookie(
34             'secure.dplay.se', 'dsc-geo',
35             '{"countryCode":"NL","expiry":%d}' % ((time.time() + 20 * 60) * 1000))
36         # TODO: consider adding support for 'stream_type=hds', it seems to
37         # require setting some cookies
38         manifest_url = self._download_json(
39             'https://secure.dplay.se/secure/api/v2/user/authorization/stream/%s?stream_type=hls' % video_id,
40             video_id, 'Getting manifest url for hls stream')['hls']
41         formats = self._extract_m3u8_formats(
42             manifest_url, video_id, ext='mp4', entry_protocol='m3u8_native')
43
44         return {
45             'id': video_id,
46             'display_id': display_id,
47             'title': info['title'],
48             'formats': formats,
49             'duration': int_or_none(info.get('video_metadata_length'), scale=1000),
50         }