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.

143 lines
5.2 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_urllib_request
  5. from ..utils import (
  6. float_or_none,
  7. unescapeHTML,
  8. )
  9. class TwitterCardIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:www\.)?twitter\.com/i/cards/tfw/v1/(?P<id>\d+)'
  11. _TESTS = [
  12. {
  13. 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
  14. 'md5': 'a74f50b310c83170319ba16de6955192',
  15. 'info_dict': {
  16. 'id': '560070183650213889',
  17. 'ext': 'mp4',
  18. 'title': 'TwitterCard',
  19. 'thumbnail': 're:^https?://.*\.jpg$',
  20. 'duration': 30.033,
  21. }
  22. },
  23. {
  24. 'url': 'https://twitter.com/i/cards/tfw/v1/623160978427936768',
  25. 'md5': '7ee2a553b63d1bccba97fbed97d9e1c8',
  26. 'info_dict': {
  27. 'id': '623160978427936768',
  28. 'ext': 'mp4',
  29. 'title': 'TwitterCard',
  30. 'thumbnail': 're:^https?://.*\.jpg',
  31. 'duration': 80.155,
  32. },
  33. }
  34. ]
  35. def _real_extract(self, url):
  36. video_id = self._match_id(url)
  37. # Different formats served for different User-Agents
  38. USER_AGENTS = [
  39. 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)', # mp4
  40. 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0', # webm
  41. ]
  42. config = None
  43. formats = []
  44. for user_agent in USER_AGENTS:
  45. request = compat_urllib_request.Request(url)
  46. request.add_header('User-Agent', user_agent)
  47. webpage = self._download_webpage(request, video_id)
  48. config = self._parse_json(
  49. unescapeHTML(self._search_regex(
  50. r'data-player-config="([^"]+)"', webpage, 'data player config')),
  51. video_id)
  52. if 'playlist' not in config:
  53. if 'vmapUrl' in config:
  54. webpage = self._download_webpage(config['vmapUrl'], video_id + ' (xml)')
  55. video_url = self._search_regex(
  56. r'<MediaFile>\s*<!\[CDATA\[(https?://.+?)\]\]>', webpage, 'data player config (xml)')
  57. f = {
  58. 'url': video_url,
  59. }
  60. ext = re.search(r'\.([a-z0-9]{2,4})(\?.+)?$', video_url)
  61. if ext:
  62. f['ext'] = ext.group(1)
  63. formats.append(f)
  64. break # same video regardless of UA
  65. continue
  66. video_url = config['playlist'][0]['source']
  67. f = {
  68. 'url': video_url,
  69. }
  70. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  71. if m:
  72. f.update({
  73. 'width': int(m.group('width')),
  74. 'height': int(m.group('height')),
  75. })
  76. formats.append(f)
  77. self._sort_formats(formats)
  78. thumbnail = config.get('posterImageUrl')
  79. duration = float_or_none(config.get('duration'))
  80. return {
  81. 'id': video_id,
  82. 'title': 'TwitterCard',
  83. 'thumbnail': thumbnail,
  84. 'duration': duration,
  85. 'formats': formats,
  86. }
  87. class TwitterIE(TwitterCardIE):
  88. _VALID_URL = r'https?://(?:www|m|mobile)?\.?twitter\.com/(?P<id>[^/]+/status/\d+)'
  89. _TESTS = [{
  90. 'url': 'https://m.twitter.com/thereaIbanksy/status/614301758345490432',
  91. 'md5': '8bbccb487bd7a31349b775915fcd412f',
  92. 'info_dict': {
  93. 'id': '614301758345490432',
  94. 'ext': 'mp4',
  95. 'title': 'thereaIbanksy - This time lapse is so pretty \U0001f60d\U0001f60d',
  96. 'thumbnail': 're:^https?://.*\.jpg',
  97. 'duration': 29.5,
  98. 'description': 'banksy on Twitter: "This time lapse is so pretty \U0001f60d\U0001f60d http://t.co/QB8DDbqiR1"',
  99. 'uploader': 'banksy',
  100. 'uploader_id': 'thereaIbanksy',
  101. },
  102. }]
  103. def _real_extract(self, url):
  104. id = self._match_id(url)
  105. username, twid = re.match(r'([^/]+)/status/(\d+)', id).groups()
  106. name = username
  107. url = re.sub(r'https?://(m|mobile)\.', 'https://', url)
  108. webpage = self._download_webpage(url, 'tweet: ' + url)
  109. description = unescapeHTML(self._search_regex('<title>\s*(.+?)\s*</title>', webpage, 'title'))
  110. title = description.replace('\n', ' ')
  111. splitdesc = re.match(r'^(.+?)\s*on Twitter:\s* "(.+?)"$', title)
  112. if splitdesc:
  113. name, title = splitdesc.groups()
  114. title = re.sub(r'\s*https?://[^ ]+', '', title) # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  115. card_id = self._search_regex(r'["\']/i/cards/tfw/v1/(\d+)', webpage, '/i/card/...')
  116. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  117. return {
  118. '_type': 'url_transparent',
  119. 'ie_key': 'TwitterCard',
  120. 'uploader_id': username,
  121. 'uploader': name,
  122. 'url': card_url,
  123. 'webpage_url': url,
  124. 'description': description,
  125. 'title': username + ' - ' + title,
  126. }