Debian Codegraph @

gallery_dl/extractor/dcinside.py

raw
1 # -*- coding: utf-8 -*-
2
3 # Copyright 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://www.dcinside.com/"""
10
11 from .common import GalleryExtractor, Extractor, Message
12 from .. import text
13
14
15 class DcinsideGalleryExtractor(GalleryExtractor):
16 category = "dcinside"
17 subcategory = "gallery"
18 root = "https://gall.dcinside.com"
19 directory_fmt = ("{category}", "{username}")
20 filename_fmt = "{id} {title} {num:>02}.{extension}"
21 archive_fmt = "{id}_{num}"
22 pattern = (r"(?:https?://)?gall\.dcinside\.com"
23 r"(/(?:mgallery/)?board/view/?\?([^#]+))")
24 example = "https://gall.dcinside.com/board/view/?id=ID&no=12345"
25
26 def metadata(self, page):
27 # potentially invalid JSON-LD due to unescaped " quotes
28 # -> extract data manually
29 extr = text.extract_from(page)
30 params = text.parse_query(self.groups[1])
31
32 return {
33 "id" : text.parse_int(params.get("no")),
34 "board" : params.get("id"),
35 "title" : extr('"headline":"', '",\r'),
36 "content" : extr('"articleBody":"', '",\r'),
37 "date" : self.parse_datetime_iso(extr(
38 '"datePublished":"', '"')),
39 "display_name" : extr('"name":"', '",\r'),
40 "username" : extr('"url":"https://gallog.dcinside.com/', '"'),
41 "comments" : text.parse_int(extr(
42 '"userInteractionCount": ', ' ')),
43 "views" : text.parse_int(extr(
44 '"userInteractionCount": ', ' ')),
45 "_http_headers": {"Accept-Encoding": "identity"},
46 }
47
48 def images(self, page):
49 if write_div := text.extr(page, 'class="write_div', "</div>"):
50 results = []
51 for img in text.extract_iter(write_div, "<img", ">"):
52 url = text.unescape(text.extr(img, ' data-original="', '"') or
53 text.extr(img, ' src="', '"') or
54 text.extr(img, " src='", "'"))
55 results.append((url, {
56 "hash" : text.extr(img, ' alt="', '"'),
57 "extension": "jpg",
58 }))
59 return results
60
61 if image := text.extr(page, '"image":{', '}'):
62 url = text.extr(image, '"URL":"', '"')
63 return ((url, {"extension": "jpg"}),)
64
65
66 class DcinsideUserExtractor(Extractor):
67 category = "dcinside"
68 subcategory = "user"
69 root = "https://gallog.dcinside.com"
70 pattern = (r"(?:https?://)?gallog\.dcinside\.com/([^/?#]+)"
71 r"(?:(/posting(?:/index)?)/?\?([^#]+))?")
72 example = "https://gallog.dcinside.com/USER"
73
74 def items(self):
75 username, path, query = self.groups
76 url = f"{self.root}/{username}{path or '/posting'}"
77 params = text.parse_query(query)
78 params["p"] = text.parse_int(params.get("p"), 1)
79
80 data = {"_extractor": DcinsideGalleryExtractor}
81 while True:
82 page = self.request(url, params=params).text
83
84 for href in text.extract_iter(
85 page, '<a class="link " href="', '"'):
86 yield Message.Queue, text.unescape(href), data
87
88 if "</em><a href=" not in page:
89 break
90 params["p"] += 1