Skip to content

Commit f9aeea6

Browse files
committed
feat: support customize filename format
Closes #116
1 parent 6549e21 commit f9aeea6

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

ktoolbox/action/job.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from ktoolbox._enum import PostFileTypeEnum, DataStorageNameEnum
1313
from ktoolbox.action import ActionRet, fetch_creator_posts, FetchInterruptError
14-
from ktoolbox.action.utils import generate_post_path_name, filter_posts_by_date
14+
from ktoolbox.action.utils import generate_post_path_name, filter_posts_by_date, generate_filename
1515
from ktoolbox.api.model import Post, Attachment
1616
from ktoolbox.configuration import config, PostStructureConfiguration
1717
from ktoolbox.job import Job, CreatorIndices
@@ -68,7 +68,8 @@ async def create_job_from_post(
6868
config.job.block_list
6969
)
7070
):
71-
alt_filename = f"{i + 1}{file_path_obj.suffix}" if config.job.sequential_filename else file_path_obj.name
71+
basic_filename = f"{i + 1}{file_path_obj.suffix}" if config.job.sequential_filename else file_path_obj.name
72+
alt_filename = generate_filename(post, basic_filename)
7273
jobs.append(
7374
Job(
7475
path=attachments_path,

ktoolbox/action/utils.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,53 @@
88
from ktoolbox.configuration import config
99
from ktoolbox.job import CreatorIndices
1010

11-
__all__ = ["generate_post_path_name", "filter_posts_by_date", "filter_posts_by_indices"]
11+
__all__ = ["generate_post_path_name", "generate_filename", "filter_posts_by_date", "filter_posts_by_indices"]
12+
13+
TIME_FORMAT = "%Y-%m-%d"
1214

1315

1416
def generate_post_path_name(post: Post) -> str:
1517
"""Generate directory name for post to save."""
1618
if config.job.post_id_as_path or not post.title:
1719
return post.id
1820
else:
19-
time_format = "%Y-%m-%d"
2021
try:
2122
return sanitize_filename(
2223
config.job.post_dirname_format.format(
2324
id=post.id,
2425
user=post.user,
2526
service=post.service,
2627
title=post.title,
27-
added=post.added.strftime(time_format) if post.added else "",
28-
published=post.published.strftime(time_format) if post.published else "",
29-
edited=post.edited.strftime(time_format) if post.edited else ""
28+
added=post.added.strftime(TIME_FORMAT) if post.added else "",
29+
published=post.published.strftime(TIME_FORMAT) if post.published else "",
30+
edited=post.edited.strftime(TIME_FORMAT) if post.edited else ""
3031
)
3132
)
3233
except KeyError as e:
3334
logger.error(f"`JobConfiguration.post_dirname_format` contains invalid key: {e}")
3435
exit(1)
3536

3637

38+
def generate_filename(post: Post, basic_name: str) -> str:
39+
"""Generate download filename"""
40+
try:
41+
return sanitize_filename(
42+
config.job.filename_format.format(
43+
basic_name,
44+
id=post.id,
45+
user=post.user,
46+
service=post.service,
47+
title=post.title,
48+
added=post.added.strftime(TIME_FORMAT) if post.added else "",
49+
published=post.published.strftime(TIME_FORMAT) if post.published else "",
50+
edited=post.edited.strftime(TIME_FORMAT) if post.edited else ""
51+
)
52+
)
53+
except KeyError as e:
54+
logger.error(f"`JobConfiguration.filename_format` contains invalid key: {e}")
55+
exit(1)
56+
57+
3758
def _match_post_date(
3859
post: Post,
3960
start_date: Optional[datetime],

0 commit comments

Comments
 (0)