From 823fbeaae69cdea6602011b6bf2c7a3580ebf8ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Tue, 10 Mar 2020 01:07:09 +0100 Subject: [PATCH] [newgrounds] add 'favorite' extractor (#394) --- docs/supportedsites.rst | 2 +- gallery_dl/extractor/newgrounds.py | 51 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/docs/supportedsites.rst b/docs/supportedsites.rst index 6fa66b38..7e1c8de6 100644 --- a/docs/supportedsites.rst +++ b/docs/supportedsites.rst @@ -146,7 +146,7 @@ Turboimagehost https://www.turboimagehost.com/ individual Images .. |hentaifoundry-C| replace:: Favorites, individual Images, Popular Images, Recent Images, Scraps, User Profiles .. |imgur-C| replace:: Albums, Favorites, Galleries, individual Images, Subreddits, User Profiles .. |instagram-C| replace:: Channels, individual Images, Stories, Tag-Searches, User Profiles -.. |newgrounds-C| replace:: Art, Audio, individual Images, Media Files, Movies, User Profiles +.. |newgrounds-C| replace:: Art, Audio, Favorites, individual Images, Media Files, Movies, User Profiles .. |nijie-C| replace:: Doujin, Favorites, individual Images, User Profiles .. |pixiv-C| replace:: Favorites, Follows, pixiv.me Links, Rankings, Search Results, User Profiles, individual Images .. |reddit-C| replace:: individual Images, Submissions, Subreddits, User Profiles diff --git a/gallery_dl/extractor/newgrounds.py b/gallery_dl/extractor/newgrounds.py index c2072d67..4e7a3a7b 100644 --- a/gallery_dl/extractor/newgrounds.py +++ b/gallery_dl/extractor/newgrounds.py @@ -11,6 +11,7 @@ from .common import Extractor, Message from .. import text, exception from ..cache import cache +import itertools import json @@ -334,3 +335,53 @@ class NewgroundsUserExtractor(NewgroundsExtractor): (NewgroundsAudioExtractor , base + "audio"), (NewgroundsMoviesExtractor, base + "movies"), ), ("art",)) + + +class NewgroundsFavoriteExtractor(NewgroundsExtractor): + """Extractor for posts favorited by a newgrounds user""" + subcategory = "favorite" + directory_fmt = ("{category}", "{user}", "Favorites") + pattern = (r"(?:https?://)?([^.]+)\.newgrounds\.com" + r"/favorites(?:/(art|audio|movies))?/?") + test = ( + ("https://tomfulp.newgrounds.com/favorites/art", { + "range": "1-10", + "count": ">= 10", + }), + ("https://tomfulp.newgrounds.com/favorites/audio"), + ("https://tomfulp.newgrounds.com/favorites/movies"), + ("https://tomfulp.newgrounds.com/favorites/"), + ) + + def __init__(self, match): + NewgroundsExtractor.__init__(self, match) + self.kind = match.group(2) + + def posts(self): + if self.kind: + return self._pagination(self.kind) + return itertools.chain.from_iterable( + self._pagination(k) for k in ("art", "audio", "movies") + ) + + def _pagination(self, kind): + num = 1 + headers = { + "Accept": "application/json, text/javascript, */*; q=0.01", + "X-Requested-With": "XMLHttpRequest", + "Referer": self.user_root, + } + + while True: + url = "{}/favorites/{}/{}".format(self.user_root, kind, num) + response = self.request(url, headers=headers) + if response.history: + return + + favs = list(text.extract_iter( + response.text, 'href="//www.newgrounds.com', '"')) + for path in favs: + yield self.root + path + if len(favs) < 24: + return + num += 1