9bb8d42e6e5f5936e4cecfa18c2a3c52d0283e81
[youtube-dl] / youtube_dl / extractor / bbcnews.py
1 from __future__ import unicode_literals
2
3 from .common import InfoExtractor
4 from ..utils import (
5     ExtractorError,
6     int_or_none,
7 )
8 from ..compat import compat_HTTPError
9 import re
10 from .bbccouk import BBCCoUkIE
11
12 class BBCNewsIE(BBCCoUkIE):
13     IE_NAME = 'bbc.com'
14     IE_DESC = 'BBC news'
15     _VALID_URL = r'https?://(?:www\.)?(?:bbc\.co\.uk|bbc\.com)/news/(?P<id>[^/]+)'
16
17     mediaselector_url = 'http://open.live.bbc.co.uk/mediaselector/4/mtis/stream/%s'
18
19     _TESTS = [{
20         'url': 'http://www.bbc.com/news/world-europe-32668511',
21         'info_dict': {
22             'id': 'world-europe-32668511',
23             'title': 'Russia stages massive WW2 parade despite Western boycott',
24         },
25         'playlist_count': 2,
26     },{
27         'url': 'http://www.bbc.com/news/business-28299555',
28         'info_dict': {
29             'id': 'business-28299555',
30             'title': 'Farnborough Airshow: Video highlights',
31         },
32         'playlist_count': 9,
33     },{
34         'url': 'http://www.bbc.com/news/world-europe-32041533',
35         'note': 'Video',
36         'info_dict': {
37             'id': 'p02mprgb',
38             'ext': 'mp4',
39             'title': 'Aerial footage showed the site of the crash in the Alps - courtesy BFM TV',
40             'description': 'Germanwings plane crash site in aerial video - Aerial footage showed the site of the crash in the Alps - courtesy BFM TV',
41             'duration': 47,
42         },
43         'params': {
44             'skip_download': True,
45         }
46     }]
47
48     def _duration_str2int(self, str):
49         if not str:
50             return None
51         ret = re.match(r'^\d+$', str)
52         if ret:
53             return int(ret.group(0))
54         ret = re.match(r'PT((?P<h>\d+)H)?((?P<m>\d+)M)?(?P<s>\d+)S$', str)
55         if ret:
56             total=int(ret.group('s'))
57             if ret.group('m'):
58                 total+=(int(ret.group('m'))*60)
59             if ret.group('h'):
60                 total+=(int(ret.group('h'))*3600)
61             return total
62         return None
63
64     def _real_extract(self, url):
65         list_id = self._match_id(url)
66         webpage = self._download_webpage(url, list_id)
67
68         list_title = self._html_search_regex(r'<title>(.*?)(?:\s*-\s*BBC News)?</title>', webpage, 'list title')
69
70         pubdate = self._html_search_regex(r'"datePublished":\s*"(\d+-\d+-\d+)', webpage, 'date', default=None)
71         if pubdate:
72            pubdate = pubdate.replace('-','')
73
74         ret = []
75         # works with bbc.com/news/something-something-123456 articles
76         matches = re.findall(r"data-media-meta='({[^']+})'", webpage)
77         if not matches:
78            # stubbornly generic extractor for {json with "image":{allvideoshavethis},etc}
79            # in http://www.bbc.com/news/video_and_audio/international
80            matches = re.findall(r"({[^{}]+image\":{[^}]+}[^}]+})", webpage)
81         if not matches:
82            raise ExtractorError('No video found', expected=True)
83
84         for ent in matches:
85             jent = self._parse_json(ent,list_id)
86
87             programme_id = jent.get('externalId',None)
88             xml_url = jent.get('href', None)
89
90             title = jent['caption']
91             duration = self._duration_str2int(jent.get('duration',None))
92             description = list_title + ' - ' + jent.get('caption','')
93             thumbnail = None
94             if jent.has_key('image'):
95                thumbnail=jent['image'].get('href',None)
96
97             if programme_id:
98                formats, subtitles = self._download_media_selector(programme_id)
99             elif xml_url:
100                # Cheap fallback
101                # http://playlists.bbc.co.uk/news/(list_id)[ABC..]/playlist.sxml
102                xml = self._download_webpage(xml_url, programme_id, 'Downloading playlist.sxml for externalId (fallback)')
103                programme_id = self._search_regex(r'<mediator [^>]*identifier="(.+?)"', xml, 'playlist.sxml (externalId fallback)')
104                formats, subtitles = self._download_media_selector(programme_id)
105             else:
106                raise ExtractorError('data-media-meta entry has no externalId or href value.')
107                
108             self._sort_formats(formats)
109
110             ret.append( {
111                 'id': programme_id,
112                 'uploader': 'BBC News',
113                 'upload_date': pubdate,
114                 'title': title,
115                 'description': description,
116                 'thumbnail': thumbnail,
117                 'duration': duration,
118                 'formats': formats,
119                 'subtitles': subtitles,
120             } )
121
122         if len(ret) > 0:
123            return self.playlist_result(ret, list_id, list_title)
124         raise ExtractorError('No video found', expected=True)