tumblr.py 5.1 KB

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