|
8 | 8 | from ktoolbox.configuration import config
|
9 | 9 | from ktoolbox.job import CreatorIndices
|
10 | 10 |
|
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" |
12 | 14 |
|
13 | 15 |
|
14 | 16 | def generate_post_path_name(post: Post) -> str:
|
15 | 17 | """Generate directory name for post to save."""
|
16 | 18 | if config.job.post_id_as_path or not post.title:
|
17 | 19 | return post.id
|
18 | 20 | else:
|
19 |
| - time_format = "%Y-%m-%d" |
20 | 21 | try:
|
21 | 22 | return sanitize_filename(
|
22 | 23 | config.job.post_dirname_format.format(
|
23 | 24 | id=post.id,
|
24 | 25 | user=post.user,
|
25 | 26 | service=post.service,
|
26 | 27 | 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 "" |
30 | 31 | )
|
31 | 32 | )
|
32 | 33 | except KeyError as e:
|
33 | 34 | logger.error(f"`JobConfiguration.post_dirname_format` contains invalid key: {e}")
|
34 | 35 | exit(1)
|
35 | 36 |
|
36 | 37 |
|
| 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 | + |
37 | 58 | def _match_post_date(
|
38 | 59 | post: Post,
|
39 | 60 | start_date: Optional[datetime],
|
|
0 commit comments