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.

112 lines
3.9 KiB

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