Skip to content

Commit 5289798

Browse files
authored
feat: add reserve queue (#248)
1 parent 815ac33 commit 5289798

File tree

4 files changed

+74
-43
lines changed

4 files changed

+74
-43
lines changed

src/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
DESC = "{artist}直播回放,直播间地址:{source_link} 内容仅供娱乐,直播中主播的言论、观点和行为均由主播本人负责,不代表录播员的观点或立场。"
1919
# You can change the description as you like.
2020
GIFT_PRICE_FILTER = 1 # The gift whose price is less than this value will be filtered, unit: RMB
21+
RESERVE_FOR_FIXING = False # If encounter MOOV crash error, delete the video or reserve for fixing
2122
# ============================ The video slice configuration ==================
2223
AUTO_SLICE = False
2324
SLICE_DURATION = 60 # better not exceed 300 seconds

src/db/conn.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def get_single_upload_queue():
3737
def get_all_upload_queue():
3838
db = connect()
3939
cursor = db.cursor()
40-
cursor.execute("select video_path from upload_queue;")
40+
cursor.execute("select id, video_path, locked from upload_queue;")
4141
rows = cursor.fetchall()
42-
result = [{'video_path': row[0]} for row in rows]
42+
result = [{'id': row[0], 'video_path': row[1], 'locked': row[2]} for row in rows]
4343
db.close()
4444
return result
4545

@@ -79,22 +79,38 @@ def update_upload_queue_lock(video_path: str, locked: int):
7979
print("Update Upload Queue failed.")
8080
return False
8181

82-
83-
82+
def get_single_lock_queue():
83+
db = connect()
84+
cursor = db.cursor()
85+
cursor.execute("select video_path from upload_queue where locked = 1 limit 1;")
86+
row = cursor.fetchone()
87+
result = {'video_path': row[0]} if row else None
88+
db.close()
89+
return result
90+
91+
def get_all_reserve_for_fixing_queue():
92+
db = connect()
93+
cursor = db.cursor()
94+
cursor.execute("select video_path from upload_queue where locked = 2;")
95+
rows = cursor.fetchall()
96+
result = [{'video_path': row[0]} for row in rows]
97+
db.close()
98+
return result
99+
84100
if __name__ == "__main__":
85101
# Create Table
86-
create_table()
102+
# create_table()
87103
# Insert Test Data
88-
insert_upload_queue('')
104+
# insert_upload_queue('')
89105
# Insert again to check the unique index
90106
# print(insert_upload_queue(''))
91107
# Get the single upload queue, shold be {'video_path': 'test.mp4'}
92108
# print(get_single_upload_queue())
93109
# Get all upload queue
94110
print(get_all_upload_queue())
95111
# unlock the upload queue
96-
update_upload_queue_lock('test.mp4', 0)
112+
# update_upload_queue_lock('test.mp4', 0)
97113
# Delete the upload queue
98-
delete_upload_queue('')
114+
# delete_upload_queue('')
99115
# Get the single upload queue after delete, should be None
100116
print(get_single_upload_queue())

src/upload/upload.py

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import subprocess
44
import os
55
import sys
6-
from src.config import SRC_DIR, BILIVE_DIR
6+
from src.config import SRC_DIR, BILIVE_DIR, RESERVE_FOR_FIXING
77
from datetime import datetime
88
from src.upload.generate_upload_data import generate_video_data, generate_slice_data
99
from src.upload.extract_video_info import generate_title
1010
from src.log.logger import upload_log
1111
import time
1212
import fcntl
1313
from concurrent.futures import ThreadPoolExecutor, as_completed
14-
from db.conn import get_single_upload_queue, delete_upload_queue, update_upload_queue_lock
14+
from db.conn import get_single_upload_queue, delete_upload_queue, update_upload_queue_lock, get_single_lock_queue
1515
import threading
1616
from .bilitool.bilitool import UploadController, FeedController, LoginController
1717

@@ -39,15 +39,6 @@ def upload_video(upload_path):
3939
update_upload_queue_lock(upload_path, 0)
4040
return False
4141

42-
def find_bv_number(target_str, my_list):
43-
for element in my_list:
44-
if target_str in element:
45-
parts = element.split('|')
46-
if len(parts) > 0:
47-
return parts[1].strip()
48-
return None
49-
50-
5142
def append_upload(upload_path, bv_result):
5243
try:
5344
result = UploadController().append_video_entry(upload_path, bv_result)
@@ -65,49 +56,72 @@ def append_upload(upload_path, bv_result):
6556
upload_log.error(f"The append_upload called failed, the files will be reserved. error: {e}")
6657
update_upload_queue_lock(upload_path, 0)
6758
return False
68-
69-
59+
60+
def video_gate(video_path):
61+
if video_path.endswith('.flv'): # slice video tag
62+
# upload slice video
63+
upload_video(video_path)
64+
else:
65+
# get the uploaded video list
66+
upload_dict = FeedController().get_video_dict_info(20, "pubed,not_pubed,is_pubing")
67+
query = generate_title(video_path)
68+
bv_result = upload_dict.get(query) # query this title
69+
if bv_result:
70+
upload_log.info(f"The series of videos has already been uploaded, the BV number is: {bv_result}")
71+
append_upload(video_path, bv_result)
72+
else:
73+
upload_log.info("First upload this live")
74+
upload_video(video_path)
75+
time.sleep(5) # avoid JIT read error
76+
7077
def read_append_and_delete_lines():
7178
while True:
72-
# upload_queue = None
73-
# read the queue and update lock status to prevent other threads from reading the same data
7479
if LoginController().check_bilibili_login():
7580
pass
7681
else:
7782
file = BILIVE_DIR + "/cookie.json"
7883
LoginController().login_bilibili_with_cookie_file(file)
79-
# LoginController().login_bilibili(export=False)
84+
# LoginController().login_bilibili(export=False) # reserve for docker version
8085
continue
81-
# with read_lock:
8286
upload_queue = get_single_upload_queue()
83-
# if there is a task in the queue, try to lock the task
87+
lock_queue = get_single_lock_queue()
8488
if upload_queue:
8589
video_path = upload_queue['video_path']
86-
time.sleep(3)
90+
time.sleep(3) # avoid JIT read error
8791
query = generate_title(video_path)
88-
if query is None:
92+
if query is None: # JIT read error or MOOV crash error or interrupted error
8993
if not os.path.exists(video_path):
90-
delete_upload_queue(video_path)
94+
# Interrupted error, the file has been uploaded. But record is not deleted.
95+
delete_upload_queue(video_path) # Directly delete the record
9196
continue
9297
else:
98+
# JIT read error or MOOV crash error
9399
upload_log.error(f"Error occurred in ffprobe: {video_path}")
94-
update_upload_queue_lock(video_path, 1)
100+
update_upload_queue_lock(video_path, 1) # Lock first, wait for the lock execute round
95101
continue
96102
upload_log.info(f"deal with {video_path}")
97-
# check if the live is already uploaded
98-
if video_path.endswith('.flv'):
99-
# upload slice video
100-
upload_video(video_path)
103+
video_gate(video_path)
104+
elif lock_queue:
105+
# check the lock video
106+
video_path = lock_queue['video_path']
107+
if not os.path.exists(video_path):
108+
# Interrupted error, the file has been uploaded. But record is not deleted.
109+
delete_upload_queue(video_path) # Directly delete the record
110+
continue
101111
else:
102-
upload_dict = FeedController().get_video_dict_info(20, "pubed,not_pubed,is_pubing")
103-
bv_result = upload_dict.get(query)
104-
if bv_result:
105-
upload_log.info(f"The series of videos has already been uploaded, the BV number is: {bv_result}")
106-
append_upload(video_path, bv_result)
112+
query = generate_title(video_path)
113+
if query is None:
114+
if RESERVE_FOR_FIXING:
115+
# MOOV crash error
116+
upload_log.error(f"MOOV crash error, {video_path} is reserved for fixing")
117+
update_upload_queue_lock(video_path, 2) # Reserve for fixing
118+
else:
119+
# JIT read error
120+
delete_upload_queue(video_path) # Directly delete the record
121+
os.remove(video_path)
107122
else:
108-
upload_log.info("First upload this live")
109-
upload_video(video_path)
110-
time.sleep(20)
123+
upload_log.info(f"deal with {video_path} in lock queue")
124+
video_gate(video_path)
111125
else:
112126
upload_log.info("Empty queue, wait 2 minutes and check again.")
113127
time.sleep(120)

0 commit comments

Comments
 (0)