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