Debian Codegraph @

gallery_dl/extractor/issuu.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2019-2026 Mike Fährmann
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation.
8
9 """Extractors for https://issuu.com/"""
10
11 from .common import GalleryExtractor, Extractor, Message
12 from .. import text, util
13
14
15 class IssuuBase():
16 """Base class for issuu extractors"""
17 category = "issuu"
18 root = "https://issuu.com"
19
20
21 class IssuuPublicationExtractor(IssuuBase, GalleryExtractor):
22 """Extractor for a single publication"""
23 subcategory = "publication"
24 directory_fmt = ("{category}", "{document[username]}",
25 "{document[date]:%Y-%m-%d} {document[title]}")
26 filename_fmt = "{num:>03}.{extension}"
27 archive_fmt = "{document[publicationId]}_{num}"
28 pattern = r"(?:https?://)?issuu\.com(/[^/?#]+/docs/[^/?#]+)"
29 example = "https://issuu.com/issuu/docs/TITLE/"
30
31 def metadata(self, page):
32 data = text.extr(
33 page, '\\"initialDocumentData\\":', ',\\"initialPageNumber')
34 data = util.json_loads(text.unescape(data.replace('\\"', '"')))
35
36 doc = data["document"]
37 doc["date"] = self.parse_datetime_iso(
38 doc["originalPublishDateInISOString"])
39
40 try:
41 path = doc.pop("path")
42 doc["username"] = path["username"]
43 doc["documentName"] = path["documentName"]
44 except Exception as exc:
45 self.log.traceback(exc)
46
47 self.count = text.parse_int(doc["pageCount"])
48 self.base = (f"https://image.isu.pub/{doc['revisionId']}-"
49 f"{doc['publicationId']}/jpg/page_")
50
51 return {"document": doc}
52
53 def images(self, page):
54 return [(f"{self.base}{i}.jpg", None)
55 for i in range(1, self.count + 1)]
56
57
58 class IssuuUserExtractor(IssuuBase, Extractor):
59 """Extractor for all publications of a user/publisher"""
60 subcategory = "user"
61 pattern = r"(?:https?://)?issuu\.com/([^/?#]+)(?:/(\d*))?$"
62 example = "https://issuu.com/USER"
63
64 def items(self):
65 user, pnum = self.groups
66 base = f"{self.root}/{user}"
67 pnum = text.parse_int(pnum, 1)
68
69 while True:
70 url = f"{base}/{pnum}" if pnum > 1 else base
71 try:
72 html = self.request(url).text
73 data = text.extr(html, '\\"docs\\":', '}],')
74 docs = util.json_loads(data.replace('\\"', '"') + "}]")
75 except Exception as exc:
76 self.log.traceback(exc)
77 return
78
79 for publication in docs:
80 url = self.root + "/" + publication["uri"]
81 publication["_extractor"] = IssuuPublicationExtractor
82 yield Message.Queue, url, publication
83
84 if len(docs) < 48:
85 return
86 pnum += 1