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.

160 lines
5.8 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'https?://(?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. 'url': 'http://sutiblr.tumblr.com/post/139638707273',
  68. 'md5': '2dd184b3669e049ba40563a7d423f95c',
  69. 'info_dict': {
  70. 'id': 'ir7qBEIKqvq',
  71. 'ext': 'mp4',
  72. 'title': 'Vine by sutiblr',
  73. 'alt_title': 'Vine by sutiblr',
  74. 'uploader': 'sutiblr',
  75. 'uploader_id': '1198993975374495744',
  76. 'upload_date': '20160220',
  77. 'like_count': int,
  78. 'comment_count': int,
  79. 'repost_count': int,
  80. },
  81. 'add_ie': ['Vine'],
  82. }, {
  83. 'url': 'http://vitasidorkina.tumblr.com/post/134652425014/joskriver-victoriassecret-invisibility-or',
  84. 'md5': '01c12ceb82cbf6b2fe0703aa56b3ad72',
  85. 'info_dict': {
  86. 'id': '-7LnUPGlSo',
  87. 'ext': 'mp4',
  88. 'title': 'Video by victoriassecret',
  89. 'description': 'Invisibility or flight…which superpower would YOU choose? #VSFashionShow #ThisOrThat',
  90. 'uploader_id': 'victoriassecret',
  91. 'thumbnail': 're:^https?://.*\.jpg'
  92. },
  93. 'add_ie': ['Instagram'],
  94. }]
  95. def _real_extract(self, url):
  96. m_url = re.match(self._VALID_URL, url)
  97. video_id = m_url.group('id')
  98. blog = m_url.group('blog_name')
  99. url = 'http://%s.tumblr.com/post/%s/' % (blog, video_id)
  100. webpage, urlh = self._download_webpage_handle(url, video_id)
  101. iframe_url = self._search_regex(
  102. r'src=\'(https?://www\.tumblr\.com/video/[^\']+)\'',
  103. webpage, 'iframe url', default=None)
  104. if iframe_url is None:
  105. return self.url_result(urlh.geturl(), 'Generic')
  106. iframe = self._download_webpage(iframe_url, video_id, 'Downloading iframe page')
  107. duration = None
  108. sources = []
  109. sd_url = self._search_regex(
  110. r'<source[^>]+src=(["\'])(?P<url>.+?)\1', iframe,
  111. 'sd video url', default=None, group='url')
  112. if sd_url:
  113. sources.append((sd_url, 'sd'))
  114. options = self._parse_json(
  115. self._search_regex(
  116. r'data-crt-options=(["\'])(?P<options>.+?)\1', iframe,
  117. 'hd video url', default='', group='options'),
  118. video_id, fatal=False)
  119. if options:
  120. duration = int_or_none(options.get('duration'))
  121. hd_url = options.get('hdUrl')
  122. if hd_url:
  123. sources.append((hd_url, 'hd'))
  124. formats = [{
  125. 'url': video_url,
  126. 'ext': 'mp4',
  127. 'format_id': format_id,
  128. 'height': int_or_none(self._search_regex(
  129. r'/(\d{3,4})$', video_url, 'height', default=None)),
  130. 'quality': quality,
  131. } for quality, (video_url, format_id) in enumerate(sources)]
  132. self._sort_formats(formats)
  133. # The only place where you can get a title, it's not complete,
  134. # but searching in other places doesn't work for all videos
  135. video_title = self._html_search_regex(
  136. r'(?s)<title>(?P<title>.*?)(?: \| Tumblr)?</title>',
  137. webpage, 'title')
  138. return {
  139. 'id': video_id,
  140. 'title': video_title,
  141. 'description': self._og_search_description(webpage, default=None),
  142. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  143. 'duration': duration,
  144. 'formats': formats,
  145. }