Skip to content

Commit 55561d9

Browse files
authored
fix: increase upload tolerance (#147)
1 parent 512024a commit 55561d9

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

src/upload/upload.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,42 +67,47 @@ def find_bv_number(target_str, my_list):
6767
return None
6868

6969
def read_append_and_delete_lines(file_path):
70-
while True:
71-
if os.path.getsize(file_path) == 0:
72-
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} Empty queue, wait 2 minutes and check again...", flush=True)
73-
time.sleep(120)
74-
return
70+
try:
71+
while True:
72+
if os.path.getsize(file_path) == 0:
73+
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} Empty queue, wait 2 minutes and check again...", flush=True)
74+
time.sleep(120)
75+
return
7576

76-
with open(file_path, "r+") as file:
77-
fcntl.flock(file, fcntl.LOCK_EX)
78-
lines = file.readlines()
79-
upload_video_path = lines.pop(0).strip()
80-
file.seek(0)
81-
file.writelines(lines)
82-
# Truncate the file to the current position
83-
file.truncate()
84-
# Release the lock
85-
fcntl.flock(file, fcntl.LOCK_UN)
77+
with open(file_path, "r+") as file:
78+
fcntl.flock(file, fcntl.LOCK_EX)
79+
lines = file.readlines()
80+
upload_video_path = lines.pop(0).strip()
81+
file.seek(0)
82+
file.writelines(lines)
83+
# Truncate the file to the current position
84+
file.truncate()
85+
# Release the lock
86+
fcntl.flock(file, fcntl.LOCK_UN)
8687

87-
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} " + "deal with " + upload_video_path, flush=True)
88-
# check if the live is already uploaded
89-
query = generate_title(upload_video_path)
90-
result = subprocess.check_output(f"{SRC_DIR}/upload/biliup" + " -u " + f"{SRC_DIR}/upload/cookies.json" + " list", shell=True)
91-
upload_list = result.decode("utf-8").splitlines()
92-
limit_list = upload_list[:30]
93-
bv_result = find_bv_number(query, limit_list)
94-
if bv_result:
95-
print(f"BV number is: {bv_result}", flush=True)
96-
append_upload(upload_video_path, bv_result)
97-
else:
98-
print("First upload this live", flush=True)
99-
# generate the yaml template
100-
yaml_template = generate_yaml_template(upload_video_path)
101-
yaml_file_path = SRC_DIR + "/upload/upload.yaml"
102-
with open(yaml_file_path, 'w', encoding='utf-8') as file:
103-
file.write(yaml_template)
104-
upload_video(upload_video_path, yaml_file_path)
105-
return
88+
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} " + "deal with " + upload_video_path, flush=True)
89+
# check if the live is already uploaded
90+
query = generate_title(upload_video_path)
91+
result = subprocess.check_output(f"{SRC_DIR}/upload/biliup" + " -u " + f"{SRC_DIR}/upload/cookies.json" + " list", shell=True)
92+
upload_list = result.decode("utf-8").splitlines()
93+
limit_list = upload_list[:30]
94+
bv_result = find_bv_number(query, limit_list)
95+
if bv_result:
96+
print(f"BV number is: {bv_result}", flush=True)
97+
append_upload(upload_video_path, bv_result)
98+
else:
99+
print("First upload this live", flush=True)
100+
# generate the yaml template
101+
yaml_template = generate_yaml_template(upload_video_path)
102+
yaml_file_path = SRC_DIR + "/upload/upload.yaml"
103+
with open(yaml_file_path, 'w', encoding='utf-8') as file:
104+
file.write(yaml_template)
105+
upload_video(upload_video_path, yaml_file_path)
106+
return
107+
108+
except subprocess.CalledProcessError:
109+
print("Fail to upload, the files will be reserved.")
110+
sys.exit(1)
106111

107112
def append_upload(upload_path, bv_result):
108113
try:

0 commit comments

Comments
 (0)