twitter.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. 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(self._html_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. formats.append({
  59. 'url': video_url,
  60. })
  61. break # same video regardless of UA
  62. continue
  63. video_url = config['playlist'][0]['source']
  64. f = {
  65. 'url': video_url,
  66. }
  67. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  68. if m:
  69. f.update({
  70. 'width': int(m.group('width')),
  71. 'height': int(m.group('height')),
  72. })
  73. formats.append(f)
  74. self._sort_formats(formats)
  75. thumbnail = config.get('posterImageUrl')
  76. duration = float_or_none(config.get('duration'))
  77. return {
  78. 'id': video_id,
  79. 'title': 'TwitterCard',
  80. 'thumbnail': thumbnail,
  81. 'duration': duration,
  82. 'formats': formats,
  83. }
  84. class TwitterIE(InfoExtractor):
  85. IE_NAME = 'twitter'
  86. _VALID_URL = r'https?://(?:www\.|m\.|mobile\.)?twitter\.com/(?P<user_id>[^/]+)/status/(?P<id>\d+)'
  87. _TEMPLATE_URL = 'https://twitter.com/%s/status/%s'
  88. _TEST = {
  89. 'url': 'https://twitter.com/freethenipple/status/643211948184596480',
  90. 'md5': '31cd83a116fc41f99ae3d909d4caf6a0',
  91. 'info_dict': {
  92. 'id': '643211948184596480',
  93. 'ext': 'mp4',
  94. 'title': 'FREE THE NIPPLE - 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. mobj = re.match(self._VALID_URL, url)
  104. user_id = mobj.group('user_id')
  105. twid = mobj.group('id')
  106. webpage = self._download_webpage(self._TEMPLATE_URL % (user_id, twid), twid)
  107. username = remove_end(self._og_search_title(webpage), ' on Twitter')
  108. title = self._og_search_description(webpage).strip('').replace('\n', ' ')
  109. # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  110. mobj = re.match(r'“(.*)\s+(https?://[^ ]+)”', title)
  111. title, short_url = mobj.groups()
  112. card_id = self._search_regex(
  113. r'["\']/i/cards/tfw/v1/(\d+)', webpage, 'twitter card url')
  114. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  115. return {
  116. '_type': 'url_transparent',
  117. 'ie_key': 'TwitterCard',
  118. 'uploader_id': user_id,
  119. 'uploader': username,
  120. 'url': card_url,
  121. 'webpage_url': url,
  122. 'description': '%s on Twitter: "%s %s"' % (username, title, short_url),
  123. 'title': username + ' - ' + title,
  124. }