88import traceback
99import platform
1010from datetime import datetime , timezone
11-
11+ import mimetypes
1212import aiofiles
1313import httpx
1414from tqdm import tqdm
@@ -298,7 +298,7 @@ def checkLaterFiles(self):
298298 return
299299 print (f"Start to download { len (self .laterInfo )} file(s)!" )
300300 laterFiles = []
301- for ( fileUrl , path ) in self .laterFiles :
301+ for fileUrl , path in self .laterFiles :
302302 localCreatedTimeStamp = int (os .path .getctime (path ))
303303 try :
304304 newPath = os .path .join (
@@ -319,6 +319,42 @@ def checkLaterFiles(self):
319319 print (f"{ e .__class__ .__name__ } ! Skipped: { path } " )
320320 self .laterFiles = laterFiles
321321
322+ def checkAllowDownload (self , filename ):
323+ fileType = (mimetypes .guess_type (filename ))[0 ]
324+ if fileType is None :
325+ return True
326+ if not self .config ["allowAudio" ]:
327+ if fileType .split ("/" )[0 ] == "audio" :
328+ print (
329+ f"Remove { filename } from the download list because of its file type: audio."
330+ )
331+ return False
332+ if not self .config ["allowVideo" ]:
333+ if fileType .split ("/" )[0 ] == "video" :
334+ print (
335+ f"Remove { filename } the download list because of its file type: video."
336+ )
337+ return False
338+ if not self .config ["allowImage" ]:
339+ if fileType .split ("/" )[0 ] == "image" :
340+ print (
341+ f"Remove { filename } the download list because of its file type: image."
342+ )
343+ return False
344+ return True
345+
346+ def checkFilesType (self ):
347+ self .laterFiles = [
348+ (fileUrl , path )
349+ for (fileUrl , path ) in self .laterFiles
350+ if self .checkAllowDownload (path )
351+ ]
352+ self .newFiles = [
353+ (fileUrl , path )
354+ for (fileUrl , path ) in self .newFiles
355+ if self .checkAllowDownload (path )
356+ ]
357+
322358 async def sync (self ):
323359 print ("Getting course IDs..." )
324360 await self .getCourseID ()
@@ -335,6 +371,7 @@ async def sync(self):
335371 return print ("All local files are synced!" )
336372 self .checkNewFiles ()
337373 self .checkLaterFiles ()
374+ self .checkFilesType ()
338375 await self .client .downloadMany (
339376 self .newFiles + self .laterFiles , self .downloadSize + self .laterDownloadSize
340377 )
@@ -362,7 +399,9 @@ def promptConfigStr(promptStr, key, *, defaultValOnMissing=None):
362399 else :
363400 defaultValOnRemove = ""
364401 tipStr = f"(Default: { defaultVal } )" if defaultVal else ""
365- tipRemove = "(If you input remove, value will become " + (f"{ defaultValOnRemove } )" if defaultValOnRemove != "" else "empty)" )
402+ tipRemove = "(If you input remove, value will become " + (
403+ f"{ defaultValOnRemove } )" if defaultValOnRemove != "" else "empty)"
404+ )
366405 res = input (f"{ promptStr } { tipStr } { tipRemove } : " ).strip ()
367406 if not res :
368407 res = defaultVal
@@ -391,17 +430,33 @@ def promptConfigStr(promptStr, key, *, defaultValOnMissing=None):
391430 filesizeThreshStr = promptConfigStr (
392431 "Maximum file size to download(MB)" , "filesizeThresh" , defaultValOnMissing = 250
393432 )
433+ allowAudio = promptConfigStr (
434+ "Whether allow downloading audios" , "allowAudio" , defaultValOnMissing = True
435+ )
436+ allowVideo = promptConfigStr (
437+ "Whether allow downloading videos" , "allowVideo" , defaultValOnMissing = True
438+ )
439+ allowImage = promptConfigStr (
440+ "Whether allow downloading images" , "allowImage" , defaultValOnMissing = True
441+ )
442+
394443 try :
395444 filesizeThresh = float (filesizeThreshStr )
396445 except Exception :
397446 filesizeThresh = 250
447+ allowAudio = (allowAudio == "True" ) or (allowAudio == "true" )
448+ allowVideo = (allowVideo == "True" ) or (allowVideo == "true" )
449+ allowImage = (allowImage == "True" ) or (allowImage == "true" )
398450 return {
399451 "canvasURL" : url ,
400452 "token" : token ,
401453 "courseCodes" : courseCodes ,
402454 "courseIDs" : courseIDs ,
403455 "downloadDir" : downloadDir ,
404456 "filesizeThresh" : filesizeThresh ,
457+ "allowAudio" : allowAudio ,
458+ "allowVideo" : allowVideo ,
459+ "allowImage" : allowImage ,
405460 }
406461
407462
@@ -457,6 +512,13 @@ def getConfig():
457512 config ["connection_count" ] = args .connection
458513 config ["no_keep_older_version" ] = args .no_keep_older_version
459514 config ["debug" ] = args .debug
515+ if not "allowAudio" in config :
516+ config ["allowAudio" ] = True
517+ if not "allowVideo" in config :
518+ config ["allowVideo" ] = True
519+ if not "allowImage" in config :
520+ config ["allowImage" ] = True
521+
460522 return config
461523
462524
0 commit comments