From 9a2af39be783030b9c3e3b9daaaa208274a3edc6 Mon Sep 17 00:00:00 2001 From: VikingKong Date: Fri, 12 May 2023 00:41:33 +0300 Subject: [PATCH] Implement downloading pictures to local cache and opening them from there. --- App.py | 10 +++++++--- FileCache.py | 29 +++++++++++++++++++++++++++++ Utils.py | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 FileCache.py diff --git a/App.py b/App.py index c629f90..917fc0d 100644 --- a/App.py +++ b/App.py @@ -7,6 +7,7 @@ from concurrent.futures import ThreadPoolExecutor from API import Fetcher from Cache import Cache from Render import Render +from FileCache import FileCache import Utils warnings.filterwarnings("ignore") @@ -359,9 +360,12 @@ class Links(urwid.ListBox): self.body = walker def parseLink(self, link): - ext = link.split(".")[-1] - if Utils.checkPic(ext.lower()): - os.system('nohup feh ' + link + ' /dev/null 2>&1 &') + ext = Utils.checkPic(link.split(".")[-1]).lower() + if ext: + fc = FileCache(link, ext) + fp = fc.fetch() + if fp is not None: + os.system('nohup feh ' + fp + ' /dev/null 2>&1 &') elif Utils.checkStreamingVideo(link): tui.destroyOverlay() os.system( diff --git a/FileCache.py b/FileCache.py new file mode 100644 index 0000000..f06b3df --- /dev/null +++ b/FileCache.py @@ -0,0 +1,29 @@ +import httpx +import os +import Utils +import hashlib + + +class FileCache: + + def __init__(self, link, ext): + self.link = link + path = os.path.expanduser("~") + "/.config/inomnibus/file_cache/" + h = hashlib.sha1() + h.update(self.link.encode()) + self.file_path = path + h.hexdigest() + "." + ext + + def download(self): + try: + file = httpx.get(self.link, follow_redirects=True) + open(self.file_path, "wb").write(file.content) + return self.file_path + except BaseException as e: + Utils.writeLog(str(e)) + return None + + def fetch(self): + if os.path.isfile(self.file_path): + return self.file_path + else: + return self.download() diff --git a/Utils.py b/Utils.py index 0ebc9a2..0213017 100644 --- a/Utils.py +++ b/Utils.py @@ -12,7 +12,7 @@ def timestampToDate(ts): def checkPic(ext): for p in pics: if re.search(p, ext) is not None: - return True + return p[1:p.find("\\")] return False