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