[youtube] fix hd720 format position
[youtube-dl] / youtube_dl / extractor / breakcom.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from .youtube import YoutubeIE
7 from ..compat import compat_str
8 from ..utils import int_or_none
9
10
11 class BreakIE(InfoExtractor):
12     _VALID_URL = r'https?://(?:www\.)?break\.com/video/(?P<display_id>[^/]+?)(?:-(?P<id>\d+))?(?:[/?#&]|$)'
13     _TESTS = [{
14         'url': 'http://www.break.com/video/when-girls-act-like-guys-2468056',
15         'info_dict': {
16             'id': '2468056',
17             'ext': 'mp4',
18             'title': 'When Girls Act Like D-Bags',
19             'age_limit': 13,
20         },
21     }, {
22         # youtube embed
23         'url': 'http://www.break.com/video/someone-forgot-boat-brakes-work',
24         'info_dict': {
25             'id': 'RrrDLdeL2HQ',
26             'ext': 'mp4',
27             'title': 'Whale Watching Boat Crashing Into San Diego Dock',
28             'description': 'md5:afc1b2772f0a8468be51dd80eb021069',
29             'upload_date': '20160331',
30             'uploader': 'Steve Holden',
31             'uploader_id': 'sdholden07',
32         },
33         'params': {
34             'skip_download': True,
35         }
36     }, {
37         'url': 'http://www.break.com/video/ugc/baby-flex-2773063',
38         'only_matching': True,
39     }]
40
41     def _real_extract(self, url):
42         display_id, video_id = re.match(self._VALID_URL, url).groups()
43
44         webpage = self._download_webpage(url, display_id)
45
46         youtube_url = YoutubeIE._extract_url(webpage)
47         if youtube_url:
48             return self.url_result(youtube_url, ie=YoutubeIE.ie_key())
49
50         content = self._parse_json(
51             self._search_regex(
52                 r'(?s)content["\']\s*:\s*(\[.+?\])\s*[,\n]', webpage,
53                 'content'),
54             display_id)
55
56         formats = []
57         for video in content:
58             video_url = video.get('url')
59             if not video_url or not isinstance(video_url, compat_str):
60                 continue
61             bitrate = int_or_none(self._search_regex(
62                 r'(\d+)_kbps', video_url, 'tbr', default=None))
63             formats.append({
64                 'url': video_url,
65                 'format_id': 'http-%d' % bitrate if bitrate else 'http',
66                 'tbr': bitrate,
67             })
68         self._sort_formats(formats)
69
70         title = self._search_regex(
71             (r'title["\']\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1',
72              r'<h1[^>]*>(?P<value>[^<]+)'), webpage, 'title', group='value')
73
74         def get(key, name):
75             return int_or_none(self._search_regex(
76                 r'%s["\']\s*:\s*["\'](\d+)' % key, webpage, name,
77                 default=None))
78
79         age_limit = get('ratings', 'age limit')
80         video_id = video_id or get('pid', 'video id') or display_id
81
82         return {
83             'id': video_id,
84             'display_id': display_id,
85             'title': title,
86             'thumbnail': self._og_search_thumbnail(webpage),
87             'age_limit': age_limit,
88             'formats': formats,
89         }