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