files_times.py
2.61 KB
1
2
3
4
5
6
7
8
9
10
11
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
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
from datetime import timedelta
from datetime import datetime
from pathlib import Path
from conf import BASE_DIR
def get_absolute_path(relative_path: str, base_dir: str = None) -> str:
# Convert the relative path to an absolute path
absolute_path = Path(BASE_DIR) / base_dir / relative_path
return str(absolute_path)
def get_title_and_hashtags(filename):
"""
获取视频标题和 hashtag
Args:
filename: 视频文件名
Returns:
视频标题和 hashtag 列表
"""
# 获取视频标题和 hashtag txt 文件名
txt_filename = filename.replace(".mp4", ".txt")
# 读取 txt 文件
with open(txt_filename, "r", encoding="utf-8") as f:
content = f.read()
# 获取标题和 hashtag
splite_str = content.strip().split("\n")
title = splite_str[0]
hashtags = splite_str[1].replace("#", "").split(" ")
return title, hashtags
def generate_schedule_time_next_day(total_videos, videos_per_day, daily_times=None, timestamps=False, start_days=0):
"""
Generate a schedule for video uploads, starting from the next day.
Args:
- total_videos: Total number of videos to be uploaded.
- videos_per_day: Number of videos to be uploaded each day.
- daily_times: Optional list of specific times of the day to publish the videos.
- timestamps: Boolean to decide whether to return timestamps or datetime objects.
- start_days: Start from after start_days.
Returns:
- A list of scheduling times for the videos, either as timestamps or datetime objects.
"""
if videos_per_day <= 0:
raise ValueError("videos_per_day should be a positive integer")
if daily_times is None:
# Default times to publish videos if not provided
daily_times = [6, 11, 14, 16, 22]
if videos_per_day > len(daily_times):
raise ValueError("videos_per_day should not exceed the length of daily_times")
# Generate timestamps
schedule = []
current_time = datetime.now()
for video in range(total_videos):
day = video // videos_per_day + start_days + 1 # +1 to start from the next day
daily_video_index = video % videos_per_day
# Calculate the time for the current video
hour = daily_times[daily_video_index]
time_offset = timedelta(days=day, hours=hour - current_time.hour, minutes=-current_time.minute,
seconds=-current_time.second, microseconds=-current_time.microsecond)
timestamp = current_time + time_offset
schedule.append(timestamp)
if timestamps:
schedule = [int(time.timestamp()) for time in schedule]
return schedule