blawar / nut

GNU General Public License v3.0
1.09k stars 189 forks source link

[Feature] Allow custom icon images / Fix empty image squares for titles #442

Open gritstub opened 1 year ago

gritstub commented 1 year ago

Is your feature request related to a problem? Please describe. If a file doesn't have an iconUrl property in titledb, it will show up as a blank square

Describe the solution you'd like Allow the user to override this with a local file. Possibly check for a cached local file first, otherwise download a new version of the image.

gritstub commented 1 year ago

Here is a quick workaround that I made. It might be better to store an override image in a different folder, in case the user wants to clear their image cache at some time.


diff --git a/nut/Title.py b/nut/Title.py
index d4fbcaa..43c4c1f 100644
--- a/nut/Title.py
+++ b/nut/Title.py
@@ -619,7 +619,7 @@ class Title:

        def download(self, base, fileName, url):
                path = os.path.join(base, fileName)
-               if os.path.isfile(path):
+               if os.path.isfile(path) or url is None:
                        return path
                os.makedirs(base, exist_ok=True)
                urllib.request.urlretrieve(url, path)
@@ -664,10 +664,13 @@ class Title:
                if 'iconUrl' not in self.__dict__:
                        self.iconUrl = None

-               if not self.iconUrl or self.iconUrl.startswith('cocoon:/'):
+               if not self.iconUrl:
+                       baseName = self.id
+                       ext = ".jpg"
+               elif self.iconUrl.startswith('cocoon:/'):
                        return None
-
-               baseName, ext = os.path.splitext(self.iconUrl)
+               else:
+                       baseName, ext = os.path.splitext(self.iconUrl)
                return self.getResizedImage(self.download(Config.paths.titleImages + self.id, 'icon' + ext, self.iconUrl), width, height)

        def screenshotFile(self, i, width=None, height=None):```