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.

367 lines
13 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. playlist = config.get('playlist')
  94. if playlist:
  95. video_url = playlist[0]['source']
  96. f = {
  97. 'url': video_url,
  98. }
  99. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  100. if m:
  101. f.update({
  102. 'width': int(m.group('width')),
  103. 'height': int(m.group('height')),
  104. })
  105. formats.append(f)
  106. vmap_url = config.get('vmapUrl') or config.get('vmap_url')
  107. if vmap_url:
  108. formats.append({
  109. 'url': self._get_vmap_video_url(vmap_url, video_id),
  110. })
  111. media_info = None
  112. for entity in config.get('status', {}).get('entities', []):
  113. if 'mediaInfo' in entity:
  114. media_info = entity['mediaInfo']
  115. if media_info:
  116. for media_variant in media_info['variants']:
  117. media_url = media_variant['url']
  118. if media_url.endswith('.m3u8'):
  119. formats.extend(self._extract_m3u8_formats(media_url, video_id, ext='mp4', m3u8_id='hls'))
  120. elif media_url.endswith('.mpd'):
  121. formats.extend(self._extract_mpd_formats(media_url, video_id, mpd_id='dash'))
  122. else:
  123. vbr = int_or_none(media_variant.get('bitRate'), scale=1000)
  124. a_format = {
  125. 'url': media_url,
  126. 'format_id': 'http-%d' % vbr if vbr else 'http',
  127. 'vbr': vbr,
  128. }
  129. # Reported bitRate may be zero
  130. if not a_format['vbr']:
  131. del a_format['vbr']
  132. formats.append(a_format)
  133. duration = float_or_none(media_info.get('duration', {}).get('nanos'), scale=1e9)
  134. self._sort_formats(formats)
  135. title = self._search_regex(r'<title>([^<]+)</title>', webpage, 'title')
  136. thumbnail = config.get('posterImageUrl') or config.get('image_src')
  137. duration = float_or_none(config.get('duration')) or duration
  138. return {
  139. 'id': video_id,
  140. 'title': title,
  141. 'thumbnail': thumbnail,
  142. 'duration': duration,
  143. 'formats': formats,
  144. }
  145. class TwitterIE(InfoExtractor):
  146. IE_NAME = 'twitter'
  147. _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)'
  148. _TEMPLATE_URL = 'https://twitter.com/%s/status/%s'
  149. _TESTS = [{
  150. 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
  151. 'info_dict': {
  152. 'id': '643211948184596480',
  153. 'ext': 'mp4',
  154. 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
  155. 'thumbnail': 're:^https?://.*\.jpg',
  156. 'duration': 12.922,
  157. 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
  158. 'uploader': 'FREE THE NIPPLE',
  159. 'uploader_id': 'freethenipple',
  160. },
  161. 'params': {
  162. 'skip_download': True, # requires ffmpeg
  163. },
  164. }, {
  165. 'url': 'https://twitter.com/giphz/status/657991469417025536/photo/1',
  166. 'md5': 'f36dcd5fb92bf7057f155e7d927eeb42',
  167. 'info_dict': {
  168. 'id': '657991469417025536',
  169. 'ext': 'mp4',
  170. 'title': 'Gifs - tu vai cai tu vai cai tu nao eh capaz disso tu vai cai',
  171. 'description': 'Gifs on Twitter: "tu vai cai tu vai cai tu nao eh capaz disso tu vai cai https://t.co/tM46VHFlO5"',
  172. 'thumbnail': 're:^https?://.*\.png',
  173. 'uploader': 'Gifs',
  174. 'uploader_id': 'giphz',
  175. },
  176. 'expected_warnings': ['height', 'width'],
  177. }, {
  178. 'url': 'https://twitter.com/starwars/status/665052190608723968',
  179. 'md5': '39b7199856dee6cd4432e72c74bc69d4',
  180. 'info_dict': {
  181. 'id': '665052190608723968',
  182. 'ext': 'mp4',
  183. 'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.',
  184. 'description': 'Star Wars on Twitter: "A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens."',
  185. 'uploader_id': 'starwars',
  186. 'uploader': 'Star Wars',
  187. },
  188. }, {
  189. 'url': 'https://twitter.com/BTNBrentYarina/status/705235433198714880',
  190. 'info_dict': {
  191. 'id': '705235433198714880',
  192. 'ext': 'mp4',
  193. 'title': 'Brent Yarina - Khalil Iverson\'s missed highlight dunk. And made highlight dunk. In one highlight.',
  194. 'description': 'Brent Yarina on Twitter: "Khalil Iverson\'s missed highlight dunk. And made highlight dunk. In one highlight."',
  195. 'uploader_id': 'BTNBrentYarina',
  196. 'uploader': 'Brent Yarina',
  197. },
  198. 'params': {
  199. # The same video as https://twitter.com/i/videos/tweet/705235433198714880
  200. # Test case of TwitterCardIE
  201. 'skip_download': True,
  202. },
  203. }, {
  204. 'url': 'https://twitter.com/jaydingeer/status/700207533655363584',
  205. 'md5': '',
  206. 'info_dict': {
  207. 'id': '700207533655363584',
  208. 'ext': 'mp4',
  209. 'title': 'jay - BEAT PROD: @suhmeduh #Damndaniel',
  210. 'description': 'jay on Twitter: "BEAT PROD: @suhmeduh https://t.co/HBrQ4AfpvZ #Damndaniel https://t.co/byBooq2ejZ"',
  211. 'thumbnail': 're:^https?://.*\.jpg',
  212. 'uploader': 'jay',
  213. 'uploader_id': 'jaydingeer',
  214. },
  215. 'params': {
  216. 'skip_download': True, # requires ffmpeg
  217. },
  218. }]
  219. def _real_extract(self, url):
  220. mobj = re.match(self._VALID_URL, url)
  221. user_id = mobj.group('user_id')
  222. twid = mobj.group('id')
  223. webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid)
  224. username = remove_end(self._og_search_title(webpage), ' on Twitter')
  225. title = description = self._og_search_description(webpage).strip('').replace('\n', ' ').strip('“”')
  226. # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  227. title = re.sub(r'\s+(https?://[^ ]+)', '', title)
  228. info = {
  229. 'uploader_id': user_id,
  230. 'uploader': username,
  231. 'webpage_url': url,
  232. 'description': '%s on Twitter: "%s"' % (username, description),
  233. 'title': username + ' - ' + title,
  234. }
  235. card_id = self._search_regex(
  236. r'["\']/i/cards/tfw/v1/(\d+)', webpage, 'twitter card url', default=None)
  237. if card_id:
  238. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  239. info.update({
  240. '_type': 'url_transparent',
  241. 'ie_key': 'TwitterCard',
  242. 'url': card_url,
  243. })
  244. return info
  245. mobj = re.search(r'''(?x)
  246. <video[^>]+class="animated-gif"(?P<more_info>[^>]+)>\s*
  247. <source[^>]+video-src="(?P<url>[^"]+)"
  248. ''', webpage)
  249. if mobj:
  250. more_info = mobj.group('more_info')
  251. height = int_or_none(self._search_regex(
  252. r'data-height="(\d+)"', more_info, 'height', fatal=False))
  253. width = int_or_none(self._search_regex(
  254. r'data-width="(\d+)"', more_info, 'width', fatal=False))
  255. thumbnail = self._search_regex(
  256. r'poster="([^"]+)"', more_info, 'poster', fatal=False)
  257. info.update({
  258. 'id': twid,
  259. 'url': mobj.group('url'),
  260. 'height': height,
  261. 'width': width,
  262. 'thumbnail': thumbnail,
  263. })
  264. return info
  265. if 'class="PlayableMedia' in webpage:
  266. info.update({
  267. '_type': 'url_transparent',
  268. 'ie_key': 'TwitterCard',
  269. 'url': '%s//twitter.com/i/videos/tweet/%s' % (self.http_scheme(), twid),
  270. })
  271. return info
  272. raise ExtractorError('There\'s no video in this tweet.')
  273. class TwitterAmplifyIE(TwitterBaseIE):
  274. IE_NAME = 'twitter:amplify'
  275. _VALID_URL = 'https?://amp\.twimg\.com/v/(?P<id>[0-9a-f\-]{36})'
  276. _TEST = {
  277. 'url': 'https://amp.twimg.com/v/0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
  278. 'md5': '7df102d0b9fd7066b86f3159f8e81bf6',
  279. 'info_dict': {
  280. 'id': '0ba0c3c7-0af3-4c0a-bed5-7efd1ffa2951',
  281. 'ext': 'mp4',
  282. 'title': 'Twitter Video',
  283. 'thumbnail': 're:^https?://.*',
  284. },
  285. }
  286. def _real_extract(self, url):
  287. video_id = self._match_id(url)
  288. webpage = self._download_webpage(url, video_id)
  289. vmap_url = self._html_search_meta(
  290. 'twitter:amplify:vmap', webpage, 'vmap url')
  291. video_url = self._get_vmap_video_url(vmap_url, video_id)
  292. thumbnails = []
  293. thumbnail = self._html_search_meta(
  294. 'twitter:image:src', webpage, 'thumbnail', fatal=False)
  295. def _find_dimension(target):
  296. w = int_or_none(self._html_search_meta(
  297. 'twitter:%s:width' % target, webpage, fatal=False))
  298. h = int_or_none(self._html_search_meta(
  299. 'twitter:%s:height' % target, webpage, fatal=False))
  300. return w, h
  301. if thumbnail:
  302. thumbnail_w, thumbnail_h = _find_dimension('image')
  303. thumbnails.append({
  304. 'url': thumbnail,
  305. 'width': thumbnail_w,
  306. 'height': thumbnail_h,
  307. })
  308. video_w, video_h = _find_dimension('player')
  309. formats = [{
  310. 'url': video_url,
  311. 'width': video_w,
  312. 'height': video_h,
  313. }]
  314. return {
  315. 'id': video_id,
  316. 'title': 'Twitter Video',
  317. 'formats': formats,
  318. 'thumbnails': thumbnails,
  319. }