From f3fbaa5c3eda2bc04ff9ce6c6c5dbb7903253507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Tue, 10 Oct 2017 17:29:46 +0200 Subject: [PATCH] [reddit] allow users to override the API User-Agent 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 --- docs/configuration.rst | 4 ++-- gallery_dl/extractor/reddit.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index ac1539ad..8672413b 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -599,8 +599,8 @@ Description =========== ===== -extractor.reddit.client-id --------------------------- +extractor.reddit.client-id & .user-agent +---------------------------------------- =========== ===== Type ``string`` Description diff --git a/gallery_dl/extractor/reddit.py b/gallery_dl/extractor/reddit.py index 50c0928b..a32a1dce 100644 --- a/gallery_dl/extractor/reddit.py +++ b/gallery_dl/extractor/reddit.py @@ -110,11 +110,21 @@ class RedditAPI(): 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"""