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.

116 lines
3.6 KiB

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