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.

130 lines
4.1 KiB

  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. float_or_none,
  8. url_or_none,
  9. )
  10. class RedditIE(InfoExtractor):
  11. _VALID_URL = r'https?://v\.redd\.it/(?P<id>[^/?#&]+)'
  12. _TEST = {
  13. # from https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/
  14. 'url': 'https://v.redd.it/zv89llsvexdz',
  15. 'md5': '0a070c53eba7ec4534d95a5a1259e253',
  16. 'info_dict': {
  17. 'id': 'zv89llsvexdz',
  18. 'ext': 'mp4',
  19. 'title': 'zv89llsvexdz',
  20. },
  21. 'params': {
  22. 'format': 'bestvideo',
  23. },
  24. }
  25. def _real_extract(self, url):
  26. video_id = self._match_id(url)
  27. formats = self._extract_m3u8_formats(
  28. 'https://v.redd.it/%s/HLSPlaylist.m3u8' % video_id, video_id,
  29. 'mp4', entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
  30. formats.extend(self._extract_mpd_formats(
  31. 'https://v.redd.it/%s/DASHPlaylist.mpd' % video_id, video_id,
  32. mpd_id='dash', fatal=False))
  33. self._sort_formats(formats)
  34. return {
  35. 'id': video_id,
  36. 'title': video_id,
  37. 'formats': formats,
  38. }
  39. class RedditRIE(InfoExtractor):
  40. _VALID_URL = r'(?P<url>https?://(?:[^/]+\.)?reddit\.com/r/[^/]+/comments/(?P<id>[^/?#&]+))'
  41. _TESTS = [{
  42. 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj/that_small_heart_attack/',
  43. 'info_dict': {
  44. 'id': 'zv89llsvexdz',
  45. 'ext': 'mp4',
  46. 'title': 'That small heart attack.',
  47. 'thumbnail': r're:^https?://.*\.jpg$',
  48. 'timestamp': 1501941939,
  49. 'upload_date': '20170805',
  50. 'uploader': 'Antw87',
  51. 'like_count': int,
  52. 'dislike_count': int,
  53. 'comment_count': int,
  54. 'age_limit': 0,
  55. },
  56. 'params': {
  57. 'format': 'bestvideo',
  58. 'skip_download': True,
  59. },
  60. }, {
  61. 'url': 'https://www.reddit.com/r/videos/comments/6rrwyj',
  62. 'only_matching': True,
  63. }, {
  64. # imgur
  65. 'url': 'https://www.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/',
  66. 'only_matching': True,
  67. }, {
  68. # imgur @ old reddit
  69. 'url': 'https://old.reddit.com/r/MadeMeSmile/comments/6t7wi5/wait_for_it/',
  70. 'only_matching': True,
  71. }, {
  72. # streamable
  73. 'url': 'https://www.reddit.com/r/videos/comments/6t7sg9/comedians_hilarious_joke_about_the_guam_flag/',
  74. 'only_matching': True,
  75. }, {
  76. # youtube
  77. 'url': 'https://www.reddit.com/r/videos/comments/6t75wq/southern_man_tries_to_speak_without_an_accent/',
  78. 'only_matching': True,
  79. }, {
  80. # reddit video @ nm reddit
  81. 'url': 'https://nm.reddit.com/r/Cricket/comments/8idvby/lousy_cameraman_finds_himself_in_cairns_line_of/',
  82. 'only_matching': True,
  83. }]
  84. def _real_extract(self, url):
  85. mobj = re.match(self._VALID_URL, url)
  86. url, video_id = mobj.group('url', 'id')
  87. video_id = self._match_id(url)
  88. data = self._download_json(
  89. url + '/.json', video_id)[0]['data']['children'][0]['data']
  90. video_url = data['url']
  91. # Avoid recursing into the same reddit URL
  92. if 'reddit.com/' in video_url and '/%s/' % video_id in video_url:
  93. raise ExtractorError('No media found', expected=True)
  94. over_18 = data.get('over_18')
  95. if over_18 is True:
  96. age_limit = 18
  97. elif over_18 is False:
  98. age_limit = 0
  99. else:
  100. age_limit = None
  101. return {
  102. '_type': 'url_transparent',
  103. 'url': video_url,
  104. 'title': data.get('title'),
  105. 'thumbnail': url_or_none(data.get('thumbnail')),
  106. 'timestamp': float_or_none(data.get('created_utc')),
  107. 'uploader': data.get('author'),
  108. 'like_count': int_or_none(data.get('ups')),
  109. 'dislike_count': int_or_none(data.get('downs')),
  110. 'comment_count': int_or_none(data.get('num_comments')),
  111. 'age_limit': age_limit,
  112. }