From 07c8adbd8b1f7f4867604abb7e948237883507e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Tue, 8 Jun 2021 02:06:19 +0200 Subject: [PATCH] [mangadex] implement login with username & password (#1535) --- README.rst | 1 + docs/configuration.rst | 3 ++- docs/supportedsites.md | 2 +- gallery_dl/extractor/mangadex.py | 42 ++++++++++++++++++++++++++++++-- scripts/supportedsites.py | 1 + test/test_results.py | 2 +- 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 66e71e73..70dac919 100644 --- a/README.rst +++ b/README.rst @@ -209,6 +209,7 @@ and optional for ``imgbb``, ``inkbunny``, ``instagram``, +``mangadex``, ``mangoxo``, ``pillowfort``, ``pinterest``, diff --git a/docs/configuration.rst b/docs/configuration.rst index 79b35796..2da4fccc 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -331,6 +331,7 @@ Description * ``imgbb`` * ``inkbunny`` * ``instagram`` + * ``mangadex`` * ``mangoxo`` * ``pillowfort`` * ``pinterest`` @@ -2945,7 +2946,7 @@ Description It is possible to set a ``"filter"`` expression similar to `image-filter `_ to only run a post-processor - condionally. + conditionally. It is also possible set a ``"whitelist"`` or ``"blacklist"`` to only enable or disable a post-processor for the specified diff --git a/docs/supportedsites.md b/docs/supportedsites.md index 78cd025b..3e5dcb3d 100644 --- a/docs/supportedsites.md +++ b/docs/supportedsites.md @@ -413,7 +413,7 @@ Consider all sites to be NSFW unless otherwise known. MangaDex https://mangadex.org/ Chapters, Manga - + Supported MangaKakalot diff --git a/gallery_dl/extractor/mangadex.py b/gallery_dl/extractor/mangadex.py index cd881af2..fb4071c8 100644 --- a/gallery_dl/extractor/mangadex.py +++ b/gallery_dl/extractor/mangadex.py @@ -10,7 +10,7 @@ from .common import Extractor, Message from .. import text, util, exception -from ..cache import memcache +from ..cache import cache, memcache from collections import defaultdict @@ -153,6 +153,12 @@ class MangadexAPI(): def __init__(self, extr): self.extractor = extr + self.headers = {} + + self.username, self.password = self.extractor._get_auth_info() + if not self.username: + self.authenticate = util.noop + server = extr.config("api-server") self.root = ("https://api.mangadex.org" if server is None else text.ensure_http_scheme(server).rstrip("/")) @@ -185,11 +191,38 @@ class MangadexAPI(): } return self._pagination("/manga/" + uuid + "/feed", params) + def authenticate(self): + self.headers["Authorization"] = \ + self._authenticate_impl(self.username, self.password) + + @cache(maxage=900, keyarg=1) + def _authenticate_impl(self, username, password): + refresh_token = _refresh_token_cache(username) + if refresh_token: + self.extractor.log.info("Refreshing access token") + url = self.root + "/auth/refresh" + data = {"token": refresh_token} + else: + self.extractor.log.info("Logging in as %s", username) + url = self.root + "/auth/login" + data = {"username": username, "password": password} + + data = self.extractor.request( + url, method="POST", json=data, fatal=None).json() + if data.get("result") != "ok": + raise exception.AuthenticationError() + + if refresh_token != data["token"]["refresh"]: + _refresh_token_cache.update(username, data["token"]["refresh"]) + return "Bearer " + data["token"]["session"] + def _call(self, endpoint, params=None): url = self.root + endpoint while True: - response = self.extractor.request(url, params=params, fatal=None) + self.authenticate() + response = self.extractor.request( + url, params=params, headers=self.headers, fatal=None) if response.status_code < 400: return response.json() @@ -215,3 +248,8 @@ class MangadexAPI(): params["offset"] = data["offset"] + data["limit"] if params["offset"] >= data["total"]: return + + +@cache(maxage=28*24*3600, keyarg=0) +def _refresh_token_cache(username): + return None diff --git a/scripts/supportedsites.py b/scripts/supportedsites.py index 9117f4c2..72cb2670 100755 --- a/scripts/supportedsites.py +++ b/scripts/supportedsites.py @@ -219,6 +219,7 @@ AUTH_MAP = { "imgbb" : "Supported", "inkbunny" : "Supported", "instagram" : "Supported", + "mangadex" : "Supported", "mangoxo" : "Supported", "mastodon.social": _OAUTH, "newgrounds" : "Supported", diff --git a/test/test_results.py b/test/test_results.py index bf2496b1..5b22ecde 100644 --- a/test/test_results.py +++ b/test/test_results.py @@ -312,7 +312,7 @@ def setup_test_config(): config.set(("extractor", "mangoxo") , "password", "5zbQF10_5u25259Ma") for category in ("danbooru", "instagram", "twitter", "subscribestar", - "e621", "inkbunny", "tapas", "pillowfort"): + "e621", "inkbunny", "tapas", "pillowfort", "mangadex"): config.set(("extractor", category), "username", None) config.set(("extractor", "mastodon.social"), "access-token",