[konserthusetplay] Improve _VALID_URL
[youtube-dl] / youtube_dl / extractor / konserthusetplay.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     float_or_none,
7     int_or_none,
8 )
9
10
11 class KonserthusetPlayIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?konserthusetplay\.se/\?.*\bm=(?P<id>[^&]+)'
13     _TEST = {
14         'url': 'http://www.konserthusetplay.se/?m=CKDDnlCY-dhWAAqiMERd-A',
15         'info_dict': {
16             'id': 'CKDDnlCY-dhWAAqiMERd-A',
17             'ext': 'flv',
18             'title': 'Orkesterns instrument: Valthornen',
19             'description': 'md5:f10e1f0030202020396a4d712d2fa827',
20             'thumbnail': 're:^https?://.*$',
21             'duration': 398.8,
22         },
23         'params': {
24             # rtmp download
25             'skip_download': True,
26         },
27     }
28
29     def _real_extract(self, url):
30         video_id = self._match_id(url)
31
32         webpage = self._download_webpage(url, video_id)
33
34         e = self._search_regex(
35             r'https?://csp\.picsearch\.com/rest\?.*\be=(.+?)[&"\']', webpage, 'e')
36
37         rest = self._download_json(
38             'http://csp.picsearch.com/rest?e=%s&containerId=mediaplayer&i=object' % e,
39             video_id, transform_source=lambda s: s[s.index('{'):s.rindex('}') + 1])
40
41         media = rest['media']
42         player_config = media['playerconfig']
43         playlist = player_config['playlist']
44
45         source = next(f for f in playlist if f.get('bitrates'))
46
47         connection_url = (player_config.get('rtmp', {}).get(
48             'netConnectionUrl') or player_config.get(
49             'plugins', {}).get('bwcheck', {}).get('netConnectionUrl'))
50
51         FORMAT_ID_REGEX = r'_([^_]+)_h264m\.mp4'
52
53         formats = []
54
55         fallback_url = source.get('fallbackUrl')
56         fallback_format_id = None
57         if fallback_url:
58             fallback_format_id = self._search_regex(
59                 FORMAT_ID_REGEX, fallback_url, 'format id', default=None)
60
61         if connection_url:
62             for f in source['bitrates']:
63                 video_url = f.get('url')
64                 if not video_url:
65                     continue
66                 format_id = self._search_regex(
67                     FORMAT_ID_REGEX, video_url, 'format id', default=None)
68                 f_common = {
69                     'vbr': int_or_none(f.get('bitrate')),
70                     'width': int_or_none(f.get('width')),
71                     'height': int_or_none(f.get('height')),
72                 }
73                 f = f_common.copy()
74                 f.update({
75                     'url': connection_url,
76                     'play_path': video_url,
77                     'format_id': 'rtmp-%s' % format_id if format_id else 'rtmp',
78                     'ext': 'flv',
79                 })
80                 formats.append(f)
81                 if format_id and format_id == fallback_format_id:
82                     f = f_common.copy()
83                     f.update({
84                         'url': fallback_url,
85                         'format_id': 'http-%s' % format_id if format_id else 'http',
86                     })
87                     formats.append(f)
88
89         if not formats and fallback_url:
90             formats.append({
91                 'url': fallback_url,
92             })
93
94         self._sort_formats(formats)
95
96         title = player_config.get('title') or media['title']
97         description = player_config.get('mediaInfo', {}).get('description')
98         thumbnail = media.get('image')
99         duration = float_or_none(media.get('duration'), 1000)
100
101         return {
102             'id': video_id,
103             'title': title,
104             'description': description,
105             'thumbnail': thumbnail,
106             'duration': duration,
107             'formats': formats,
108         }