Unify coding cookie
[youtube-dl] / youtube_dl / extractor / wsj.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     int_or_none,
7     float_or_none,
8     unified_strdate,
9 )
10
11
12 class WSJIE(InfoExtractor):
13     _VALID_URL = r'''(?x)https?://
14         (?:
15             video-api\.wsj\.com/api-video/player/iframe\.html\?guid=|
16             (?:www\.)?wsj\.com/video/[^/]+/
17         )
18         (?P<id>[a-zA-Z0-9-]+)'''
19     IE_DESC = 'Wall Street Journal'
20     _TESTS = [{
21         'url': 'http://video-api.wsj.com/api-video/player/iframe.html?guid=1BD01A4C-BFE8-40A5-A42F-8A8AF9898B1A',
22         'md5': 'e230a5bb249075e40793b655a54a02e4',
23         'info_dict': {
24             'id': '1BD01A4C-BFE8-40A5-A42F-8A8AF9898B1A',
25             'ext': 'mp4',
26             'upload_date': '20150202',
27             'uploader_id': 'jdesai',
28             'creator': 'jdesai',
29             'categories': list,  # a long list
30             'duration': 90,
31             'title': 'Bills Coach Rex Ryan Updates His Old Jets Tattoo',
32         },
33     }, {
34         'url': 'http://www.wsj.com/video/can-alphabet-build-a-smarter-city/359DDAA8-9AC1-489C-82E6-0429C1E430E0.html',
35         'only_matching': True,
36     }]
37
38     def _real_extract(self, url):
39         video_id = self._match_id(url)
40
41         api_url = (
42             'http://video-api.wsj.com/api-video/find_all_videos.asp?'
43             'type=guid&count=1&query=%s&fields=type,hls,videoMP4List,'
44             'thumbnailList,author,description,name,duration,videoURL,'
45             'titletag,formattedCreationDate,keywords,editor' % video_id)
46         info = self._download_json(api_url, video_id)['items'][0]
47         title = info.get('name', info.get('titletag'))
48
49         formats = []
50
51         f4m_url = info.get('videoURL')
52         if f4m_url:
53             formats.extend(self._extract_f4m_formats(
54                 f4m_url, video_id, f4m_id='hds', fatal=False))
55
56         m3u8_url = info.get('hls')
57         if m3u8_url:
58             formats.extend(self._extract_m3u8_formats(
59                 info['hls'], video_id, ext='mp4',
60                 entry_protocol='m3u8_native', m3u8_id='hls', fatal=False))
61
62         for v in info.get('videoMP4List', []):
63             mp4_url = v.get('url')
64             if not mp4_url:
65                 continue
66             tbr = int_or_none(v.get('bitrate'))
67             formats.append({
68                 'url': mp4_url,
69                 'format_id': 'http' + ('-%d' % tbr if tbr else ''),
70                 'tbr': tbr,
71                 'width': int_or_none(v.get('width')),
72                 'height': int_or_none(v.get('height')),
73                 'fps': float_or_none(v.get('fps')),
74             })
75         self._sort_formats(formats)
76
77         return {
78             'id': video_id,
79             'formats': formats,
80             # Thumbnails are conveniently in the correct format already
81             'thumbnails': info.get('thumbnailList'),
82             'creator': info.get('author'),
83             'uploader_id': info.get('editor'),
84             'duration': int_or_none(info.get('duration')),
85             'upload_date': unified_strdate(info.get(
86                 'formattedCreationDate'), day_first=False),
87             'title': title,
88             'categories': info.get('keywords'),
89         }