[twitter:card] Support vine.co embeds (closes #7496)
[youtube-dl] / youtube_dl / extractor / twitter.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_urllib_request
8 from ..utils import (
9     float_or_none,
10     xpath_text,
11     remove_end,
12     int_or_none,
13     ExtractorError,
14 )
15
16
17 class TwitterCardIE(InfoExtractor):
18     IE_NAME = 'twitter:card'
19     _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/cards/tfw/v1/(?P<id>\d+)'
20     _TESTS = [
21         {
22             'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
23             'md5': '4fa26a35f9d1bf4b646590ba8e84be19',
24             'info_dict': {
25                 'id': '560070183650213889',
26                 'ext': 'mp4',
27                 'title': 'TwitterCard',
28                 'thumbnail': 're:^https?://.*\.jpg$',
29                 'duration': 30.033,
30             }
31         },
32         {
33             'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768',
34             'md5': '7ee2a553b63d1bccba97fbed97d9e1c8',
35             'info_dict': {
36                 'id': '623160978427936768',
37                 'ext': 'mp4',
38                 'title': 'TwitterCard',
39                 'thumbnail': 're:^https?://.*\.jpg',
40                 'duration': 80.155,
41             },
42         },
43         {
44             'url': 'https://twitter.com/i/cards/tfw/v1/654001591733886977',
45             'md5': 'b6f35e8b08a0bec6c8af77a2f4b3a814',
46             'info_dict': {
47                 'id': 'dq4Oj5quskI',
48                 'ext': 'mp4',
49                 'title': 'Ubuntu 11.10 Overview',
50                 'description': 'Take a quick peek at what\'s new and improved in Ubuntu 11.10.\n\nOnce installed take a look at 10 Things to Do After Installing: http://www.omgubuntu.co.uk/2011/10/10-things-to-do-after-installing-ubuntu-11-10/',
51                 'upload_date': '20111013',
52                 'uploader': 'OMG! Ubuntu!',
53                 'uploader_id': 'omgubuntu',
54             },
55         },
56         {
57             'url': 'https://twitter.com/i/cards/tfw/v1/665289828897005568',
58             'md5': 'ab2745d0b0ce53319a534fccaa986439',
59             'info_dict': {
60                 'id': 'iBb2x00UVlv',
61                 'ext': 'mp4',
62                 'upload_date': '20151113',
63                 'uploader_id': '1189339351084113920',
64                 'uploader': '@ArsenalTerje',
65                 'title': 'Vine by @ArsenalTerje',
66             },
67             'add_ie': ['Vine'],
68         }
69     ]
70
71     def _real_extract(self, url):
72         video_id = self._match_id(url)
73
74         # Different formats served for different User-Agents
75         USER_AGENTS = [
76             'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)',  # mp4
77             'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',  # webm
78         ]
79
80         config = None
81         formats = []
82         for user_agent in USER_AGENTS:
83             request = compat_urllib_request.Request(url)
84             request.add_header('User-Agent', user_agent)
85             webpage = self._download_webpage(request, video_id)
86
87             iframe_url = self._html_search_regex(
88                 r'<iframe[^>]+src="((?:https?:)?//(?:www.youtube.com/embed/[^"]+|(?:www\.)?vine\.co/v/\w+/card))"',
89                 webpage, 'video iframe', default=None)
90             if iframe_url:
91                 return self.url_result(iframe_url)
92
93             config = self._parse_json(self._html_search_regex(
94                 r'data-player-config="([^"]+)"', webpage, 'data player config'),
95                 video_id)
96             if 'playlist' not in config:
97                 if 'vmapUrl' in config:
98                     vmap_data = self._download_xml(config['vmapUrl'], video_id)
99                     video_url = xpath_text(vmap_data, './/MediaFile').strip()
100                     formats.append({
101                         'url': video_url,
102                     })
103                     break   # same video regardless of UA
104                 continue
105
106             video_url = config['playlist'][0]['source']
107
108             f = {
109                 'url': video_url,
110             }
111
112             m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
113             if m:
114                 f.update({
115                     'width': int(m.group('width')),
116                     'height': int(m.group('height')),
117                 })
118             formats.append(f)
119         self._sort_formats(formats)
120
121         thumbnail = config.get('posterImageUrl')
122         duration = float_or_none(config.get('duration'))
123
124         return {
125             'id': video_id,
126             'title': 'TwitterCard',
127             'thumbnail': thumbnail,
128             'duration': duration,
129             'formats': formats,
130         }
131
132
133 class TwitterIE(InfoExtractor):
134     IE_NAME = 'twitter'
135     _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)'
136     _TEMPLATE_URL = 'https://twitter.com/%s/status/%s'
137
138     _TESTS = [{
139         'url': 'https://twitter.com/freethenipple/status/643211948184596480',
140         'md5': 'db6612ec5d03355953c3ca9250c97e5e',
141         'info_dict': {
142             'id': '643211948184596480',
143             'ext': 'mp4',
144             'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
145             'thumbnail': 're:^https?://.*\.jpg',
146             'duration': 12.922,
147             'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
148             'uploader': 'FREE THE NIPPLE',
149             'uploader_id': 'freethenipple',
150         },
151     }, {
152         'url': 'https://twitter.com/giphz/status/657991469417025536/photo/1',
153         'md5': 'f36dcd5fb92bf7057f155e7d927eeb42',
154         'info_dict': {
155             'id': '657991469417025536',
156             'ext': 'mp4',
157             'title': 'Gifs - tu vai cai tu vai cai tu nao eh capaz disso tu vai cai',
158             'description': 'Gifs on Twitter: "tu vai cai tu vai cai tu nao eh capaz disso tu vai cai https://t.co/tM46VHFlO5"',
159             'thumbnail': 're:^https?://.*\.png',
160             'uploader': 'Gifs',
161             'uploader_id': 'giphz',
162         },
163     }, {
164         'url': 'https://twitter.com/starwars/status/665052190608723968',
165         'md5': '39b7199856dee6cd4432e72c74bc69d4',
166         'info_dict': {
167             'id': '665052190608723968',
168             'ext': 'mp4',
169             'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.',
170             'description': 'Star Wars on Twitter: "A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens."',
171             'uploader_id': 'starwars',
172             'uploader': 'Star Wars',
173         },
174     }]
175
176     def _real_extract(self, url):
177         mobj = re.match(self._VALID_URL, url)
178         user_id = mobj.group('user_id')
179         twid = mobj.group('id')
180
181         webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid)
182
183         username = remove_end(self._og_search_title(webpage), ' on Twitter')
184
185         title = description = self._og_search_description(webpage).strip('').replace('\n', ' ').strip('“”')
186
187         # strip  'https -_t.co_BJYgOjSeGA' junk from filenames
188         title = re.sub(r'\s+(https?://[^ ]+)', '', title)
189
190         info = {
191             'uploader_id': user_id,
192             'uploader': username,
193             'webpage_url': url,
194             'description': '%s on Twitter: "%s"' % (username, description),
195             'title': username + ' - ' + title,
196         }
197
198         card_id = self._search_regex(
199             r'["\']/i/cards/tfw/v1/(\d+)', webpage, 'twitter card url', default=None)
200         if card_id:
201             card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
202             info.update({
203                 '_type': 'url_transparent',
204                 'ie_key': 'TwitterCard',
205                 'url': card_url,
206             })
207             return info
208
209         mobj = re.search(r'''(?x)
210             <video[^>]+class="animated-gif"[^>]+
211                 (?:data-height="(?P<height>\d+)")?[^>]+
212                 (?:data-width="(?P<width>\d+)")?[^>]+
213                 (?:poster="(?P<poster>[^"]+)")?[^>]*>\s*
214                 <source[^>]+video-src="(?P<url>[^"]+)"
215         ''', webpage)
216
217         if mobj:
218             info.update({
219                 'id': twid,
220                 'url': mobj.group('url'),
221                 'height': int_or_none(mobj.group('height')),
222                 'width': int_or_none(mobj.group('width')),
223                 'thumbnail': mobj.group('poster'),
224             })
225             return info
226
227         raise ExtractorError('There\'s not video in this tweet.')