1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-24 03:32:33 +01:00
gallery-dl/gallery_dl/extractor/mangareader.py

115 lines
4.5 KiB
Python
Raw Normal View History

2015-06-26 22:07:36 +02:00
# -*- coding: utf-8 -*-
# Copyright 2015 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
2015-11-28 22:21:35 +01:00
"""Extract manga-chapters and entire manga from http://www.mangareader.net/"""
2015-06-26 22:07:36 +02:00
from .common import AsynchronousExtractor, Extractor, Message
2015-10-03 15:43:02 +02:00
from .. import text
2015-06-26 22:07:36 +02:00
class MangaReaderBase():
2015-11-28 22:21:35 +01:00
"""Base class for mangareader extractors"""
category = "mangareader"
directory_fmt = ["{category}", "{manga}", "c{chapter:>03} - {title}"]
filename_fmt = "{manga}_c{chapter:>03}_{page:>03}.{extension}"
url_base = "http://www.mangareader.net"
class MangaReaderMangaExtractor(MangaReaderBase, Extractor):
2015-11-28 22:21:35 +01:00
"""Extract all manga-chapters from mangareader"""
pattern = [r"(?:https?://)?(?:www\.)?mangareader\.net(/[^/]+)$"]
def __init__(self, match):
Extractor.__init__(self)
self.url_title = match.group(1)
def items(self):
yield Message.Version, 1
url = self.url_base + self.url_title
page = self.request(url).text
needle = '<a href="' + self.url_title
pos = page.index('<div id="readmangasum">')
2015-11-28 02:06:29 +01:00
for chapter in text.extract_iter(page, needle, '"', pos):
yield Message.Queue, url + chapter
class MangaReaderChapterExtractor(MangaReaderBase, AsynchronousExtractor):
2015-11-28 22:21:35 +01:00
"""Extract a single manga-chapter from mangareader"""
2015-11-21 04:26:30 +01:00
pattern = [
r"(?:https?://)?(?:www\.)?mangareader\.net((/[^/]+)/(\d+))",
r"(?:https?://)?(?:www\.)?mangareader\.net(/\d+-\d+-\d+(/[^/]+)/chapter-(\d+).html)",
]
2015-06-26 22:07:36 +02:00
def __init__(self, match):
AsynchronousExtractor.__init__(self)
2015-11-03 00:08:25 +01:00
self.part, self.url_title, self.chapter = match.groups()
2015-06-26 22:07:36 +02:00
def items(self):
2015-11-03 00:08:25 +01:00
page = self.request(self.url_base + self.part).text
data = self.get_job_metadata(page)
2015-06-26 22:07:36 +02:00
yield Message.Version, 1
2015-11-03 00:08:25 +01:00
yield Message.Directory, data
for i in range(1, int(data["count"])+1):
next_url, image_url, image_data = self.get_page_metadata(page)
image_data.update(data)
image_data["page"] = i
yield Message.Url, image_url, image_data
if next_url:
page = self.request(next_url).text
2015-06-26 22:07:36 +02:00
2015-11-03 00:08:25 +01:00
def get_job_metadata(self, chapter_page):
"""Collect metadata for extractor-job"""
page = self.request(self.url_base + self.url_title).text
data = {
2015-11-08 00:02:37 +01:00
"category": self.category,
2015-11-03 00:08:25 +01:00
"chapter": self.chapter,
"lang": "en",
"language": "English",
}
data, _ = text.extract_all(page, (
(None, '<td class="propertytitle">Name:', ''),
("manga", '<h2 class="aname">', '</h2>'),
(None, '<td class="propertytitle">Year of Release:', ''),
('manga-release', '<td>', '</td>'),
(None, '<td class="propertytitle">Author:', ''),
('author', '<td>', '</td>'),
(None, '<td class="propertytitle">Artist:', ''),
('artist', '<td>', '</td>'),
(None, '<div id="readmangasum">', ''),
('title', ' ' + self.chapter + '</a> : ', '</td>'),
('chapter-date', '<td>', '</td>'),
), values=data)
data, _ = text.extract_all(chapter_page, (
(None, '<select id="pageMenu"', ''),
('count', '</select> of ', '</div>'),
), values=data)
for key in ("author", "artist"):
data[key] = text.unescape(data[key])
data["manga"] = data["manga"].strip()
2015-11-03 00:08:25 +01:00
return data
def get_page_metadata(self, page):
2015-06-26 22:07:36 +02:00
"""Collect next url, image-url and metadata for one manga-page"""
2015-10-03 15:43:02 +02:00
extr = text.extract
width = None
2015-11-03 00:08:25 +01:00
test , pos = extr(page, "document['pu']", '')
if test is None:
2015-06-26 22:07:36 +02:00
return None, None, None
if page.find("document['imgwidth']", pos, pos+200) != -1:
width , pos = extr(page, "document['imgwidth'] = ", ";", pos)
height, pos = extr(page, "document['imgheight'] = ", ";", pos)
_ , pos = extr(page, '<div id="imgholder">', '')
url, pos = extr(page, ' href="', '"', pos)
if width is None:
width , pos = extr(page, '<img id="img" width="', '"', pos)
height, pos = extr(page, ' height="', '"', pos)
image, pos = extr(page, ' src="', '"', pos)
2015-11-16 17:32:26 +01:00
return self.url_base + url, image, text.nameext_from_url(image, {
2015-06-26 22:07:36 +02:00
"width": width,
"height": height,
2015-11-16 17:32:26 +01:00
})