]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/konserthusetplay.py
[yandexmusic] Capture and output API errors
[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         FORMAT_ID_REGEX = r'_([^_]+)_h264m\.mp4'
48
49         formats = []
50
51         fallback_url = source.get('fallbackUrl')
52         fallback_format_id = None
53         if fallback_url:
54             fallback_format_id = self._search_regex(
55                 FORMAT_ID_REGEX, fallback_url, 'format id', default=None)
56
57         connection_url = (player_config.get('rtmp', {}).get(
58             'netConnectionUrl') or player_config.get(
59             'plugins', {}).get('bwcheck', {}).get('netConnectionUrl'))
60         if connection_url:
61             for f in source['bitrates']:
62                 video_url = f.get('url')
63                 if not video_url:
64                     continue
65                 format_id = self._search_regex(
66                     FORMAT_ID_REGEX, video_url, 'format id', default=None)
67                 f_common = {
68                     'vbr': int_or_none(f.get('bitrate')),
69                     'width': int_or_none(f.get('width')),
70                     'height': int_or_none(f.get('height')),
71                 }
72                 f = f_common.copy()
73                 f.update({
74                     'url': connection_url,
75                     'play_path': video_url,
76                     'format_id': 'rtmp-%s' % format_id if format_id else 'rtmp',
77                     'ext': 'flv',
78                 })
79                 formats.append(f)
80                 if format_id and format_id == fallback_format_id:
81                     f = f_common.copy()
82                     f.update({
83                         'url': fallback_url,
84                         'format_id': 'http-%s' % format_id if format_id else 'http',
85                     })
86                     formats.append(f)
87
88         if not formats and fallback_url:
89             formats.append({
90                 'url': fallback_url,
91             })
92
93         self._sort_formats(formats)
94
95         title = player_config.get('title') or media['title']
96         description = player_config.get('mediaInfo', {}).get('description')
97         thumbnail = media.get('image')
98         duration = float_or_none(media.get('duration'), 1000)
99
100         return {
101             'id': video_id,
102             'title': title,
103             'description': description,
104             'thumbnail': thumbnail,
105             'duration': duration,
106             'formats': formats,
107         }