logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

redtube.py (5233B)


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