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.

386 lines
14 KiB

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