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