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.

214 lines
7.6 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. ExtractorError,
  8. int_or_none,
  9. urlencode_postdata
  10. )
  11. class TumblrIE(InfoExtractor):
  12. _VALID_URL = r'https?://(?P<blog_name>[^/?#&]+)\.tumblr\.com/(?:post|video)/(?P<id>[0-9]+)(?:$|[/?#])'
  13. _NETRC_MACHINE = 'tumblr'
  14. _LOGIN_URL = 'https://www.tumblr.com/login'
  15. _TESTS = [{
  16. 'url': 'http://tatianamaslanydaily.tumblr.com/post/54196191430/orphan-black-dvd-extra-behind-the-scenes',
  17. 'md5': '479bb068e5b16462f5176a6828829767',
  18. 'info_dict': {
  19. 'id': '54196191430',
  20. 'ext': 'mp4',
  21. 'title': 'tatiana maslany news, Orphan Black || DVD extra - behind the scenes ↳...',
  22. 'description': 'md5:37db8211e40b50c7c44e95da14f630b7',
  23. 'thumbnail': r're:http://.*\.jpg',
  24. }
  25. }, {
  26. 'url': 'http://5sostrum.tumblr.com/post/90208453769/yall-forgetting-the-greatest-keek-of-them-all',
  27. 'md5': 'bf348ef8c0ef84fbf1cbd6fa6e000359',
  28. 'info_dict': {
  29. 'id': '90208453769',
  30. 'ext': 'mp4',
  31. 'title': '5SOS STRUM ;]',
  32. 'description': 'md5:dba62ac8639482759c8eb10ce474586a',
  33. 'thumbnail': r're:http://.*\.jpg',
  34. }
  35. }, {
  36. 'url': 'http://hdvideotest.tumblr.com/post/130323439814/test-description-for-my-hd-video',
  37. 'md5': '7ae503065ad150122dc3089f8cf1546c',
  38. 'info_dict': {
  39. 'id': '130323439814',
  40. 'ext': 'mp4',
  41. 'title': 'HD Video Testing \u2014 Test description for my HD video',
  42. 'description': 'md5:97cc3ab5fcd27ee4af6356701541319c',
  43. 'thumbnail': r're:http://.*\.jpg',
  44. },
  45. 'params': {
  46. 'format': 'hd',
  47. },
  48. }, {
  49. 'url': 'http://naked-yogi.tumblr.com/post/118312946248/naked-smoking-stretching',
  50. 'md5': 'de07e5211d60d4f3a2c3df757ea9f6ab',
  51. 'info_dict': {
  52. 'id': 'Wmur',
  53. 'ext': 'mp4',
  54. 'title': 'naked smoking & stretching',
  55. 'upload_date': '20150506',
  56. 'timestamp': 1430931613,
  57. 'age_limit': 18,
  58. 'uploader_id': '1638622',
  59. 'uploader': 'naked-yogi',
  60. },
  61. 'add_ie': ['Vidme'],
  62. }, {
  63. 'url': 'http://camdamage.tumblr.com/post/98846056295/',
  64. 'md5': 'a9e0c8371ea1ca306d6554e3fecf50b6',
  65. 'info_dict': {
  66. 'id': '105463834',
  67. 'ext': 'mp4',
  68. 'title': 'Cam Damage-HD 720p',
  69. 'uploader': 'John Moyer',
  70. 'uploader_id': 'user32021558',
  71. },
  72. 'add_ie': ['Vimeo'],
  73. }, {
  74. 'url': 'http://sutiblr.tumblr.com/post/139638707273',
  75. 'md5': '2dd184b3669e049ba40563a7d423f95c',
  76. 'info_dict': {
  77. 'id': 'ir7qBEIKqvq',
  78. 'ext': 'mp4',
  79. 'title': 'Vine by sutiblr',
  80. 'alt_title': 'Vine by sutiblr',
  81. 'uploader': 'sutiblr',
  82. 'uploader_id': '1198993975374495744',
  83. 'upload_date': '20160220',
  84. 'like_count': int,
  85. 'comment_count': int,
  86. 'repost_count': int,
  87. },
  88. 'add_ie': ['Vine'],
  89. }, {
  90. 'url': 'http://vitasidorkina.tumblr.com/post/134652425014/joskriver-victoriassecret-invisibility-or',
  91. 'md5': '01c12ceb82cbf6b2fe0703aa56b3ad72',
  92. 'info_dict': {
  93. 'id': '-7LnUPGlSo',
  94. 'ext': 'mp4',
  95. 'title': 'Video by victoriassecret',
  96. 'description': 'Invisibility or flight…which superpower would YOU choose? #VSFashionShow #ThisOrThat',
  97. 'uploader_id': 'victoriassecret',
  98. 'thumbnail': r're:^https?://.*\.jpg'
  99. },
  100. 'add_ie': ['Instagram'],
  101. }]
  102. def _real_initialize(self):
  103. self._login()
  104. def _login(self):
  105. username, password = self._get_login_info()
  106. if username is None:
  107. return
  108. login_page = self._download_webpage(
  109. self._LOGIN_URL, None, 'Downloading login page')
  110. login_form = self._hidden_inputs(login_page)
  111. login_form.update({
  112. 'user[email]': username,
  113. 'user[password]': password
  114. })
  115. response, urlh = self._download_webpage_handle(
  116. self._LOGIN_URL, None, 'Logging in',
  117. data=urlencode_postdata(login_form), headers={
  118. 'Content-Type': 'application/x-www-form-urlencoded',
  119. 'Referer': self._LOGIN_URL,
  120. })
  121. # Successful login
  122. if '/dashboard' in urlh.geturl():
  123. return
  124. login_errors = self._parse_json(
  125. self._search_regex(
  126. r'RegistrationForm\.errors\s*=\s*(\[.+?\])\s*;', response,
  127. 'login errors', default='[]'),
  128. None, fatal=False)
  129. if login_errors:
  130. raise ExtractorError(
  131. 'Unable to login: %s' % login_errors[0], expected=True)
  132. self.report_warning('Login has probably failed')
  133. def _real_extract(self, url):
  134. m_url = re.match(self._VALID_URL, url)
  135. video_id = m_url.group('id')
  136. blog = m_url.group('blog_name')
  137. url = 'http://%s.tumblr.com/post/%s/' % (blog, video_id)
  138. webpage, urlh = self._download_webpage_handle(url, video_id)
  139. redirect_url = compat_str(urlh.geturl())
  140. if 'tumblr.com/safe-mode' in redirect_url or redirect_url.startswith('/safe-mode'):
  141. raise ExtractorError(
  142. 'This Tumblr may contain sensitive media. '
  143. 'Disable safe mode in your account settings '
  144. 'at https://www.tumblr.com/settings/account#safe_mode',
  145. expected=True)
  146. iframe_url = self._search_regex(
  147. r'src=\'(https?://www\.tumblr\.com/video/[^\']+)\'',
  148. webpage, 'iframe url', default=None)
  149. if iframe_url is None:
  150. return self.url_result(redirect_url, 'Generic')
  151. iframe = self._download_webpage(iframe_url, video_id, 'Downloading iframe page')
  152. duration = None
  153. sources = []
  154. sd_url = self._search_regex(
  155. r'<source[^>]+src=(["\'])(?P<url>.+?)\1', iframe,
  156. 'sd video url', default=None, group='url')
  157. if sd_url:
  158. sources.append((sd_url, 'sd'))
  159. options = self._parse_json(
  160. self._search_regex(
  161. r'data-crt-options=(["\'])(?P<options>.+?)\1', iframe,
  162. 'hd video url', default='', group='options'),
  163. video_id, fatal=False)
  164. if options:
  165. duration = int_or_none(options.get('duration'))
  166. hd_url = options.get('hdUrl')
  167. if hd_url:
  168. sources.append((hd_url, 'hd'))
  169. formats = [{
  170. 'url': video_url,
  171. 'ext': 'mp4',
  172. 'format_id': format_id,
  173. 'height': int_or_none(self._search_regex(
  174. r'/(\d{3,4})$', video_url, 'height', default=None)),
  175. 'quality': quality,
  176. } for quality, (video_url, format_id) in enumerate(sources)]
  177. self._sort_formats(formats)
  178. # The only place where you can get a title, it's not complete,
  179. # but searching in other places doesn't work for all videos
  180. video_title = self._html_search_regex(
  181. r'(?s)<title>(?P<title>.*?)(?: \| Tumblr)?</title>',
  182. webpage, 'title')
  183. return {
  184. 'id': video_id,
  185. 'title': video_title,
  186. 'description': self._og_search_description(webpage, default=None),
  187. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  188. 'duration': duration,
  189. 'formats': formats,
  190. }