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.

132 lines
4.7 KiB

  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import int_or_none
  6. class TumblrIE(InfoExtractor):
  7. _VALID_URL = r'http://(?P<blog_name>.*?)\.tumblr\.com/(?:post|video)/(?P<id>[0-9]+)(?:$|[/?#])'
  8. _TESTS = [{
  9. 'url': 'http://tatianamaslanydaily.tumblr.com/post/54196191430/orphan-black-dvd-extra-behind-the-scenes',
  10. 'md5': '479bb068e5b16462f5176a6828829767',
  11. 'info_dict': {
  12. 'id': '54196191430',
  13. 'ext': 'mp4',
  14. 'title': 'tatiana maslany news, Orphan Black || DVD extra - behind the scenes ↳...',
  15. 'description': 'md5:37db8211e40b50c7c44e95da14f630b7',
  16. 'thumbnail': 're:http://.*\.jpg',
  17. }
  18. }, {
  19. 'url': 'http://5sostrum.tumblr.com/post/90208453769/yall-forgetting-the-greatest-keek-of-them-all',
  20. 'md5': 'bf348ef8c0ef84fbf1cbd6fa6e000359',
  21. 'info_dict': {
  22. 'id': '90208453769',
  23. 'ext': 'mp4',
  24. 'title': '5SOS STRUM ;]',
  25. 'description': 'md5:dba62ac8639482759c8eb10ce474586a',
  26. 'thumbnail': 're:http://.*\.jpg',
  27. }
  28. }, {
  29. 'url': 'http://hdvideotest.tumblr.com/post/130323439814/test-description-for-my-hd-video',
  30. 'md5': '7ae503065ad150122dc3089f8cf1546c',
  31. 'info_dict': {
  32. 'id': '130323439814',
  33. 'ext': 'mp4',
  34. 'title': 'HD Video Testing \u2014 Test description for my HD video',
  35. 'description': 'md5:97cc3ab5fcd27ee4af6356701541319c',
  36. 'thumbnail': 're:http://.*\.jpg',
  37. },
  38. 'params': {
  39. 'format': 'hd',
  40. },
  41. }, {
  42. 'url': 'http://naked-yogi.tumblr.com/post/118312946248/naked-smoking-stretching',
  43. 'md5': 'de07e5211d60d4f3a2c3df757ea9f6ab',
  44. 'info_dict': {
  45. 'id': 'Wmur',
  46. 'ext': 'mp4',
  47. 'title': 'naked smoking & stretching',
  48. 'upload_date': '20150506',
  49. 'timestamp': 1430931613,
  50. 'age_limit': 18,
  51. 'uploader_id': '1638622',
  52. 'uploader': 'naked-yogi',
  53. },
  54. 'add_ie': ['Vidme'],
  55. }, {
  56. 'url': 'http://camdamage.tumblr.com/post/98846056295/',
  57. 'md5': 'a9e0c8371ea1ca306d6554e3fecf50b6',
  58. 'info_dict': {
  59. 'id': '105463834',
  60. 'ext': 'mp4',
  61. 'title': 'Cam Damage-HD 720p',
  62. 'uploader': 'John Moyer',
  63. 'uploader_id': 'user32021558',
  64. },
  65. 'add_ie': ['Vimeo'],
  66. }]
  67. def _real_extract(self, url):
  68. m_url = re.match(self._VALID_URL, url)
  69. video_id = m_url.group('id')
  70. blog = m_url.group('blog_name')
  71. url = 'http://%s.tumblr.com/post/%s/' % (blog, video_id)
  72. webpage, urlh = self._download_webpage_handle(url, video_id)
  73. iframe_url = self._search_regex(
  74. r'src=\'(https?://www\.tumblr\.com/video/[^\']+)\'',
  75. webpage, 'iframe url', default=None)
  76. if iframe_url is None:
  77. return self.url_result(urlh.geturl(), 'Generic')
  78. iframe = self._download_webpage(iframe_url, video_id, 'Downloading iframe page')
  79. duration = None
  80. sources = []
  81. sd_url = self._search_regex(
  82. r'<source[^>]+src=(["\'])(?P<url>.+?)\1', iframe,
  83. 'sd video url', default=None, group='url')
  84. if sd_url:
  85. sources.append((sd_url, 'sd'))
  86. options = self._parse_json(
  87. self._search_regex(
  88. r'data-crt-options=(["\'])(?P<options>.+?)\1', iframe,
  89. 'hd video url', default='', group='options'),
  90. video_id, fatal=False)
  91. if options:
  92. duration = int_or_none(options.get('duration'))
  93. hd_url = options.get('hdUrl')
  94. if hd_url:
  95. sources.append((hd_url, 'hd'))
  96. formats = [{
  97. 'url': video_url,
  98. 'ext': 'mp4',
  99. 'format_id': format_id,
  100. 'height': int_or_none(self._search_regex(
  101. r'/(\d{3,4})$', video_url, 'height', default=None)),
  102. 'quality': quality,
  103. } for quality, (video_url, format_id) in enumerate(sources)]
  104. self._sort_formats(formats)
  105. # The only place where you can get a title, it's not complete,
  106. # but searching in other places doesn't work for all videos
  107. video_title = self._html_search_regex(
  108. r'(?s)<title>(?P<title>.*?)(?: \| Tumblr)?</title>',
  109. webpage, 'title')
  110. return {
  111. 'id': video_id,
  112. 'title': video_title,
  113. 'description': self._og_search_description(webpage, default=None),
  114. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  115. 'duration': duration,
  116. 'formats': formats,
  117. }