[imagechest] update
- don't load HTML page when using API - restructure some code - add more methods to ImagechestAPI
This commit is contained in:
@@ -46,14 +46,14 @@ class ImagechestGalleryExtractor(GalleryExtractor):
|
|||||||
self.gallery_id = match.group(1)
|
self.gallery_id = match.group(1)
|
||||||
url = self.root + "/p/" + self.gallery_id
|
url = self.root + "/p/" + self.gallery_id
|
||||||
GalleryExtractor.__init__(self, match, url)
|
GalleryExtractor.__init__(self, match, url)
|
||||||
|
|
||||||
self.access_token = self.config("access-token")
|
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):
|
def metadata(self, page):
|
||||||
if self.access_token:
|
|
||||||
return self._metadata_api()
|
|
||||||
return self._metadata_html(page)
|
|
||||||
|
|
||||||
def _metadata_html(self, page):
|
|
||||||
if "Sorry, but the page you requested could not be found." in page:
|
if "Sorry, but the page you requested could not be found." in page:
|
||||||
raise exception.NotFoundError("gallery")
|
raise exception.NotFoundError("gallery")
|
||||||
|
|
||||||
@@ -63,28 +63,7 @@ class ImagechestGalleryExtractor(GalleryExtractor):
|
|||||||
page, 'property="og:title" content="', '"').strip())
|
page, 'property="og:title" content="', '"').strip())
|
||||||
}
|
}
|
||||||
|
|
||||||
def _metadata_api(self):
|
|
||||||
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(self, page):
|
def images(self, page):
|
||||||
if self.access_token:
|
|
||||||
return self._images_api()
|
|
||||||
return self._images_html(page)
|
|
||||||
|
|
||||||
def _images_html(self, page):
|
|
||||||
if " More Files</button>" in page:
|
if " More Files</button>" in page:
|
||||||
url = "{}/p/{}/loadAll".format(self.root, self.gallery_id)
|
url = "{}/p/{}/loadAll".format(self.root, self.gallery_id)
|
||||||
headers = {
|
headers = {
|
||||||
@@ -102,7 +81,23 @@ class ImagechestGalleryExtractor(GalleryExtractor):
|
|||||||
for url in text.extract_iter(page, 'data-url="', '"')
|
for url in text.extract_iter(page, 'data-url="', '"')
|
||||||
]
|
]
|
||||||
|
|
||||||
def _images_api(self):
|
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 [
|
return [
|
||||||
(img["link"], img)
|
(img["link"], img)
|
||||||
for img in self._image_list
|
for img in self._image_list
|
||||||
@@ -120,10 +115,18 @@ class ImagechestAPI():
|
|||||||
self.extractor = extractor
|
self.extractor = extractor
|
||||||
self.headers = {"Authorization": "Bearer " + access_token}
|
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):
|
def post(self, post_id):
|
||||||
endpoint = "/v1/post/" + post_id
|
endpoint = "/v1/post/" + post_id
|
||||||
return self._call(endpoint)
|
return self._call(endpoint)
|
||||||
|
|
||||||
|
def user(self, username):
|
||||||
|
endpoint = "/v1/user/" + username
|
||||||
|
return self._call(endpoint)
|
||||||
|
|
||||||
def _call(self, endpoint):
|
def _call(self, endpoint):
|
||||||
url = self.root + endpoint
|
url = self.root + endpoint
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user