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.

166 lines
5.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. )
  11. class TwitterCardIE(InfoExtractor):
  12. IE_NAME = 'twitter:card'
  13. _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/cards/tfw/v1/(?P<id>\d+)'
  14. _TESTS = [
  15. {
  16. 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
  17. 'md5': '7d2f6b4d2eb841a7ccc893d479bfceb4',
  18. 'info_dict': {
  19. 'id': '560070183650213889',
  20. 'ext': 'mp4',
  21. 'title': 'TwitterCard',
  22. 'thumbnail': 're:^https?://.*\.jpg$',
  23. 'duration': 30.033,
  24. }
  25. },
  26. {
  27. 'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768',
  28. 'md5': '7ee2a553b63d1bccba97fbed97d9e1c8',
  29. 'info_dict': {
  30. 'id': '623160978427936768',
  31. 'ext': 'mp4',
  32. 'title': 'TwitterCard',
  33. 'thumbnail': 're:^https?://.*\.jpg',
  34. 'duration': 80.155,
  35. },
  36. },
  37. {
  38. 'url': 'https://twitter.com/i/cards/tfw/v1/654001591733886977',
  39. 'md5': 'b6f35e8b08a0bec6c8af77a2f4b3a814',
  40. 'info_dict': {
  41. 'id': 'dq4Oj5quskI',
  42. 'ext': 'mp4',
  43. 'title': 'Ubuntu 11.10 Overview',
  44. '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/',
  45. 'upload_date': '20111013',
  46. 'uploader': 'OMG! Ubuntu!',
  47. 'uploader_id': 'omgubuntu',
  48. },
  49. }
  50. ]
  51. def _real_extract(self, url):
  52. video_id = self._match_id(url)
  53. # Different formats served for different User-Agents
  54. USER_AGENTS = [
  55. 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)', # mp4
  56. 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0', # webm
  57. ]
  58. config = None
  59. formats = []
  60. for user_agent in USER_AGENTS:
  61. request = compat_urllib_request.Request(url)
  62. request.add_header('User-Agent', user_agent)
  63. webpage = self._download_webpage(request, video_id)
  64. youtube_url = self._html_search_regex(
  65. r'<iframe[^>]+src="((?:https?:)?//www.youtube.com/embed/[^"]+)"',
  66. webpage, 'youtube iframe', default=None)
  67. if youtube_url:
  68. return self.url_result(youtube_url, 'Youtube')
  69. config = self._parse_json(self._html_search_regex(
  70. r'data-player-config="([^"]+)"', webpage, 'data player config'),
  71. video_id)
  72. if 'playlist' not in config:
  73. if 'vmapUrl' in config:
  74. vmap_data = self._download_xml(config['vmapUrl'], video_id)
  75. video_url = xpath_text(vmap_data, './/MediaFile').strip()
  76. formats.append({
  77. 'url': video_url,
  78. })
  79. break # same video regardless of UA
  80. continue
  81. video_url = config['playlist'][0]['source']
  82. f = {
  83. 'url': video_url,
  84. }
  85. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  86. if m:
  87. f.update({
  88. 'width': int(m.group('width')),
  89. 'height': int(m.group('height')),
  90. })
  91. formats.append(f)
  92. self._sort_formats(formats)
  93. thumbnail = config.get('posterImageUrl')
  94. duration = float_or_none(config.get('duration'))
  95. return {
  96. 'id': video_id,
  97. 'title': 'TwitterCard',
  98. 'thumbnail': thumbnail,
  99. 'duration': duration,
  100. 'formats': formats,
  101. }
  102. class TwitterIE(InfoExtractor):
  103. IE_NAME = 'twitter'
  104. _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)'
  105. _TEMPLATE_URL = 'https://twitter.com/%s/status/%s'
  106. _TEST = {
  107. 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
  108. 'md5': '31cd83a116fc41f99ae3d909d4caf6a0',
  109. 'info_dict': {
  110. 'id': '643211948184596480',
  111. 'ext': 'mp4',
  112. 'title': 'FREE THE NIPPLE - FTN supporters on Hollywood Blvd today!',
  113. 'thumbnail': 're:^https?://.*\.jpg',
  114. 'duration': 12.922,
  115. 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
  116. 'uploader': 'FREE THE NIPPLE',
  117. 'uploader_id': 'freethenipple',
  118. },
  119. }
  120. def _real_extract(self, url):
  121. mobj = re.match(self._VALID_URL, url)
  122. user_id = mobj.group('user_id')
  123. twid = mobj.group('id')
  124. webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid)
  125. username = remove_end(self._og_search_title(webpage), ' on Twitter')
  126. title = self._og_search_description(webpage).strip('').replace('\n', ' ')
  127. # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  128. mobj = re.match(r'“(.*)\s+(https?://[^ ]+)”', title)
  129. title, short_url = mobj.groups()
  130. card_id = self._search_regex(
  131. r'["\']/i/cards/tfw/v1/(\d+)', webpage, 'twitter card url')
  132. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  133. return {
  134. '_type': 'url_transparent',
  135. 'ie_key': 'TwitterCard',
  136. 'uploader_id': user_id,
  137. 'uploader': username,
  138. 'url': card_url,
  139. 'webpage_url': url,
  140. 'description': '%s on Twitter: "%s %s"' % (username, title, short_url),
  141. 'title': username + ' - ' + title,
  142. }