Merge branch 'sportbox-fix' of https://github.com/maddoger/youtube-dl into maddoger...
[youtube-dl] / youtube_dl / extractor / sportbox.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     parse_duration,
9     parse_iso8601,
10 )
11
12
13 class SportBoxIE(InfoExtractor):
14     _VALID_URL = r'https?://news\.sportbox\.ru/(?:[^/]+/)+spbvideo_NI\d+_(?P<display_id>.+)'
15     _TESTS = [
16         {
17             'url': 'http://news.sportbox.ru/Vidy_sporta/Avtosport/Rossijskij/spbvideo_NI483529_Gonka-2-zaezd-Obyedinenniy-2000-klassi-Turing-i-S',            
18             'md5': 'ff56a598c2cf411a9a38a69709e97079',
19             'info_dict': {
20                 'id': '80822',
21                 'ext': 'mp4',
22                 'title': 'Гонка 2  заезд ««Объединенный 2000»: классы Туринг и Супер-продакшн',
23                 'description': 'md5:81715fa9c4ea3d9e7915dc8180c778ed',
24                 'thumbnail': 're:^https?://.*\.jpg$',
25                 'timestamp': 1411896237,
26                 'upload_date': '20140928',
27                 'duration': 4846,
28             },
29             'params': {
30                 # m3u8 download
31                 'skip_download': True,
32             },
33         }, 
34         {
35             'url': 'http://news.sportbox.ru/video/no_ads/spbvideo_NI536574_V_Novorossijske_proshel_detskij_turnir_Pole_slavy_bojevoj?ci=211355',            
36             'md5': 'ff56a598c2cf411a9a38a69709e97079',
37             'info_dict': {
38                 'id': '211355',
39                 'ext': 'mp4',
40                 'title': 'В Новороссийске прошел детский турнир «Поле славы боевой»',
41                 'description': '16 детских коллективов приняли участие в суперфинале турнира «Поле славы боевой».',
42                 'thumbnail': 're:^https?://.*\.jpg$',
43                 'timestamp': 1426237001,
44                 'upload_date': '20150313',
45                 'duration': 292,
46             },
47             'params': {
48                 # m3u8 download
49                 'skip_download': True,
50             },
51         }, 
52         {
53             'url': 'http://news.sportbox.ru/Vidy_sporta/billiard/spbvideo_NI486287_CHempionat-mira-po-dinamichnoy-piramide-4',
54             'only_matching': True,
55         },
56     ]
57
58     def _real_extract(self, url):
59         mobj = re.match(self._VALID_URL, url)
60         display_id = mobj.group('display_id')
61
62         webpage = self._download_webpage(url, display_id)
63
64         sobj = re.search(r'src="/vdl/player/(?P<media_type>\w+)/(?P<video_id>\d+)"', webpage)
65         if (sobj):
66             video_id = sobj.group('video_id')
67             media_type = sobj.group('media_type')
68         else:
69             raise RegexNotFoundError('Unable to extract video_id')
70
71         player = self._download_webpage(
72             'http://news.sportbox.ru/vdl/player/%s/%s' % (media_type, video_id),
73             display_id, 'Downloading player webpage')
74
75         hls = self._search_regex(
76             r"sportboxPlayer\.jwplayer_common_params\.file\s*=\s*['\"]+([^\"]+)['\"]+", player, 'hls file')
77
78         formats = self._extract_m3u8_formats(hls, display_id, 'mp4')
79
80         title = self._html_search_regex(
81             r'<h1 itemprop="name">([^<]+)</h1>', webpage, 'title')
82         description = self._html_search_regex(
83             r'(?s)<div itemprop="description">(.+?)</div>', webpage, 'description', fatal=False)
84         thumbnail = self._og_search_thumbnail(webpage)
85         timestamp = parse_iso8601(self._search_regex(
86             r'<span itemprop="uploadDate">([^<]+)</span>', webpage, 'timestamp', fatal=False))
87         duration = parse_duration(self._html_search_regex(
88             r'<meta itemprop="duration" content="PT([^"]+)">', webpage, 'duration', fatal=False))
89
90         return {
91             'id': video_id,
92             'display_id': display_id,
93             'title': title,
94             'description': description,
95             'thumbnail': thumbnail,
96             'timestamp': timestamp,
97             'duration': duration,
98             'formats': formats,
99         }