[reddit] ignore Authorization errors for subreddits
- also made the limit for retrieved comments customizable via the 'extractor.reddit.comments' config value - default is 500; 0 ignores comments completely
This commit is contained in:
@@ -102,18 +102,17 @@ class RedditSubmissionExtractor(RedditExtractor):
|
|||||||
class RedditAPI():
|
class RedditAPI():
|
||||||
"""Minimal interface for the reddit API"""
|
"""Minimal interface for the reddit API"""
|
||||||
def __init__(self, extractor, client_id="6N9uN0krSDE-ig"):
|
def __init__(self, extractor, client_id="6N9uN0krSDE-ig"):
|
||||||
|
self.extractor = extractor
|
||||||
|
self.client_id = extractor.config("client-id", client_id)
|
||||||
|
self.comments = extractor.config("comments", 200)
|
||||||
self.session = extractor.session
|
self.session = extractor.session
|
||||||
self.date_min = int(extractor.config("date-min", 0))
|
|
||||||
# 253402210800 == datetime.max.timestamp()
|
|
||||||
self.date_max = int(extractor.config("date-max", 253402210800))
|
|
||||||
self.client_id = client_id
|
|
||||||
self.session.headers["User-Agent"] = ("Python:gallery-dl:0.8.4"
|
self.session.headers["User-Agent"] = ("Python:gallery-dl:0.8.4"
|
||||||
" (by /u/mikf1)")
|
" (by /u/mikf1)")
|
||||||
|
|
||||||
def submission(self, submission_id):
|
def submission(self, submission_id):
|
||||||
"""Fetch the (submission, comments)=-tuple for a submission id"""
|
"""Fetch the (submission, comments)=-tuple for a submission id"""
|
||||||
endpoint = "/comments/" + submission_id + "/.json"
|
endpoint = "/comments/" + submission_id + "/.json"
|
||||||
submission, comments = self._call(endpoint, {"limit": 500})
|
submission, comments = self._call(endpoint, {"limit": self.comments})
|
||||||
return (submission["data"]["children"][0]["data"],
|
return (submission["data"]["children"][0]["data"],
|
||||||
self._unfold(comments))
|
self._unfold(comments))
|
||||||
|
|
||||||
@@ -155,14 +154,20 @@ class RedditAPI():
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
def _pagination(self, endpoint, params, _empty=()):
|
def _pagination(self, endpoint, params, _empty=()):
|
||||||
|
date_min = int(self.extractor.config("date-min", 0))
|
||||||
|
date_max = int(self.extractor.config("date-max", 253402210800))
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
data = self._call(endpoint, params)["data"]
|
data = self._call(endpoint, params)["data"]
|
||||||
|
|
||||||
for submission in data["children"]:
|
for submission in data["children"]:
|
||||||
submission = submission["data"]
|
submission = submission["data"]
|
||||||
if self.date_min <= submission["created_utc"] <= self.date_max:
|
if date_min <= submission["created_utc"] <= date_max:
|
||||||
if submission["num_comments"]:
|
if submission["num_comments"] and self.comments:
|
||||||
yield self.submission(submission["id"])
|
try:
|
||||||
|
yield self.submission(submission["id"])
|
||||||
|
except exception.AuthorizationError:
|
||||||
|
pass
|
||||||
else:
|
else:
|
||||||
yield submission, _empty
|
yield submission, _empty
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user