2
2
import logging
3
3
import os
4
4
import tempfile
5
+ import warnings
5
6
from pathlib import Path
6
- from typing import Literal , Union , Optional , Set
7
+ from typing import Literal , Union , Optional , Set , ClassVar
7
8
8
9
from loguru import logger
9
- from pydantic import BaseModel , model_validator
10
+ from pydantic import BaseModel , model_validator , Field , field_validator
10
11
from pydantic_settings import BaseSettings , SettingsConfigDict
11
12
12
13
__all__ = [
@@ -60,6 +61,10 @@ class DownloaderConfiguration(BaseModel):
60
61
:ivar retry_interval: Seconds of downloader retry interval
61
62
:ivar use_bucket: Enable local storage bucket mode
62
63
:ivar bucket_path: Path of local storage bucket
64
+ :ivar reverse_proxy: Reverse proxy format for download URL. \
65
+ Customize the filename format by inserting an empty ``{}`` to represent the original URL. \
66
+ For example: ``https://example.com/{}`` will be ``https://example.com/https://n1.kemono.su/data/66/83/xxxxx.jpg``; \
67
+ ``https://example.com/?url={}`` will be ``https://example.com/?url=https://n1.kemono.su/data/66/83/xxxxx.jpg``
63
68
"""
64
69
scheme : Literal ["http" , "https" ] = "https"
65
70
timeout : float = 30.0
@@ -72,6 +77,7 @@ class DownloaderConfiguration(BaseModel):
72
77
retry_interval : float = 3.0
73
78
use_bucket : bool = False
74
79
bucket_path : Path = Path ("./.ktoolbox/bucket_storage" )
80
+ reverse_proxy : str = "{}"
75
81
76
82
@model_validator (mode = "after" )
77
83
def check_bucket_path (self ) -> "DownloaderConfiguration" :
@@ -107,18 +113,48 @@ class PostStructureConfiguration(BaseModel):
107
113
└─ 2.png
108
114
```
109
115
116
+ - Available properties for ``file``
117
+
118
+ | Property | Type |
119
+ |---------------|--------|
120
+ | ``id`` | String |
121
+ | ``user`` | String |
122
+ | ``service`` | String |
123
+ | ``title`` | String |
124
+ | ``added`` | Date |
125
+ | ``published`` | Date |
126
+ | ``edited`` | Date |
127
+
110
128
:ivar attachments: Sub path of attachment directory
111
- :ivar content_filepath: Sub path of post content file
129
+ :ivar content: Sub path of post content file
130
+ :ivar content_filepath: (**Deprecated**, Use ``content`` instead) Sub path of post content file
131
+ :ivar file: The format of the post `file` filename (`file` is not `attachment`, each post has only one `file`, usually the cover image) \
132
+ Customize the filename format by inserting an empty ``{}`` to represent the basic filename. \
133
+ You can use some of the [properties][ktoolbox.configuration.JobConfiguration] \
134
+ in Post. For example: ``{title}_{}`` could result in filenames like \
135
+ ``TheTitle_Stelle_lv5_logo.gif``, ``TheTitle_ScxHjZIdxt5cnjaAwf3ql2p7.jpg``, etc.
112
136
"""
113
137
attachments : Path = Path ("attachments" )
138
+ content : Path = Path ("content.txt" )
114
139
content_filepath : Path = Path ("content.txt" )
140
+ file : str = "{id}_{}"
141
+
142
+ @field_validator ("content_filepath" )
143
+ def content_filepath_validator (cls , v ):
144
+ # noinspection PyUnresolvedReferences
145
+ if v != cls .model_fields ["content_filepath" ].default :
146
+ warnings .warn (
147
+ "`PostStructureConfiguration.content_filepath` is deprecated and is scheduled for removal in further version. "
148
+ "Use `PostStructureConfiguration.content` instead" ,
149
+ FutureWarning
150
+ )
115
151
116
152
117
153
class JobConfiguration (BaseModel ):
118
154
"""
119
155
Download jobs Configuration
120
156
121
- - Available properties for ``post_dirname_format``
157
+ - Available properties for ``post_dirname_format`` and ``filename_format``
122
158
123
159
| Property | Type |
124
160
|---------------|--------|
@@ -133,15 +169,15 @@ class JobConfiguration(BaseModel):
133
169
:ivar count: Number of coroutines for concurrent download
134
170
:ivar post_dirname_format: Customize the post directory name format, you can use some of the \
135
171
[properties][ktoolbox.configuration.JobConfiguration] in ``Post``. \
136
- e.g. ``[{published}]{id}`` > ``[2024-1-1]123123``, ``{user}_{published}_{title}`` > ``234234_2024-1-1_HelloWorld ``
172
+ e.g. ``[{published}]{id}`` > ``[2024-1-1]123123``, ``{user}_{published}_{title}`` > ``234234_2024-1-1_TheTitle ``
137
173
:ivar post_structure: Post path structure
138
174
:ivar mix_posts: Save all files from different posts at same path in creator directory. \
139
175
It would not create any post directory, and ``CreatorIndices`` would not been recorded.
140
176
:ivar sequential_filename: Rename attachments in numerical order, e.g. ``1.png``, ``2.png``, ...
141
177
:ivar filename_format: Customize the filename format by inserting an empty ``{}`` to represent the basic filename.
142
178
Similar to post_dirname_format, you can use some of the [properties][ktoolbox.configuration.JobConfiguration] \
143
179
in Post. For example: ``{title}_{}`` could result in filenames like \
144
- ``HelloWorld_b4b41de2 -8736-480d-b5c3-ebf0d917561b``, ``HelloWorld_af349b25 -ac08-46d7-98fb-6ce99a237b90``, etc. \
180
+ ``TheTitle_b4b41de2 -8736-480d-b5c3-ebf0d917561b``, ``TheTitle_af349b25 -ac08-46d7-98fb-6ce99a237b90``, etc. \
145
181
You can also use it with ``sequential_filename``. For instance, \
146
182
``[{published}]_{}`` could result in filenames like ``[2024-1-1]_1.png``, ``[2024-1-1]_2.png``, etc.
147
183
:ivar allow_list: Download files which match these patterns (Unix shell-style), e.g. ``["*.png"]``
@@ -153,8 +189,10 @@ class JobConfiguration(BaseModel):
153
189
mix_posts : bool = False
154
190
sequential_filename : bool = False
155
191
filename_format : str = "{}"
156
- allow_list : Set [str ] = set ()
157
- block_list : Set [str ] = set ()
192
+ # noinspection PyDataclass
193
+ allow_list : Set [str ] = Field (default_factory = set )
194
+ # noinspection PyDataclass
195
+ block_list : Set [str ] = Field (default_factory = set )
158
196
159
197
160
198
class LoggerConfiguration (BaseModel ):
@@ -195,13 +233,13 @@ class Configuration(BaseSettings):
195
233
use_uvloop : bool = True
196
234
197
235
# noinspection SpellCheckingInspection
198
- model_config = SettingsConfigDict (
236
+ model_config : ClassVar [ SettingsConfigDict ] = SettingsConfigDict (
199
237
env_prefix = 'ktoolbox_' ,
200
238
env_nested_delimiter = '__' ,
201
- env_file = '.env' ,
239
+ env_file = [ '.env' , 'prod.env' ] ,
202
240
env_file_encoding = 'utf-8' ,
203
241
extra = 'ignore'
204
242
)
205
243
206
244
207
- config = Configuration (_env_file = [ '.env' , 'prod.env' ] )
245
+ config = Configuration ()
0 commit comments