Debian Codegraph @

gallery_dl/extractor/cara.py

raw symbols SCIP
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://cara.app/"""
10
11 from .common import Extractor, Message
12 from .. import text
13
14 BASE_PATTERN = r"(?:https?://)?(?:www\.)?cara\.app"
15
16
17 class CaraExtractor(Extractor):
18 """Base class for cara extractors"""
19 category = "cara"
20 root = "https://cara.app"
21 root_cdn = "https://cdn.cara.app"
22 directory_fmt = ("{category}", "{name} ({authorId})")
23 filename_fmt = "{date}{title:? //} {num:>02} ({id}).{extension}"
24 archive_fmt = "{file_id}"
25
26 def items(self):
27 for post in self.posts():
28 imgs = [img for img in post.pop("images", ())
29 if img.get("order", 0) >= 0]
30 imgs.sort(key=lambda img: img.get("order", 0))
31
32 post["count"] = len(imgs)
33 post["date"] = self.parse_datetime_iso(post["createdAt"])
34 yield Message.Directory, "", post
35
36 for post["num"], img in enumerate(imgs, 1):
37 img["file_id"] = img.pop("id", None)
38 src = img["src"]
39 post.update(img)
40 text.nameext_from_url(src, post)
41 yield Message.Url, f"{self.root_cdn}/{src}", post
42
43
44 class CaraPostExtractor(CaraExtractor):
45 subcategory = "post"
46 pattern = BASE_PATTERN + r"/post/([0-9a-f-]{36})"
47 example = "https://cara.app/post/01234567-89ab-cdef-0123-456789abcdef"
48
49 def posts(self):
50 url = f"{self.root}/api/posts/{self.groups[0]}"
51 return (self.request_json(url)["data"],)
52
53
54 class CaraUserExtractor(CaraExtractor):
55 subcategory = "user"
56 pattern = BASE_PATTERN + r"/([\w-]+)"
57 example = "https://cara.app/USER"
58
59 def posts(self):
60 url = "https://cara.app/api/posts/getAllByUser"
61 params = {
62 "slug": self.groups[0],
63 "take": 15,
64 "skip": None,
65 }
66
67 while True:
68 data = self.request_json(url, params=params)
69
70 if imgs := data.get("data"):
71 yield from imgs
72 else:
73 break
74
75 try:
76 params.update(data["paging"])
77 params["skip"] += params["take"]
78 except Exception:
79 break