@@ -355,16 +355,34 @@ def updateProgress(self, count, blockSize, totalSize)
355355 uri = URIS .uri (URIS .DOWNLOAD , self .id )
356356 return FSRequest .retrieve (uri , self .client , path , reporthook )
357357
358- def retrieve_preview (self , directory , name = None ):
358+ def retrieve_preview (self , directory , name = None , quality = 'lq' , file_format = 'mp3' ):
359359 """
360- Download the low quality mp3 preview.
360+ Download the sound preview.
361+ If no quality or file format is specified, preview_lq_mp3 is returned.
362+
363+ Parameters:
364+ directory (str): The directory where the sound preview will be downloaded.
365+ name (str, optional): The name of the downloaded sound preview file. If no file
366+ extension is specified or if it mismatches the chosen one in
367+ file_format, then the file_format is added as a file extension.
368+ quality (str, optional): The quality of the audio preview. Available values:
369+ 'lq' (low quality) or 'hq' (high quality).
370+ file_format (str, optional): The desired file format of the audio preview.
371+ Available values: 'mp3','ogg' (only!).
361372
362373 >>> sound.retrieve_preview("/tmp")
363374 """
375+ preview_type = 'preview_' + quality + '_' + file_format
376+ preview_attr = getattr (self .previews , preview_type )
364377 try :
365- path = Path (directory ,
366- name if name else self .previews .preview_lq_mp3 .split ("/" )[- 1 ],
367- )
378+ if name :
379+ if name .split ('.' )[- 1 ] != file_format :
380+ file_name = name + '.' + file_format
381+ else :
382+ file_name = name
383+ else :
384+ file_name = preview_attr .split ("/" )[- 1 ]
385+ path = Path (directory , file_name )
368386 except AttributeError as exc :
369387 raise FreesoundException (
370388 '-' ,
@@ -373,7 +391,7 @@ def retrieve_preview(self, directory, name=None):
373391 ' https://www.freesound.org/docs/api/resources_apiv2.html#response-sound-list.' # noqa
374392 ) from exc
375393 return FSRequest .retrieve (
376- self . previews . preview_lq_mp3 ,
394+ preview_attr ,
377395 self .client ,
378396 path
379397 )
0 commit comments