3333 get_hash ,
3434)
3535
36- VERSION = ''
36+ VERSION = ""
3737version_path = Path ("VERSION" )
3838if version_path .exists ():
39- with open (Path ("VERSION" ), 'r' , encoding = ' utf-8' ) as f :
39+ with open (Path ("VERSION" ), "r" , encoding = " utf-8" ) as f :
4040 VERSION = f .read ()
4141 f .close ()
4242else :
@@ -512,9 +512,7 @@ async def check_files(self):
512512 if not miss :
513513 logger .success (f"Checked all files: { len (files ) * len (self .storages )} !" )
514514 else :
515- logger .info (
516- f"File missing: { unit .format_number (len (miss ))} ."
517- )
515+ logger .info (f"File missing: { unit .format_number (len (miss ))} ." )
518516 await downloader .download (self .storages , list (miss ))
519517 if os .path .exists ("./cache/download" ):
520518 paths = []
@@ -565,6 +563,7 @@ async def start(
565563 await self .check_files ()
566564 await dashboard .set_status ("启动服务" )
567565 await self .enable ()
566+
568567 async def get (self , hash , offset : int = 0 ) -> File :
569568 storage = self .storages [0 ]
570569 stat = self .storage_stats [storage ]
@@ -606,6 +605,7 @@ async def enable(self) -> None:
606605 },
607606 )
608607 await dashboard .set_status ("巡检中" )
608+
609609 async def message (self , type , data : list [Any ]):
610610 if len (data ) == 1 :
611611 data .append (None )
@@ -629,7 +629,9 @@ async def message(self, type, data: list[Any]):
629629 f"Hosting on { CLUSTER_ID } .openbmclapi.933.moe:{ PUBLIC_PORT or PORT } ."
630630 )
631631 await self .start_keepalive ()
632- await dashboard .set_status ("正常工作" + ("" if self .trusted else "(节点信任度过低)" ))
632+ await dashboard .set_status (
633+ "正常工作" + ("" if self .trusted else "(节点信任度过低)" )
634+ )
633635 elif type == "keep-alive" :
634636 if err :
635637 logger .error (f"Unable to keep alive! Reconnecting..." )
@@ -718,21 +720,22 @@ async def disable(self):
718720cluster : Optional [Cluster ] = None
719721last_status : str = "-"
720722github_api = "https://api.github.com"
723+
724+
721725async def check_update ():
722- async with aiohttp .ClientSession (
723- base_url = github_api
724- ) as session :
725- logger .info ("Checking update..." )
726- try :
727- async with session .get ("/repos/TTB-Network/python-openbmclapi/tags" ) as req :
728- req .raise_for_status ()
729- fetched_version : str = (await req .json ())[0 ]["name" ]
730- if fetched_version != VERSION :
731- logger .success (f"New version found: { fetched_version } !" )
732- else :
733- logger .info (f"Already up to date." )
734- except aiohttp .ClientError as e :
735- logger .error (f"An error occured whilst checking update: { e } ." )
726+ async with aiohttp .ClientSession (base_url = github_api ) as session :
727+ logger .info ("Checking update..." )
728+ try :
729+ async with session .get ("/repos/TTB-Network/python-openbmclapi/tags" ) as req :
730+ req .raise_for_status ()
731+ fetched_version : str = (await req .json ())[0 ]["name" ]
732+ if fetched_version != VERSION :
733+ logger .success (f"New version found: { fetched_version } !" )
734+ else :
735+ logger .info (f"Already up to date." )
736+ except aiohttp .ClientError as e :
737+ logger .error (f"An error occured whilst checking update: { e } ." )
738+
736739
737740async def init ():
738741 await check_update ()
@@ -778,8 +781,10 @@ async def _(request: web.Request, hash: str):
778781 if not await cluster .exists (hash ):
779782 return web .Response (status_code = 404 )
780783 start_bytes = 0
781- range_str = await request .get_headers ('range' , '' )
782- range_match = re .search (r'bytes=(\d+)-(\d+)' , range_str , re .S ) or re .search (r'bytes=(\d+)-' , range_str , re .S )
784+ range_str = await request .get_headers ("range" , "" )
785+ range_match = re .search (r"bytes=(\d+)-(\d+)" , range_str , re .S ) or re .search (
786+ r"bytes=(\d+)-" , range_str , re .S
787+ )
783788 if range_match :
784789 start_bytes = int (range_match .group (1 )) if range_match else 0
785790 data = await cluster .get (hash , start_bytes )
@@ -797,7 +802,9 @@ async def _(request: web.Request, ws: web.WebSocket):
797802 auth = False
798803 for cookie in await request .get_cookies ():
799804 if cookie .name == "auth" and dashboard .token_isvaild (cookie .value ):
800- await ws .send (dashboard .to_bytes ("auth" , DASHBOARD_USERNAME ).io .getbuffer ())
805+ await ws .send (
806+ dashboard .to_bytes ("auth" , DASHBOARD_USERNAME ).io .getbuffer ()
807+ )
801808 auth = True
802809 break
803810 if not auth :
@@ -810,18 +817,29 @@ async def _(request: web.Request, ws: web.WebSocket):
810817 input = utils .DataInputStream (raw_data )
811818 type = input .readString ()
812819 data = dashboard .deserialize (input )
813- await ws .send (dashboard .to_bytes (type , await dashboard .process (type , data )).io .getbuffer ())
820+ await ws .send (
821+ dashboard .to_bytes (
822+ type , await dashboard .process (type , data )
823+ ).io .getbuffer ()
824+ )
825+
814826 @router .get ("/auth" )
815827 async def _ (request : web .Request ):
816828 auth = (await request .get_headers ("Authorization" )).split (" " , 1 )[1 ]
817829 try :
818830 info = json .loads (base64 .b64decode (auth ))
819831 except :
820832 return web .Response (status_code = 401 )
821- if info ["username" ] != DASHBOARD_USERNAME or info ["password" ] != DASHBOARD_PASSWORD :
833+ if (
834+ info ["username" ] != DASHBOARD_USERNAME
835+ or info ["password" ] != DASHBOARD_PASSWORD
836+ ):
822837 return web .Response (status_code = 401 )
823838 token = dashboard .generate_token (request )
824- return web .Response (DASHBOARD_USERNAME , cookies = [web .Cookie ("auth" , token .value , expires = int (time .time () + 86400 ))])
839+ return web .Response (
840+ DASHBOARD_USERNAME ,
841+ cookies = [web .Cookie ("auth" , token .value , expires = int (time .time () + 86400 ))],
842+ )
825843
826844 app .mount (router )
827845 app .redirect ("/bmcl" , "/bmcl/" )
0 commit comments