Merge remote-tracking branch 'duncankl/airmozilla'
[youtube-dl] / youtube_dl / extractor / r7.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     js_to_json,
7     unescapeHTML,
8     int_or_none,
9 )
10
11
12 class R7IE(InfoExtractor):
13     _VALID_URL = r'''(?x)https?://
14                         (?:
15                             (?:[a-zA-Z]+)\.r7\.com(?:/[^/]+)+/idmedia/|
16                             noticias\.r7\.com(?:/[^/]+)+/[^/]+-|
17                             player\.r7\.com/video/i/
18                         )
19                         (?P<id>[\da-f]{24})
20                         '''
21     _TESTS = [{
22         'url': 'http://videos.r7.com/policiais-humilham-suspeito-a-beira-da-morte-morre-com-dignidade-/idmedia/54e7050b0cf2ff57e0279389.html',
23         'md5': '403c4e393617e8e8ddc748978ee8efde',
24         'info_dict': {
25             'id': '54e7050b0cf2ff57e0279389',
26             'ext': 'mp4',
27             'title': 'Policiais humilham suspeito à beira da morte: "Morre com dignidade"',
28             'thumbnail': 're:^https?://.*\.jpg$',
29             'duration': 98,
30             'like_count': int,
31             'view_count': int,
32         },
33     }, {
34         'url': 'http://esportes.r7.com/videos/cigano-manda-recado-aos-fas/idmedia/4e176727b51a048ee6646a1b.html',
35         'only_matching': True,
36     }, {
37         'url': 'http://noticias.r7.com/record-news/video/representante-do-instituto-sou-da-paz-fala-sobre-fim-do-estatuto-do-desarmamento-5480fc580cf2285b117f438d/',
38         'only_matching': True,
39     }, {
40         'url': 'http://player.r7.com/video/i/54e7050b0cf2ff57e0279389?play=true&video=http://vsh.r7.com/54e7050b0cf2ff57e0279389/ER7_RE_BG_MORTE_JOVENS_570kbps_2015-02-2009f17818-cc82-4c8f-86dc-89a66934e633-ATOS_copy.mp4&linkCallback=http://videos.r7.com/policiais-humilham-suspeito-a-beira-da-morte-morre-com-dignidade-/idmedia/54e7050b0cf2ff57e0279389.html&thumbnail=http://vtb.r7.com/ER7_RE_BG_MORTE_JOVENS_570kbps_2015-02-2009f17818-cc82-4c8f-86dc-89a66934e633-thumb.jpg&idCategory=192&share=true&layout=full&full=true',
41         'only_matching': True,
42     }]
43
44     def _real_extract(self, url):
45         video_id = self._match_id(url)
46
47         webpage = self._download_webpage(
48             'http://player.r7.com/video/i/%s' % video_id, video_id)
49
50         item = self._parse_json(js_to_json(self._search_regex(
51             r'(?s)var\s+item\s*=\s*({.+?});', webpage, 'player')), video_id)
52
53         title = unescapeHTML(item['title'])
54         thumbnail = item.get('init', {}).get('thumbUri')
55         duration = None
56
57         statistics = item.get('statistics', {})
58         like_count = int_or_none(statistics.get('likes'))
59         view_count = int_or_none(statistics.get('views'))
60
61         formats = []
62         for format_key, format_dict in item['playlist'][0].items():
63             src = format_dict.get('src')
64             if not src:
65                 continue
66             format_id = format_dict.get('format') or format_key
67             if duration is None:
68                 duration = format_dict.get('duration')
69             if '.f4m' in src:
70                 formats.extend(self._extract_f4m_formats(src, video_id, preference=-1))
71             elif src.endswith('.m3u8'):
72                 formats.extend(self._extract_m3u8_formats(src, video_id, 'mp4', preference=-2))
73             else:
74                 formats.append({
75                     'url': src,
76                     'format_id': format_id,
77                 })
78         self._sort_formats(formats)
79
80         return {
81             'id': video_id,
82             'title': title,
83             'thumbnail': thumbnail,
84             'duration': duration,
85             'like_count': like_count,
86             'view_count': view_count,
87             'formats': formats,
88         }