You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.2 KiB

  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. }