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.

228 lines
8.4 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. 'add_ie': ['Youtube'],
  52. },
  53. {
  54. 'url': 'https://twitter.com/i/cards/tfw/v1/665289828897005568',
  55. 'md5': 'ab2745d0b0ce53319a534fccaa986439',
  56. 'info_dict': {
  57. 'id': 'iBb2x00UVlv',
  58. 'ext': 'mp4',
  59. 'upload_date': '20151113',
  60. 'uploader_id': '1189339351084113920',
  61. 'uploader': '@ArsenalTerje',
  62. 'title': 'Vine by @ArsenalTerje',
  63. },
  64. 'add_ie': ['Vine'],
  65. }
  66. ]
  67. def _real_extract(self, url):
  68. video_id = self._match_id(url)
  69. # Different formats served for different User-Agents
  70. USER_AGENTS = [
  71. 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)', # mp4
  72. 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0', # webm
  73. ]
  74. config = None
  75. formats = []
  76. for user_agent in USER_AGENTS:
  77. request = compat_urllib_request.Request(url)
  78. request.add_header('User-Agent', user_agent)
  79. webpage = self._download_webpage(request, video_id)
  80. iframe_url = self._html_search_regex(
  81. r'<iframe[^>]+src="((?:https?:)?//(?:www.youtube.com/embed/[^"]+|(?:www\.)?vine\.co/v/\w+/card))"',
  82. webpage, 'video iframe', default=None)
  83. if iframe_url:
  84. return self.url_result(iframe_url)
  85. config = self._parse_json(self._html_search_regex(
  86. r'data-player-config="([^"]+)"', webpage, 'data player config'),
  87. video_id)
  88. if 'playlist' not in config:
  89. if 'vmapUrl' in config:
  90. vmap_data = self._download_xml(config['vmapUrl'], video_id)
  91. video_url = xpath_text(vmap_data, './/MediaFile').strip()
  92. formats.append({
  93. 'url': video_url,
  94. })
  95. break # same video regardless of UA
  96. continue
  97. video_url = config['playlist'][0]['source']
  98. f = {
  99. 'url': video_url,
  100. }
  101. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  102. if m:
  103. f.update({
  104. 'width': int(m.group('width')),
  105. 'height': int(m.group('height')),
  106. })
  107. formats.append(f)
  108. self._sort_formats(formats)
  109. thumbnail = config.get('posterImageUrl')
  110. duration = float_or_none(config.get('duration'))
  111. return {
  112. 'id': video_id,
  113. 'title': 'TwitterCard',
  114. 'thumbnail': thumbnail,
  115. 'duration': duration,
  116. 'formats': formats,
  117. }
  118. class TwitterIE(InfoExtractor):
  119. IE_NAME = 'twitter'
  120. _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)'
  121. _TEMPLATE_URL = 'https://twitter.com/%s/status/%s'
  122. _TESTS = [{
  123. 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
  124. 'md5': 'db6612ec5d03355953c3ca9250c97e5e',
  125. 'info_dict': {
  126. 'id': '643211948184596480',
  127. 'ext': 'mp4',
  128. 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
  129. 'thumbnail': 're:^https?://.*\.jpg',
  130. 'duration': 12.922,
  131. 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
  132. 'uploader': 'FREE THE NIPPLE',
  133. 'uploader_id': 'freethenipple',
  134. },
  135. }, {
  136. 'url': 'https://twitter.com/giphz/status/657991469417025536/photo/1',
  137. 'md5': 'f36dcd5fb92bf7057f155e7d927eeb42',
  138. 'info_dict': {
  139. 'id': '657991469417025536',
  140. 'ext': 'mp4',
  141. 'title': 'Gifs - tu vai cai tu vai cai tu nao eh capaz disso tu vai cai',
  142. 'description': 'Gifs on Twitter: "tu vai cai tu vai cai tu nao eh capaz disso tu vai cai https://t.co/tM46VHFlO5"',
  143. 'thumbnail': 're:^https?://.*\.png',
  144. 'uploader': 'Gifs',
  145. 'uploader_id': 'giphz',
  146. },
  147. }, {
  148. 'url': 'https://twitter.com/starwars/status/665052190608723968',
  149. 'md5': '39b7199856dee6cd4432e72c74bc69d4',
  150. 'info_dict': {
  151. 'id': '665052190608723968',
  152. 'ext': 'mp4',
  153. 'title': 'Star Wars - A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens.',
  154. 'description': 'Star Wars on Twitter: "A new beginning is coming December 18. Watch the official 60 second #TV spot for #StarWars: #TheForceAwakens."',
  155. 'uploader_id': 'starwars',
  156. 'uploader': 'Star Wars',
  157. },
  158. }]
  159. def _real_extract(self, url):
  160. mobj = re.match(self._VALID_URL, url)
  161. user_id = mobj.group('user_id')
  162. twid = mobj.group('id')
  163. webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid)
  164. username = remove_end(self._og_search_title(webpage), ' on Twitter')
  165. title = description = self._og_search_description(webpage).strip('').replace('\n', ' ').strip('“”')
  166. # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  167. title = re.sub(r'\s+(https?://[^ ]+)', '', title)
  168. info = {
  169. 'uploader_id': user_id,
  170. 'uploader': username,
  171. 'webpage_url': url,
  172. 'description': '%s on Twitter: "%s"' % (username, description),
  173. 'title': username + ' - ' + title,
  174. }
  175. card_id = self._search_regex(
  176. r'["\']/i/cards/tfw/v1/(\d+)', webpage, 'twitter card url', default=None)
  177. if card_id:
  178. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  179. info.update({
  180. '_type': 'url_transparent',
  181. 'ie_key': 'TwitterCard',
  182. 'url': card_url,
  183. })
  184. return info
  185. mobj = re.search(r'''(?x)
  186. <video[^>]+class="animated-gif"[^>]+
  187. (?:data-height="(?P<height>\d+)")?[^>]+
  188. (?:data-width="(?P<width>\d+)")?[^>]+
  189. (?:poster="(?P<poster>[^"]+)")?[^>]*>\s*
  190. <source[^>]+video-src="(?P<url>[^"]+)"
  191. ''', webpage)
  192. if mobj:
  193. info.update({
  194. 'id': twid,
  195. 'url': mobj.group('url'),
  196. 'height': int_or_none(mobj.group('height')),
  197. 'width': int_or_none(mobj.group('width')),
  198. 'thumbnail': mobj.group('poster'),
  199. })
  200. return info
  201. raise ExtractorError('There\'s not video in this tweet.')