[imgur] add 'favorite-folder' extractor (#4016)
This commit is contained in:
@@ -388,7 +388,7 @@ Consider all sites to be NSFW unless otherwise known.
|
|||||||
<tr>
|
<tr>
|
||||||
<td>imgur</td>
|
<td>imgur</td>
|
||||||
<td>https://imgur.com/</td>
|
<td>https://imgur.com/</td>
|
||||||
<td>Albums, Favorites, Galleries, individual Images, Search Results, Subreddits, Tag Searches, User Profiles</td>
|
<td>Albums, Favorites, Favorites Folders, Galleries, individual Images, Search Results, Subreddits, Tag Searches, User Profiles</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|||||||
@@ -285,7 +285,7 @@ class ImgurUserExtractor(ImgurExtractor):
|
|||||||
class ImgurFavoriteExtractor(ImgurExtractor):
|
class ImgurFavoriteExtractor(ImgurExtractor):
|
||||||
"""Extractor for a user's favorites"""
|
"""Extractor for a user's favorites"""
|
||||||
subcategory = "favorite"
|
subcategory = "favorite"
|
||||||
pattern = BASE_PATTERN + r"/user/([^/?#]+)/favorites"
|
pattern = BASE_PATTERN + r"/user/([^/?#]+)/favorites/?$"
|
||||||
test = ("https://imgur.com/user/Miguenzo/favorites", {
|
test = ("https://imgur.com/user/Miguenzo/favorites", {
|
||||||
"range": "1-100",
|
"range": "1-100",
|
||||||
"count": 100,
|
"count": 100,
|
||||||
@@ -296,6 +296,28 @@ class ImgurFavoriteExtractor(ImgurExtractor):
|
|||||||
return self._items_queue(self.api.account_favorites(self.key))
|
return self._items_queue(self.api.account_favorites(self.key))
|
||||||
|
|
||||||
|
|
||||||
|
class ImgurFavoriteFolderExtractor(ImgurExtractor):
|
||||||
|
"""Extractor for a user's favorites folder"""
|
||||||
|
subcategory = "favorite-folder"
|
||||||
|
pattern = BASE_PATTERN + r"/user/([^/?#]+)/favorites/folder/(\d+)"
|
||||||
|
test = (
|
||||||
|
("https://imgur.com/user/mikf1/favorites/folder/11896757/public", {
|
||||||
|
"count": 3,
|
||||||
|
}),
|
||||||
|
("https://imgur.com/user/mikf1/favorites/folder/11896741/private", {
|
||||||
|
"count": 5,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, match):
|
||||||
|
ImgurExtractor.__init__(self, match)
|
||||||
|
self.folder_id = match.group(2)
|
||||||
|
|
||||||
|
def items(self):
|
||||||
|
return self._items_queue(self.api.account_favorites_folder(
|
||||||
|
self.key, self.folder_id))
|
||||||
|
|
||||||
|
|
||||||
class ImgurSubredditExtractor(ImgurExtractor):
|
class ImgurSubredditExtractor(ImgurExtractor):
|
||||||
"""Extractor for a subreddits's imgur links"""
|
"""Extractor for a subreddits's imgur links"""
|
||||||
subcategory = "subreddit"
|
subcategory = "subreddit"
|
||||||
@@ -346,15 +368,18 @@ class ImgurAPI():
|
|||||||
"""
|
"""
|
||||||
def __init__(self, extractor):
|
def __init__(self, extractor):
|
||||||
self.extractor = extractor
|
self.extractor = extractor
|
||||||
self.headers = {
|
self.client_id = extractor.config("client-id") or "546c25a59c58ad7"
|
||||||
"Authorization": "Client-ID " + (
|
self.headers = {"Authorization": "Client-ID " + self.client_id}
|
||||||
extractor.config("client-id") or "546c25a59c58ad7"),
|
|
||||||
}
|
|
||||||
|
|
||||||
def account_favorites(self, account):
|
def account_favorites(self, account):
|
||||||
endpoint = "/3/account/{}/gallery_favorites".format(account)
|
endpoint = "/3/account/{}/gallery_favorites".format(account)
|
||||||
return self._pagination(endpoint)
|
return self._pagination(endpoint)
|
||||||
|
|
||||||
|
def account_favorites_folder(self, account, folder_id):
|
||||||
|
endpoint = "/3/account/{}/folders/{}/favorites".format(
|
||||||
|
account, folder_id)
|
||||||
|
return self._pagination_v2(endpoint)
|
||||||
|
|
||||||
def gallery_search(self, query):
|
def gallery_search(self, query):
|
||||||
endpoint = "/3/gallery/search"
|
endpoint = "/3/gallery/search"
|
||||||
params = {"q": query}
|
params = {"q": query}
|
||||||
@@ -386,12 +411,12 @@ class ImgurAPI():
|
|||||||
endpoint = "/post/v1/posts/" + gallery_hash
|
endpoint = "/post/v1/posts/" + gallery_hash
|
||||||
return self._call(endpoint)
|
return self._call(endpoint)
|
||||||
|
|
||||||
def _call(self, endpoint, params=None):
|
def _call(self, endpoint, params=None, headers=None):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
return self.extractor.request(
|
return self.extractor.request(
|
||||||
"https://api.imgur.com" + endpoint,
|
"https://api.imgur.com" + endpoint,
|
||||||
params=params, headers=self.headers,
|
params=params, headers=(headers or self.headers),
|
||||||
).json()
|
).json()
|
||||||
except exception.HttpError as exc:
|
except exception.HttpError as exc:
|
||||||
if exc.status not in (403, 429) or \
|
if exc.status not in (403, 429) or \
|
||||||
@@ -410,3 +435,23 @@ class ImgurAPI():
|
|||||||
return
|
return
|
||||||
yield from data
|
yield from data
|
||||||
num += 1
|
num += 1
|
||||||
|
|
||||||
|
def _pagination_v2(self, endpoint, params=None, key=None):
|
||||||
|
if params is None:
|
||||||
|
params = {}
|
||||||
|
params["client_id"] = self.client_id
|
||||||
|
params["page"] = 0
|
||||||
|
params["sort"] = "newest"
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
"Referer": "https://imgur.com/",
|
||||||
|
"Origin": "https://imgur.com",
|
||||||
|
}
|
||||||
|
|
||||||
|
while True:
|
||||||
|
data = self._call(endpoint, params, headers)["data"]
|
||||||
|
if not data:
|
||||||
|
return
|
||||||
|
yield from data
|
||||||
|
|
||||||
|
params["page"] += 1
|
||||||
|
|||||||
@@ -183,6 +183,9 @@ SUBCATEGORY_MAP = {
|
|||||||
"hentaifoundry": {
|
"hentaifoundry": {
|
||||||
"story": "",
|
"story": "",
|
||||||
},
|
},
|
||||||
|
"imgur": {
|
||||||
|
"favorite-folder": "Favorites Folders",
|
||||||
|
},
|
||||||
"instagram": {
|
"instagram": {
|
||||||
"posts": "",
|
"posts": "",
|
||||||
"saved": "Saved Posts",
|
"saved": "Saved Posts",
|
||||||
|
|||||||
Reference in New Issue
Block a user