You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
15 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. float_or_none,
  8. xpath_text,
  9. remove_end,
  10. int_or_none,
  11. ExtractorError,
  12. )
  13. class TwitterBaseIE(InfoExtractor):
  14. def _get_vmap_video_url(self, vmap_url, video_id):
  15. vmap_data = self._download_xml(vmap_url, video_id)
  16. return xpath_text(vmap_data, './/MediaFile').strip()
  17. class TwitterCardIE(TwitterBaseIE):
  18. IE_NAME = 'twitter:card'
  19. _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/(?:cards/tfw/v1|videos/tweet)/(?P<id>\d+)'
  20. _TESTS = [
  21. {
  22. 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
  23. # MD5 checksums are different in different places
  24. 'info_dict': {
  25. 'id': '560070183650213889',
  26. 'ext': 'mp4',
  27. 'title': 'Twitter Card',
  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': 'Twitter Card',
  39. 'thumbnail': 're:^https?://.*\.jpg',
  40. 'duration': 80.155,
  41. },
  42. },
  43. {
  44. 'url': 'https://twitter.com/i/cards/tfw/v1/654001591733886977',
  45. 'md5': 'd4724ffe6d2437886d004fa5de1043b3',
  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...',
  51. 'upload_date': '20111013',
  52. 'uploader': 'OMG! Ubuntu!',
  53. 'uploader_id': 'omgubuntu',
  54. },
  55. 'add_ie': ['Youtube'],
  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'],
  69. }, {
  70. 'url': 'https://twitter.com/i/videos/tweet/705235433198714880',
  71. 'md5': '3846d0a07109b5ab622425449b59049d',
  72. 'info_dict': {
  73. 'id': '705235433198714880',
  74. 'ext': 'mp4',
  75. 'title': 'Twitter web player',
  76. 'thumbnail': 're:^https?://.*\.jpg',
  77. },
  78. },
  79. ]
  80. def _real_extract(self, url):
  81. video_id = self._match_id(url)
  82. config = None
  83. formats = []
  84. duration = None
  85. webpage = self._download_webpage(url, video_id)
  86. iframe_url = self._html_search_regex(
  87. r'<iframe[^>]+src="((?:https?:)?//(?:www.youtube.com/embed/[^"]+|(?:www\.)?vine\.co/v/\w+/card))"',
  88. webpage, 'video iframe', default=None)
  89. if iframe_url:
  90. return self.url_result(iframe_url)
  91. config = self._parse_json(self._html_search_regex(
  92. r'data-(?:player-)?config="([^"]+)"', webpage, 'data player config'),
  93. video_id)
  94. if config.get('source_type') == 'vine':
  95. return self.url_result(config['player_url'], 'Vine')
  96. def _search_dimensions_in_video_url(a_format, video_url):
  97. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  98. if m:
  99. a_format.update({
  100. 'width': int(m.group('width')),
  101. 'height': int(m.group('height')),
  102. })
  103. video_url = config.get('video_url') or config.get('playlist', [{}])[0].get('source')
  104. if video_url:
  105. if determine_ext(video_url) == 'm3u8':
  106. formats.extend(self._extract_m3u8_formats(video_url, video_id, ext='mp4', m3u8_id='hls'))
  107. else:
  108. f = {
  109. 'url': video_url,
  110. }
  111. _search_dimensions_in_video_url(f, video_url)
  112. formats.append(f)
  113. vmap_url = config.get('vmapUrl') or config.get('vmap_url')
  114. if vmap_url:
  115. formats.append({
  116. 'url': self._get_vmap_video_url(vmap_url, video_id),
  117. })
  118. media_info = None
  119. for entity in config.get('status', {}).get('entities', []):
  120. if 'mediaInfo' in entity:
  121. media_info = entity['mediaInfo']
  122. if media_info:
  123. for media_variant in media_info['variants']:
  124. media_url = media_variant['url']
  125. if media_url.endswith('.m3u8'):
  126. formats.extend(self._extract_m3u8_formats(media_url, video_id, ext='mp4', m3u8_id='hls'))
  127. elif media_url.endswith('.mpd'):
  128. formats.extend(self._extract_mpd_formats(media_url, video_id, mpd_id='dash'))
  129. else:
  130. vbr = int_or_none(media_variant.get('bitRate'), scale=1000)
  131. a_format = {
  132. 'url': media_url,
  133. 'format_id': 'http-%d' % vbr if vbr else 'http',
  134. 'vbr': vbr,
  135. }
  136. # Reported bitRate may be zero
  137. if not a_format['vbr']:
  138. del a_format['vbr']
  139. _search_dimensions_in_video_url(a_format, media_url)
  140. formats.append(a_format)
  141. duration = float_or_none(media_info.get('duration', {}).get('nanos'), scale=1e9)
  142. self._sort_formats(formats)
  143. title = self._search_regex(r'<title>([^<]+)</title>', webpage, 'title')
  144. thumbnail = config.get('posterImageUrl') or config.get('image_src')
  145. duration = float_or_none(config.get('duration')) or duration
  146. return {
  147. 'id': video_id,
  148. 'title': title,
  149. 'thumbnail': thumbnail,
  150. 'duration': duration,
  151. 'formats': formats,
  152. }
  153. class TwitterIE(InfoExtractor):
  154. IE_NAME = 'twitter'
  155. _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)'
  156. _TEMPLATE_URL = 'https://twitter.com/%s/status/%s'
  157. _TESTS = [{
  158. 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
  159. 'info_dict': {
  160. 'id': '643211948184596480',
  161. 'ext': 'mp4',
  162. 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
  163. 'thumbnail': 're:^https?://.*\.jpg',
  164. 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
  165. 'uploader': 'FREE THE NIPPLE',
  166. 'uploader_id': 'freethenipple',
  167. },
  168. 'params': {
  169. 'skip_download': True, # requires ffmpeg
  170. },
  171. }, {
  172. 'url': 'https://twitter.com/giphz/status/657991469417025536/photo/1',
  173. 'md5': 'f36dcd5fb92bf7057f155e7d927eeb42',
  174. 'info_dict': {
  175. 'id': '657991469417025536',
  176. 'ext': 'mp4',
  177. 'title': 'Gifs - tu vai cai tu vai cai tu nao eh capaz disso tu vai cai',
  178. 'description': 'Gifs on Twitter: "tu vai cai tu vai cai tu nao eh capaz disso tu vai cai https://t.co/tM46VHFlO5"',
  179. 'thumbnail': 're:^https?://.*\.png',
  180. 'uploader': 'Gifs',
  181. 'uploader_id': 'giphz',
  182. },
  183. 'expected_warnings': ['height', 'width'],
  184. 'skip': 'Account suspended',
  185. }, {
  186. 'url': 'https://twitter.com/starwars/status/665052190608723968',
  187. 'md5': '39b7199856dee6cd4432e72c74bc69d4',
  188. 'info_dict': {
  189. 'id': '665052190608723968',
  190. 'ext': 'mp4',
  191. 'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.',
  192. 'description': 'Star Wars on Twitter: "A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens."',
  193. 'uploader_id': 'starwars',
  194. 'uploader': 'Star Wars',
  195. },
  196. }, {
  197. 'url': 'https://twitter.com/BTNBrentYarina/status/705235433198714880',
  198. 'info_dict': {
  199. 'id': '705235433198714880',
  200. 'ext': 'mp4',
  201. 'title': 'Brent Yarina - Khalil Iverson\'s missed highlight dunk. And made highlight dunk. In one highlight.',
  202. 'description': 'Brent Yarina on Twitter: "Khalil Iverson\'s missed highlight dunk. And made highlight dunk. In one highlight."',
  203. 'uploader_id': 'BTNBrentYarina',
  204. 'uploader': 'Brent Yarina',
  205. },
  206. 'params': {
  207. # The same video as https://twitter.com/i/videos/tweet/705235433198714880
  208. # Test case of TwitterCardIE
  209. 'skip_download': True,
  210. },
  211. }, {
  212. 'url': 'https://twitter.com/jaydingeer/status/700207533655363584',
  213. 'md5': '',
  214. 'info_dict': {
  215. 'id': '700207533655363584',
  216. 'ext': 'mp4',
  217. 'title': 'Donte The Dumbass - BEAT PROD: @suhmeduh #Damndaniel',
  218. 'description': 'Donte The Dumbass on Twitter: "BEAT PROD: @suhmeduh https://t.co/HBrQ4AfpvZ #Damndaniel https://t.co/byBooq2ejZ"',
  219. 'thumbnail': 're:^https?://.*\.jpg',
  220. 'uploader': 'Donte The Dumbass',
  221. 'uploader_id': 'jaydingeer',
  222. },
  223. 'params': {
  224. 'skip_download': True, # requires ffmpeg
  225. },
  226. }, {
  227. 'url': 'https://twitter.com/Filmdrunk/status/713801302971588609',
  228. 'md5': '89a15ed345d13b86e9a5a5e051fa308a',
  229. 'info_dict': {
  230. 'id': 'MIOxnrUteUd',
  231. 'ext': 'mp4',
  232. 'title': 'Dr.Pepperの飲み方 #japanese #バカ #ドクペ #電動ガン',
  233. 'uploader': 'TAKUMA',
  234. 'uploader_id': '1004126642786242560',
  235. 'upload_date': '20140615',
  236. },
  237. 'add_ie': ['Vine'],
  238. }, {
  239. 'url': 'https://twitter.com/captainamerica/status/719944021058060289',
  240. 'info_dict': {
  241. 'id': '719944021058060289',
  242. 'ext': 'mp4',
  243. 'title': 'Captain America - @King0fNerd Are you sure you made the right choice? Find out in theaters.',
  244. 'description': 'Captain America on Twitter: "@King0fNerd Are you sure you made the right choice? Find out in theaters. https://t.co/GpgYi9xMJI"',
  245. 'uploader_id': 'captainamerica',
  246. 'uploader': 'Captain America',
  247. },
  248. 'params': {
  249. 'skip_download': True, # requires ffmpeg
  250. },
  251. }]
  252. def _real_extract(self, url):
  253. mobj = re.match(self._VALID_URL, url)
  254. user_id = mobj.group('user_id')
  255. twid = mobj.group('id')
  256. webpage, urlh = self._download_webpage_handle(
  257. self._TEMPLATE_URL % (user_id, twid), twid)
  258. if 'twitter.com/account/suspended' in urlh.geturl():
  259. raise ExtractorError('Account suspended by Twitter.', expected=True)
  260. username = remove_end(self._og_search_title(webpage), ' on Twitter')
  261. title = description = self._og_search_description(webpage).strip('').replace('\n', ' ').strip('“”')
  262. # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  263. title = re.sub(r'\s+(https?://[^ ]+)', '', title)
  264. info = {
  265. 'uploader_id': user_id,
  266. 'uploader': username,
  267. 'webpage_url': url,
  268. 'description': '%s on Twitter: "%s"' % (username, description),
  269. 'title': username + ' - ' + title,
  270. }
  271. mobj = re.search(r'''(?x)
  272. <video[^>]+class="animated-gif"(?P<more_info>[^>]+)>\s*
  273. <source[^>]+video-src="(?P<url>[^"]+)"
  274. ''', webpage)
  275. if mobj:
  276. more_info = mobj.group('more_info')
  277. height = int_or_none(self._search_regex(
  278. r'data-height="(\d+)"', more_info, 'height', fatal=False))
  279. width = int_or_none(self._search_regex(
  280. r'data-width="(\d+)"', more_info, 'width', fatal=False))
  281. thumbnail = self._search_regex(
  282. r'poster="([^"]+)"', more_info, 'poster', fatal=False)
  283. info.update({
  284. 'id': twid,
  285. 'url': mobj.group('url'),
  286. 'height': height,
  287. 'width': width,
  288. 'thumbnail': thumbnail,
  289. })
  290. return info
  291. if 'class="PlayableMedia' in webpage:
  292. info.update({
  293. '_type': 'url_transparent',
  294. 'ie_key': 'TwitterCard',
  295. 'url': '%s//twitter.com/i/videos/tweet/%s' % (self.http_scheme(), twid),
  296. })
  297. return info
  298. raise ExtractorError('There\'s no video in this tweet.')
  299. class TwitterAmplifyIE(TwitterBaseIE):
  300. IE_NAME = 'twitter:amplify'
  301. _VALID_URL = 'https?://amp\.twimg\.com/v/(?P<id>[0-9a-f\-]{36})'
  302. _TEST = {
  303. 'url': 'https://amp.twimg.com/v/0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
  304. 'md5': '7df102d0b9fd7066b86f3159f8e81bf6',
  305. 'info_dict': {
  306. 'id': '0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
  307. 'ext': 'mp4',
  308. 'title': 'Twitter Video',
  309. 'thumbnail': 're:^https?://.*',
  310. },
  311. }
  312. def _real_extract(self, url):
  313. video_id = self._match_id(url)
  314. webpage = self._download_webpage(url, video_id)
  315. vmap_url = self._html_search_meta(
  316. 'twitter:amplify:vmap', webpage, 'vmap url')
  317. video_url = self._get_vmap_video_url(vmap_url, video_id)
  318. thumbnails = []
  319. thumbnail = self._html_search_meta(
  320. 'twitter:image:src', webpage, 'thumbnail', fatal=False)
  321. def _find_dimension(target):
  322. w = int_or_none(self._html_search_meta(
  323. 'twitter:%s:width' % target, webpage, fatal=False))
  324. h = int_or_none(self._html_search_meta(
  325. 'twitter:%s:height' % target, webpage, fatal=False))
  326. return w, h
  327. if thumbnail:
  328. thumbnail_w, thumbnail_h = _find_dimension('image')
  329. thumbnails.append({
  330. 'url': thumbnail,
  331. 'width': thumbnail_w,
  332. 'height': thumbnail_h,
  333. })
  334. video_w, video_h = _find_dimension('player')
  335. formats = [{
  336. 'url': video_url,
  337. 'width': video_w,
  338. 'height': video_h,
  339. }]
  340. return {
  341. 'id': video_id,
  342. 'title': 'Twitter Video',
  343. 'formats': formats,
  344. 'thumbnails': thumbnails,
  345. }