Twitter: get and describe video from status urls
[youtube-dl] / youtube_dl / extractor / twitter.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_urllib_request
7 from ..utils import (
8     float_or_none,
9     unescapeHTML,
10 )
11
12
13 class TwitterCardIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/cards/tfw/v1/(?P<id>\d+)'
15     _TEST = {
16         'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
17         'md5': 'a74f50b310c83170319ba16de6955192',
18         'info_dict': {
19             'id': '560070183650213889',
20             'ext': 'mp4',
21             'title': 'TwitterCard',
22             'thumbnail': 're:^https?://.*\.jpg$',
23             'duration': 30.033,
24         },
25     }
26
27     def _real_extract(self, url):
28         video_id = self._match_id(url)
29
30         # Different formats served for different User-Agents
31         USER_AGENTS = [
32             'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)',  # mp4
33             'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',  # webm
34         ]
35
36         config = None
37         formats = []
38         for user_agent in USER_AGENTS:
39             request = compat_urllib_request.Request(url)
40             request.add_header('User-Agent', user_agent)
41             webpage = self._download_webpage(request, video_id)
42
43             config = self._parse_json(
44                 unescapeHTML(self._search_regex(
45                     r'data-player-config="([^"]+)"', webpage, 'data player config')),
46                 video_id)
47
48             video_url = config['playlist'][0]['source']
49
50             f = {
51                 'url': video_url,
52             }
53
54             m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
55             if m:
56                 f.update({
57                     'width': int(m.group('width')),
58                     'height': int(m.group('height')),
59                 })
60             formats.append(f)
61         self._sort_formats(formats)
62
63         thumbnail = config.get('posterImageUrl')
64         duration = float_or_none(config.get('duration'))
65
66         return {
67             'id': video_id,
68             'title': 'TwitterCard',
69             'thumbnail': thumbnail,
70             'duration': duration,
71             'formats': formats,
72         }
73
74
75 class TwitterIE(TwitterCardIE):
76     _VALID_URL = r'https?://(?:www|m|mobile)?\.?twitter\.com/(?P<id>[^/]+/status/\d+)'
77
78     _TESTS = [{
79         'url': 'https://m.twitter.com/thereaIbanksy/status/614301758345490432',
80         'md5': '8bbccb487bd7a31349b775915fcd412f',
81         'info_dict': {
82             'id': '614301758345490432',
83             'ext': 'mp4',
84             'title': 'thereaIbanksy - This time lapse is so pretty \U0001f60d\U0001f60d',
85             'thumbnail': 're:^https?://.*\.jpg',
86             'duration': 29.5,
87             'description': 'banksy on Twitter: "This time lapse is so pretty \U0001f60d\U0001f60d http://t.co/QB8DDbqiR1"',
88             'uploader': 'banksy',
89             'uploader_id': 'thereaIbanksy',
90         },
91     }]
92
93     def _real_extract(self, url):
94         id = self._match_id(url)
95         username, twid = re.match(r'([^/]+)/status/(\d+)', id).groups()
96         name = username
97         url = re.sub(r'https?://(m|mobile)\.', 'https://', url)
98         webpage = self._download_webpage(url, 'tweet: ' + url)
99         description = unescapeHTML(self._search_regex('<title>\s*(.+?)\s*</title>', webpage, 'title'))
100         title = description.replace('\n', ' ')
101         splitdesc = re.match(r'^(.+?)\s*on Twitter:\s* "(.+?)"$', title)
102         if splitdesc:
103             name, title = splitdesc.groups()
104         title = re.sub(r'\s*https?://[^ ]+', '', title)  # strip  'https -_t.co_BJYgOjSeGA' junk from filenames
105         card_id = self._search_regex(r'["\']/i/cards/tfw/v1/(\d+)', webpage, '/i/card/...')
106         card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
107         return {
108             '_type': 'url_transparent',
109             'ie_key': 'TwitterCard',
110             'uploader_id': username,
111             'uploader': name,
112             'url': card_url,
113             'webpage_url': url,
114             'description': description,
115             'title': username + ' - ' + title,
116         }