twitter.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. formats.append({
  60. 'url': video_url,
  61. })
  62. break # same video regardless of UA
  63. continue
  64. video_url = config['playlist'][0]['source']
  65. f = {
  66. 'url': video_url,
  67. }
  68. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  69. if m:
  70. f.update({
  71. 'width': int(m.group('width')),
  72. 'height': int(m.group('height')),
  73. })
  74. formats.append(f)
  75. self._sort_formats(formats)
  76. thumbnail = config.get('posterImageUrl')
  77. duration = float_or_none(config.get('duration'))
  78. return {
  79. 'id': video_id,
  80. 'title': 'TwitterCard',
  81. 'thumbnail': thumbnail,
  82. 'duration': duration,
  83. 'formats': formats,
  84. }
  85. class TwitterIE(InfoExtractor):
  86. IE_NAME = 'twitter'
  87. _VALID_URL = r'https?://(?:www|m|mobile)?\.?twitter\.com/(?P<id>[^/]+/status/\d+)'
  88. _TEST = {
  89. 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
  90. 'md5': '31cd83a116fc41f99ae3d909d4caf6a0',
  91. 'info_dict': {
  92. 'id': '643211948184596480',
  93. 'ext': 'mp4',
  94. 'title': 'freethenipple - FTN supporters on Hollywood Blvd today!',
  95. 'thumbnail': 're:^https?://.*\.jpg',
  96. 'duration': 12.922,
  97. 'description': 'FREE THE NIPPLE on Twitter: "FTN supporters on Hollywood Blvd today! http://t.co/c7jHH749xJ"',
  98. 'uploader': 'FREE THE NIPPLE',
  99. 'uploader_id': 'freethenipple',
  100. },
  101. }
  102. def _real_extract(self, url):
  103. id = self._match_id(url)
  104. username, twid = re.match(r'([^/]+)/status/(\d+)', id).groups()
  105. name = username
  106. url = re.sub(r'https?://(m|mobile)\.', 'https://', url)
  107. webpage = self._download_webpage(url, 'tweet: ' + url)
  108. description = self._html_search_regex('<title>\s*(.+?)\s*</title>', webpage, 'title')
  109. title = description.replace('\n', ' ')
  110. splitdesc = re.match(r'^(.+?)\s*on Twitter:\s* "(.+?)"$', title)
  111. if splitdesc:
  112. name, title = splitdesc.groups()
  113. title = re.sub(r'\s*https?://[^ ]+', '', title) # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  114. card_id = self._search_regex(r'["\']/i/cards/tfw/v1/(\d+)', webpage, '/i/card/...')
  115. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  116. return {
  117. '_type': 'url_transparent',
  118. 'ie_key': 'TwitterCard',
  119. 'uploader_id': username,
  120. 'uploader': name,
  121. 'url': card_url,
  122. 'webpage_url': url,
  123. 'description': description,
  124. 'title': username + ' - ' + title,
  125. }