@@ -102,7 +102,9 @@ async def fetchToken(self):
102102 logger .info ("Fetched token." )
103103
104104 except aiohttp .ClientError as e :
105- logger .error (f"An error occured whilst fetching token, retrying in 5s: { e } ." )
105+ logger .error (
106+ f"An error occured whilst fetching token, retrying in 5s: { e } ."
107+ )
106108 await asyncio .sleep (5 )
107109 return self .fetchToken ()
108110
@@ -324,10 +326,12 @@ async def __call__(
324326 self .checked = True
325327 more_files = {storage : [] for storage in storages .get_storages ()}
326328 prefix_files = {
327- prefix : [] for prefix in (prefix .to_bytes (1 , "big" ).hex () for prefix in range (256 ))
329+ prefix : []
330+ for prefix in (prefix .to_bytes (1 , "big" ).hex () for prefix in range (256 ))
328331 }
329332 prefix_hash = {
330- prefix : [] for prefix in (prefix .to_bytes (1 , "big" ).hex () for prefix in range (256 ))
333+ prefix : []
334+ for prefix in (prefix .to_bytes (1 , "big" ).hex () for prefix in range (256 ))
331335 }
332336
333337 for file in files :
@@ -579,7 +583,15 @@ async def get_cache_stats(self) -> StatsCache:
579583
580584
581585class WebDav (Storage ):
582- def __init__ (self , username : str , password : str , hostname : str , endpoint : str , token : Optional [str ] = None , dav : str = "/dav" ) -> None :
586+ def __init__ (
587+ self ,
588+ username : str ,
589+ password : str ,
590+ hostname : str ,
591+ endpoint : str ,
592+ token : Optional [str ] = None ,
593+ dav : str = "/dav" ,
594+ ) -> None :
583595 self .username = username
584596 self .password = password
585597 self .hostname = hostname
@@ -591,31 +603,44 @@ def __init__(self, username: str, password: str, hostname: str, endpoint: str, t
591603 self .cache : dict [str , File ] = {}
592604 self .empty = File ("" , "" , 0 )
593605 Timer .delay (self ._list_all )
606+
594607 def _endpoint (self , file : str ):
595608 return f"{ self .endpoint } /{ file .removeprefix ('/' )} "
609+
596610 def _client (self ):
597- return webdav3_client .Client ({
598- "webdav_username" : self .username ,
599- "webdav_password" : self .password ,
600- "webdav_hostname" : self .hostname + self .dav ,
601- "webdav_token" : self .token
602- })
611+ return webdav3_client .Client (
612+ {
613+ "webdav_username" : self .username ,
614+ "webdav_password" : self .password ,
615+ "webdav_hostname" : self .hostname + self .dav ,
616+ "webdav_token" : self .token ,
617+ }
618+ )
619+
603620 async def get (self , file : str ) -> File :
604621 if file in self .cache and self .cache [file ].expiry - 10 > time .time ():
605622 self .cache [file ].cache = True
606623 self .cache [file ].last_hit = time .time ()
607624 return self .cache [file ]
608- async with aiohttp .ClientSession (self .hostname , auth = aiohttp .BasicAuth (self .username , self .password )) as session :
609- async with session .get (self .dav + self ._endpoint (file [:2 ] + "/" + file ), allow_redirects = False ) as resp :
610- f = File (self .dav + self ._endpoint (file [:2 ] + "/" + file ), file , size = int (resp .headers .get ("Content-Length" , 0 )))
625+ async with aiohttp .ClientSession (
626+ self .hostname , auth = aiohttp .BasicAuth (self .username , self .password )
627+ ) as session :
628+ async with session .get (
629+ self .dav + self ._endpoint (file [:2 ] + "/" + file ), allow_redirects = False
630+ ) as resp :
631+ f = File (
632+ self .dav + self ._endpoint (file [:2 ] + "/" + file ),
633+ file ,
634+ size = int (resp .headers .get ("Content-Length" , 0 )),
635+ )
611636 if resp .status == 200 :
612637 f .set_data (await resp .read ())
613638 elif resp .status // 100 == 3 :
614639 f .path = resp .headers .get ("Location" )
615640 self .cache [file ] = f
616641 return self .cache [file ]
617-
618- async def _list_all (self , force = False ):
642+
643+ async def _list_all (self , force = False ):
619644 if self .fetch and not force :
620645 return
621646 self .fetch = True
@@ -626,16 +651,27 @@ async def _list_all(self, force = False):
626651 await dashboard .set_status_by_tqdm ("正在获取 WebDav 文件列表中" , pbar )
627652 for dir in (await client .list (self .endpoint ))[1 :]:
628653 pbar .update (1 )
629- for file in (await client .list (self ._endpoint (dir ,), get_info = True ))[1 :]:
630- self .files [file ['name' ]] = File (file ['path' ].removeprefix (f"/dav/{ self .endpoint } /" ), file ['name' ], int (file ['size' ]))
654+ for file in (
655+ await client .list (
656+ self ._endpoint (
657+ dir ,
658+ ),
659+ get_info = True ,
660+ )
661+ )[1 :]:
662+ self .files [file ["name" ]] = File (
663+ file ["path" ].removeprefix (f"/dav/{ self .endpoint } /" ),
664+ file ["name" ],
665+ int (file ["size" ]),
666+ )
631667 await asyncio .sleep (0 )
632668 return self .files
669+
633670 async def exists (self , hash : str ) -> bool :
634671 if not self .fetch :
635672 self .fetch = True
636673 await self ._list_all ()
637674 return hash in self .files
638-
639675
640676 async def get_size (self , hash : str ) -> int :
641677 return self .files .get (hash , self .empty ).size
@@ -649,17 +685,20 @@ async def write(self, hash: str, io: io.BytesIO) -> int:
649685
650686 async def get_files (self , dir : str ) -> list [str ]:
651687 return list ((hash for hash in self .files .keys () if hash .startswith (dir )))
652-
688+
653689 async def get_hash (self , hash : str ) -> str :
654690 async with self ._client () as session :
655691 h = get_hash (hash )
656- async for data in await session .download_iter (self ._endpoint (f"{ hash [:2 ]} /{ hash } " )):
692+ async for data in await session .download_iter (
693+ self ._endpoint (f"{ hash [:2 ]} /{ hash } " )
694+ ):
657695 h .update (data )
658696 return h .hexdigest ()
659697
660-
661698 async def get_files_size (self , dir : str ) -> int :
662- return sum ((file .size for hash , file in self .files .items () if hash .startswith (dir )))
699+ return sum (
700+ (file .size for hash , file in self .files .items () if hash .startswith (dir ))
701+ )
663702
664703 async def removes (self , hashs : list [str ]) -> int :
665704 success = 0
@@ -669,10 +708,10 @@ async def removes(self, hashs: list[str]) -> int:
669708 success += 1
670709 return success
671710
672-
673711 async def get_cache_stats (self ) -> StatsCache :
674712 return StatsCache ()
675713
714+
676715class TypeStorage (Enum ):
677716 FILE = "file"
678717 WEBDAV = "webdav"
@@ -739,7 +778,6 @@ def is_enable(value):
739778 )
740779
741780
742-
743781class Cluster :
744782 def __init__ (self ) -> None :
745783 self .connected = False
@@ -752,17 +790,17 @@ def __init__(self) -> None:
752790 self .keepaliveTimer : Optional [Task ] = None
753791 self .keepaliveTimeoutTimer : Optional [Task ] = None
754792 self .keepalive_lock = asyncio .Lock ()
755-
793+
756794 def _message (self , message ):
757795 logger .info (f"[Remote] { message } " )
758796 if "信任度过低" in message :
759797 self .trusted = False
760-
798+
761799 async def emit (self , channel , data = None ):
762800 await self .sio .emit (
763801 channel , data , callback = lambda x : Timer .delay (self .message , (channel , x ))
764802 )
765-
803+
766804 async def init (self ):
767805 if not self .sio .connected :
768806 try :
@@ -777,29 +815,34 @@ async def init(self):
777815 return
778816 await self .start ()
779817
780-
781818 async def start (self ):
782819 await self .cert ()
783820 await self .file_check ()
784821 await self .enable ()
822+
785823 async def cert (self ):
786824 await self .emit ("request-cert" )
825+
787826 async def enable (self ):
788827 if self .connected :
789- logger .debug ("Still trying to enable cluster? You has been blocked. (\n From bangbang93:\n 谁他妈\n 一秒钟发了好几百个enable请求\n ban了解一下等我回杭州再看\n ban了先\n \n > Timestamp at 2024/3/30 14:07 GMT+8\n )" )
828+ logger .debug (
829+ "Still trying to enable cluster? You has been blocked. (\n From bangbang93:\n 谁他妈\n 一秒钟发了好几百个enable请求\n ban了解一下等我回杭州再看\n ban了先\n \n > Timestamp at 2024/3/30 14:07 GMT+8\n )"
830+ )
790831 return
791832 self .connected = True
792833 if self ._enable_timer != None :
793834 self ._enable_timer .block ()
794835 self ._enable_timer = Timer .delay (self .reconnect , (), 30 )
795836 await self ._enable ()
837+
796838 async def reconnect (self ):
797839 if self .connected :
798840 await self .disable ()
799841 self .connected = False
800842 logger .info ("Retrying after 5s." )
801843 await asyncio .sleep (5 )
802844 await self .enable ()
845+
803846 async def _enable (self ):
804847 storage_str = {"file" : 0 , "webdav" : 0 }
805848 self .trusted = True
@@ -825,7 +868,7 @@ async def _enable(self):
825868 },
826869 )
827870 await dashboard .set_status ("巡检中" )
828-
871+
829872 async def message (self , type , data : list [Any ]):
830873 if len (data ) == 1 :
831874 data .append (None )
@@ -875,7 +918,7 @@ async def message(self, type, data: list[Any]):
875918 self .keepalive_lock .release ()
876919 if type != "request-cert" :
877920 logger .debug (type , data )
878-
921+
879922 async def start_keepalive (self , delay = 0 ):
880923 if self .keepaliveTimer :
881924 self .keepaliveTimer .block ()
@@ -934,6 +977,7 @@ async def get_cache_stats(self) -> StatsCache:
934977github_api = "https://api.github.com"
935978download_url = ""
936979
980+
937981async def check_update ():
938982 global fetched_version
939983 fetched_version = "Unknown"
@@ -955,6 +999,7 @@ async def check_update():
955999 logger .error (f"An error occured whilst checking update: { e } ." )
9561000 Timer .delay (check_update , (), 3600 )
9571001
1002+
9581003async def init ():
9591004 await check_update ()
9601005 global cluster
0 commit comments