API 文档¶
__description__ = 'A useful CLI tool for downloading posts in Kemono.party / .su'
module-attribute
¶
__title__ = 'KToolBox'
module-attribute
¶
__version__ = '0.6.0'
module-attribute
¶
__main__
¶
main()
¶
Source code in ktoolbox/__main__.py
9 10 11 12 13 14 15 16 |
|
action
¶
base
¶
fetch
¶
__all__ = ['FetchInterruptError', 'fetch_creator_posts']
module-attribute
¶
FetchInterruptError
¶
Bases: Exception
Exception for interrupt of data fetching
Source code in ktoolbox/action/fetch.py
11 12 13 14 15 16 |
|
fetch_creator_posts(service, creator_id, o=0)
async
¶
Fetch posts from a creator
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service |
str
|
The service where the post is located |
required |
creator_id |
str
|
The ID of the creator |
required |
o |
int
|
Result offset, stepping of 50 is enforced |
0
|
Returns:
Type | Description |
---|---|
AsyncGenerator[List[Post], Any]
|
Async generator of several list of posts |
Source code in ktoolbox/action/fetch.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
job
¶
__all__ = ['create_job_from_post', 'create_job_from_creator']
module-attribute
¶
create_job_from_creator(service, creator_id, path, *, all_pages=False, offset=0, length=50, save_creator_indices=False, mix_posts=None, start_time, end_time)
async
¶
Create a list of download job from a creator
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service |
str
|
The service where the post is located |
required |
creator_id |
str
|
The ID of the creator |
required |
path |
Path
|
The path for downloading posts, which needs to be sanitized |
required |
all_pages |
bool
|
Fetch all posts, |
False
|
offset |
int
|
Result offset (or start offset) |
0
|
length |
Optional[int]
|
The number of posts to fetch |
50
|
save_creator_indices |
bool
|
Record |
False
|
mix_posts |
bool
|
Save all files from different posts at same path, |
None
|
start_time |
Optional[datetime]
|
Start time of the time range |
required |
end_time |
Optional[datetime]
|
End time of the time range |
required |
Source code in ktoolbox/action/job.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
create_job_from_post(post, post_path, *, post_structure=None, dump_post_data=True)
async
¶
Create a list of download job from a post data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
post |
Post
|
post data |
required |
post_path |
Path
|
Path of the post directory, which needs to be sanitized |
required |
post_structure |
Union[PostStructureConfiguration, bool]
|
post path structure, |
None
|
dump_post_data |
bool
|
Whether to dump post data (post.json) in post directory |
True
|
Source code in ktoolbox/action/job.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
search
¶
__all__ = ['search_creator', 'search_creator_post']
module-attribute
¶
search_creator(id=None, name=None, service=None)
async
¶
Search creator with multiple keywords support.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id |
str
|
The ID of the creator |
None
|
name |
str
|
The name of the creator |
None
|
service |
str
|
The service for the creator |
None
|
Source code in ktoolbox/action/search.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
search_creator_post(id=None, name=None, service=None, q=None, o=None)
async
¶
Search posts from creator with multiple keywords support.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id |
str
|
The ID of the creator |
None
|
name |
str
|
The name of the creator |
None
|
service |
str
|
The service for the creator |
None
|
q |
str
|
Search query |
None
|
o |
str
|
Result offset, stepping of 50 is enforced |
None
|
Source code in ktoolbox/action/search.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
utils
¶
__all__ = ['generate_post_path_name', 'filter_posts_by_date', 'filter_posts_by_indices']
module-attribute
¶
filter_posts_by_date(post_list, start_date, end_date)
¶
Filter posts by publish date range
Parameters:
Name | Type | Description | Default |
---|---|---|---|
post_list |
List[Post]
|
List of posts |
required |
start_date |
Optional[datetime]
|
Start time of the time range |
required |
end_date |
Optional[datetime]
|
End time of the time range |
required |
Source code in ktoolbox/action/utils.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
filter_posts_by_indices(posts, indices)
¶
Compare and filter posts by CreatorIndices
data
Only keep posts that was edited after last download.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
posts |
List[Post]
|
Posts to filter |
required |
indices |
CreatorIndices
|
|
required |
Returns:
Type | Description |
---|---|
Tuple[List[Post], CreatorIndices]
|
A updated |
Source code in ktoolbox/action/utils.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
generate_post_path_name(post)
¶
Generate directory name for post to save.
Source code in ktoolbox/action/utils.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
api
¶
-
Kemono API version:
1.0.0
-
current App commit hash:
7ee4a7b18ee92a442c13950c05dc8236cfb14a60
base
¶
__all__ = ['APITenacityStop', 'APIRet', 'BaseAPI']
module-attribute
¶
APIRet
¶
Bases: BaseRet[_T]
Return data model of API call
Source code in ktoolbox/api/base.py
62 63 64 |
|
APITenacityStop
¶
Bases: stop_base
APIs Stop strategies
Source code in ktoolbox/api/base.py
21 22 23 24 25 26 27 28 |
|
__call__(retry_state)
¶
Source code in ktoolbox/api/base.py
24 25 26 27 28 |
|
BaseAPI
¶
Source code in ktoolbox/api/base.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
Response = BaseModel
class-attribute
instance-attribute
¶
API response model
extra_validator: Optional[Callable[[str], BaseModel]] = None
class-attribute
instance-attribute
¶
method: Literal['get', 'post']
instance-attribute
¶
path: str = '/'
class-attribute
instance-attribute
¶
__call__(*args, **kwargs)
abstractmethod
async
classmethod
¶
Function to call API
Source code in ktoolbox/api/base.py
130 131 132 133 134 |
|
handle_res(res)
classmethod
¶
Handle API response
Source code in ktoolbox/api/base.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
request(path=None, **kwargs)
async
classmethod
¶
Make a request to the API
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
Fully initialed URL path |
None
|
kwargs |
Keyword arguments of |
{}
|
Source code in ktoolbox/api/base.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
misc
¶
model
¶
announcement
¶
__all__ = ['Announcement']
module-attribute
¶
Announcement
¶
Bases: BaseModel
Source code in ktoolbox/api/model/announcement.py
9 10 11 12 13 14 15 16 17 |
|
added: Optional[datetime] = None
class-attribute
instance-attribute
¶
isoformat UTC
content: Optional[str] = None
class-attribute
instance-attribute
¶
hash: Optional[str] = None
class-attribute
instance-attribute
¶
sha256
service: Optional[str] = None
class-attribute
instance-attribute
¶
user_id: Optional[str] = None
class-attribute
instance-attribute
¶
creator
¶
__all__ = ['Creator']
module-attribute
¶
Creator
¶
Bases: BaseModel
Source code in ktoolbox/api/model/creator.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
favorited: int
instance-attribute
¶
The number of times this creator has been favorited
id: str
instance-attribute
¶
The ID of the creator
indexed: float
instance-attribute
¶
Timestamp when the creator was indexed, Unix time as integer
name: str
instance-attribute
¶
The name of the creator
service: str
instance-attribute
¶
The service for the creator
updated: float
instance-attribute
¶
Timestamp when the creator was last updated, Unix time as integer
post
¶
__all__ = ['File', 'Attachment', 'Post']
module-attribute
¶
Attachment
¶
File
¶
Post
¶
Bases: BaseModel
Source code in ktoolbox/api/model/post.py
21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
added: Optional[datetime] = None
class-attribute
instance-attribute
¶
attachments: Optional[List[Attachment]] = None
class-attribute
instance-attribute
¶
content: Optional[str] = None
class-attribute
instance-attribute
¶
edited: Optional[datetime] = None
class-attribute
instance-attribute
¶
embed: Optional[Dict[str, Any]] = None
class-attribute
instance-attribute
¶
file: Optional[File] = None
class-attribute
instance-attribute
¶
id: Optional[str] = None
class-attribute
instance-attribute
¶
published: Optional[datetime] = None
class-attribute
instance-attribute
¶
service: Optional[str] = None
class-attribute
instance-attribute
¶
shared_file: Optional[bool] = None
class-attribute
instance-attribute
¶
title: Optional[str] = None
class-attribute
instance-attribute
¶
user: Optional[str] = None
class-attribute
instance-attribute
¶
posts
¶
utils
¶
SEARCH_STEP = 50
module-attribute
¶
Searching APIs result steps
__all__ = ['SEARCH_STEP', 'get_creator_icon', 'get_creator_banner']
module-attribute
¶
get_creator_banner(creator_id, service)
¶
Get the creator banner for a given creator ID and service.
Returns:
Type | Description |
---|---|
str
|
The banner URL. |
Source code in ktoolbox/api/utils.py
21 22 23 24 25 26 27 28 |
|
get_creator_icon(creator_id, service)
¶
Get the creator icon for a given creator ID and service.
Returns:
Type | Description |
---|---|
str
|
The icon URL. |
Source code in ktoolbox/api/utils.py
11 12 13 14 15 16 17 18 |
|
cli
¶
__all__ = ['KToolBoxCli']
module-attribute
¶
KToolBoxCli
¶
Source code in ktoolbox/cli.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
|
download_post(url=None, service=None, creator_id=None, post_id=None, path=Path('.'), *, dump_post_data=True)
async
staticmethod
¶
Download a specific post
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
The post URL |
None
|
service |
str
|
The service name |
None
|
creator_id |
str
|
The creator's ID |
None
|
post_id |
str
|
The post ID |
None
|
path |
Union[Path, str]
|
Download path, default is current directory |
Path('.')
|
dump_post_data |
Whether to dump post data (post.json) in post directory |
True
|
Source code in ktoolbox/cli.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
get_post(service, creator_id, post_id, *, dump=None)
async
staticmethod
¶
Get a specific post
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service |
str
|
The service name |
required |
creator_id |
str
|
The creator's ID |
required |
post_id |
str
|
The post ID |
required |
dump |
Path
|
Dump the result to a JSON file |
None
|
Source code in ktoolbox/cli.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
search_creator(name=None, id=None, service=None, *, dump=None)
async
staticmethod
¶
Search creator, you can use multiple parameters as keywords.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id |
str
|
The ID of the creator |
None
|
name |
str
|
The name of the creator |
None
|
service |
str
|
The service for the creator |
None
|
dump |
Path
|
Dump the result to a JSON file |
None
|
Source code in ktoolbox/cli.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
search_creator_post(id=None, name=None, service=None, q=None, o=None, *, dump=None)
async
staticmethod
¶
Search posts from creator, you can use multiple parameters as keywords.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id |
str
|
The ID of the creator |
None
|
name |
str
|
The name of the creator |
None
|
service |
str
|
The service for the creator |
None
|
q |
str
|
Search query |
None
|
o |
int
|
Result offset, stepping of 50 is enforced |
None
|
dump |
Path
|
Dump the result to a JSON file |
None
|
Source code in ktoolbox/cli.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
site_version()
async
staticmethod
¶
Show current Kemono site app commit hash
Source code in ktoolbox/cli.py
28 29 30 31 32 33 |
|
sync_creator(url=None, service=None, creator_id=None, path=Path('.'), *, save_creator_indices=False, mix_posts=None, start_time=None, end_time=None, offset=0, length=None)
async
staticmethod
¶
Sync posts from a creator
You can update the directory anytime after download finished, such as to update after creator published new posts.
start_time
&end_time
example:2023-12-7
,2023-12-07
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
The post URL |
None
|
service |
str
|
The service where the post is located |
None
|
creator_id |
str
|
The ID of the creator |
None
|
path |
Union[Path, str]
|
Download path, default is current directory |
Path('.')
|
save_creator_indices |
bool
|
Record |
False
|
mix_posts |
bool
|
Save all_pages files from different posts at same path, |
None
|
start_time |
str
|
Start time of the published time range for posts downloading. Set to |
None
|
end_time |
str
|
End time of the published time range for posts downloading. Set to latest time (infinity) if |
None
|
offset |
int
|
Result offset (or start offset) |
0
|
length |
int
|
The number of posts to fetch, defaults to fetching all posts after |
None
|
Source code in ktoolbox/cli.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
|
version()
async
staticmethod
¶
Show KToolBox version
Source code in ktoolbox/cli.py
23 24 25 26 |
|
configuration
¶
__all__ = ['config', 'APIConfiguration', 'DownloaderConfiguration', 'PostStructureConfiguration', 'JobConfiguration', 'LoggerConfiguration', 'Configuration']
module-attribute
¶
config = Configuration(_env_file='prod.env')
module-attribute
¶
APIConfiguration
¶
Bases: BaseModel
Kemono API Configuration
Attributes:
Name | Type | Description |
---|---|---|
scheme |
Literal['http', 'https']
|
Kemono API URL scheme |
netloc |
str
|
Kemono API URL netloc |
statics_netloc |
str
|
URL netloc of Kemono server for static files (e.g. images) |
files_netloc |
str
|
URL netloc of Kemono server for post files |
path |
str
|
Kemono API URL root path |
timeout |
float
|
API request timeout |
retry_times |
int
|
API request retry times (when request failed) |
retry_interval |
float
|
Seconds of API request retry interval |
Source code in ktoolbox/configuration.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
files_netloc: str = 'kemono.su'
class-attribute
instance-attribute
¶
netloc: str = 'kemono.su'
class-attribute
instance-attribute
¶
path: str = '/api/v1'
class-attribute
instance-attribute
¶
retry_interval: float = 2.0
class-attribute
instance-attribute
¶
retry_times: int = 3
class-attribute
instance-attribute
¶
scheme: Literal['http', 'https'] = 'https'
class-attribute
instance-attribute
¶
statics_netloc: str = 'img.kemono.su'
class-attribute
instance-attribute
¶
timeout: float = 5.0
class-attribute
instance-attribute
¶
Configuration
¶
Bases: BaseSettings
KToolBox Configuration
Attributes:
Name | Type | Description |
---|---|---|
api |
APIConfiguration
|
Kemono API Configuration |
downloader |
DownloaderConfiguration
|
File Downloader Configuration |
job |
JobConfiguration
|
Download jobs Configuration |
logger |
LoggerConfiguration
|
Logger configuration |
ssl_verify |
bool
|
Enable SSL certificate verification for Kemono API server and download server |
json_dump_indent |
int
|
Indent of JSON file dump |
use_uvloop |
bool
|
Use uvloop for asyncio (Disabled on Windows by default) uvloop will improve concurrent performance, but it is not compatible with Windows. Install uvloop by |
Source code in ktoolbox/configuration.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
|
api: APIConfiguration = APIConfiguration()
class-attribute
instance-attribute
¶
downloader: DownloaderConfiguration = DownloaderConfiguration()
class-attribute
instance-attribute
¶
job: JobConfiguration = JobConfiguration()
class-attribute
instance-attribute
¶
json_dump_indent: int = 4
class-attribute
instance-attribute
¶
logger: LoggerConfiguration = LoggerConfiguration()
class-attribute
instance-attribute
¶
model_config = SettingsConfigDict(env_prefix='ktoolbox_', env_nested_delimiter='__', env_file='.env', env_file_encoding='utf-8', extra='ignore')
class-attribute
instance-attribute
¶
ssl_verify: bool = True
class-attribute
instance-attribute
¶
use_uvloop: bool = True
class-attribute
instance-attribute
¶
DownloaderConfiguration
¶
Bases: BaseModel
File Downloader Configuration
Attributes:
Name | Type | Description |
---|---|---|
scheme |
Literal['http', 'https']
|
Downloader URL scheme |
timeout |
float
|
Downloader request timeout |
encoding |
str
|
Charset for filename parsing and post content text saving |
buffer_size |
int
|
Number of bytes of file I/O buffer for each downloading file |
chunk_size |
int
|
Number of bytes of chunk of downloader stream |
temp_suffix |
str
|
Temp filename suffix of downloading files |
retry_times |
int
|
Downloader retry times (when download failed) |
retry_stop_never |
bool
|
Never stop downloader from retrying (when download failed) ( |
retry_interval |
float
|
Seconds of downloader retry interval |
use_bucket |
bool
|
Enable local storage bucket mode |
bucket_path |
Path
|
Path of local storage bucket |
Source code in ktoolbox/configuration.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
bucket_path: Path = Path('./.ktoolbox/bucket_storage')
class-attribute
instance-attribute
¶
buffer_size: int = 20480
class-attribute
instance-attribute
¶
chunk_size: int = 1024
class-attribute
instance-attribute
¶
encoding: str = 'utf-8'
class-attribute
instance-attribute
¶
retry_interval: float = 3.0
class-attribute
instance-attribute
¶
retry_stop_never: bool = False
class-attribute
instance-attribute
¶
retry_times: int = 10
class-attribute
instance-attribute
¶
scheme: Literal['http', 'https'] = 'https'
class-attribute
instance-attribute
¶
temp_suffix: str = 'tmp'
class-attribute
instance-attribute
¶
timeout: float = 30.0
class-attribute
instance-attribute
¶
use_bucket: bool = False
class-attribute
instance-attribute
¶
check_bucket_path()
¶
Source code in ktoolbox/configuration.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
JobConfiguration
¶
Bases: BaseModel
Download jobs Configuration
-
Available properties for
post_dirname_format
Property Type id
String user
String service
String title
String added
Date published
Date edited
Date
Attributes:
Name | Type | Description |
---|---|---|
count |
int
|
Number of coroutines for concurrent download |
post_id_as_path |
bool
|
(Deprecated) Use post ID as post directory name |
post_dirname_format |
str
|
Customize the post directory name format, you can use some of the properties in |
post_structure |
PostStructureConfiguration
|
Post path structure |
mix_posts |
bool
|
Save all files from different posts at same path in creator directory. It would not create any post directory, and |
sequential_filename |
bool
|
Rename attachments in numerical order, e.g. |
allow_list |
Set[str]
|
Download files which match these patterns (Unix shell-style), e.g. |
block_list |
Set[str]
|
Not to download files which match these patterns (Unix shell-style), e.g. |
Source code in ktoolbox/configuration.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
|
allow_list: Set[str] = set()
class-attribute
instance-attribute
¶
block_list: Set[str] = set()
class-attribute
instance-attribute
¶
count: int = 4
class-attribute
instance-attribute
¶
mix_posts: bool = False
class-attribute
instance-attribute
¶
post_dirname_format: str = '{title}'
class-attribute
instance-attribute
¶
post_id_as_path: bool = False
class-attribute
instance-attribute
¶
post_structure: PostStructureConfiguration = PostStructureConfiguration()
class-attribute
instance-attribute
¶
sequential_filename: bool = False
class-attribute
instance-attribute
¶
post_id_as_path_validator(v)
¶
Source code in ktoolbox/configuration.py
158 159 160 161 162 163 164 165 |
|
LoggerConfiguration
¶
Bases: BaseModel
Logger configuration
Attributes:
Name | Type | Description |
---|---|---|
path |
Optional[Path]
|
Path to save logs, |
level |
Union[str, int]
|
Log filter level |
rotation |
Union[str, int, time, timedelta]
|
Log rotation |
Source code in ktoolbox/configuration.py
168 169 170 171 172 173 174 175 176 177 178 |
|
PostStructureConfiguration
¶
Bases: BaseModel
Post path structure model
- Default:
.. ├─ content.txt ├─ <Post file> ├─ <Post data (post.json)> └─ attachments ├─ 1.png └─ 2.png
Attributes:
Name | Type | Description |
---|---|---|
attachments |
Path
|
Sub path of attachment directory |
content_filepath |
Path
|
Sub path of post content HTML file |
Source code in ktoolbox/configuration.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
downloader
¶
base
¶
downloader
¶
__all__ = ['Downloader']
module-attribute
¶
Downloader
¶
Attributes:
Name | Type | Description |
---|---|---|
_save_filename |
The actual filename for saving. |
Source code in ktoolbox/downloader/downloader.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
__call__ = run
class-attribute
instance-attribute
¶
buffer_size: int
cached
property
¶
Number of bytes for file I/O buffer
chunk_size: int
cached
property
¶
Number of bytes for chunk of download stream
filename: Optional[str]
property
¶
Actual filename of the download file
finished: bool
property
¶
Check if the download finished
Returns:
Type | Description |
---|---|
bool
|
|
path: Path
cached
property
¶
Directory path to save the file
url: str
cached
property
¶
Download URL
__init__(url, path, *, buffer_size=None, chunk_size=None, designated_filename=None, server_path=None)
¶
Initialize a file downloader
- About filename:
- If
designated_filename
parameter is set, use it. - Else if
Content-Disposition
is set in headers, use filename from it. - Else use filename from 'file' part of
server_path
.
- If
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
Download URL |
required |
path |
Path
|
Directory path to save the file, which needs to be sanitized |
required |
buffer_size |
int
|
Number of bytes for file I/O buffer |
None
|
chunk_size |
int
|
Number of bytes for chunk of download stream |
None
|
designated_filename |
str
|
Manually specify the filename for saving, which needs to be sanitized |
None
|
server_path |
str
|
Server path of the file. if |
None
|
Source code in ktoolbox/downloader/downloader.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
cancel()
¶
Cancel the download
It will raise asyncio.CancelledError
in chunk_iterator
(writing chunk to file) iteration.
Source code in ktoolbox/downloader/downloader.py
106 107 108 109 110 111 112 |
|
run(*, sync_callable=None, async_callable=None, tqdm_class=None, progress=False)
async
¶
Start to download
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sync_callable |
Callable[[Downloader], Any]
|
Sync callable for download finished |
None
|
async_callable |
Callable[[Downloader], Coroutine]
|
Async callable for download finished |
None
|
tqdm_class |
Type[tqdm]
|
|
None
|
progress |
bool
|
Show progress bar |
False
|
Returns:
Type | Description |
---|---|
DownloaderRet[str]
|
|
Source code in ktoolbox/downloader/downloader.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
utils
¶
__all__ = ['filename_from_headers', 'duplicate_file_check']
module-attribute
¶
duplicate_file_check(local_file_path, bucket_file_path=None)
¶
Check if the file existed, and link the bucket filepath to local filepath if DownloaderConfiguration.use_bucket
enabled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
local_file_path |
Path
|
Download target path |
required |
bucket_file_path |
Path
|
The bucket filepath of the local download path |
None
|
Returns:
Type | Description |
---|---|
Tuple[bool, Optional[str]]
|
|
Source code in ktoolbox/downloader/utils.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
|
filename_from_headers(headers)
¶
Get file name from headers.
Parse from Content-Disposition
.
-
Example:
filename_from_headers('attachment;filename*=utf-8\'\'README%2Emd;filename="README.md"')
-
Return:
README.md
Parameters:
Name | Type | Description | Default |
---|---|---|---|
headers |
Dict[str, str]
|
HTTP headers |
required |
Returns:
Type | Description |
---|---|
Optional[str]
|
File name |
Source code in ktoolbox/downloader/utils.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
parse_header(line)
¶
Alternative resolution for parsing header line.
Apply when cgi.parse_header
is unable to use due to the deprecation of cgi
module.
https://peps.python.org/pep-0594/#cgi
-
Example:
parse_header("text/html; charset=utf-8")
-
Return:
{'text/html': None, 'charset': 'utf-8'}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
line |
str
|
Header line |
required |
Returns:
Type | Description |
---|---|
Dict[str, Optional[str]]
|
Dict of header line |
Source code in ktoolbox/downloader/utils.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
job
¶
model
¶
__all__ = ['Job', 'JobListData', 'CreatorIndices']
module-attribute
¶
CreatorIndices
¶
Bases: BaseKToolBoxData
Creator directory indices model
Record the path of each downloaded post.
Source code in ktoolbox/job/model.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
Job
¶
Bases: BaseModel
Download job model
Source code in ktoolbox/job/model.py
13 14 15 16 17 18 19 20 21 22 23 24 |
|
alt_filename: Optional[str] = None
class-attribute
instance-attribute
¶
Use this name if no filename given by the server
path: Path
instance-attribute
¶
Directory path to save the file
server_path: str
instance-attribute
¶
The path
part of download URL
type: Optional[Literal[PostFileTypeEnum.Attachment, PostFileTypeEnum.File]] = None
class-attribute
instance-attribute
¶
Target file type
JobListData
¶
Bases: BaseKToolBoxData
Download job list data model
For saving the list of jobs to disk.
Source code in ktoolbox/job/model.py
41 42 43 44 45 46 47 48 |
|
jobs: List[Job] = []
class-attribute
instance-attribute
¶
All jobs
runner
¶
__all__ = ['JobRunner']
module-attribute
¶
JobRunner
¶
Source code in ktoolbox/job/runner.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
done_size: int
property
¶
Get the number of jobs that done
downloaders
cached
property
¶
Get downloaders with task
finished
property
¶
Check if all jobs finished
Returns:
Type | Description |
---|---|
|
processing_size: int
property
¶
Get the number of jobs that in process
waiting_size: int
property
¶
Get the number of jobs waiting to be processed
__init__(*, job_list=None, tqdm_class=None, progress=True)
¶
Create a job runner
Parameters:
Name | Type | Description | Default |
---|---|---|---|
job_list |
List[Job]
|
Jobs to initial |
None
|
tqdm_class |
tqdm
|
|
None
|
progress |
bool
|
Show progress bar |
True
|
Source code in ktoolbox/job/runner.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
add_jobs(*jobs)
async
¶
Add jobs to self._job_queue
Source code in ktoolbox/job/runner.py
169 170 171 172 |
|
cancel_downloader(target)
async
¶
Cancel downloader
Returns:
Type | Description |
---|---|
bool
|
Whether cancelled successfully |
Source code in ktoolbox/job/runner.py
187 188 189 190 191 192 193 194 195 196 197 |
|
processor()
async
¶
Process each job in self._job_queue
Returns:
Type | Description |
---|---|
int
|
Number of jobs that failed |
Source code in ktoolbox/job/runner.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
start()
async
¶
Start processing jobs concurrently
It will Block until other call of self.start()
method finished
Source code in ktoolbox/job/runner.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
model
¶
__all__ = ['BaseKToolBoxData', 'SearchResult']
module-attribute
¶
BaseKToolBoxData
¶
Bases: BaseModel
Base class for all KToolBox data models.
Source code in ktoolbox/model.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
type: Union[Type[BaseKToolBoxData], str] = None
class-attribute
instance-attribute
¶
version: str = __version__
class-attribute
instance-attribute
¶
_(value, _info)
¶
Source code in ktoolbox/model.py
24 25 26 |
|
__init__(**data)
¶
Source code in ktoolbox/model.py
17 18 19 |
|
SearchResult
¶
Bases: BaseKToolBoxData
, Generic[_T]
Cli search result
Source code in ktoolbox/model.py
29 30 31 |
|
result: List[_T] = []
class-attribute
instance-attribute
¶
utils
¶
__all__ = ['BaseRet', 'generate_msg', 'logger_init', 'dump_search', 'parse_webpage_url', 'uvloop_init']
module-attribute
¶
BaseRet
¶
Bases: BaseModel
, Generic[_T]
Base data model of function return value
Source code in ktoolbox/utils.py
27 28 29 30 31 32 33 34 35 36 37 |
|
code: int = RetCodeEnum.Success.value
class-attribute
instance-attribute
¶
data: Optional[_T] = None
class-attribute
instance-attribute
¶
exception: Optional[Exception] = None
class-attribute
instance-attribute
¶
message: str = ''
class-attribute
instance-attribute
¶
model_config = ConfigDict(arbitrary_types_allowed=True)
class-attribute
instance-attribute
¶
__bool__()
¶
Source code in ktoolbox/utils.py
36 37 |
|
dump_search(result, path)
async
¶
Source code in ktoolbox/utils.py
78 79 80 81 82 83 |
|
generate_msg(title=None, **kwargs)
¶
Generate message for BaseRet
and logger
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
Message title |
None
|
kwargs |
Extra data |
{}
|
Source code in ktoolbox/utils.py
40 41 42 43 44 45 46 47 48 |
|
logger_init(cli_use=False, disable_stdout=False)
¶
Initialize loguru
logger
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cli_use |
bool
|
Set logger level |
False
|
disable_stdout |
bool
|
Disable default output stream |
False
|
Source code in ktoolbox/utils.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
parse_webpage_url(url)
¶
Fetch service, user_id, post_id from webpage url
Each part can be None
if not found in url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
Kemono Webpage url |
required |
Returns:
Type | Description |
---|---|
Tuple[Optional[str], Optional[str], Optional[str]]
|
Tuple of service, user_id, post_id |
Source code in ktoolbox/utils.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
uvloop_init()
¶
Set event loop policy to uvloop if available.
Returns:
Type | Description |
---|---|
bool
|
If uvloop enabled successfully |
Source code in ktoolbox/utils.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|