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.

126 lines
4.7 KiB

11 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. int_or_none,
  7. merge_dicts,
  8. str_to_int,
  9. unified_strdate,
  10. url_or_none,
  11. )
  12. class RedTubeIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:(?:www\.)?redtube\.com/|embed\.redtube\.com/\?.*?\bid=)(?P<id>[0-9]+)'
  14. _TESTS = [{
  15. 'url': 'http://www.redtube.com/66418',
  16. 'md5': 'fc08071233725f26b8f014dba9590005',
  17. 'info_dict': {
  18. 'id': '66418',
  19. 'ext': 'mp4',
  20. 'title': 'Sucked on a toilet',
  21. 'upload_date': '20110811',
  22. 'duration': 596,
  23. 'view_count': int,
  24. 'age_limit': 18,
  25. }
  26. }, {
  27. 'url': 'http://embed.redtube.com/?bgcolor=000000&id=1443286',
  28. 'only_matching': True,
  29. }]
  30. @staticmethod
  31. def _extract_urls(webpage):
  32. return re.findall(
  33. r'<iframe[^>]+?src=["\'](?P<url>(?:https?:)?//embed\.redtube\.com/\?.*?\bid=\d+)',
  34. webpage)
  35. def _real_extract(self, url):
  36. video_id = self._match_id(url)
  37. webpage = self._download_webpage(
  38. 'http://www.redtube.com/%s' % video_id, video_id)
  39. ERRORS = (
  40. (('video-deleted-info', '>This video has been removed'), 'has been removed'),
  41. (('private_video_text', '>This video is private', '>Send a friend request to its owner to be able to view it'), 'is private'),
  42. )
  43. for patterns, message in ERRORS:
  44. if any(p in webpage for p in patterns):
  45. raise ExtractorError(
  46. 'Video %s %s' % (video_id, message), expected=True)
  47. info = self._search_json_ld(webpage, video_id, default={})
  48. if not info.get('title'):
  49. info['title'] = self._html_search_regex(
  50. (r'<h(\d)[^>]+class="(?:video_title_text|videoTitle)[^"]*">(?P<title>(?:(?!\1).)+)</h\1>',
  51. r'(?:videoTitle|title)\s*:\s*(["\'])(?P<title>(?:(?!\1).)+)\1',),
  52. webpage, 'title', group='title',
  53. default=None) or self._og_search_title(webpage)
  54. formats = []
  55. sources = self._parse_json(
  56. self._search_regex(
  57. r'sources\s*:\s*({.+?})', webpage, 'source', default='{}'),
  58. video_id, fatal=False)
  59. if sources and isinstance(sources, dict):
  60. for format_id, format_url in sources.items():
  61. if format_url:
  62. formats.append({
  63. 'url': format_url,
  64. 'format_id': format_id,
  65. 'height': int_or_none(format_id),
  66. })
  67. medias = self._parse_json(
  68. self._search_regex(
  69. r'mediaDefinition\s*:\s*(\[.+?\])', webpage,
  70. 'media definitions', default='{}'),
  71. video_id, fatal=False)
  72. if medias and isinstance(medias, list):
  73. for media in medias:
  74. format_url = url_or_none(media.get('videoUrl'))
  75. if not format_url:
  76. continue
  77. format_id = media.get('quality')
  78. formats.append({
  79. 'url': format_url,
  80. 'format_id': format_id,
  81. 'height': int_or_none(format_id),
  82. })
  83. if not formats:
  84. video_url = self._html_search_regex(
  85. r'<source src="(.+?)" type="video/mp4">', webpage, 'video URL')
  86. formats.append({'url': video_url})
  87. self._sort_formats(formats)
  88. thumbnail = self._og_search_thumbnail(webpage)
  89. upload_date = unified_strdate(self._search_regex(
  90. r'<span[^>]+>(?:ADDED|Published on) ([^<]+)<',
  91. webpage, 'upload date', default=None))
  92. duration = int_or_none(self._og_search_property(
  93. 'video:duration', webpage, default=None) or self._search_regex(
  94. r'videoDuration\s*:\s*(\d+)', webpage, 'duration', default=None))
  95. view_count = str_to_int(self._search_regex(
  96. (r'<div[^>]*>Views</div>\s*<div[^>]*>\s*([\d,.]+)',
  97. r'<span[^>]*>VIEWS</span>\s*</td>\s*<td>\s*([\d,.]+)',
  98. r'<span[^>]+\bclass=["\']video_view_count[^>]*>\s*([\d,.]+)'),
  99. webpage, 'view count', default=None))
  100. # No self-labeling, but they describe themselves as
  101. # "Home of Videos Porno"
  102. age_limit = 18
  103. return merge_dicts(info, {
  104. 'id': video_id,
  105. 'ext': 'mp4',
  106. 'thumbnail': thumbnail,
  107. 'upload_date': upload_date,
  108. 'duration': duration,
  109. 'view_count': view_count,
  110. 'age_limit': age_limit,
  111. 'formats': formats,
  112. })