33import subprocess
44import os
55import sys
6- from src .config import SRC_DIR , BILIVE_DIR
6+ from src .config import SRC_DIR , BILIVE_DIR , RESERVE_FOR_FIXING
77from datetime import datetime
88from src .upload .generate_upload_data import generate_video_data , generate_slice_data
99from src .upload .extract_video_info import generate_title
1010from src .log .logger import upload_log
1111import time
1212import fcntl
1313from 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
1515import threading
1616from .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-
5142def 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+
7077def 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