API 文档¶
__description__ = 'A useful CLI tool for downloading posts in Kemono.cr / .su / .party'
module-attribute
¶
__title__ = 'KToolBox'
module-attribute
¶
__version__ = 'v0.24.0'
module-attribute
¶
__main__
¶
main()
¶
Source code in ktoolbox/__main__.py
10 11 12 13 14 15 16 17 18 19 20 21 | |
action
¶
ActionRet
¶
Bases: BaseRet[_T]
Return data model of action call
Source code in ktoolbox/action/base.py
10 11 12 | |
FetchInterruptError
¶
Bases: Exception
Exception for interrupt of data fetching
Source code in ktoolbox/action/fetch.py
11 12 13 14 15 16 | |
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, keywords=None, keywords_exclude=None)
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 |
keywords |
Optional[Set[str]]
|
Set of keywords to filter posts by title (case-insensitive) |
None
|
keywords_exclude |
Optional[Set[str]]
|
Set of keywords to exclude posts by title (case-insensitive) |
None
|
Source code in ktoolbox/action/job.py
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
create_job_from_post(post, post_path, *, post_dir=True, dump_post_data=True)
async
¶
Create a list of download job from a post data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
post |
Union[Post, Revision]
|
post data |
required |
post_path |
Path
|
Path of the post directory, which needs to be sanitized |
required |
post_dir |
bool
|
Whether to create post directory |
True
|
dump_post_data |
bool
|
Whether to dump post data (post.json) in post directory |
True
|
Raises:
| Type | Description |
|---|---|
FetchInterruptError
|
If fetching post content fails |
Source code in ktoolbox/action/job.py
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 | |
extract_content_images(content)
¶
Extract image sources from HTML content
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content |
str
|
HTML content string |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List of image source URLs/paths |
Source code in ktoolbox/action/utils.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
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 |
Raises:
| Type | Description |
|---|---|
FetchInterruptError
|
Exception for interrupt of data fetching |
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 | |
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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
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
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
filter_posts_by_keywords(post_list, keywords)
¶
Filter posts by keywords in title
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
post_list |
List[Post]
|
List of posts |
required |
keywords |
Optional[Set[str]]
|
Set of keywords to search for (case-insensitive), None means no filtering |
required |
Source code in ktoolbox/action/utils.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
filter_posts_by_keywords_exclude(post_list, keywords_exclude)
¶
Filter out posts that contain any of the specified keywords in title
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
post_list |
List[Post]
|
List of posts |
required |
keywords_exclude |
Optional[Set[str]]
|
Set of keywords to exclude (case-insensitive), None means no filtering |
required |
Source code in ktoolbox/action/utils.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
generate_filename(post, basic_name, filename_format)
¶
Generate download filename
Source code in ktoolbox/action/utils.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
generate_grouped_post_path(post, base_path)
¶
Generate the full path for a post considering year/month grouping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
post |
Post
|
Post object |
required |
base_path |
Path
|
Base path (usually creator directory) |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Full path where the post should be saved |
Source code in ktoolbox/action/utils.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
generate_month_dirname(post)
¶
Generate month directory name for post grouping.
Source code in ktoolbox/action/utils.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
generate_post_path_name(post)
¶
Generate directory name for post to save.
Source code in ktoolbox/action/utils.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
generate_year_dirname(post)
¶
Generate year directory name for post grouping.
Source code in ktoolbox/action/utils.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
match_post_keywords(post, keywords)
¶
Check if the post contains any of the specified keywords.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
post |
Post
|
Target post object |
required |
keywords |
Set[str]
|
Set of keywords to search for (case-insensitive) |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the post contains any of the keywords in title |
Source code in ktoolbox/action/utils.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
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 | |
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 |
Raises:
| Type | Description |
|---|---|
FetchInterruptError
|
Exception for interrupt of data fetching |
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, keywords=None, keywords_exclude=None)
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 |
keywords |
Optional[Set[str]]
|
Set of keywords to filter posts by title (case-insensitive) |
None
|
keywords_exclude |
Optional[Set[str]]
|
Set of keywords to exclude posts by title (case-insensitive) |
None
|
Source code in ktoolbox/action/job.py
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
create_job_from_post(post, post_path, *, post_dir=True, dump_post_data=True)
async
¶
Create a list of download job from a post data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
post |
Union[Post, Revision]
|
post data |
required |
post_path |
Path
|
Path of the post directory, which needs to be sanitized |
required |
post_dir |
bool
|
Whether to create post directory |
True
|
dump_post_data |
bool
|
Whether to dump post data (post.json) in post directory |
True
|
Raises:
| Type | Description |
|---|---|
FetchInterruptError
|
If fetching post content fails |
Source code in ktoolbox/action/job.py
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 | |
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
¶
TIME_FORMAT = '%Y-%m-%d'
module-attribute
¶
__all__ = ['generate_post_path_name', 'generate_filename', 'generate_year_dirname', 'generate_month_dirname', 'generate_grouped_post_path', 'filter_posts_by_date', 'filter_posts_by_indices', 'match_post_keywords', 'filter_posts_by_keywords', 'filter_posts_by_keywords_exclude', 'extract_content_images']
module-attribute
¶
extract_content_images(content)
¶
Extract image sources from HTML content
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content |
str
|
HTML content string |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List of image source URLs/paths |
Source code in ktoolbox/action/utils.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
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
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
filter_posts_by_keywords(post_list, keywords)
¶
Filter posts by keywords in title
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
post_list |
List[Post]
|
List of posts |
required |
keywords |
Optional[Set[str]]
|
Set of keywords to search for (case-insensitive), None means no filtering |
required |
Source code in ktoolbox/action/utils.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
filter_posts_by_keywords_exclude(post_list, keywords_exclude)
¶
Filter out posts that contain any of the specified keywords in title
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
post_list |
List[Post]
|
List of posts |
required |
keywords_exclude |
Optional[Set[str]]
|
Set of keywords to exclude (case-insensitive), None means no filtering |
required |
Source code in ktoolbox/action/utils.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
generate_filename(post, basic_name, filename_format)
¶
Generate download filename
Source code in ktoolbox/action/utils.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | |
generate_grouped_post_path(post, base_path)
¶
Generate the full path for a post considering year/month grouping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
post |
Post
|
Post object |
required |
base_path |
Path
|
Base path (usually creator directory) |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Full path where the post should be saved |
Source code in ktoolbox/action/utils.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
generate_month_dirname(post)
¶
Generate month directory name for post grouping.
Source code in ktoolbox/action/utils.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
generate_post_path_name(post)
¶
Generate directory name for post to save.
Source code in ktoolbox/action/utils.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
generate_year_dirname(post)
¶
Generate year directory name for post grouping.
Source code in ktoolbox/action/utils.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
match_post_keywords(post, keywords)
¶
Check if the post contains any of the specified keywords.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
post |
Post
|
Target post object |
required |
keywords |
Set[str]
|
Set of keywords to search for (case-insensitive) |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the post contains any of the keywords in title |
Source code in ktoolbox/action/utils.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
api
¶
-
Kemono API version:
1.0.0 -
current App commit hash:
7ee4a7b18ee92a442c13950c05dc8236cfb14a60
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 | |
Response = BaseModel
class-attribute
instance-attribute
¶
API response model
client = httpx.AsyncClient(verify=config.ssl_verify, headers={'Accept': 'text/css'}, cookies={'session': config.api.session_key} if config.api.session_key else None)
class-attribute
instance-attribute
¶
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
127 128 129 130 131 | |
handle_res(res)
classmethod
¶
Handle API response
Source code in ktoolbox/api/base.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
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
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 | |
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 | |
Response = BaseModel
class-attribute
instance-attribute
¶
API response model
client = httpx.AsyncClient(verify=config.ssl_verify, headers={'Accept': 'text/css'}, cookies={'session': config.api.session_key} if config.api.session_key else None)
class-attribute
instance-attribute
¶
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
127 128 129 130 131 | |
handle_res(res)
classmethod
¶
Handle API response
Source code in ktoolbox/api/base.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
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
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 | |
misc
¶
get_app_version = GetAppVersion.__call__
module-attribute
¶
Show current App commit hash
GetAppVersion
¶
Bases: BaseAPI
Source code in ktoolbox/api/misc/get_app_version.py
8 9 10 11 12 13 14 15 16 17 18 19 | |
extra_validator = Response.model_validate_strings
class-attribute
instance-attribute
¶
method = 'get'
class-attribute
instance-attribute
¶
path = '/app_version'
class-attribute
instance-attribute
¶
Response
¶
__call__()
async
classmethod
¶
Source code in ktoolbox/api/misc/get_app_version.py
17 18 19 | |
model
¶
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
¶
Attachment
¶
Creator
¶
Bases: BaseModel
Source code in ktoolbox/api/model/creator.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
favorited: int
instance-attribute
¶
The number of times this creator has been favorited
id: str
instance-attribute
¶
The ID of the creator
indexed: datetime
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: datetime
instance-attribute
¶
Timestamp when the creator was last updated, Unix time as integer
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 34 | |
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
¶
substring: Optional[str] = None
class-attribute
instance-attribute
¶
title: Optional[str] = None
class-attribute
instance-attribute
¶
user: Optional[str] = None
class-attribute
instance-attribute
¶
Revision
¶
Bases: Post
Revision model that extends Post with revision_id field
Source code in ktoolbox/api/model/post.py
37 38 39 | |
revision_id: Optional[int] = None
class-attribute
instance-attribute
¶
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
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
favorited: int
instance-attribute
¶
The number of times this creator has been favorited
id: str
instance-attribute
¶
The ID of the creator
indexed: datetime
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: datetime
instance-attribute
¶
Timestamp when the creator was last updated, Unix time as integer
post
¶
__all__ = ['File', 'Attachment', 'Post', 'Revision']
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 34 | |
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
¶
substring: Optional[str] = None
class-attribute
instance-attribute
¶
title: Optional[str] = None
class-attribute
instance-attribute
¶
user: Optional[str] = None
class-attribute
instance-attribute
¶
Revision
¶
Bases: Post
Revision model that extends Post with revision_id field
Source code in ktoolbox/api/model/post.py
37 38 39 | |
revision_id: Optional[int] = None
class-attribute
instance-attribute
¶
posts
¶
get_announcement = GetAnnouncement.__call__
module-attribute
¶
get_creator_post = GetCreatorPost.__call__
module-attribute
¶
get_creators = GetCreators.__call__
module-attribute
¶
get_post = GetPost.__call__
module-attribute
¶
get_post_revisions = GetPostRevisions.__call__
module-attribute
¶
GetAnnouncement
¶
Bases: BaseAPI
Source code in ktoolbox/api/posts/get_announcement.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
method = 'get'
class-attribute
instance-attribute
¶
path = '/{service}/user/{creator_id}/announcements'
class-attribute
instance-attribute
¶
Response
¶
Bases: RootModel[List[Announcement]]
Source code in ktoolbox/api/posts/get_announcement.py
15 16 | |
root: List[Announcement]
instance-attribute
¶
__call__(service, creator_id)
async
classmethod
¶
Get creator announcements
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service |
str
|
The service name |
required |
creator_id |
str
|
The creator's ID |
required |
Source code in ktoolbox/api/posts/get_announcement.py
18 19 20 21 22 23 24 25 26 | |
GetCreatorPost
¶
Bases: BaseAPI
Source code in ktoolbox/api/posts/get_creator_post.py
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 40 41 | |
method = 'get'
class-attribute
instance-attribute
¶
path = '/{service}/user/{creator_id}/posts'
class-attribute
instance-attribute
¶
Response
¶
__call__(service, creator_id, *, q=None, o=None)
async
classmethod
¶
Get a list of creator posts
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service |
str
|
The service where the post is located |
required |
creator_id |
str
|
The ID of the creator |
required |
q |
str
|
Search query |
None
|
o |
int
|
Result offset, stepping of 50 is enforced |
None
|
Source code in ktoolbox/api/posts/get_creator_post.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
handle_res(res)
classmethod
¶
Source code in ktoolbox/api/posts/get_creator_post.py
39 40 41 | |
GetCreators
¶
Bases: BaseAPI
List All Creators
Source code in ktoolbox/api/posts/get_creators.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
method = 'get'
class-attribute
instance-attribute
¶
path = '/creators'
class-attribute
instance-attribute
¶
Response
¶
__call__()
async
classmethod
¶
List of all creators
List all creators with details. I blame DDG for .txt.
Source code in ktoolbox/api/posts/get_creators.py
19 20 21 22 23 24 25 26 | |
GetPost
¶
Bases: BaseAPI
Source code in ktoolbox/api/posts/get_post.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
method = 'get'
class-attribute
instance-attribute
¶
path = '/{service}/user/{creator_id}/post/{post_id}'
class-attribute
instance-attribute
¶
Response
¶
__call__(service, creator_id, post_id, revision_id=None)
async
classmethod
¶
Get a specific post or revision
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 |
revision_id |
Optional[str]
|
The revision ID (optional, for revision posts) |
None
|
Source code in ktoolbox/api/posts/get_post.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
GetPostRevisions
¶
Bases: BaseAPI
Source code in ktoolbox/api/posts/get_post_revisions.py
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 40 | |
method = 'get'
class-attribute
instance-attribute
¶
path = '/{service}/user/{creator_id}/post/{post_id}/revisions'
class-attribute
instance-attribute
¶
Response
¶
Bases: RootModel
Source code in ktoolbox/api/posts/get_post_revisions.py
18 19 | |
root: List[Revision]
instance-attribute
¶
__call__(service, creator_id, post_id)
async
classmethod
¶
Get all revisions of 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 |
Source code in ktoolbox/api/posts/get_post_revisions.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
handle_res(res)
classmethod
¶
Source code in ktoolbox/api/posts/get_post_revisions.py
38 39 40 | |
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
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
config_editor()
async
staticmethod
¶
Launch graphical KToolBox configuration editor
Source code in ktoolbox/cli.py
53 54 55 56 57 58 59 60 61 62 63 64 | |
download_post(url=None, service=None, creator_id=None, post_id=None, revision_id=None, path=Path('.'), *, dump_post_data=True)
async
staticmethod
¶
Download a specific post or revision
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
|
revision_id |
str
|
The revision ID (optional, for revision posts) |
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
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 | |
example_env()
async
staticmethod
¶
Generate an example configuration .env file.
Source code in ktoolbox/cli.py
66 67 68 69 70 71 72 73 74 | |
get_post(service, creator_id, post_id, revision_id=None, *, dump=None)
async
staticmethod
¶
Get a specific post or revision
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 |
revision_id |
str
|
The revision ID (optional, for revision posts) |
None
|
dump |
Path
|
Dump the result to a JSON file |
None
|
Source code in ktoolbox/cli.py
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 | |
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
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 | |
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
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 | |
site_version()
async
staticmethod
¶
Show current Kemono site app commit hash
Source code in ktoolbox/cli.py
45 46 47 48 49 50 51 | |
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, keywords=None, keywords_exclude=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_timeexample: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
|
keywords |
Tuple[str]
|
Comma-separated keywords to filter posts by title (case-insensitive) |
None
|
keywords_exclude |
Tuple[str]
|
Comma-separated keywords to exclude posts by title (case-insensitive) |
None
|
Source code in ktoolbox/cli.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
version()
async
staticmethod
¶
Show KToolBox version
Source code in ktoolbox/cli.py
38 39 40 41 42 43 | |
configuration
¶
__all__ = ['config', 'APIConfiguration', 'DownloaderConfiguration', 'PostStructureConfiguration', 'JobConfiguration', 'LoggerConfiguration', 'Configuration']
module-attribute
¶
config = Configuration()
module-attribute
¶
APIConfiguration
¶
Bases: BaseModel
Kemono API Configuration
Attributes:
| Name | Type | Description | Default |
|---|---|---|---|
scheme |
Literal['http', 'https']
|
Kemono API URL scheme |
'https'
|
netloc |
str
|
Kemono API URL netloc |
'kemono.cr'
|
statics_netloc |
str
|
URL netloc of Kemono server for static files (e.g. images) |
'img.kemono.cr'
|
files_netloc |
str
|
URL netloc of Kemono server for post files |
'kemono.cr'
|
path |
str
|
Kemono API URL root path |
'/api/v1'
|
timeout |
float
|
API request timeout |
5.0
|
retry_times |
int
|
API request retry times (when request failed) |
3
|
retry_interval |
float
|
Seconds of API request retry interval |
2.0
|
session_key |
str
|
Session key that can be found in cookies after a successful login |
''
|
Source code in ktoolbox/configuration.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
files_netloc: str = 'kemono.cr'
class-attribute
instance-attribute
¶
netloc: str = 'kemono.cr'
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
¶
session_key: str = ''
class-attribute
instance-attribute
¶
statics_netloc: str = 'img.kemono.cr'
class-attribute
instance-attribute
¶
timeout: float = 5.0
class-attribute
instance-attribute
¶
Configuration
¶
Bases: BaseSettings
KToolBox Configuration
Attributes:
| Name | Type | Description | Default |
|---|---|---|---|
api |
APIConfiguration
|
Kemono API Configuration |
APIConfiguration()
|
downloader |
DownloaderConfiguration
|
File Downloader Configuration |
DownloaderConfiguration()
|
job |
JobConfiguration
|
Download jobs Configuration |
JobConfiguration()
|
logger |
LoggerConfiguration
|
Logger configuration |
LoggerConfiguration()
|
ssl_verify |
bool
|
Enable SSL certificate verification for Kemono API server and download server |
True
|
json_dump_indent |
int
|
Indent of JSON file dump |
4
|
use_uvloop |
bool
|
Use uvloop/winloop for asyncio performance optimization Uses winloop on Windows and uvloop on Unix-like systems for better concurrent performance. Install winloop on Windows with |
True
|
Source code in ktoolbox/configuration.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
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 = SettingsConfigDict(env_prefix='ktoolbox_', env_nested_delimiter='__', env_file=['.env', 'prod.env'], env_file_encoding='utf-8', extra='ignore')
class-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 | Default |
|---|---|---|---|
scheme |
Literal['http', 'https']
|
Downloader URL scheme |
'https'
|
timeout |
float
|
Downloader request timeout |
30.0
|
encoding |
str
|
Charset for filename parsing and post |
'utf-8'
|
buffer_size |
int
|
Number of bytes of file I/O buffer for each downloading file |
20480
|
chunk_size |
int
|
Number of bytes of chunk of downloader stream |
1024
|
temp_suffix |
str
|
Temp filename suffix of downloading files |
'tmp'
|
retry_times |
int
|
Downloader retry times (when download failed) |
10
|
retry_stop_never |
bool
|
Never stop downloader from retrying (when download failed) ( |
False
|
retry_interval |
float
|
Seconds of downloader retry interval |
3.0
|
tps_limit |
float
|
Maximum connections established per second |
5.0
|
use_bucket |
bool
|
Enable local storage bucket mode |
False
|
bucket_path |
Path
|
Path of local storage bucket |
Path('./.ktoolbox/bucket_storage')
|
reverse_proxy |
str
|
Reverse proxy format for download URL. Customize the filename format by inserting an empty |
'{}'
|
keep_metadata |
bool
|
Keep the file metadata when downloading files (e.g. last modified time, etc.) |
True
|
Source code in ktoolbox/configuration.py
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 | |
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
¶
keep_metadata: bool = True
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
¶
reverse_proxy: str = '{}'
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
¶
tps_limit: float = 5.0
class-attribute
instance-attribute
¶
use_bucket: bool = False
class-attribute
instance-attribute
¶
check_bucket_path()
¶
Source code in ktoolbox/configuration.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
JobConfiguration
¶
Bases: BaseModel
Download jobs Configuration
-
Available properties for
post_dirname_formatandfilename_formatProperty Type idString userString serviceString titleString addedDate publishedDate editedDate -
Available properties for
year_dirname_formatandmonth_dirname_formatProperty Type yearString monthString -
Python Format Specification Mini-Language reference:
https://docs.python.org/3.13/library/string.html#format-specification-mini-language
Attributes:
| Name | Type | Description | Default |
|---|---|---|---|
count |
int
|
Number of coroutines for concurrent download |
4
|
include_revisions |
bool
|
Include and download revision posts when available |
False
|
post_dirname_format |
str
|
Customize the post directory name format, you can use some of the properties in |
'{title}'
|
post_structure |
PostStructureConfiguration
|
Post path structure |
PostStructureConfiguration()
|
mix_posts |
bool
|
Save all files from different posts at same path in creator directory. It would not create any post directory, and |
False
|
sequential_filename |
bool
|
Rename attachments in numerical order, e.g. |
False
|
sequential_filename_excludes |
Set[str]
|
File extensions to exclude from sequential naming when |
Field(default_factory=set)
|
filename_format |
str
|
Customize the filename format by inserting an empty |
'{}'
|
allow_list |
Set[str]
|
Download files which match these patterns (Unix shell-style), e.g. |
Field(default_factory=set)
|
block_list |
Set[str]
|
Not to download files which match these patterns (Unix shell-style), e.g. |
Field(default_factory=set)
|
extract_content |
bool
|
Extract post content and save to separate file (filename was defined in |
False
|
extract_content_images |
bool
|
Extract images from post content and download them. |
False
|
extract_external_links |
bool
|
Extract external file sharing links from post content and save to separate file (filename was defined in |
False
|
external_link_patterns |
List[str]
|
Regex patterns for extracting external links. |
['https?://drive\\.google\\.com/[^\\s]+', 'https?://docs\\.google\\.com/[^\\s]+', 'https?://mega\\.nz/[^\\s]+', 'https?://mega\\.co\\.nz/[^\\s]+', 'https?://(?:www\\.)?dropbox\\.com/[^\\s]+', 'https?://db\\.tt/[^\\s]+', 'https?://onedrive\\.live\\.com/[^\\s]+', 'https?://1drv\\.ms/[^\\s]+', 'https?://(?:www\\.)?mediafire\\.com/[^\\s]+', 'https?://(?:www\\.)?wetransfer\\.com/[^\\s]+', 'https?://we\\.tl/[^\\s]+', 'https?://(?:www\\.)?sendspace\\.com/[^\\s]+', 'https?://(?:www\\.)?4shared\\.com/[^\\s]+', 'https?://(?:www\\.)?zippyshare\\.com/[^\\s]+', 'https?://(?:www\\.)?uploadfiles\\.io/[^\\s]+', 'https?://(?:www\\.)?box\\.com/[^\\s]+', 'https?://(?:www\\.)?pcloud\\.com/[^\\s]+', 'https?://disk\\.yandex\\.[a-z]+/[^\\s]+', 'https?://[^\\s]*(?:file|upload|share|download|drive|storage)[^\\s]*\\.[a-z]{2,4}/[^\\s]+']
|
group_by_year |
bool
|
Group posts by year in separate directories based on published date |
False
|
group_by_month |
bool
|
Group posts by month in separate directories based on published date (requires group_by_year) |
False
|
year_dirname_format |
str
|
Customize the year directory name format. Available properties: |
'{year}'
|
month_dirname_format |
str
|
Customize the month directory name format. Available properties: |
'{year}-{month:02d}'
|
keywords |
Set[str]
|
keywords to filter posts by title (case-insensitive) |
Field(default_factory=set)
|
keywords_exclude |
Set[str]
|
keywords to exclude posts by title (case-insensitive) |
Field(default_factory=set)
|
download_file |
bool
|
Download post file (usually cover image). Set to False to skip file downloads. |
True
|
download_attachments |
bool
|
Download post attachments. Set to False to skip attachment downloads. |
True
|
min_file_size |
Optional[int]
|
Minimum file size in bytes to download. Files smaller than this will be skipped. Set to None to disable minimum size filtering. |
None
|
max_file_size |
Optional[int]
|
Maximum file size in bytes to download. Files larger than this will be skipped. Set to None to disable maximum size filtering. |
None
|
Source code in ktoolbox/configuration.py
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 305 | |
allow_list: Set[str] = Field(default_factory=set)
class-attribute
instance-attribute
¶
block_list: Set[str] = Field(default_factory=set)
class-attribute
instance-attribute
¶
count: int = 4
class-attribute
instance-attribute
¶
download_attachments: bool = True
class-attribute
instance-attribute
¶
download_file: bool = True
class-attribute
instance-attribute
¶
external_link_patterns: List[str] = ['https?://drive\\.google\\.com/[^\\s]+', 'https?://docs\\.google\\.com/[^\\s]+', 'https?://mega\\.nz/[^\\s]+', 'https?://mega\\.co\\.nz/[^\\s]+', 'https?://(?:www\\.)?dropbox\\.com/[^\\s]+', 'https?://db\\.tt/[^\\s]+', 'https?://onedrive\\.live\\.com/[^\\s]+', 'https?://1drv\\.ms/[^\\s]+', 'https?://(?:www\\.)?mediafire\\.com/[^\\s]+', 'https?://(?:www\\.)?wetransfer\\.com/[^\\s]+', 'https?://we\\.tl/[^\\s]+', 'https?://(?:www\\.)?sendspace\\.com/[^\\s]+', 'https?://(?:www\\.)?4shared\\.com/[^\\s]+', 'https?://(?:www\\.)?zippyshare\\.com/[^\\s]+', 'https?://(?:www\\.)?uploadfiles\\.io/[^\\s]+', 'https?://(?:www\\.)?box\\.com/[^\\s]+', 'https?://(?:www\\.)?pcloud\\.com/[^\\s]+', 'https?://disk\\.yandex\\.[a-z]+/[^\\s]+', 'https?://[^\\s]*(?:file|upload|share|download|drive|storage)[^\\s]*\\.[a-z]{2,4}/[^\\s]+']
class-attribute
instance-attribute
¶
extract_content: bool = False
class-attribute
instance-attribute
¶
extract_content_images: bool = False
class-attribute
instance-attribute
¶
extract_external_links: bool = False
class-attribute
instance-attribute
¶
filename_format: str = '{}'
class-attribute
instance-attribute
¶
group_by_month: bool = False
class-attribute
instance-attribute
¶
group_by_year: bool = False
class-attribute
instance-attribute
¶
include_revisions: bool = False
class-attribute
instance-attribute
¶
keywords: Set[str] = Field(default_factory=set)
class-attribute
instance-attribute
¶
keywords_exclude: Set[str] = Field(default_factory=set)
class-attribute
instance-attribute
¶
max_file_size: Optional[int] = None
class-attribute
instance-attribute
¶
min_file_size: Optional[int] = None
class-attribute
instance-attribute
¶
mix_posts: bool = False
class-attribute
instance-attribute
¶
month_dirname_format: str = '{year}-{month:02d}'
class-attribute
instance-attribute
¶
post_dirname_format: str = '{title}'
class-attribute
instance-attribute
¶
post_structure: PostStructureConfiguration = PostStructureConfiguration()
class-attribute
instance-attribute
¶
sequential_filename: bool = False
class-attribute
instance-attribute
¶
sequential_filename_excludes: Set[str] = Field(default_factory=set)
class-attribute
instance-attribute
¶
year_dirname_format: str = '{year}'
class-attribute
instance-attribute
¶
LoggerConfiguration
¶
Bases: BaseModel
Logger configuration
Attributes:
| Name | Type | Description | Default |
|---|---|---|---|
path |
Optional[Path]
|
Path to save logs, |
None
|
level |
Union[str, int]
|
Log filter level |
logging.getLevelName(logging.DEBUG)
|
rotation |
Union[str, int, time, timedelta]
|
Log rotation |
'1 week'
|
Source code in ktoolbox/configuration.py
308 309 310 311 312 313 314 315 316 317 318 | |
PostStructureConfiguration
¶
Bases: BaseModel
Post path structure model
-
Default:
.. ├─ content.txt ├─ external_links.txt ├─ {id}_{}.png (file) ├─ post.json (metadata) ├─ attachments │ ├─ 1.png │ └─ 2.png └─ revisions ├─ <PostStructure> │ ├─ ... │ └─ ... └─ <PostStructure> ├─ ... └─ ... -
Available properties for
fileProperty Type idString userString serviceString titleString addedDate publishedDate editedDate
Attributes:
| Name | Type | Description | Default |
|---|---|---|---|
attachments |
Path
|
Sub path of attachment directory |
Path('attachments')
|
content |
Path
|
Sub path of post content file |
Path('content.txt')
|
external_links |
Path
|
Sub path of external links file (for cloud storage links found in content) |
Path('external_links.txt')
|
file |
str
|
The format of the post |
'{id}_{}'
|
revisions |
Path
|
Sub path of revisions directory |
Path('revisions')
|
Source code in ktoolbox/configuration.py
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 | |
attachments: Path = Path('attachments')
class-attribute
instance-attribute
¶
content: Path = Path('content.txt')
class-attribute
instance-attribute
¶
external_links: Path = Path('external_links.txt')
class-attribute
instance-attribute
¶
file: str = '{id}_{}'
class-attribute
instance-attribute
¶
revisions: Path = Path('revisions')
class-attribute
instance-attribute
¶
downloader
¶
Downloader
¶
Attributes:
| Name | Type | Description | Default |
|---|---|---|---|
_save_filename |
The actual filename for saving. |
designated_filename
|
Source code in ktoolbox/downloader/downloader.py
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |
__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
client: httpx.AsyncClient
cached
property
¶
HTTPX AsyncClient
failure_servers: Set[int] = set()
class-attribute
instance-attribute
¶
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
post: Post
cached
property
¶
Post that the file belongs to
succeeded_servers: Set[int] = set()
class-attribute
instance-attribute
¶
url: str
cached
property
¶
Download URL
wait_lock = Lock()
class-attribute
instance-attribute
¶
__init__(url, path, client, *, buffer_size=None, chunk_size=None, designated_filename=None, server_path=None, post=None)
¶
Initialize a file downloader
- About filename:
- If
designated_filenameparameter is set, use it. - Else if
Content-Dispositionis 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 |
client |
AsyncClient
|
HTTPX AsyncClient |
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
|
post |
Post
|
Post object, use for logging. |
None
|
Source code in ktoolbox/downloader/downloader.py
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 | |
cancel()
¶
Cancel the download
It will raise asyncio.CancelledError in chunk_iterator (writing chunk to file) iteration.
Source code in ktoolbox/downloader/downloader.py
127 128 129 130 131 132 133 | |
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]
|
|
Raises:
| Type | Description |
|---|---|
CancelledError
|
Job cancelled |
Source code in ktoolbox/downloader/downloader.py
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
DownloaderRet
¶
Bases: BaseRet[_T]
Return data model of action call
Source code in ktoolbox/downloader/base.py
10 11 12 | |
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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
filename_from_headers(headers)
¶
Get file name from headers.
Parse from Content-Disposition.
-
Example:
filename_from_headers({'Content-Disposition': '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
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 | |
utime_from_headers(headers, path)
¶
Run os.utime on specific file using Last-Modified or Date in HTTP headers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
headers |
Dict[str, str]
|
HTTP Headers |
required |
path |
Union[Path, str]
|
File path |
required |
Source code in ktoolbox/downloader/utils.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
base
¶
downloader
¶
__all__ = ['Downloader']
module-attribute
¶
Downloader
¶
Attributes:
| Name | Type | Description | Default |
|---|---|---|---|
_save_filename |
The actual filename for saving. |
designated_filename
|
Source code in ktoolbox/downloader/downloader.py
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | |
__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
client: httpx.AsyncClient
cached
property
¶
HTTPX AsyncClient
failure_servers: Set[int] = set()
class-attribute
instance-attribute
¶
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
post: Post
cached
property
¶
Post that the file belongs to
succeeded_servers: Set[int] = set()
class-attribute
instance-attribute
¶
url: str
cached
property
¶
Download URL
wait_lock = Lock()
class-attribute
instance-attribute
¶
__init__(url, path, client, *, buffer_size=None, chunk_size=None, designated_filename=None, server_path=None, post=None)
¶
Initialize a file downloader
- About filename:
- If
designated_filenameparameter is set, use it. - Else if
Content-Dispositionis 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 |
client |
AsyncClient
|
HTTPX AsyncClient |
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
|
post |
Post
|
Post object, use for logging. |
None
|
Source code in ktoolbox/downloader/downloader.py
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 | |
cancel()
¶
Cancel the download
It will raise asyncio.CancelledError in chunk_iterator (writing chunk to file) iteration.
Source code in ktoolbox/downloader/downloader.py
127 128 129 130 131 132 133 | |
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]
|
|
Raises:
| Type | Description |
|---|---|
CancelledError
|
Job cancelled |
Source code in ktoolbox/downloader/downloader.py
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
utils
¶
__all__ = ['filename_from_headers', 'duplicate_file_check', 'utime_from_headers']
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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
filename_from_headers(headers)
¶
Get file name from headers.
Parse from Content-Disposition.
-
Example:
filename_from_headers({'Content-Disposition': '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
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 | |
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 40 | |
utime_from_headers(headers, path)
¶
Run os.utime on specific file using Last-Modified or Date in HTTP headers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
headers |
Dict[str, str]
|
HTTP Headers |
required |
path |
Union[Path, str]
|
File path |
required |
Source code in ktoolbox/downloader/utils.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
editor
¶
__all__ = ['EditWithSignalWidget', 'CascadingBoxes', 'run_config_editor']
module-attribute
¶
default_config = Configuration(_env_file='')
module-attribute
¶
default_config_envs = set(dump_envs(default_config))
module-attribute
¶
initial_envs = set(dump_envs(config))
module-attribute
¶
menu_top = menu('KToolBox Configuration Editor', [sub_menu('Edit', [sub_menu('API', model_to_widgets(config.api)), sub_menu('Downloader', model_to_widgets(config.downloader)), sub_menu('Job', model_to_widgets(config.job)), sub_menu('Logger', model_to_widgets(config.logger)), urwid.Divider()] + list(model_to_widgets(config, ['ssl_verify', 'json_dump_indent', 'use_uvloop']))), urwid.Divider(), menu_option(urwid.Button('JSON Preview', lambda x: top.open_box(sub_menu_with_menu_widget('JSON Preview', [urwid.Text(config.model_dump_json(indent=4))])[1]))), menu_option(urwid.Button('JSON Preview (Python Mode)', lambda x: top.open_box(sub_menu_with_menu_widget('JSON Preview (Python Serialize Mode)', [urwid.Text(pprint.pformat(config.model_dump(mode='python'), sort_dicts=False))])[1]))), menu_option(urwid.Button('DotEnv Preview (.env / prod.env)', lambda x: top.open_box(sub_menu_with_menu_widget('DotEnv Preview (.env / prod.env)', [urwid.Text('\n'.join(dump_modified_envs(dump_envs(config))) or 'Same as the default configuration, DotEnv will be left empty.')])[1]))), urwid.Divider(), sub_menu('Save', [menu_option(urwid.Button("Save to '.env' / 'prod.env' file", on_save_dotenv))]), urwid.Divider(bottom=2), menu_option(urwid.Button('Help', lambda x: webbrowser.open('https://ktoolbox.readthedocs.io/latest/configuration/guide/'))), menu_option(urwid.Button('Exit', exit_program)), urwid.Divider(bottom=2), urwid.Text('For detailed information, please refer to https://ktoolbox.readthedocs.io', align=urwid.CENTER), urwid.Divider(), urwid.Text(__version__, align=urwid.CENTER)])
module-attribute
¶
top = CascadingBoxes(menu_top)
module-attribute
¶
CascadingBoxes
¶
Bases: WidgetPlaceholder
Source code in ktoolbox/editor.py
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 | |
box_level = 0
instance-attribute
¶
max_box_levels = 4
class-attribute
instance-attribute
¶
__init__(box)
¶
Source code in ktoolbox/editor.py
50 51 52 53 | |
back()
¶
Source code in ktoolbox/editor.py
74 75 76 77 | |
exit()
¶
Source code in ktoolbox/editor.py
79 80 | |
keypress(size, key)
¶
Source code in ktoolbox/editor.py
82 83 84 85 86 87 88 | |
open_box(box)
¶
Source code in ktoolbox/editor.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
EditWithSignalWidget
¶
Bases: Edit
Custom urwid.Edit, support callback when changed.
Source code in ktoolbox/editor.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
__on_state_change = on_state_change
instance-attribute
¶
__user_data = user_data
instance-attribute
¶
__init__(*args, on_state_change, user_data, **kwargs)
¶
Source code in ktoolbox/editor.py
30 31 32 33 34 35 36 37 38 39 | |
keypress(size, key)
¶
Source code in ktoolbox/editor.py
41 42 43 44 | |
dump_envs(model)
¶
Dump environment variables, with no Env prefix
Source code in ktoolbox/editor.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
dump_modified_envs(envs)
¶
Dump modified environment variables, with Env prefix
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
envs |
List[str]
|
Current Envs |
required |
Source code in ktoolbox/editor.py
107 108 109 110 111 112 113 114 115 | |
exit_program(_=None)
¶
Source code in ktoolbox/editor.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
get_item(model, field, get_value_callback, widget_list, list_walker)
¶
Source code in ktoolbox/editor.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
get_value(item_types)
¶
Source code in ktoolbox/editor.py
326 327 328 329 330 331 332 333 334 335 | |
has_changed()
¶
Source code in ktoolbox/editor.py
133 134 | |
menu(title, choices)
¶
Source code in ktoolbox/editor.py
170 171 172 173 174 175 | |
menu_option(widget)
¶
Return focus_map="reversed" Widget
Source code in ktoolbox/editor.py
137 138 139 | |
model_to_widgets(model, fields=None)
¶
Generate urwid widgets for Pydantic model
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
BaseModel
|
Pydantic model |
required |
fields |
Iterable[str]
|
Only generate for these fields, default to all fields. |
None
|
Source code in ktoolbox/editor.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | |
on_add_item(_, user_data)
¶
Call when add item to List/Set/Tuple field
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_ |
Button
|
Widget |
required |
user_data |
Tuple[BaseModel, str, Callable[[], Optional[Any]], Union[List[_T], List[None]], Callable[[], _T], Union[MonitoredFocusList[_T], ListWalker]]
|
(model, field, () -> (default value), item list, () -> (new item), menu widget) |
required |
Source code in ktoolbox/editor.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
on_checkbox_change(_, state, user_data)
¶
Source code in ktoolbox/editor.py
184 185 186 | |
on_edit_change(widget, user_data)
¶
Source code in ktoolbox/editor.py
263 264 265 266 267 268 269 270 271 | |
on_item_changed(widget, user_data)
¶
Call when List/Set/Tuple field item changed
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
widget |
EditWithSignalWidget
|
Widget |
required |
user_data |
Tuple[BaseModel, str, Callable[[EditWithSignalWidget], Any], Union[List[_T], List[None]], _T]
|
(model, field, (edit widget) -> (value), item list, item) |
required |
Source code in ktoolbox/editor.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
on_radio_button_change(_, state, user_data)
¶
Source code in ktoolbox/editor.py
178 179 180 181 | |
on_remove_item(_, user_data)
¶
Call when remove item to List/Set/Tuple field
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_ |
Button
|
Widget |
required |
user_data |
Tuple[BaseModel, str, Union[List[_T], List[None]], _T, Union[MonitoredFocusList[_T], ListWalker]]
|
(model, field, item list, item, menu widget) |
required |
Source code in ktoolbox/editor.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
on_save_dotenv(_)
¶
Source code in ktoolbox/editor.py
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 | |
run_config_editor()
¶
Source code in ktoolbox/editor.py
455 456 | |
save_dotenv()
¶
Source code in ktoolbox/editor.py
118 119 120 121 122 123 124 125 126 127 128 129 130 | |
sub_menu(caption, choices)
¶
Source code in ktoolbox/editor.py
162 163 164 165 166 167 | |
sub_menu_with_menu_widget(caption, choices)
¶
Source code in ktoolbox/editor.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
job
¶
CreatorIndices
¶
Bases: BaseKToolBoxData
Creator directory indices model
Record the path of each downloaded post.
Source code in ktoolbox/job/model.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
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 25 26 | |
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
post: Optional[Post] = None
class-attribute
instance-attribute
¶
Post object
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
43 44 45 46 47 48 49 50 | |
jobs: List[Job] = []
class-attribute
instance-attribute
¶
All jobs
JobRunner
¶
Source code in ktoolbox/job/runner.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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
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, centralized_progress=True, use_colors=True, use_emojis=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
|
centralized_progress |
bool
|
Use centralized progress manager to prevent display chaos |
True
|
use_colors |
bool
|
Enable colorful progress bars (requires ANSI terminal support) |
True
|
use_emojis |
bool
|
Enable emoji indicators in progress bars |
True
|
Source code in ktoolbox/job/runner.py
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 | |
add_jobs(*jobs)
async
¶
Add jobs to self._job_queue
Source code in ktoolbox/job/runner.py
295 296 297 298 299 300 301 302 303 | |
cancel_downloader(target)
async
¶
Cancel downloader
Returns:
| Type | Description |
|---|---|
bool
|
Whether cancelled successfully |
Source code in ktoolbox/job/runner.py
318 319 320 321 322 323 324 325 326 327 328 | |
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
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 | |
start()
async
¶
Start processing jobs concurrently
It will Block until other call of self.start() method finished
Returns:
| Type | Description |
|---|---|
int
|
Number of jobs that failed |
Source code in ktoolbox/job/runner.py
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 | |
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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
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 25 26 | |
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
post: Optional[Post] = None
class-attribute
instance-attribute
¶
Post object
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
43 44 45 46 47 48 49 50 | |
jobs: List[Job] = []
class-attribute
instance-attribute
¶
All jobs
runner
¶
__all__ = ['JobRunner']
module-attribute
¶
JobRunner
¶
Source code in ktoolbox/job/runner.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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
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, centralized_progress=True, use_colors=True, use_emojis=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
|
centralized_progress |
bool
|
Use centralized progress manager to prevent display chaos |
True
|
use_colors |
bool
|
Enable colorful progress bars (requires ANSI terminal support) |
True
|
use_emojis |
bool
|
Enable emoji indicators in progress bars |
True
|
Source code in ktoolbox/job/runner.py
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 | |
add_jobs(*jobs)
async
¶
Add jobs to self._job_queue
Source code in ktoolbox/job/runner.py
295 296 297 298 299 300 301 302 303 | |
cancel_downloader(target)
async
¶
Cancel downloader
Returns:
| Type | Description |
|---|---|
bool
|
Whether cancelled successfully |
Source code in ktoolbox/job/runner.py
318 319 320 321 322 323 324 325 326 327 328 | |
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
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 | |
start()
async
¶
Start processing jobs concurrently
It will Block until other call of self.start() method finished
Returns:
| Type | Description |
|---|---|
int
|
Number of jobs that failed |
Source code in ktoolbox/job/runner.py
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 | |
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
¶
progress
¶
Centralized Progress Management for KToolBox
This module provides a centralized progress display system that prevents multiple concurrent progress bars from interfering with each other. Inspired by rclone's progress display approach with enhanced visual effects.
RICH_AVAILABLE = True
module-attribute
¶
__all__ = ['ProgressManager', 'ManagedTqdm', 'ColorTheme', 'setup_logger_for_progress', 'create_managed_tqdm_class']
module-attribute
¶
ColorTheme
¶
Rich-based color themes and styles for progress display
Source code in ktoolbox/progress.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 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 | |
BG_GREEN = '\x1b[42m'
class-attribute
instance-attribute
¶
BG_RED = '\x1b[41m'
class-attribute
instance-attribute
¶
BG_YELLOW = '\x1b[43m'
class-attribute
instance-attribute
¶
BLUE = '\x1b[34m'
class-attribute
instance-attribute
¶
BOLD = '\x1b[1m'
class-attribute
instance-attribute
¶
BRIGHT_BLUE = '\x1b[94m'
class-attribute
instance-attribute
¶
BRIGHT_CYAN = '\x1b[96m'
class-attribute
instance-attribute
¶
BRIGHT_GREEN = '\x1b[92m'
class-attribute
instance-attribute
¶
BRIGHT_MAGENTA = '\x1b[95m'
class-attribute
instance-attribute
¶
BRIGHT_RED = '\x1b[91m'
class-attribute
instance-attribute
¶
BRIGHT_WHITE = '\x1b[97m'
class-attribute
instance-attribute
¶
BRIGHT_YELLOW = '\x1b[93m'
class-attribute
instance-attribute
¶
COMPLETED = '✅'
class-attribute
instance-attribute
¶
CYAN = '\x1b[36m'
class-attribute
instance-attribute
¶
DOWNLOAD = '📥'
class-attribute
instance-attribute
¶
FAILED = '❌'
class-attribute
instance-attribute
¶
GREEN = '\x1b[32m'
class-attribute
instance-attribute
¶
MAGENTA = '\x1b[35m'
class-attribute
instance-attribute
¶
RED = '\x1b[31m'
class-attribute
instance-attribute
¶
RESET = '\x1b[0m'
class-attribute
instance-attribute
¶
RICH_STYLES = {RED: 'red', GREEN: 'green', YELLOW: 'yellow', BLUE: 'blue', MAGENTA: 'magenta', CYAN: 'cyan', WHITE: 'white', BRIGHT_RED: 'bright_red', BRIGHT_GREEN: 'bright_green', BRIGHT_YELLOW: 'bright_yellow', BRIGHT_BLUE: 'bright_blue', BRIGHT_MAGENTA: 'bright_magenta', BRIGHT_CYAN: 'bright_cyan', BRIGHT_WHITE: 'bright_white'}
class-attribute
instance-attribute
¶
ROCKET = '🚀'
class-attribute
instance-attribute
¶
RUNNING = '🔄'
class-attribute
instance-attribute
¶
SPEED = '⚡'
class-attribute
instance-attribute
¶
SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
class-attribute
instance-attribute
¶
WAITING = '⏳'
class-attribute
instance-attribute
¶
WHITE = '\x1b[37m'
class-attribute
instance-attribute
¶
YELLOW = '\x1b[33m'
class-attribute
instance-attribute
¶
colorize(text, color, bold=False)
classmethod
¶
Apply color to text with optional bold using Rich when available
Source code in ktoolbox/progress.py
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 | |
supports_color()
classmethod
¶
Check if terminal supports color using Rich when available
Source code in ktoolbox/progress.py
154 155 156 157 158 159 160 161 162 163 164 165 | |
ManagedTqdm
¶
A tqdm-compatible progress bar that works with ProgressManager. This class mimics the tqdm interface but delegates to ProgressManager.
Source code in ktoolbox/progress.py
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 | |
manager = None
instance-attribute
¶
n
property
writable
¶
progress_id = None
instance-attribute
¶
total
property
writable
¶
__bool__()
¶
For compatibility with tqdm boolean checks
Source code in ktoolbox/progress.py
849 850 851 | |
__del__()
¶
Cleanup when object is garbage collected
Source code in ktoolbox/progress.py
853 854 855 856 857 858 | |
__enter__()
¶
Source code in ktoolbox/progress.py
789 790 791 792 | |
__exit__(exc_type, exc_val, exc_tb)
¶
Source code in ktoolbox/progress.py
794 795 796 797 798 | |
__init__(desc=None, total=None, initial=0, disable=False, unit='it', unit_scale=False, manager=None, **kwargs)
¶
Source code in ktoolbox/progress.py
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 | |
close()
¶
Close/finish the progress bar
Source code in ktoolbox/progress.py
780 781 782 783 784 785 786 787 | |
set_description(desc)
¶
Set the description of the progress bar
Source code in ktoolbox/progress.py
771 772 773 774 775 776 777 778 | |
set_failed(failed=True)
¶
Mark the progress bar as failed
Source code in ktoolbox/progress.py
800 801 802 803 804 805 806 807 | |
set_paused(paused=True)
¶
Mark the progress bar as paused
Source code in ktoolbox/progress.py
809 810 811 812 813 814 815 816 817 818 | |
update(n=1)
¶
Update the progress bar by n units
Source code in ktoolbox/progress.py
761 762 763 764 765 766 767 768 769 | |
ProgressAwareHandler
¶
Custom loguru handler that works with progress manager
Source code in ktoolbox/progress.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
original_handler = original_handler
instance-attribute
¶
__init__(original_handler)
¶
Source code in ktoolbox/progress.py
41 42 | |
flush()
¶
Source code in ktoolbox/progress.py
57 58 59 | |
write(message)
¶
Source code in ktoolbox/progress.py
44 45 46 47 48 49 50 51 52 53 54 55 | |
ProgressManager
¶
Centralized progress manager that coordinates multiple progress bars in a fixed terminal display area with enhanced visual effects.
Source code in ktoolbox/progress.py
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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 | |
file = file or sys.stdout
instance-attribute
¶
max_workers = max_workers
instance-attribute
¶
use_colors = use_colors and ColorTheme.supports_color()
instance-attribute
¶
use_emojis = use_emojis
instance-attribute
¶
__init__(max_workers=5, file=None, use_colors=True, use_emojis=True, update_interval=0.1)
¶
Initialize the progress manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_workers |
int
|
Maximum number of concurrent progress bars to display |
5
|
file |
Optional[TextIO]
|
Output stream (defaults to sys.stdout) |
None
|
use_colors |
bool
|
Enable color output |
True
|
use_emojis |
bool
|
Enable emoji indicators |
True
|
Source code in ktoolbox/progress.py
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 | |
create_progress_bar(desc, total=None, unit='B', unit_scale=True)
¶
Create a new managed progress bar
Source code in ktoolbox/progress.py
256 257 258 259 260 | |
finish_progress(progress_id, failed=False)
¶
Mark a progress bar as finished
Source code in ktoolbox/progress.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
increment_existed(n=1)
¶
Atomically increment the existed count by n and return the new value
Source code in ktoolbox/progress.py
250 251 252 253 254 | |
restore_display()
¶
Restore display after log output
Source code in ktoolbox/progress.py
355 356 357 358 359 | |
set_job_totals(total, completed=0, failed=0, existed=0)
¶
Set the total number of jobs for overall progress tracking
Source code in ktoolbox/progress.py
232 233 234 235 236 237 238 | |
start_display()
¶
Start the progress display loop
Source code in ktoolbox/progress.py
311 312 313 314 315 316 317 318 319 | |
stop_display()
¶
Stop the progress display loop
Source code in ktoolbox/progress.py
321 322 323 324 325 326 327 328 329 330 | |
temporary_clear_for_log(log_message=None)
¶
Temporarily clear display to allow log output
Source code in ktoolbox/progress.py
345 346 347 348 349 350 351 352 353 | |
update_display()
¶
Update the terminal display
Source code in ktoolbox/progress.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 | |
update_job_progress(completed=None, failed=None, existed=None)
¶
Update overall job progress
Source code in ktoolbox/progress.py
240 241 242 243 244 245 246 247 248 | |
update_progress(progress_id, current, desc=None, failed=False)
¶
Update progress for a specific progress bar
Source code in ktoolbox/progress.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
ProgressState
dataclass
¶
Represents the state of a single progress bar
Source code in ktoolbox/progress.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
current: int = 0
class-attribute
instance-attribute
¶
desc: str = ''
class-attribute
instance-attribute
¶
failed: bool = False
class-attribute
instance-attribute
¶
finished: bool = False
class-attribute
instance-attribute
¶
last_update: float = field(default_factory=time.time)
class-attribute
instance-attribute
¶
paused: bool = False
class-attribute
instance-attribute
¶
rate: Optional[float] = None
class-attribute
instance-attribute
¶
total: Optional[int] = None
class-attribute
instance-attribute
¶
unit: str = 'it'
class-attribute
instance-attribute
¶
unit_scale: bool = False
class-attribute
instance-attribute
¶
__init__(desc='', total=None, current=0, unit='it', unit_scale=False, rate=None, last_update=time.time(), _last_render_seen=0.0, finished=False, failed=False, paused=False)
¶
create_managed_tqdm_class(progress_manager)
¶
Create a tqdm class factory that uses the given ProgressManager. This allows us to create a drop-in replacement for tqdm.
Source code in ktoolbox/progress.py
861 862 863 864 865 866 867 868 869 870 871 | |
setup_logger_for_progress(progress_manager=None)
¶
Setup logger to work with progress manager
Source code in ktoolbox/progress.py
32 33 34 35 | |
utils
¶
__all__ = ['BaseRet', 'generate_msg', 'logger_init', 'dump_search', 'parse_webpage_url', 'uvloop_init', 'extract_external_links', 'check_for_updates']
module-attribute
¶
BaseRet
¶
Bases: BaseModel, Generic[_T]
Base data model of function return value
Source code in ktoolbox/utils.py
32 33 34 35 36 37 38 39 40 41 42 | |
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
41 42 | |
check_for_updates()
async
¶
Check for updates from GitHub and PyPI (backup). Show information if a newer version is available.
Source code in ktoolbox/utils.py
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 | |
dump_search(result, path)
async
¶
Source code in ktoolbox/utils.py
91 92 93 94 95 96 | |
extract_external_links(content, custom_patterns=None)
¶
Extract external file sharing links from text content.
Targets common cloud storage and file sharing services like: - Google Drive - MEGA - Dropbox - OneDrive - MediaFire - And other common file hosting services
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content |
str
|
Text content to extract links from |
required |
custom_patterns |
Optional[List[str]]
|
Custom regex patterns to use. |
None
|
Returns:
| Type | Description |
|---|---|
Set[str]
|
Set of unique external links found |
Source code in ktoolbox/utils.py
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 | |
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
45 46 47 48 49 50 51 52 53 54 55 56 57 | |
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
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 | |
parse_webpage_url(url)
¶
Fetch service, user_id, post_id, revision_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], Optional[str]]
|
Tuple of service, user_id, post_id, revision_id |
Source code in ktoolbox/utils.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
uvloop_init()
¶
Set event loop policy to uvloop or winloop if available.
Uses winloop on Windows and uvloop on Unix-like systems for performance optimization.
Returns:
| Type | Description |
|---|---|
bool
|
If event loop policy was set successfully |
Source code in ktoolbox/utils.py
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 | |