]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/twitter.py
Annotate changelog with bugs closed.
[youtubedl] / 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 )
13
14
15 class TwitterCardIE(InfoExtractor):
16 IE_NAME = 'twitter:card'
17 _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/cards/tfw/v1/(?P<id>\d+)'
18 _TESTS = [
19 {
20 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
21 'md5': '7d2f6b4d2eb841a7ccc893d479bfceb4',
22 'info_dict': {
23 'id': '560070183650213889',
24 'ext': 'mp4',
25 'title': 'TwitterCard',
26 'thumbnail': 're:^https?://.*\.jpg$',
27 'duration': 30.033,
28 }
29 },
30 {
31 'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768',
32 'md5': '7ee2a553b63d1bccba97fbed97d9e1c8',
33 'info_dict': {
34 'id': '623160978427936768',
35 'ext': 'mp4',
36 'title': 'TwitterCard',
37 'thumbnail': 're:^https?://.*\.jpg',
38 'duration': 80.155,
39 },
40 },
41 {
42 'url': 'https://twitter.com/i/cards/tfw/v1/654001591733886977',
43 'md5': 'b6f35e8b08a0bec6c8af77a2f4b3a814',
44 'info_dict': {
45 'id': 'dq4Oj5quskI',
46 'ext': 'mp4',
47 'title': 'Ubuntu 11.10 Overview',
48 '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/',
49 'upload_date': '20111013',
50 'uploader': 'OMG! Ubuntu!',
51 'uploader_id': 'omgubuntu',
52 },
53 }
54 ]
55
56 def _real_extract(self, url):
57 video_id = self._match_id(url)
58
59 # Different formats served for different User-Agents
60 USER_AGENTS = [
61 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)', # mp4
62 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0', # webm
63 ]
64
65 config = None
66 formats = []
67 for user_agent in USER_AGENTS:
68 request = compat_urllib_request.Request(url)
69 request.add_header('User-Agent', user_agent)
70 webpage = self._download_webpage(request, video_id)
71
72 youtube_url = self._html_search_regex(
73 r'<iframe[^>]+src="((?:https?:)?//www.youtube.com/embed/[^"]+)"',
74 webpage, 'youtube iframe', default=None)
75 if youtube_url:
76 return self.url_result(youtube_url, 'Youtube')
77
78 config = self._parse_json(self._html_search_regex(
79 r'data-player-config="([^"]+)"', webpage, 'data player config'),
80 video_id)
81 if 'playlist' not in config:
82 if 'vmapUrl' in config:
83 vmap_data = self._download_xml(config['vmapUrl'], video_id)
84 video_url = xpath_text(vmap_data, './/MediaFile').strip()
85 formats.append({
86 'url': video_url,
87 })
88 break # same video regardless of UA
89 continue
90
91 video_url = config['playlist'][0]['source']
92
93 f = {
94 'url': video_url,
95 }
96
97 m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
98 if m:
99 f.update({
100 'width': int(m.group('width')),
101 'height': int(m.group('height')),
102 })
103 formats.append(f)
104 self._sort_formats(formats)
105
106 thumbnail = config.get('posterImageUrl')
107 duration = float_or_none(config.get('duration'))
108
109 return {
110 'id': video_id,
111 'title': 'TwitterCard',
112 'thumbnail': thumbnail,
113 'duration': duration,
114 'formats': formats,
115 }
116
117
118 class TwitterIE(InfoExtractor):
119 IE_NAME = 'twitter'
120 _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)'
121 _TEMPLATE_URL = 'https://twitter.com/%s/status/%s'
122
123 _TEST = {
124 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
125 'md5': '31cd83a116fc41f99ae3d909d4caf6a0',
126 'info_dict': {
127 'id': '643211948184596480',
128 'ext': 'mp4',
129 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
130 'thumbnail': 're:^https?://.*\.jpg',
131 'duration': 12.922,
132 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
133 'uploader': 'FREE THE NIPPLE',
134 'uploader_id': 'freethenipple',
135 },
136 }
137
138 def _real_extract(self, url):
139 mobj = re.match(self._VALID_URL, url)
140 user_id = mobj.group('user_id')
141 twid = mobj.group('id')
142
143 webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid)
144
145 username = remove_end(self._og_search_title(webpage), ' on Twitter')
146
147 title = self._og_search_description(webpage).strip('').replace('\n', ' ')
148
149 # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
150 mobj = re.match(r'“(.*)\s+(https?://[^ ]+)”', title)
151 title, short_url = mobj.groups()
152
153 card_id = self._search_regex(
154 r'["\']/i/cards/tfw/v1/(\d+)', webpage, 'twitter card url')
155 card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
156
157 return {
158 '_type': 'url_transparent',
159 'ie_key': 'TwitterCard',
160 'uploader_id': user_id,
161 'uploader': username,
162 'url': card_url,
163 'webpage_url': url,
164 'description': '%s on Twitter: "%s %s"' % (username, title, short_url),
165 'title': username + ' - ' + title,
166 }