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.

373 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. def _search_dimensions_in_video_url(a_format, video_url):
  94. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  95. if m:
  96. a_format.update({
  97. 'width': int(m.group('width')),
  98. 'height': int(m.group('height')),
  99. })
  100. playlist = config.get('playlist')
  101. if playlist:
  102. video_url = playlist[0]['source']
  103. f = {
  104. 'url': video_url,
  105. }
  106. _search_dimensions_in_video_url(f, video_url)
  107. formats.append(f)
  108. vmap_url = config.get('vmapUrl') or config.get('vmap_url')
  109. if vmap_url:
  110. formats.append({
  111. 'url': self._get_vmap_video_url(vmap_url, video_id),
  112. })
  113. media_info = None
  114. for entity in config.get('status', {}).get('entities', []):
  115. if 'mediaInfo' in entity:
  116. media_info = entity['mediaInfo']
  117. if media_info:
  118. for media_variant in media_info['variants']:
  119. media_url = media_variant['url']
  120. if media_url.endswith('.m3u8'):
  121. formats.extend(self._extract_m3u8_formats(media_url, video_id, ext='mp4', m3u8_id='hls'))
  122. elif media_url.endswith('.mpd'):
  123. formats.extend(self._extract_mpd_formats(media_url, video_id, mpd_id='dash'))
  124. else:
  125. vbr = int_or_none(media_variant.get('bitRate'), scale=1000)
  126. a_format = {
  127. 'url': media_url,
  128. 'format_id': 'http-%d' % vbr if vbr else 'http',
  129. 'vbr': vbr,
  130. }
  131. # Reported bitRate may be zero
  132. if not a_format['vbr']:
  133. del a_format['vbr']
  134. _search_dimensions_in_video_url(a_format, media_url)
  135. formats.append(a_format)
  136. duration = float_or_none(media_info.get('duration', {}).get('nanos'), scale=1e9)
  137. self._sort_formats(formats)
  138. title = self._search_regex(r'<title>([^<]+)</title>', webpage, 'title')
  139. thumbnail = config.get('posterImageUrl') or config.get('image_src')
  140. duration = float_or_none(config.get('duration')) or duration
  141. return {
  142. 'id': video_id,
  143. 'title': title,
  144. 'thumbnail': thumbnail,
  145. 'duration': duration,
  146. 'formats': formats,
  147. }
  148. class TwitterIE(InfoExtractor):
  149. IE_NAME = 'twitter'
  150. _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)'
  151. _TEMPLATE_URL = 'https://twitter.com/%s/status/%s'
  152. _TESTS = [{
  153. 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
  154. 'info_dict': {
  155. 'id': '643211948184596480',
  156. 'ext': 'mp4',
  157. 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
  158. 'thumbnail': 're:^https?://.*\.jpg',
  159. 'duration': 12.922,
  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. def _real_extract(self, url):
  223. mobj = re.match(self._VALID_URL, url)
  224. user_id = mobj.group('user_id')
  225. twid = mobj.group('id')
  226. webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid)
  227. username = remove_end(self._og_search_title(webpage), ' on Twitter')
  228. title = description = self._og_search_description(webpage).strip('').replace('\n', ' ').strip('“”')
  229. # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  230. title = re.sub(r'\s+(https?://[^ ]+)', '', title)
  231. info = {
  232. 'uploader_id': user_id,
  233. 'uploader': username,
  234. 'webpage_url': url,
  235. 'description': '%s on Twitter: "%s"' % (username, description),
  236. 'title': username + ' - ' + title,
  237. }
  238. card_id = self._search_regex(
  239. r'["\']/i/cards/tfw/v1/(\d+)', webpage, 'twitter card url', default=None)
  240. if card_id:
  241. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  242. info.update({
  243. '_type': 'url_transparent',
  244. 'ie_key': 'TwitterCard',
  245. 'url': card_url,
  246. })
  247. return info
  248. mobj = re.search(r'''(?x)
  249. <video[^>]+class="animated-gif"(?P<more_info>[^>]+)>\s*
  250. <source[^>]+video-src="(?P<url>[^"]+)"
  251. ''', webpage)
  252. if mobj:
  253. more_info = mobj.group('more_info')
  254. height = int_or_none(self._search_regex(
  255. r'data-height="(\d+)"', more_info, 'height', fatal=False))
  256. width = int_or_none(self._search_regex(
  257. r'data-width="(\d+)"', more_info, 'width', fatal=False))
  258. thumbnail = self._search_regex(
  259. r'poster="([^"]+)"', more_info, 'poster', fatal=False)
  260. info.update({
  261. 'id': twid,
  262. 'url': mobj.group('url'),
  263. 'height': height,
  264. 'width': width,
  265. 'thumbnail': thumbnail,
  266. })
  267. return info
  268. if 'class="PlayableMedia' in webpage:
  269. info.update({
  270. '_type': 'url_transparent',
  271. 'ie_key': 'TwitterCard',
  272. 'url': '%s//twitter.com/i/videos/tweet/%s' % (self.http_scheme(), twid),
  273. })
  274. return info
  275. raise ExtractorError('There\'s no video in this tweet.')
  276. class TwitterAmplifyIE(TwitterBaseIE):
  277. IE_NAME = 'twitter:amplify'
  278. _VALID_URL = 'https?://amp\.twimg\.com/v/(?P<id>[0-9a-f\-]{36})'
  279. _TEST = {
  280. 'url': 'https://amp.twimg.com/v/0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
  281. 'md5': '7df102d0b9fd7066b86f3159f8e81bf6',
  282. 'info_dict': {
  283. 'id': '0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
  284. 'ext': 'mp4',
  285. 'title': 'Twitter Video',
  286. 'thumbnail': 're:^https?://.*',
  287. },
  288. }
  289. def _real_extract(self, url):
  290. video_id = self._match_id(url)
  291. webpage = self._download_webpage(url, video_id)
  292. vmap_url = self._html_search_meta(
  293. 'twitter:amplify:vmap', webpage, 'vmap url')
  294. video_url = self._get_vmap_video_url(vmap_url, video_id)
  295. thumbnails = []
  296. thumbnail = self._html_search_meta(
  297. 'twitter:image:src', webpage, 'thumbnail', fatal=False)
  298. def _find_dimension(target):
  299. w = int_or_none(self._html_search_meta(
  300. 'twitter:%s:width' % target, webpage, fatal=False))
  301. h = int_or_none(self._html_search_meta(
  302. 'twitter:%s:height' % target, webpage, fatal=False))
  303. return w, h
  304. if thumbnail:
  305. thumbnail_w, thumbnail_h = _find_dimension('image')
  306. thumbnails.append({
  307. 'url': thumbnail,
  308. 'width': thumbnail_w,
  309. 'height': thumbnail_h,
  310. })
  311. video_w, video_h = _find_dimension('player')
  312. formats = [{
  313. 'url': video_url,
  314. 'width': video_w,
  315. 'height': video_h,
  316. }]
  317. return {
  318. 'id': video_id,
  319. 'title': 'Twitter Video',
  320. 'formats': formats,
  321. 'thumbnails': thumbnails,
  322. }