twitter.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. unescapeHTML,
  9. xpath_text,
  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. def _real_extract(self, url):
  39. video_id = self._match_id(url)
  40. # Different formats served for different User-Agents
  41. USER_AGENTS = [
  42. 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)', # mp4
  43. 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0', # webm
  44. ]
  45. config = None
  46. formats = []
  47. for user_agent in USER_AGENTS:
  48. request = compat_urllib_request.Request(url)
  49. request.add_header('User-Agent', user_agent)
  50. webpage = self._download_webpage(request, video_id)
  51. config = self._parse_json(
  52. unescapeHTML(self._search_regex(
  53. r'data-player-config="([^"]+)"', webpage, 'data player config')),
  54. video_id)
  55. if 'playlist' not in config:
  56. if 'vmapUrl' in config:
  57. vmap_data = self._download_xml(config['vmapUrl'], video_id)
  58. video_url = xpath_text(vmap_data, './/MediaFile').strip()
  59. f = {
  60. 'url': video_url,
  61. }
  62. ext = re.search(r'\.([a-z0-9]{2,4})(\?.+)?$', video_url)
  63. if ext:
  64. f['ext'] = ext.group(1)
  65. formats.append(f)
  66. break # same video regardless of UA
  67. continue
  68. video_url = config['playlist'][0]['source']
  69. f = {
  70. 'url': video_url,
  71. }
  72. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  73. if m:
  74. f.update({
  75. 'width': int(m.group('width')),
  76. 'height': int(m.group('height')),
  77. })
  78. formats.append(f)
  79. self._sort_formats(formats)
  80. thumbnail = config.get('posterImageUrl')
  81. duration = float_or_none(config.get('duration'))
  82. return {
  83. 'id': video_id,
  84. 'title': 'TwitterCard',
  85. 'thumbnail': thumbnail,
  86. 'duration': duration,
  87. 'formats': formats,
  88. }
  89. class TwitterIE(TwitterCardIE):
  90. IE_NAME = 'twitter'
  91. _VALID_URL = r'https?://(?:www|m|mobile)?\.?twitter\.com/(?P<id>[^/]+/status/\d+)'
  92. _TESTS = [{
  93. 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
  94. 'md5': '31cd83a116fc41f99ae3d909d4caf6a0',
  95. 'info_dict': {
  96. 'id': '643211948184596480',
  97. 'ext': 'mp4',
  98. 'title': 'freethenipple - FTN supporters on Hollywood Blvd today!',
  99. 'thumbnail': 're:^https?://.*\.jpg',
  100. 'duration': 12.922,
  101. 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
  102. 'uploader': 'FREE THE NIPPLE',
  103. 'uploader_id': 'freethenipple',
  104. },
  105. }]
  106. def _real_extract(self, url):
  107. id = self._match_id(url)
  108. username, twid = re.match(r'([^/]+)/status/(\d+)', id).groups()
  109. name = username
  110. url = re.sub(r'https?://(m|mobile)\.', 'https://', url)
  111. webpage = self._download_webpage(url, 'tweet: ' + url)
  112. description = unescapeHTML(self._search_regex('<title>\s*(.+?)\s*</title>', webpage, 'title'))
  113. title = description.replace('\n', ' ')
  114. splitdesc = re.match(r'^(.+?)\s*on Twitter:\s* "(.+?)"$', title)
  115. if splitdesc:
  116. name, title = splitdesc.groups()
  117. title = re.sub(r'\s*https?://[^ ]+', '', title) # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  118. card_id = self._search_regex(r'["\']/i/cards/tfw/v1/(\d+)', webpage, '/i/card/...')
  119. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  120. return {
  121. '_type': 'url_transparent',
  122. 'ie_key': 'TwitterCard',
  123. 'uploader_id': username,
  124. 'uploader': name,
  125. 'url': card_url,
  126. 'webpage_url': url,
  127. 'description': description,
  128. 'title': username + ' - ' + title,
  129. }