]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/r7.py
Update changelog
[youtubedl] / 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 int_or_none
6
7
8 class R7IE(InfoExtractor):
9 _VALID_URL = r'''(?x)
10 https?://
11 (?:
12 (?:[a-zA-Z]+)\.r7\.com(?:/[^/]+)+/idmedia/|
13 noticias\.r7\.com(?:/[^/]+)+/[^/]+-|
14 player\.r7\.com/video/i/
15 )
16 (?P<id>[\da-f]{24})
17 '''
18 _TESTS = [{
19 'url': 'http://videos.r7.com/policiais-humilham-suspeito-a-beira-da-morte-morre-com-dignidade-/idmedia/54e7050b0cf2ff57e0279389.html',
20 'md5': '403c4e393617e8e8ddc748978ee8efde',
21 'info_dict': {
22 'id': '54e7050b0cf2ff57e0279389',
23 'ext': 'mp4',
24 'title': 'Policiais humilham suspeito à beira da morte: "Morre com dignidade"',
25 'description': 'md5:01812008664be76a6479aa58ec865b72',
26 'thumbnail': r're:^https?://.*\.jpg$',
27 'duration': 98,
28 'like_count': int,
29 'view_count': int,
30 },
31 }, {
32 'url': 'http://esportes.r7.com/videos/cigano-manda-recado-aos-fas/idmedia/4e176727b51a048ee6646a1b.html',
33 'only_matching': True,
34 }, {
35 'url': 'http://noticias.r7.com/record-news/video/representante-do-instituto-sou-da-paz-fala-sobre-fim-do-estatuto-do-desarmamento-5480fc580cf2285b117f438d/',
36 'only_matching': True,
37 }, {
38 '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',
39 'only_matching': True,
40 }]
41
42 def _real_extract(self, url):
43 video_id = self._match_id(url)
44
45 video = self._download_json(
46 'http://player-api.r7.com/video/i/%s' % video_id, video_id)
47
48 title = video['title']
49
50 formats = []
51 media_url_hls = video.get('media_url_hls')
52 if media_url_hls:
53 formats.extend(self._extract_m3u8_formats(
54 media_url_hls, video_id, 'mp4', entry_protocol='m3u8_native',
55 m3u8_id='hls', fatal=False))
56 media_url = video.get('media_url')
57 if media_url:
58 f = {
59 'url': media_url,
60 'format_id': 'http',
61 }
62 # m3u8 format always matches the http format, let's copy metadata from
63 # one to another
64 m3u8_formats = list(filter(
65 lambda f: f.get('vcodec') != 'none' and f.get('resolution') != 'multiple',
66 formats))
67 if len(m3u8_formats) == 1:
68 f_copy = m3u8_formats[0].copy()
69 f_copy.update(f)
70 f_copy['protocol'] = 'http'
71 f = f_copy
72 formats.append(f)
73 self._sort_formats(formats)
74
75 description = video.get('description')
76 thumbnail = video.get('thumb')
77 duration = int_or_none(video.get('media_duration'))
78 like_count = int_or_none(video.get('likes'))
79 view_count = int_or_none(video.get('views'))
80
81 return {
82 'id': video_id,
83 'title': title,
84 'description': description,
85 'thumbnail': thumbnail,
86 'duration': duration,
87 'like_count': like_count,
88 'view_count': view_count,
89 'formats': formats,
90 }
91
92
93 class R7ArticleIE(InfoExtractor):
94 _VALID_URL = r'https?://(?:[a-zA-Z]+)\.r7\.com/(?:[^/]+/)+[^/?#&]+-(?P<id>\d+)'
95 _TEST = {
96 'url': 'http://tv.r7.com/record-play/balanco-geral/videos/policiais-humilham-suspeito-a-beira-da-morte-morre-com-dignidade-16102015',
97 'only_matching': True,
98 }
99
100 @classmethod
101 def suitable(cls, url):
102 return False if R7IE.suitable(url) else super(R7ArticleIE, cls).suitable(url)
103
104 def _real_extract(self, url):
105 display_id = self._match_id(url)
106
107 webpage = self._download_webpage(url, display_id)
108
109 video_id = self._search_regex(
110 r'<div[^>]+(?:id=["\']player-|class=["\']embed["\'][^>]+id=["\'])([\da-f]{24})',
111 webpage, 'video id')
112
113 return self.url_result('http://player.r7.com/video/i/%s' % video_id, R7IE.ie_key())