Debian Codegraph @

gallery_dl/extractor/sankakucomplex.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2019-2025 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://news.sankakucomplex.com/"""
10
11 from .common import Extractor, Message
12 from .. import text, util
13
14
15 class SankakucomplexExtractor(Extractor):
16 """Base class for sankakucomplex extractors"""
17 category = "sankakucomplex"
18 root = "https://news.sankakucomplex.com"
19
20 def __init__(self, match):
21 Extractor.__init__(self, match)
22 self.path = match[1]
23
24
25 class SankakucomplexArticleExtractor(SankakucomplexExtractor):
26 """Extractor for articles on news.sankakucomplex.com"""
27 subcategory = "article"
28 directory_fmt = ("{category}", "{date:%Y-%m-%d} {title}")
29 filename_fmt = "{filename}.{extension}"
30 archive_fmt = "{date:%Y%m%d}_{filename}"
31 pattern = (r"(?:https?://)?(?:news|www)\.sankakucomplex\.com"
32 r"/(\d\d\d\d/\d\d/\d\d/[^/?#]+)")
33 example = "https://news.sankakucomplex.com/1970/01/01/TITLE"
34
35 def items(self):
36 url = f"{self.root}/{self.path}/?pg=X"
37 extr = text.extract_from(self.request(url).text)
38 data = {
39 "title" : text.unescape(
40 extr('property="og:title" content="', '"')),
41 "description": text.unescape(
42 extr('property="og:description" content="', '"')),
43 "date" : self.parse_datetime_iso(
44 extr('property="article:published_time" content="', '"')),
45 }
46 content = extr('<div class="entry-content">', '</article>')
47 data["tags"] = text.split_html(extr('="meta-tags">', '</div>'))[::2]
48
49 files = self._extract_images(content)
50 if self.config("videos", True):
51 files += self._extract_videos(content)
52 if self.config("embeds", False):
53 files += self._extract_embeds(content)
54 data["count"] = len(files)
55
56 yield Message.Directory, "", data
57 for num, url in enumerate(files, 1):
58 file = text.nameext_from_url(url)
59 if url[0] == "/":
60 url = text.urljoin(self.root, url)
61 file["url"] = url
62 file["num"] = num
63 file.update(data)
64 yield Message.Url, url, file
65
66 def _extract_images(self, content):
67 orig_sub = text.re(r"-\d+x\d+\.").sub
68 return [
69 orig_sub(".", url) for url in
70 util.unique(text.extract_iter(content, 'data-lazy-src="', '"'))
71 ]
72
73 def _extract_videos(self, content):
74 return text.re(r"<source [^>]*src=[\"']([^\"']+)").findall(content)
75
76 def _extract_embeds(self, content):
77 return [
78 "ytdl:" + url for url in
79 text.re(r"<iframe [^>]*src=[\"']([^\"']+)").findall(content)
80 ]
81
82
83 class SankakucomplexTagExtractor(SankakucomplexExtractor):
84 """Extractor for sankakucomplex blog articles by tag or author"""
85 subcategory = "tag"
86 pattern = (r"(?:https?://)?(?:news|www)\.sankakucomplex\.com"
87 r"/((?:tag|category|author)/[^/?#]+)")
88 example = "https://news.sankakucomplex.com/tag/TAG/"
89
90 def items(self):
91 pnum = 1
92 data = {"_extractor": SankakucomplexArticleExtractor}
93
94 while True:
95 url = f"{self.root}/{self.path}/page/{pnum}/"
96 response = self.request(url, fatal=False)
97 if response.status_code >= 400:
98 return
99 for url in util.unique_sequence(text.extract_iter(
100 response.text, 'data-direct="', '"')):
101 yield Message.Queue, url, data
102 pnum += 1