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.

213 lines
7.5 KiB

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