1212from bilitool .utils .parse_cookies import parse_cookies
1313from bilitool .model .model import Model
1414import hashlib
15-
16- # you can test your best cdn line https://member.bilibili.com/preupload?r=ping
17- # cdn_lines = {
18- # 'qn': 'upos-sz-upcdnqn.bilivideo.com',
19- # 'bda2': 'upos-sz-upcdnbda2.bilivideo.com',
20- # }
15+ import time
2116
2217class BiliUploader (object ):
2318 def __init__ (self , logger ):
@@ -28,7 +23,38 @@ def __init__(self, logger):
2823 self .session .cookies = requests .utils .cookiejar_from_dict (self .config ["cookies" ])
2924 self .headers = Model ().get_headers_with_cookies_and_refer ()
3025
31- def preupload (self , * , filename , filesize ):
26+ def probe (self ):
27+ ret = requests .get ('https://member.bilibili.com/preupload?r=probe' , headers = self .headers , timeout = 5 ).json ()
28+ print (f"线路:{ ret ['lines' ]} " )
29+ data , auto_os = None , None
30+ min_cost = 0
31+ if ret ['probe' ].get ('get' ):
32+ method = 'get'
33+ else :
34+ method = 'post'
35+ data = bytes (int (1024 * 0.1 * 1024 ))
36+ for line in ret ['lines' ]:
37+ start = time .perf_counter ()
38+ test = requests .request (method , f"https:{ line ['probe_url' ]} " , data = data , timeout = 30 )
39+ cost = time .perf_counter () - start
40+ print (line ['query' ], cost )
41+ if test .status_code != 200 :
42+ return
43+ if not min_cost or min_cost > cost :
44+ auto_os = line
45+ min_cost = cost
46+ auto_os ['cost' ] = min_cost
47+ self .logger .info (f"the best cdn line is:{ auto_os } " )
48+ upos_url = auto_os ['probe_url' ].rstrip ('OK' )
49+ self .logger .info (f"the upos_url is:{ upos_url } " )
50+ query_params = dict (param .split ('=' ) for param in auto_os ['query' ].split ('&' ))
51+ cdn = query_params .get ('upcdn' )
52+ self .logger .info (f"the cdn is:{ cdn } " )
53+ probe_version = query_params .get ('probe_version' )
54+ self .logger .info (f"the probe_version is:{ probe_version } " )
55+ return upos_url , cdn , probe_version
56+
57+ def preupload (self , * , filename , filesize , cdn , probe_version ):
3258 """The preupload process to get `upos_uri` and `auth` information.
3359 Parameters
3460 ----------
@@ -60,8 +86,8 @@ def preupload(self, *, filename, filesize):
6086 'ssl' : 0 ,
6187 'version' : '2.8.9' ,
6288 'build' : '2080900' ,
63- 'upcdn' : 'bda2' ,
64- 'probe_version' : '20200810'
89+ 'upcdn' : cdn ,
90+ 'probe_version' : probe_version
6591 }
6692 res_json = self .session .get (
6793 url ,
@@ -73,7 +99,7 @@ def preupload(self, *, filename, filesize):
7399 # print(res_json)
74100 return res_json
75101
76- def get_upload_video_id (self , * , upos_uri , auth ):
102+ def get_upload_video_id (self , * , upos_uri , auth , upos_url ):
77103 """Get the `upload_id` of video.
78104
79105 Parameters
@@ -87,14 +113,14 @@ def get_upload_video_id(self, *, upos_uri, auth):
87113 - upload_id: str
88114 the id of the video to be uploaded
89115 """
90- url = f'https://upos-sz-upcdnbda2.bilivideo.com/ { upos_uri } ?uploads&output=json'
116+ url = f'https:{ upos_url } { upos_uri } ?uploads&output=json'
91117 res_json = self .session .post (url , headers = {'X-Upos-Auth' : auth }).json ()
92118 assert res_json ['OK' ] == 1
93119 self .logger .info ('Completed upload_id obtaining phase' )
94120 # print(res_json)
95121 return res_json
96122
97- def upload_video_in_chunks (self , * , upos_uri , auth , upload_id , fileio , filesize , chunk_size , chunks ):
123+ def upload_video_in_chunks (self , * , upos_uri , auth , upload_id , fileio , filesize , chunk_size , chunks , upos_url ):
98124 """Upload the video in chunks.
99125
100126 Parameters
@@ -114,7 +140,7 @@ def upload_video_in_chunks(self, *, upos_uri, auth, upload_id, fileio, filesize,
114140 - chunks: int
115141 the number of chunks to be uploaded
116142 """
117- url = f'https://upos-sz-upcdnbda2.bilivideo.com/ { upos_uri } '
143+ url = f'https:{ upos_url } { upos_uri } '
118144 params = {
119145 'partNumber' : None , # start from 1
120146 'uploadId' : upload_id ,
@@ -142,7 +168,7 @@ def upload_video_in_chunks(self, *, upos_uri, auth, upload_id, fileio, filesize,
142168 pbar .update (len (batchbytes ))
143169 # print(res)
144170
145- def finish_upload (self , * , upos_uri , auth , filename , upload_id , biz_id , chunks ):
171+ def finish_upload (self , * , upos_uri , auth , filename , upload_id , biz_id , chunks , upos_url ):
146172 """Notify the all chunks have been uploaded.
147173
148174 Parameters
@@ -160,7 +186,7 @@ def finish_upload(self, *, upos_uri, auth, filename, upload_id, biz_id, chunks):
160186 - chunks: int
161187 the number of chunks to be uploaded
162188 """
163- url = f'https://upos-sz-upcdnbda2.bilivideo.com/ { upos_uri } '
189+ url = f'https:{ upos_url } { upos_uri } '
164190 params = {
165191 'output' : 'json' ,
166192 'name' : filename ,
0 commit comments