twitter.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. _TEST = {
  12. 'url': 'https://twitter.com/i/cards/tfw/v1/560070183650213889',
  13. 'md5': 'a74f50b310c83170319ba16de6955192',
  14. 'info_dict': {
  15. 'id': '560070183650213889',
  16. 'ext': 'mp4',
  17. 'title': 'TwitterCard',
  18. 'thumbnail': 're:^https?://.*\.jpg$',
  19. 'duration': 30.033,
  20. },
  21. }
  22. def _real_extract(self, url):
  23. video_id = self._match_id(url)
  24. # Different formats served for different User-Agents
  25. USER_AGENTS = [
  26. 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/20.0 (Chrome)', # mp4
  27. 'Mozilla/5.0 (Windows NT 5.2; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0', # webm
  28. ]
  29. config = None
  30. formats = []
  31. for user_agent in USER_AGENTS:
  32. request = compat_urllib_request.Request(url)
  33. request.add_header('User-Agent', user_agent)
  34. webpage = self._download_webpage(request, video_id)
  35. config = self._parse_json(
  36. unescapeHTML(self._search_regex(
  37. r'data-player-config="([^"]+)"', webpage, 'data player config')),
  38. video_id)
  39. video_url = config['playlist'][0]['source']
  40. f = {
  41. 'url': video_url,
  42. }
  43. m = re.search(r'/(?P<width>\d+)x(?P<height>\d+)/', video_url)
  44. if m:
  45. f.update({
  46. 'width': int(m.group('width')),
  47. 'height': int(m.group('height')),
  48. })
  49. formats.append(f)
  50. self._sort_formats(formats)
  51. thumbnail = config.get('posterImageUrl')
  52. duration = float_or_none(config.get('duration'))
  53. return {
  54. 'id': video_id,
  55. 'title': 'TwitterCard',
  56. 'thumbnail': thumbnail,
  57. 'duration': duration,
  58. 'formats': formats,
  59. }
  60. class TwitterIE(TwitterCardIE):
  61. _VALID_URL = r'https?://(?:www|m|mobile)?\.?twitter\.com/(?P<id>[^/]+/status/\d+)'
  62. _TESTS = [{
  63. 'url': 'https://m.twitter.com/thereaIbanksy/status/614301758345490432',
  64. 'md5': '8bbccb487bd7a31349b775915fcd412f',
  65. 'info_dict': {
  66. 'id': '614301758345490432',
  67. 'ext': 'mp4',
  68. 'title': 'thereaIbanksy - This time lapse is so pretty \U0001f60d\U0001f60d',
  69. 'thumbnail': 're:^https?://.*\.jpg',
  70. 'duration': 29.5,
  71. 'description': 'banksy on Twitter: "This time lapse is so pretty \U0001f60d\U0001f60d http://t.co/QB8DDbqiR1"',
  72. 'uploader': 'banksy',
  73. 'uploader_id': 'thereaIbanksy',
  74. },
  75. }]
  76. def _real_extract(self, url):
  77. id = self._match_id(url)
  78. username, twid = re.match(r'([^/]+)/status/(\d+)', id).groups()
  79. name = username
  80. url = re.sub(r'https?://(m|mobile)\.', 'https://', url)
  81. webpage = self._download_webpage(url, 'tweet: ' + url)
  82. description = unescapeHTML(self._search_regex('<title>\s*(.+?)\s*</title>', webpage, 'title'))
  83. title = description.replace('\n', ' ')
  84. splitdesc = re.match(r'^(.+?)\s*on Twitter:\s* "(.+?)"$', title)
  85. if splitdesc:
  86. name, title = splitdesc.groups()
  87. title = re.sub(r'\s*https?://[^ ]+', '', title) # strip 'https -_t.co_BJYgOjSeGA' junk from filenames
  88. card_id = self._search_regex(r'["\']/i/cards/tfw/v1/(\d+)', webpage, '/i/card/...')
  89. card_url = 'https://twitter.com/i/cards/tfw/v1/' + card_id
  90. return {
  91. '_type': 'url_transparent',
  92. 'ie_key': 'TwitterCard',
  93. 'uploader_id': username,
  94. 'uploader': name,
  95. 'url': card_url,
  96. 'webpage_url': url,
  97. 'description': description,
  98. 'title': username + ' - ' + title,
  99. }