tumblr.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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': '99a84522f60972bf064a0b80f87bcbb5',
  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': 'sd',
  40. },
  41. }, {
  42. 'url': 'http://hdvideotest.tumblr.com/post/130323439814/test-description-for-my-hd-video',
  43. 'md5': '7ae503065ad150122dc3089f8cf1546c',
  44. 'info_dict': {
  45. 'id': '130323439814',
  46. 'ext': 'mp4',
  47. 'title': 'HD Video Testing \u2014 Test description for my HD video',
  48. 'description': 'md5:97cc3ab5fcd27ee4af6356701541319c',
  49. 'thumbnail': 're:http://.*\.jpg',
  50. },
  51. 'params': {
  52. 'format': 'hd',
  53. },
  54. }, {
  55. 'url': 'http://naked-yogi.tumblr.com/post/118312946248/naked-smoking-stretching',
  56. 'md5': 'de07e5211d60d4f3a2c3df757ea9f6ab',
  57. 'info_dict': {
  58. 'id': 'Wmur',
  59. 'ext': 'mp4',
  60. 'title': 'naked smoking & stretching',
  61. 'upload_date': '20150506',
  62. 'timestamp': 1430931613,
  63. },
  64. 'add_ie': ['Vidme'],
  65. }, {
  66. 'url': 'http://camdamage.tumblr.com/post/98846056295/',
  67. 'md5': 'a9e0c8371ea1ca306d6554e3fecf50b6',
  68. 'info_dict': {
  69. 'id': '105463834',
  70. 'ext': 'mp4',
  71. 'title': 'Cam Damage-HD 720p',
  72. 'uploader': 'John Moyer',
  73. 'uploader_id': 'user32021558',
  74. },
  75. 'add_ie': ['Vimeo'],
  76. }]
  77. def _real_extract(self, url):
  78. m_url = re.match(self._VALID_URL, url)
  79. video_id = m_url.group('id')
  80. blog = m_url.group('blog_name')
  81. url = 'http://%s.tumblr.com/post/%s/' % (blog, video_id)
  82. webpage, urlh = self._download_webpage_handle(url, video_id)
  83. iframe_url = self._search_regex(
  84. r'src=\'(https?://www\.tumblr\.com/video/[^\']+)\'',
  85. webpage, 'iframe url', default=None)
  86. if iframe_url is None:
  87. return self.url_result(urlh.geturl(), 'Generic')
  88. iframe = self._download_webpage(iframe_url, video_id, 'Downloading iframe page')
  89. duration = None
  90. sources = []
  91. sd_url = self._search_regex(
  92. r'<source[^>]+src=(["\'])(?P<url>.+?)\1', iframe,
  93. 'sd video url', default=None, group='url')
  94. if sd_url:
  95. sources.append((sd_url, 'sd'))
  96. options = self._parse_json(
  97. self._search_regex(
  98. r'data-crt-options=(["\'])(?P<options>.+?)\1', iframe,
  99. 'hd video url', default='', group='options'),
  100. video_id, fatal=False)
  101. if options:
  102. duration = int_or_none(options.get('duration'))
  103. hd_url = options.get('hdUrl')
  104. if hd_url:
  105. sources.append((hd_url, 'hd'))
  106. formats = [{
  107. 'url': video_url,
  108. 'ext': 'mp4',
  109. 'format_id': format_id,
  110. 'height': int_or_none(self._search_regex(
  111. r'/(\d{3,4})$', video_url, 'height', default=None)),
  112. 'quality': quality,
  113. } for quality, (video_url, format_id) in enumerate(sources)]
  114. self._sort_formats(formats)
  115. # The only place where you can get a title, it's not complete,
  116. # but searching in other places doesn't work for all videos
  117. video_title = self._html_search_regex(
  118. r'(?s)<title>(?P<title>.*?)(?: \| Tumblr)?</title>',
  119. webpage, 'title')
  120. return {
  121. 'id': video_id,
  122. 'ext': 'mp4',
  123. 'title': video_title,
  124. 'description': self._og_search_description(webpage, default=None),
  125. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  126. 'duration': duration,
  127. 'formats': formats,
  128. }