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