manga-download / hakuneko

Manga & Anime Downloader for Linux, Windows & MacOS
https://hakuneko.download
The Unlicense
4.43k stars 456 forks source link

Mangadex: Clipboard connector should obey throttle #614

Open hakuneko-user opened 4 years ago

hakuneko-user commented 4 years ago

From:

It's possible to get banned from mangadex temporarily by pasting a lot of urls into the clipboard connector.

What has been done?
ronny1982 commented 4 years ago

The clipboard is very simple and has no mechanism to distinguish which website are the pasted links from. It seems to be quite complicated to group the pasted links and introduce a manager that will throttle the requests based on the corresponding website...

shinigamichan commented 4 years ago

@ronny1982 Prior to a recent update the clipboard connector worked in serial and it was possible to simply add a throttle in _getMangaFromURI. After that update, I simply made the throttling enforced in a central place, like so (allow_request_after initialized to Date.now()):

     _requestAPI( url, requestOptions, label ) {
-        return fetch( url, requestOptions )
+        let now = Date.now();
+        let towait = Math.max(this.allow_request_after - now, 0);
+        this.allow_request_after = now + towait + Number(this.config.throttle.value);
+        return this.wait(towait).then(() => fetch( url, requestOptions ))
             .then( response => {

In some sense this is hacky, and you can get a bunch of promises that take a really long time to resolve. In another sense it makes sense to enforce throttling in a central place rather than throughout the code. Maybe there's a better way to enforce the throttling centrally.