11from future .utils import PY2
22import os
3- import xbmc ,xbmcaddon
3+ import xbmc , xbmcaddon
44import xbmcvfs
55
66from datetime import datetime , timedelta
7- #split the art library to not have import problems, as the function is used by
8- #service and the main plugin
9- #library used both by service and main plugin, DO NOT INCLUDE OTHER LOCAL
10- #LIBRARIES
7+ # split the art library to not have import problems, as the function is used by
8+ # service and the main plugin
9+ # library used both by service and main plugin, DO NOT INCLUDE OTHER LOCAL
10+ # LIBRARIES
1111
1212ampache = xbmcaddon .Addon ("plugin.audio.ampache" )
1313
1414# Cache configuration constants
1515ART_CACHE_EXPIRY_DAYS = 30
1616ART_CACHE_TYPES = ["album" , "artist" , "song" , "podcast" , "playlist" ]
1717
18- #different functions in kodi 19 (python3) and kodi 18 (python2)
18+ # different functions in kodi 19 (python3) and kodi 18 (python2)
1919if PY2 :
20- user_dir = xbmc .translatePath ( ampache .getAddonInfo (' profile' ))
21- user_dir = user_dir .decode (' utf-8' )
20+ user_dir = xbmc .translatePath (ampache .getAddonInfo (" profile" ))
21+ user_dir = user_dir .decode (" utf-8" )
2222else :
23- user_dir = xbmcvfs .translatePath ( ampache .getAddonInfo ('profile' ))
24- user_mediaDir = os .path .join ( user_dir , 'media' )
25- cacheDir = os .path .join ( user_mediaDir , 'cache' )
23+ user_dir = xbmcvfs .translatePath (ampache .getAddonInfo ("profile" ))
24+ user_mediaDir = os .path .join (user_dir , "media" )
25+ cacheDir = os .path .join (user_mediaDir , "cache" )
26+
2627
2728def clean_settings ():
2829 ampache .setSetting ("session_expire" , "" )
@@ -37,9 +38,10 @@ def clean_settings():
3738 ampache .setSetting ("podcasts" , "" )
3839 ampache .setSetting ("live_streams" , "" )
3940
40- #hack to force the creation of profile directory if don't exists
41+ # hack to force the creation of profile directory if don't exists
4142 if not os .path .isdir (user_dir ):
42- ampache .setSetting ("api-version" ,"350001" )
43+ ampache .setSetting ("api-version" , "350001" )
44+
4345
4446def is_expired (cache_file_path : str ) -> bool :
4547 """Check if the cache file has expired (older than one month)."""
@@ -52,42 +54,55 @@ def is_expired(cache_file_path: str) -> bool:
5254 last_modified = datetime .fromtimestamp (mod_time )
5355
5456 # Calculate if more than a month has passed since modification
55- expiration_duration = timedelta (days = ART_CACHE_EXPIRY_DAYS ) # ART_CACHE_EXPIRY_DAYS
57+ expiration_duration = timedelta (
58+ days = ART_CACHE_EXPIRY_DAYS
59+ ) # ART_CACHE_EXPIRY_DAYS
5660
5761 return (now - last_modified ) > expiration_duration
5862 except FileNotFoundError :
5963 return True # Treat missing files as expired
6064
65+
6166def delete_expired_files ():
6267
6368 for c_type in ART_CACHE_TYPES :
64- cacheDirType = os .path .join ( cacheDir , c_type )
69+ cacheDirType = os .path .join (cacheDir , c_type )
6570 if not os .path .isdir (cacheDirType ):
6671 continue
6772 for currentFile in os .listdir (cacheDirType ):
68- #xbmc.log("Clear Cache Art " + str(currentFile),xbmc.LOGDEBUG)
69- pathDel = os .path .join ( cacheDirType , currentFile )
73+ # xbmc.log("Clear Cache Art " + str(currentFile),xbmc.LOGDEBUG)
74+ pathDel = os .path .join (cacheDirType , currentFile )
7075 if is_expired (pathDel ):
7176 try :
7277 os .remove (pathDel )
7378 except OSError as e :
74- raise
79+ xbmc .log (
80+ "AmpachePlugin::art_clean: Failed to delete file %s: %s"
81+ % (pathDel , repr (e )),
82+ xbmc .LOGDEBUG ,
83+ )
84+ continue
85+
7586
7687def remove_expired ():
7788 try :
7889 print ("Starting cache cleanup..." )
7990 delete_expired_files ()
8091 print ("Cache cleanup completed." )
8192 except Exception as e :
82- xbmc .log ("AmpachePlugin::Service failed to cleanup cache: %s" % repr (e ), xbmc .LOGERROR )
93+ xbmc .log (
94+ "AmpachePlugin::Service failed to cleanup cache: %s" % repr (e ),
95+ xbmc .LOGERROR ,
96+ )
97+
8398
8499def init_cache ():
85- #if cacheDir doesn't exist, create it
100+ # if cacheDir doesn't exist, create it
86101 if not os .path .isdir (user_mediaDir ):
87102 os .mkdir (user_mediaDir )
88103 if not os .path .isdir (cacheDir ):
89104 os .mkdir (cacheDir )
90105 for c_type in ART_CACHE_TYPES :
91- cacheDirType = os .path .join ( cacheDir , c_type )
106+ cacheDirType = os .path .join (cacheDir , c_type )
92107 if not os .path .isdir (cacheDirType ):
93- os .mkdir ( cacheDirType )
108+ os .mkdir (cacheDirType )
0 commit comments