[streamango] Make title optional
[youtube-dl] / youtube_dl / extractor / streamango.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 (
8     determine_ext,
9     int_or_none,
10     js_to_json,
11 )
12
13
14 class StreamangoIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?streamango\.com/(?:f|embed)/(?P<id>[^/?#&]+)'
16     _TESTS = [{
17         'url': 'https://streamango.com/f/clapasobsptpkdfe/20170315_150006_mp4',
18         'md5': 'e992787515a182f55e38fc97588d802a',
19         'info_dict': {
20             'id': 'clapasobsptpkdfe',
21             'ext': 'mp4',
22             'title': '20170315_150006.mp4',
23         }
24     }, {
25         'url': 'https://streamango.com/embed/foqebrpftarclpob/asdf_asd_2_mp4',
26         'info_dict': {
27             'id': 'foqebrpftarclpob',
28             'ext': 'mp4',
29             'title': 'foqebrpftarclpob',
30         }
31     }, {
32         'url': 'https://streamango.com/embed/clapasobsptpkdfe/20170315_150006_mp4',
33         'only_matching': True,
34     }]
35
36     def _real_extract(self, url):
37         video_id = self._match_id(url)
38
39         webpage = self._download_webpage(url, video_id)
40
41         title = self._og_search_title(webpage, default=video_id)
42
43         formats = []
44         for format_ in re.findall(r'({[^}]*\bsrc\s*:\s*[^}]*})', webpage):
45             video = self._parse_json(
46                 format_, video_id, transform_source=js_to_json, fatal=False)
47             if not video:
48                 continue
49             src = video.get('src')
50             if not src:
51                 continue
52             ext = determine_ext(src, default_ext=None)
53             if video.get('type') == 'application/dash+xml' or ext == 'mpd':
54                 formats.extend(self._extract_mpd_formats(
55                     src, video_id, mpd_id='dash', fatal=False))
56             else:
57                 formats.append({
58                     'url': src,
59                     'ext': ext or 'mp4',
60                     'width': int_or_none(video.get('width')),
61                     'height': int_or_none(video.get('height')),
62                     'tbr': int_or_none(video.get('bitrate')),
63                 })
64         self._sort_formats(formats)
65
66         return {
67             'id': video_id,
68             'url': url,
69             'title': title,
70             'formats': formats,
71         }