twitter.py 5.1 KB

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