Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 39 additions & 34 deletions src/upload/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,47 @@ def find_bv_number(target_str, my_list):
return None

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

with open(file_path, "r+") as file:
fcntl.flock(file, fcntl.LOCK_EX)
lines = file.readlines()
upload_video_path = lines.pop(0).strip()
file.seek(0)
file.writelines(lines)
# Truncate the file to the current position
file.truncate()
# Release the lock
fcntl.flock(file, fcntl.LOCK_UN)
with open(file_path, "r+") as file:
fcntl.flock(file, fcntl.LOCK_EX)
lines = file.readlines()
upload_video_path = lines.pop(0).strip()
file.seek(0)
file.writelines(lines)
# Truncate the file to the current position
file.truncate()
# Release the lock
fcntl.flock(file, fcntl.LOCK_UN)

print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} " + "deal with " + upload_video_path, flush=True)
# check if the live is already uploaded
query = generate_title(upload_video_path)
result = subprocess.check_output(f"{SRC_DIR}/upload/biliup" + " -u " + f"{SRC_DIR}/upload/cookies.json" + " list", shell=True)
upload_list = result.decode("utf-8").splitlines()
limit_list = upload_list[:30]
bv_result = find_bv_number(query, limit_list)
if bv_result:
print(f"BV number is: {bv_result}", flush=True)
append_upload(upload_video_path, bv_result)
else:
print("First upload this live", flush=True)
# generate the yaml template
yaml_template = generate_yaml_template(upload_video_path)
yaml_file_path = SRC_DIR + "/upload/upload.yaml"
with open(yaml_file_path, 'w', encoding='utf-8') as file:
file.write(yaml_template)
upload_video(upload_video_path, yaml_file_path)
return
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} " + "deal with " + upload_video_path, flush=True)
# check if the live is already uploaded
query = generate_title(upload_video_path)
result = subprocess.check_output(f"{SRC_DIR}/upload/biliup" + " -u " + f"{SRC_DIR}/upload/cookies.json" + " list", shell=True)
upload_list = result.decode("utf-8").splitlines()
limit_list = upload_list[:30]
bv_result = find_bv_number(query, limit_list)
if bv_result:
print(f"BV number is: {bv_result}", flush=True)
append_upload(upload_video_path, bv_result)
else:
print("First upload this live", flush=True)
# generate the yaml template
yaml_template = generate_yaml_template(upload_video_path)
yaml_file_path = SRC_DIR + "/upload/upload.yaml"
with open(yaml_file_path, 'w', encoding='utf-8') as file:
file.write(yaml_template)
upload_video(upload_video_path, yaml_file_path)
return

except subprocess.CalledProcessError:
print("Fail to upload, the files will be reserved.")
sys.exit(1)

def append_upload(upload_path, bv_result):
try:
Expand Down