Skip to content

Commit

Permalink
[reddit] allow users to override the API User-Agent
Browse files Browse the repository at this point in the history
Only overriding the Client-ID is not enough if you want to follow
Reddit's API access rules [1].

[1] https://github.com/reddit/reddit/wiki/API#rules
  • Loading branch information
mikf committed Oct 10, 2017
1 parent 31ea600 commit f3fbaa5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,8 @@ Description
=========== =====


extractor.reddit.client-id
--------------------------
extractor.reddit.client-id & .user-agent
----------------------------------------
=========== =====
Type ``string``
Description
Expand Down
14 changes: 12 additions & 2 deletions gallery_dl/extractor/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,21 @@ def __init__(self, extractor):
self.extractor = extractor
self.comments = extractor.config("comments", 500)
self.morecomments = extractor.config("morecomments", False)
self.client_id = extractor.config("client-id", self.CLIENT_ID)
self.refresh_token = extractor.config("refresh-token")
self.log = extractor.log
self.session = extractor.session
self.session.headers["User-Agent"] = self.USER_AGENT

client_id = extractor.config("client-id", self.CLIENT_ID)
user_agent = extractor.config("user-agent", self.USER_AGENT)

if (client_id == self.CLIENT_ID) ^ (user_agent == self.USER_AGENT):
self.client_id = None
self.log.warning(
"Conflicting values for 'client-id' and 'user-agent': "
"override either both or non of them.")
else:
self.client_id = client_id
self.session.headers["User-Agent"] = user_agent

def submission(self, submission_id):
"""Fetch the (submission, comments)=-tuple for a submission id"""
Expand Down

0 comments on commit f3fbaa5

Please sign in to comment.