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.

119 lines
4.4 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. if any(s in webpage for s in ['video-deleted-info', '>This video has been removed']):
  40. raise ExtractorError('Video %s has been removed' % video_id, expected=True)
  41. info = self._search_json_ld(webpage, video_id, default={})
  42. if not info.get('title'):
  43. info['title'] = self._html_search_regex(
  44. (r'<h(\d)[^>]+class="(?:video_title_text|videoTitle)[^"]*">(?P<title>(?:(?!\1).)+)</h\1>',
  45. r'(?:videoTitle|title)\s*:\s*(["\'])(?P<title>(?:(?!\1).)+)\1',),
  46. webpage, 'title', group='title',
  47. default=None) or self._og_search_title(webpage)
  48. formats = []
  49. sources = self._parse_json(
  50. self._search_regex(
  51. r'sources\s*:\s*({.+?})', webpage, 'source', default='{}'),
  52. video_id, fatal=False)
  53. if sources and isinstance(sources, dict):
  54. for format_id, format_url in sources.items():
  55. if format_url:
  56. formats.append({
  57. 'url': format_url,
  58. 'format_id': format_id,
  59. 'height': int_or_none(format_id),
  60. })
  61. medias = self._parse_json(
  62. self._search_regex(
  63. r'mediaDefinition\s*:\s*(\[.+?\])', webpage,
  64. 'media definitions', default='{}'),
  65. video_id, fatal=False)
  66. if medias and isinstance(medias, list):
  67. for media in medias:
  68. format_url = url_or_none(media.get('videoUrl'))
  69. if not format_url:
  70. continue
  71. format_id = media.get('quality')
  72. formats.append({
  73. 'url': format_url,
  74. 'format_id': format_id,
  75. 'height': int_or_none(format_id),
  76. })
  77. if not formats:
  78. video_url = self._html_search_regex(
  79. r'<source src="(.+?)" type="video/mp4">', webpage, 'video URL')
  80. formats.append({'url': video_url})
  81. self._sort_formats(formats)
  82. thumbnail = self._og_search_thumbnail(webpage)
  83. upload_date = unified_strdate(self._search_regex(
  84. r'<span[^>]+>(?:ADDED|Published on) ([^<]+)<',
  85. webpage, 'upload date', default=None))
  86. duration = int_or_none(self._og_search_property(
  87. 'video:duration', webpage, default=None) or self._search_regex(
  88. r'videoDuration\s*:\s*(\d+)', webpage, 'duration', default=None))
  89. view_count = str_to_int(self._search_regex(
  90. (r'<div[^>]*>Views</div>\s*<div[^>]*>\s*([\d,.]+)',
  91. r'<span[^>]*>VIEWS</span>\s*</td>\s*<td>\s*([\d,.]+)',
  92. r'<span[^>]+\bclass=["\']video_view_count[^>]*>\s*([\d,.]+)'),
  93. webpage, 'view count', default=None))
  94. # No self-labeling, but they describe themselves as
  95. # "Home of Videos Porno"
  96. age_limit = 18
  97. return merge_dicts(info, {
  98. 'id': video_id,
  99. 'ext': 'mp4',
  100. 'thumbnail': thumbnail,
  101. 'upload_date': upload_date,
  102. 'duration': duration,
  103. 'view_count': view_count,
  104. 'age_limit': age_limit,
  105. 'formats': formats,
  106. })