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.

214 lines
7.9 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_urllib_request
  6. from ..utils import (
  7. float_or_none,
  8. xpath_text,
  9. remove_end,
  10. int_or_none,
  11. ExtractorError,
  12. )
  13. class TwitterCardIE(InfoExtractor):
  14. IE_NAME = 'twitter:card'
  15. _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/cards/tfw/v1/(?P<id>\d+)'
  16. _TESTS = [
  17. {
  18. 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
  19. 'md5': '4fa26a35f9d1bf4b646590ba8e84be19',
  20. 'info_dict': {
  21. 'id': '560070183650213889',
  22. 'ext': 'mp4',
  23. 'title': 'TwitterCard',
  24. 'thumbnail': 're:^https?://.*\.jpg$',
  25. 'duration': 30.033,
  26. }
  27. },
  28. {
  29. 'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768',
  30. 'md5': '7ee2a553b63d1bccba97fbed97d9e1c8',
  31. 'info_dict': {
  32. 'id': '623160978427936768',
  33. 'ext': 'mp4',
  34. 'title': 'TwitterCard',
  35. 'thumbnail': 're:^https?://.*\.jpg',
  36. 'duration': 80.155,
  37. },
  38. },
  39. {
  40. 'url': 'https://twitter.com/i/cards/tfw/v1/654001591733886977',
  41. 'md5': 'b6f35e8b08a0bec6c8af77a2f4b3a814',
  42. 'info_dict': {
  43. 'id': 'dq4Oj5quskI',
  44. 'ext': 'mp4',
  45. 'title': 'Ubuntu 11.10 Overview',
  46. '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/',
  47. 'upload_date': '20111013',
  48. 'uploader': 'OMG! Ubuntu!',
  49. 'uploader_id': 'omgubuntu',
  50. },
  51. }
  52. ]
  53. def _real_extract(self, url):
  54. video_id = self._match_id(url)
  55. # Different formats served for different User-Agents
  56. USER_AGENTS = [
  57. 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)', # mp4
  58. 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0', # webm
  59. ]
  60. config = None
  61. formats = []
  62. for user_agent in USER_AGENTS:
  63. request = compat_urllib_request.Request(url)
  64. request.add_header('User-Agent', user_agent)
  65. webpage = self._download_webpage(request, video_id)
  66. youtube_url = self._html_search_regex(
  67. r'<iframe[^>]+src="((?:https?:)?//www.youtube.com/embed/[^"]+)"',
  68. webpage, 'youtube iframe', default=None)
  69. if youtube_url:
  70. return self.url_result(youtube_url, 'Youtube')
  71. config = self._parse_json(self._html_search_regex(
  72. r'data-player-config="([^"]+)"', webpage, 'data player config'),
  73. video_id)
  74. if 'playlist' not in config:
  75. if 'vmapUrl' in config:
  76. vmap_data = self._download_xml(config['vmapUrl'], video_id)
  77. video_url = xpath_text(vmap_data, './/MediaFile').strip()
  78. formats.append({
  79. 'url': video_url,
  80. })
  81. break # same video regardless of UA
  82. continue
  83. video_url = config['playlist'][0]['source']
  84. f = {
  85. 'url': video_url,
  86. }
  87. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  88. if m:
  89. f.update({
  90. 'width': int(m.group('width')),
  91. 'height': int(m.group('height')),
  92. })
  93. formats.append(f)
  94. self._sort_formats(formats)
  95. thumbnail = config.get('posterImageUrl')
  96. duration = float_or_none(config.get('duration'))
  97. return {
  98. 'id': video_id,
  99. 'title': 'TwitterCard',
  100. 'thumbnail': thumbnail,
  101. 'duration': duration,
  102. 'formats': formats,
  103. }
  104. class TwitterIE(InfoExtractor):
  105. IE_NAME = 'twitter'
  106. _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)'
  107. _TEMPLATE_URL = 'https://twitter.com/%s/status/%s'
  108. _TESTS = [{
  109. 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
  110. 'md5': 'db6612ec5d03355953c3ca9250c97e5e',
  111. 'info_dict': {
  112. 'id': '643211948184596480',
  113. 'ext': 'mp4',
  114. 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
  115. 'thumbnail': 're:^https?://.*\.jpg',
  116. 'duration': 12.922,
  117. 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
  118. 'uploader': 'FREE THE NIPPLE',
  119. 'uploader_id': 'freethenipple',
  120. },
  121. }, {
  122. 'url': 'https://twitter.com/giphz/status/657991469417025536/photo/1',
  123. 'md5': 'f36dcd5fb92bf7057f155e7d927eeb42',
  124. 'info_dict': {
  125. 'id': '657991469417025536',
  126. 'ext': 'mp4',
  127. 'title': 'Gifs - tu vai cai tu vai cai tu nao eh capaz disso tu vai cai',
  128. 'description': 'Gifs on Twitter: "tu vai cai tu vai cai tu nao eh capaz disso tu vai cai https://t.co/tM46VHFlO5"',
  129. 'thumbnail': 're:^https?://.*\.png',
  130. 'uploader': 'Gifs',
  131. 'uploader_id': 'giphz',
  132. },
  133. }, {
  134. 'url': 'https://twitter.com/starwars/status/665052190608723968',
  135. 'md5': '39b7199856dee6cd4432e72c74bc69d4',
  136. 'info_dict': {
  137. 'id': '665052190608723968',
  138. 'ext': 'mp4',
  139. 'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.',
  140. 'description': 'Star Wars on Twitter: "A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens."',
  141. 'uploader_id': 'starwars',
  142. 'uploader': 'Star Wars',
  143. },
  144. }]
  145. def _real_extract(self, url):
  146. mobj = re.match(self._VALID_URL, url)
  147. user_id = mobj.group('user_id')
  148. twid = mobj.group('id')
  149. webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid)
  150. username = remove_end(self._og_search_title(webpage), ' on Twitter')
  151. title = description = self._og_search_description(webpage).strip('').replace('\n', ' ').strip('“”')
  152. # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  153. title = re.sub(r'\s+(https?://[^ ]+)', '', title)
  154. info = {
  155. 'uploader_id': user_id,
  156. 'uploader': username,
  157. 'webpage_url': url,
  158. 'description': '%s on Twitter: "%s"' % (username, description),
  159. 'title': username + ' - ' + title,
  160. }
  161. card_id = self._search_regex(
  162. r'["\']/i/cards/tfw/v1/(\d+)', webpage, 'twitter card url', default=None)
  163. if card_id:
  164. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  165. info.update({
  166. '_type': 'url_transparent',
  167. 'ie_key': 'TwitterCard',
  168. 'url': card_url,
  169. })
  170. return info
  171. mobj = re.search(r'''(?x)
  172. <video[^>]+class="animated-gif"[^>]+
  173. (?:data-height="(?P<height>\d+)")?[^>]+
  174. (?:data-width="(?P<width>\d+)")?[^>]+
  175. (?:poster="(?P<poster>[^"]+)")?[^>]*>\s*
  176. <source[^>]+video-src="(?P<url>[^"]+)"
  177. ''', webpage)
  178. if mobj:
  179. info.update({
  180. 'id': twid,
  181. 'url': mobj.group('url'),
  182. 'height': int_or_none(mobj.group('height')),
  183. 'width': int_or_none(mobj.group('width')),
  184. 'thumbnail': mobj.group('poster'),
  185. })
  186. return info
  187. raise ExtractorError('There\'s not video in this tweet.')