[reddit] Generate random session ID (closes #29986)

This commit is contained in:
Sol Toder 2021-09-24 23:16:47 -04:00
parent a803582717
commit 211d67fad1

View File

@ -1,6 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import re import re
import random
from .common import InfoExtractor from .common import InfoExtractor
from ..utils import ( from ..utils import (
@ -29,9 +30,19 @@ class RedditIE(InfoExtractor):
}, },
} }
@staticmethod
def _gen_session_id():
# The actual value of the cookie does not seem to matter.
# Using a random 16-char hex string for now.
id_length = 16
rand_max = 1 << (id_length * 4)
return '%0.*x' % (id_length, random.randrange(rand_max))
def _real_extract(self, url): def _real_extract(self, url):
video_id = self._match_id(url) video_id = self._match_id(url)
self._set_cookie('.reddit.com', 'reddit_session', RedditIE._gen_session_id())
formats = self._extract_m3u8_formats( formats = self._extract_m3u8_formats(
'https://v.redd.it/%s/HLSPlaylist.m3u8' % video_id, video_id, 'https://v.redd.it/%s/HLSPlaylist.m3u8' % video_id, video_id,
'mp4', entry_protocol='m3u8_native', m3u8_id='hls', fatal=False) 'mp4', entry_protocol='m3u8_native', m3u8_id='hls', fatal=False)
@ -103,6 +114,8 @@ class RedditRIE(InfoExtractor):
video_id = self._match_id(url) video_id = self._match_id(url)
self._set_cookie('.reddit.com', 'reddit_session', RedditIE._gen_session_id())
data = self._download_json( data = self._download_json(
url + '/.json', video_id)[0]['data']['children'][0]['data'] url + '/.json', video_id)[0]['data']['children'][0]['data']