返回 Social Auto Upload
files_times.py
根目录 / utils / files_times.py
1 from datetime import timedelta
2
3 from datetime import datetime
4 from pathlib import Path
5
6 from conf import BASE_DIR
7
8
9 def get_absolute_path(relative_path: str, base_dir: str = None) -> str:
10 # Convert the relative path to an absolute path
11 absolute_path = Path(BASE_DIR) / base_dir / relative_path
12 return str(absolute_path)
13
14
15 def get_title_and_hashtags(filename):
16 """
17 获取视频标题和 hashtag
18
19 Args:
20 filename: 视频文件名
21
22 Returns:
23 视频标题和 hashtag 列表
24 """
25
26 # 获取视频标题和 hashtag txt 文件名
27 txt_filename = filename.replace(".mp4", ".txt")
28
29 # 读取 txt 文件
30 with open(txt_filename, "r", encoding="utf-8") as f:
31 content = f.read()
32
33 # 获取标题和 hashtag
34 splite_str = content.strip().split("\n")
35 title = splite_str[0]
36 hashtags = splite_str[1].replace("#", "").split(" ")
37
38 return title, hashtags
39
40
41 def generate_schedule_time_next_day(total_videos, videos_per_day = 1, daily_times=None, timestamps=False, start_days=0):
42 """
43 Generate a schedule for video uploads, starting from the next day.
44
45 Args:
46 - total_videos: Total number of videos to be uploaded.
47 - videos_per_day: Number of videos to be uploaded each day.
48 - daily_times: Optional list of specific times of the day to publish the videos.
49 - timestamps: Boolean to decide whether to return timestamps or datetime objects.
50 - start_days: Start from after start_days.
51
52 Returns:
53 - A list of scheduling times for the videos, either as timestamps or datetime objects.
54 """
55 if videos_per_day <= 0:
56 raise ValueError("videos_per_day should be a positive integer")
57
58 if daily_times is None:
59 # Default times to publish videos if not provided
60 daily_times = [6, 11, 14, 16, 22]
61
62 if videos_per_day > len(daily_times):
63 raise ValueError("videos_per_day should not exceed the length of daily_times")
64
65 # Generate timestamps
66 schedule = []
67 current_time = datetime.now()
68
69 for video in range(total_videos):
70 day = video // videos_per_day + start_days + 1 # +1 to start from the next day
71 daily_video_index = video % videos_per_day
72
73 # Calculate the time for the current video
74 hour = daily_times[daily_video_index]
75 time_offset = timedelta(days=day, hours=hour - current_time.hour, minutes=-current_time.minute,
76 seconds=-current_time.second, microseconds=-current_time.microsecond)
77 timestamp = current_time + time_offset
78
79 schedule.append(timestamp)
80
81 if timestamps:
82 schedule = [int(time.timestamp()) for time in schedule]
83 return schedule
84
84 lines PYTHON