[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / usatoday.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     get_element_by_attribute,
8     parse_duration,
9     try_get,
10     update_url_query,
11 )
12 from ..compat import compat_str
13
14
15 class USATodayIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?usatoday\.com/(?:[^/]+/)*(?P<id>[^?/#]+)'
17     _TESTS = [{
18         # Brightcove Partner ID = 29906170001
19         'url': 'http://www.usatoday.com/media/cinematic/video/81729424/us-france-warn-syrian-regime-ahead-of-new-peace-talks/',
20         'md5': '033587d2529dc3411a1ab3644c3b8827',
21         'info_dict': {
22             'id': '4799374959001',
23             'ext': 'mp4',
24             'title': 'US, France warn Syrian regime ahead of new peace talks',
25             'timestamp': 1457891045,
26             'description': 'md5:7e50464fdf2126b0f533748d3c78d58f',
27             'uploader_id': '29906170001',
28             'upload_date': '20160313',
29         }
30     }, {
31         # ui-video-data[asset_metadata][items][brightcoveaccount] = 28911775001
32         'url': 'https://www.usatoday.com/story/tech/science/2018/08/21/yellowstone-supervolcano-eruption-stop-worrying-its-blow/973633002/',
33         'info_dict': {
34             'id': '5824495846001',
35             'ext': 'mp4',
36             'title': 'Yellowstone more likely to crack rather than explode',
37             'timestamp': 1534790612,
38             'description': 'md5:3715e7927639a4f16b474e9391687c62',
39             'uploader_id': '28911775001',
40             'upload_date': '20180820',
41         }
42     }]
43     BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
44
45     def _real_extract(self, url):
46         display_id = self._match_id(url)
47         webpage = self._download_webpage(update_url_query(url, {'ajax': 'true'}), display_id)
48         ui_video_data = get_element_by_attribute('class', 'ui-video-data', webpage)
49         if not ui_video_data:
50             raise ExtractorError('no video on the webpage', expected=True)
51         video_data = self._parse_json(ui_video_data, display_id)
52         item = try_get(video_data, lambda x: x['asset_metadata']['items'], dict) or {}
53
54         return {
55             '_type': 'url_transparent',
56             'url': self.BRIGHTCOVE_URL_TEMPLATE % (item.get('brightcoveaccount', '29906170001'), item.get('brightcoveid') or video_data['brightcove_id']),
57             'id': compat_str(video_data['id']),
58             'title': video_data['title'],
59             'thumbnail': video_data.get('thumbnail'),
60             'description': video_data.get('description'),
61             'duration': parse_duration(video_data.get('length')),
62             'ie_key': 'BrightcoveNew',
63         }