diff options
Diffstat (limited to 'gallery_dl/extractor/imagechest.py')
| -rw-r--r-- | gallery_dl/extractor/imagechest.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/gallery_dl/extractor/imagechest.py b/gallery_dl/extractor/imagechest.py index 086b95d..9229617 100644 --- a/gallery_dl/extractor/imagechest.py +++ b/gallery_dl/extractor/imagechest.py @@ -37,6 +37,9 @@ class ImagechestGalleryExtractor(GalleryExtractor): "url": "f5674e8ba79d336193c9f698708d9dcc10e78cc7", "count": 52, }), + ("https://imgchest.com/p/xxxxxxxxxxx", { + "exception": exception.NotFoundError, + }), ) def __init__(self, match): @@ -44,6 +47,12 @@ class ImagechestGalleryExtractor(GalleryExtractor): url = self.root + "/p/" + self.gallery_id GalleryExtractor.__init__(self, match, url) + self.access_token = self.config("access-token") + if self.access_token: + self.gallery_url = None + self.metadata = self._metadata_api + self.images = self._images_api + def metadata(self, page): if "Sorry, but the page you requested could not be found." in page: raise exception.NotFoundError("gallery") @@ -71,3 +80,69 @@ class ImagechestGalleryExtractor(GalleryExtractor): (url, None) for url in text.extract_iter(page, 'data-url="', '"') ] + + def _metadata_api(self, page): + api = ImagechestAPI(self, self.access_token) + post = api.post(self.gallery_id) + + post["date"] = text.parse_datetime( + post["created"], "%Y-%m-%dT%H:%M:%S.%fZ") + for img in post["images"]: + img["date"] = text.parse_datetime( + img["created"], "%Y-%m-%dT%H:%M:%S.%fZ") + + post["gallery_id"] = self.gallery_id + post.pop("image_count", None) + self._image_list = post.pop("images") + + return post + + def _images_api(self, page): + return [ + (img["link"], img) + for img in self._image_list + ] + + +class ImagechestAPI(): + """Interface for the Image Chest API + + https://imgchest.com/docs/api/1.0/general/overview + """ + root = "https://api.imgchest.com" + + def __init__(self, extractor, access_token): + self.extractor = extractor + self.headers = {"Authorization": "Bearer " + access_token} + + def file(self, file_id): + endpoint = "/v1/file/" + file_id + return self._call(endpoint) + + def post(self, post_id): + endpoint = "/v1/post/" + post_id + return self._call(endpoint) + + def user(self, username): + endpoint = "/v1/user/" + username + return self._call(endpoint) + + def _call(self, endpoint): + url = self.root + endpoint + + while True: + response = self.extractor.request( + url, headers=self.headers, fatal=None, allow_redirects=False) + + if response.status_code < 300: + return response.json()["data"] + + elif response.status_code < 400: + raise exception.AuthenticationError("Invalid API access token") + + elif response.status_code == 429: + self.extractor.wait(seconds=600) + + else: + self.extractor.log.debug(response.text) + raise exception.StopExtraction("API request failed") |
