Skip to content

Commit

Permalink
[exhentai] add 'metadata' option (#1325)
Browse files Browse the repository at this point in the history
to select between gallery metadata from 'api' or 'html'
  • Loading branch information
mikf committed Feb 22, 2021
1 parent 8f095a0 commit e573536
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
13 changes: 13 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,19 @@ Description
instead of the value listed on ``https://e-hentai.org/home.php``


extractor.exhentai.metadata
---------------------------
Type
``string``
Default
``"html"``
Description
Select the gallery metadata source.

* ``"api"``: Get data from the `API <https://ehwiki.org/wiki/API>`_.
* ``"html"``: Extract data from HTML.


extractor.exhentai.original
---------------------------
Type
Expand Down
28 changes: 25 additions & 3 deletions gallery_dl/extractor/exhentai.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ def items(self):
gpage = self._gallery_page()

data = self.get_metadata(gpage)
self.count = data["count"]

yield Message.Version, 1
yield Message.Directory, data

images = itertools.chain(
Expand All @@ -200,6 +197,11 @@ def items(self):

def get_metadata(self, page):
"""Extract gallery metadata"""
if self.config("metadata") == "api":
return self.metadata_from_api()
return self.metadata_from_page(page)

def metadata_from_page(self, page):
extr = text.extract_from(page)
data = {
"gallery_id" : self.gallery_id,
Expand All @@ -225,6 +227,7 @@ def get_metadata(self, page):
"torrentcount" : text.parse_int(extr('>Torrent Download (', ')')),
}

self.count = data["count"]
data["lang"] = util.language_to_code(data["language"])
data["tags"] = [
text.unquote(tag.replace("+", " "))
Expand All @@ -233,6 +236,25 @@ def get_metadata(self, page):

return data

def metadata_from_api(self):
url = self.root + "/api.php"
data = {
"method": "gdata",
"gidlist": ((self.gallery_id, self.gallery_token),),
"namespace": 1,
}

data = self.request(url, method="POST", json=data).json()
if "error" in data:
raise exception.StopExtraction(data["error"])

data = data["gmetadata"][0]
data["eh_category"] = data["category"]
data["date"] = text.parse_timestamp(data["posted"])
self.count = data["filecount"]

return data

def image_from_page(self, page):
"""Get image url and data from webpage"""
pos = page.index('<div id="i3"><a onclick="return load_image(') + 26
Expand Down

0 comments on commit e573536

Please sign in to comment.