1 from __future__ import unicode_literals
5 from .common import InfoExtractor
6 from ..compat import compat_urllib_request
13 class TwitterCardIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/cards/tfw/v1/(?P<id>\d+)'
16 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
17 'md5': 'a74f50b310c83170319ba16de6955192',
19 'id': '560070183650213889',
21 'title': 'TwitterCard',
22 'thumbnail': 're:^https?://.*\.jpg$',
27 def _real_extract(self, url):
28 video_id = self._match_id(url)
30 # Different formats served for different 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
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)
43 config = self._parse_json(
44 unescapeHTML(self._search_regex(
45 r'data-player-config="([^"]+)"', webpage, 'data player config')),
48 video_url = config['playlist'][0]['source']
54 m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
57 'width': int(m.group('width')),
58 'height': int(m.group('height')),
61 self._sort_formats(formats)
63 thumbnail = config.get('posterImageUrl')
64 duration = float_or_none(config.get('duration'))
68 'title': 'TwitterCard',
69 'thumbnail': thumbnail,
75 class TwitterIE(TwitterCardIE):
76 _VALID_URL = r'https?://(?:www|m|mobile)?\.?twitter\.com/(?P<id>[^/]+/status/\d+)'
79 'url': 'https://m.twitter.com/thereaIbanksy/status/614301758345490432',
80 'md5': '8bbccb487bd7a31349b775915fcd412f',
82 'id': '614301758345490432',
84 'title': 'thereaIbanksy - This time lapse is so pretty \U0001f60d\U0001f60d',
85 'thumbnail': 're:^https?://.*\.jpg',
87 'description': 'banksy on Twitter: "This time lapse is so pretty \U0001f60d\U0001f60d http://t.co/QB8DDbqiR1"',
89 'uploader_id': 'thereaIbanksy',
93 def _real_extract(self, url):
94 id = self._match_id(url)
95 username, twid = re.match(r'([^/]+)/status/(\d+)', id).groups()
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)
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
108 '_type': 'url_transparent',
109 'ie_key': 'TwitterCard',
110 'uploader_id': username,
114 'description': description,
115 'title': username + ' - ' + title,