Skip to content

Commit

Permalink
[newgrounds] add 'favorite' extractor (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Mar 10, 2020
1 parent a45fbc3 commit 823fbea
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/supportedsites.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions gallery_dl/extractor/newgrounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .common import Extractor, Message
from .. import text, exception
from ..cache import cache
import itertools
import json


Expand Down Expand Up @@ -334,3 +335,53 @@ def items(self):
(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

0 comments on commit 823fbea

Please sign in to comment.