2 from __future__ import unicode_literals
6 from .common import InfoExtractor
7 from ..compat import compat_urllib_request
15 class TwitterCardIE(InfoExtractor):
16 IE_NAME = 'twitter:card'
17 _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/cards/tfw/v1/(?P<id>\d+)'
20 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
21 'md5': '7d2f6b4d2eb841a7ccc893d479bfceb4',
23 'id': '560070183650213889',
25 'title': 'TwitterCard',
26 'thumbnail': 're:^https?://.*\.jpg$',
31 'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768',
32 'md5': '7ee2a553b63d1bccba97fbed97d9e1c8',
34 'id': '623160978427936768',
36 'title': 'TwitterCard',
37 'thumbnail': 're:^https?://.*\.jpg',
43 def _real_extract(self, url):
44 video_id = self._match_id(url)
46 # Different formats served for different User-Agents
48 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)', # mp4
49 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0', # webm
54 for user_agent in USER_AGENTS:
55 request = compat_urllib_request.Request(url)
56 request.add_header('User-Agent', user_agent)
57 webpage = self._download_webpage(request, video_id)
59 config = self._parse_json(
60 unescapeHTML(self._search_regex(
61 r'data-player-config="([^"]+)"', webpage, 'data player config')),
63 if 'playlist' not in config:
64 if 'vmapUrl' in config:
65 vmap_data = self._download_xml(config['vmapUrl'], video_id)
66 video_url = xpath_text(vmap_data, './/MediaFile').strip()
70 ext = re.search(r'\.([a-z0-9]{2,4})(\?.+)?$', video_url)
72 f['ext'] = ext.group(1)
74 break # same video regardless of UA
77 video_url = config['playlist'][0]['source']
83 m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
86 'width': int(m.group('width')),
87 'height': int(m.group('height')),
90 self._sort_formats(formats)
92 thumbnail = config.get('posterImageUrl')
93 duration = float_or_none(config.get('duration'))
97 'title': 'TwitterCard',
98 'thumbnail': thumbnail,
104 class TwitterIE(TwitterCardIE):
106 _VALID_URL = r'https?://(?:www|m|mobile)?\.?twitter\.com/(?P<id>[^/]+/status/\d+)'
109 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
110 'md5': '31cd83a116fc41f99ae3d909d4caf6a0',
112 'id': '643211948184596480',
114 'title': 'freethenipple - FTN supporters on Hollywood Blvd today!',
115 'thumbnail': 're:^https?://.*\.jpg',
117 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
118 'uploader': 'FREE THE NIPPLE',
119 'uploader_id': 'freethenipple',
123 def _real_extract(self, url):
124 id = self._match_id(url)
125 username, twid = re.match(r'([^/]+)/status/(\d+)', id).groups()
127 url = re.sub(r'https?://(m|mobile)\.', 'https://', url)
128 webpage = self._download_webpage(url, 'tweet: ' + url)
129 description = unescapeHTML(self._search_regex('<title>\s*(.+?)\s*</title>', webpage, 'title'))
130 title = description.replace('\n', ' ')
131 splitdesc = re.match(r'^(.+?)\s*on Twitter:\s* "(.+?)"$', title)
133 name, title = splitdesc.groups()
134 title = re.sub(r'\s*https?://[^ ]+', '', title) # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
135 card_id = self._search_regex(r'["\']/i/cards/tfw/v1/(\d+)', webpage, '/i/card/...')
136 card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
138 '_type': 'url_transparent',
139 'ie_key': 'TwitterCard',
140 'uploader_id': username,
144 'description': description,
145 'title': username + ' - ' + title,