[hitomi] implement workaround for "broken" redirects
Some galleries redirect to a new "version" with different gallery id. This new version might not be available any more, but the /reader/ page for the original gallery id can still work.
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
# it under the terms of the GNU General Public License version 2 as
|
# it under the terms of the GNU General Public License version 2 as
|
||||||
# published by the Free Software Foundation.
|
# published by the Free Software Foundation.
|
||||||
|
|
||||||
"""Extract images from https://hitomi.la/"""
|
"""Extractors for https://hitomi.la/"""
|
||||||
|
|
||||||
from .common import GalleryExtractor
|
from .common import GalleryExtractor
|
||||||
from .. import text, util
|
from .. import text, util
|
||||||
@@ -27,21 +27,25 @@ class HitomiGalleryExtractor(GalleryExtractor):
|
|||||||
"keyword": "6701f8f588f119ef84cd29bdf99a399417b0a6a2",
|
"keyword": "6701f8f588f119ef84cd29bdf99a399417b0a6a2",
|
||||||
"count": 16,
|
"count": 16,
|
||||||
}),
|
}),
|
||||||
|
# download test
|
||||||
("https://hitomi.la/galleries/1401410.html", {
|
("https://hitomi.la/galleries/1401410.html", {
|
||||||
# download test
|
|
||||||
"range": "1",
|
"range": "1",
|
||||||
"content": "b3ca8c6c8cc5826cf8b4ceb7252943abad7b8b4c",
|
"content": "b3ca8c6c8cc5826cf8b4ceb7252943abad7b8b4c",
|
||||||
}),
|
}),
|
||||||
|
# Game CG with scenes (#321)
|
||||||
("https://hitomi.la/galleries/733697.html", {
|
("https://hitomi.la/galleries/733697.html", {
|
||||||
# Game CG with scenes (#321)
|
|
||||||
"url": "21064f9e3c244aca87f1a91967a3fbe79032c4ce",
|
"url": "21064f9e3c244aca87f1a91967a3fbe79032c4ce",
|
||||||
"count": 210,
|
"count": 210,
|
||||||
}),
|
}),
|
||||||
|
# fallback for galleries only available through /reader/ URLs
|
||||||
("https://hitomi.la/galleries/1045954.html", {
|
("https://hitomi.la/galleries/1045954.html", {
|
||||||
# fallback for galleries only available through /reader/ URLs
|
|
||||||
"url": "0a67f5e6c3c6a384b578e328f4817fa6ccdf856a",
|
"url": "0a67f5e6c3c6a384b578e328f4817fa6ccdf856a",
|
||||||
"count": 1413,
|
"count": 1413,
|
||||||
}),
|
}),
|
||||||
|
# gallery with "broken" redirect
|
||||||
|
("https://hitomi.la/cg/scathacha-sama-okuchi-ecchi-1291900.html", {
|
||||||
|
"count": 10,
|
||||||
|
}),
|
||||||
("https://hitomi.la/manga/amazon-no-hiyaku-867789.html"),
|
("https://hitomi.la/manga/amazon-no-hiyaku-867789.html"),
|
||||||
("https://hitomi.la/manga/867789.html"),
|
("https://hitomi.la/manga/867789.html"),
|
||||||
("https://hitomi.la/doujinshi/867789.html"),
|
("https://hitomi.la/doujinshi/867789.html"),
|
||||||
@@ -52,31 +56,34 @@ class HitomiGalleryExtractor(GalleryExtractor):
|
|||||||
|
|
||||||
def __init__(self, match):
|
def __init__(self, match):
|
||||||
self.gallery_id = match.group(1)
|
self.gallery_id = match.group(1)
|
||||||
self.fallback = False
|
url = "https://ltn.hitomi.la/galleries/{}.js".format(self.gallery_id)
|
||||||
url = "{}/galleries/{}.html".format(self.root, self.gallery_id)
|
|
||||||
GalleryExtractor.__init__(self, match, url)
|
GalleryExtractor.__init__(self, match, url)
|
||||||
|
self.session.headers["Referer"] = "{}/reader/{}.html".format(
|
||||||
|
self.root, self.gallery_id)
|
||||||
|
|
||||||
def request(self, url, **kwargs):
|
def metadata(self, _):
|
||||||
response = GalleryExtractor.request(self, url, fatal=False, **kwargs)
|
# try galleries page first
|
||||||
if response.status_code == 404:
|
url = "{}/galleries/{}.html".format(self.root, self.gallery_id)
|
||||||
self.fallback = True
|
response = self.request(url, fatal=False)
|
||||||
url = url.replace("/galleries/", "/reader/")
|
|
||||||
response = GalleryExtractor.request(self, url, **kwargs)
|
# follow redirects
|
||||||
elif b"<title>Redirect</title>" in response.content:
|
if b"<title>Redirect</title>" in response.content:
|
||||||
url = text.extract(response.text, "href='", "'")[0]
|
url = text.extract(response.text, "href='", "'")[0]
|
||||||
if not url.startswith("http"):
|
if not url.startswith("http"):
|
||||||
url = text.urljoin(self.root, url)
|
url = text.urljoin(self.root, url)
|
||||||
response = self.request(url, **kwargs)
|
response = self.request(url, fatal=False)
|
||||||
return response
|
|
||||||
|
|
||||||
def metadata(self, page):
|
# fallback to reader page
|
||||||
if self.fallback:
|
if response.status_code >= 400:
|
||||||
|
url = "{}/reader/{}.html".format(self.root, self.gallery_id)
|
||||||
|
page = self.request(url).text
|
||||||
return {
|
return {
|
||||||
"gallery_id": text.parse_int(self.gallery_id),
|
"gallery_id": text.parse_int(self.gallery_id),
|
||||||
"title": text.unescape(text.extract(
|
"title": text.unescape(text.extract(
|
||||||
page, "<title>", "<")[0].rpartition(" | ")[0]),
|
page, "<title>", "<")[0].rpartition(" | ")[0]),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
page = response.text
|
||||||
extr = text.extract_from(page, page.index('<h1><a href="/reader/'))
|
extr = text.extract_from(page, page.index('<h1><a href="/reader/'))
|
||||||
data = {
|
data = {
|
||||||
"gallery_id": text.parse_int(self.gallery_id),
|
"gallery_id": text.parse_int(self.gallery_id),
|
||||||
@@ -96,13 +103,6 @@ class HitomiGalleryExtractor(GalleryExtractor):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
def images(self, page):
|
def images(self, page):
|
||||||
# set Referer header before image downloads (#239)
|
|
||||||
self.session.headers["Referer"] = self.gallery_url
|
|
||||||
|
|
||||||
# get 'galleryinfo'
|
|
||||||
url = "https://ltn.hitomi.la/galleries/{}.js".format(self.gallery_id)
|
|
||||||
page = self.request(url).text
|
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
for image in json.loads(page.partition("=")[2]):
|
for image in json.loads(page.partition("=")[2]):
|
||||||
ihash = image["hash"]
|
ihash = image["hash"]
|
||||||
|
|||||||
Reference in New Issue
Block a user