diff --git a/README.md b/README.md index 57303ad..67211ea 100644 --- a/README.md +++ b/README.md @@ -8,5 +8,4 @@ Need python 3 diff --git a/bin/pytop-0-0-1-x64.deb b/bin/pytop-0-0-1-x64.deb index c21dd68..ae01d3c 100644 Binary files a/bin/pytop-0-0-1-x64.deb and b/bin/pytop-0-0-1-x64.deb differ diff --git a/src/debs/pytop-0-0-1-x64/opt/Pytop/utils/Icon.py b/src/debs/pytop-0-0-1-x64/opt/Pytop/utils/Icon.py index 4130e4a..0979247 100644 --- a/src/debs/pytop-0-0-1-x64/opt/Pytop/utils/Icon.py +++ b/src/debs/pytop-0-0-1-x64/opt/Pytop/utils/Icon.py @@ -7,11 +7,16 @@ gi.require_version('Gdk', '3.0') from gi.repository import Gtk as gtk from gi.repository import Gio as gio from gi.repository import GdkPixbuf +from xdg.DesktopEntry import DesktopEntry +# Python Imports import os, subprocess, hashlib, threading +import urllib.request as urllib + from os.path import isdir, isfile, join + def threaded(fn): def wrapper(*args, **kwargs): threading.Thread(target=fn, args=args, kwargs=kwargs).start() @@ -28,6 +33,10 @@ class Icon: self.systemIconImageWxH = settings.returnSystemIconImageWH() self.viIconWxH = settings.returnVIIconWH() + user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3' + self.headers = { 'User-Agent' : user_agent } + + def createIcon(self, dir, file): fullPath = dir + "/" + file thumbnl = self.getIconImage(file, fullPath) @@ -36,10 +45,10 @@ class Icon: def getIconImage(self, file, fullPath): thumbnl = gtk.Image() vidsList = ('.mkv', '.avi', '.flv', '.mov', '.m4v', '.mpg', '.wmv', '.mpeg', '.mp4', '.webm') - imagesList = ('.png', '.jpg', '.jpeg', '.gif') + imagesList = ('.png', '.jpg', '.jpeg', '.gif', '.ico', '.tga') try: - if file.lower().endswith(vidsList): + if file.lower().endswith(vidsList): # Video thumbnail fileHash = hashlib.sha256(str.encode(fullPath)).hexdigest() hashImgpth = self.usrHome + "/.thumbnails/normal/" + fileHash + ".png" @@ -47,11 +56,12 @@ class Icon: self.generateVideoThumbnail(fullPath, hashImgpth) thumbnl = self.createIconImageBuffer(hashImgpth, self.viIconWxH) - elif file.lower().endswith(imagesList): + elif file.lower().endswith(imagesList): # Image Icon thumbnl = self.createIconImageBuffer(fullPath, self.viIconWxH) - else: + else: # System icons thumbnl = self.nonImageOrVideoIcon(fullPath) except Exception as e: + print(e) return gtk.Image.new_from_file("resources/icons/bin.png") if thumbnl == None: # If no system icon, try stock file icon... @@ -62,21 +72,73 @@ class Icon: return thumbnl def nonImageOrVideoIcon(self, fullPath): - thumbPth = self.getSystemThumbnail(fullPath, self.systemIconImageWxH[0]) - return self.createIconImageBuffer(thumbPth, self.systemIconImageWxH) + if fullPath.lower().endswith( ('.desktop',) ): + return self.parseDesktopFiles(fullPath) + else: + thumbPth = self.getSystemThumbnail(fullPath, self.systemIconImageWxH[0]) + return self.createIconImageBuffer(thumbPth, self.systemIconImageWxH) def createIconImageBuffer(self, path, wxh): - pixbuf = None try: - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( - filename = path, - width = wxh[0], - height = wxh[1], - preserve_aspect_ratio = False) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, wxh[0], wxh[1], False) return gtk.Image.new_from_pixbuf(pixbuf) except Exception as e: return gtk.Image.new_from_file("resources/icons/bin.png") + def parseDesktopFiles(self, fullPath): + xdgObj = DesktopEntry(fullPath) + icon = xdgObj.getIcon() + iconsDirs = "/usr/share/icons" + altIconPath = "" + + if "steam" in icon: # This whole thing is iffy at best but I want my damn icons!! + steamIconsDir = self.usrHome + "/.thumbnails/steam_icons/" + name = xdgObj.getName() + fileHash = hashlib.sha256(str.encode(name)).hexdigest() + + if isdir(steamIconsDir) == False: + os.mkdir(steamIconsDir) + + hashImgpth = steamIconsDir + fileHash + ".jpg" + if isfile(hashImgpth) == True: + return self.createIconImageBuffer(hashImgpth, self.systemIconImageWxH) + + execStr = xdgObj.getExec() + parts = execStr.split("steam://rungameid/") + id = parts[len(parts) - 1] + + url = "https://steamdb.info/app/" + id + "/" + request = urllib.Request(url, None, self.headers) + response = urllib.urlopen(request) + page = response.read().decode("utf8") + response.close() # its always safe to close an open connection + imageHTML = "" + imageLink = "" + + for line in page.split("\n"): + if "app-icon avatar" in line: + imageHTML = line.strip() + break + + srcPart = imageHTML.split() + srcPart = srcPart[3].split("\"") + imageLink = srcPart[1] + proc = subprocess.Popen(["wget", "-O", hashImgpth, imageLink]) + proc.wait() + + return self.createIconImageBuffer(hashImgpth, self.systemIconImageWxH) + elif os.path.exists(icon): + return self.createIconImageBuffer(icon, self.systemIconImageWxH) + else: + for (dirpath, dirnames, filenames) in os.walk(iconsDirs): + for file in filenames: + appNM = "application-x-" + icon + if appNM in file: + altIconPath = dirpath + "/" + file + break + + return self.createIconImageBuffer(altIconPath, self.systemIconImageWxH) + def getSystemThumbnail(self, filename, size): final_filename = "" if os.path.exists(filename): diff --git a/src/versions/pytop-0.0.1/Pytop/utils/Icon.py b/src/versions/pytop-0.0.1/Pytop/utils/Icon.py index 4130e4a..0979247 100644 --- a/src/versions/pytop-0.0.1/Pytop/utils/Icon.py +++ b/src/versions/pytop-0.0.1/Pytop/utils/Icon.py @@ -7,11 +7,16 @@ gi.require_version('Gdk', '3.0') from gi.repository import Gtk as gtk from gi.repository import Gio as gio from gi.repository import GdkPixbuf +from xdg.DesktopEntry import DesktopEntry +# Python Imports import os, subprocess, hashlib, threading +import urllib.request as urllib + from os.path import isdir, isfile, join + def threaded(fn): def wrapper(*args, **kwargs): threading.Thread(target=fn, args=args, kwargs=kwargs).start() @@ -28,6 +33,10 @@ class Icon: self.systemIconImageWxH = settings.returnSystemIconImageWH() self.viIconWxH = settings.returnVIIconWH() + user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3' + self.headers = { 'User-Agent' : user_agent } + + def createIcon(self, dir, file): fullPath = dir + "/" + file thumbnl = self.getIconImage(file, fullPath) @@ -36,10 +45,10 @@ class Icon: def getIconImage(self, file, fullPath): thumbnl = gtk.Image() vidsList = ('.mkv', '.avi', '.flv', '.mov', '.m4v', '.mpg', '.wmv', '.mpeg', '.mp4', '.webm') - imagesList = ('.png', '.jpg', '.jpeg', '.gif') + imagesList = ('.png', '.jpg', '.jpeg', '.gif', '.ico', '.tga') try: - if file.lower().endswith(vidsList): + if file.lower().endswith(vidsList): # Video thumbnail fileHash = hashlib.sha256(str.encode(fullPath)).hexdigest() hashImgpth = self.usrHome + "/.thumbnails/normal/" + fileHash + ".png" @@ -47,11 +56,12 @@ class Icon: self.generateVideoThumbnail(fullPath, hashImgpth) thumbnl = self.createIconImageBuffer(hashImgpth, self.viIconWxH) - elif file.lower().endswith(imagesList): + elif file.lower().endswith(imagesList): # Image Icon thumbnl = self.createIconImageBuffer(fullPath, self.viIconWxH) - else: + else: # System icons thumbnl = self.nonImageOrVideoIcon(fullPath) except Exception as e: + print(e) return gtk.Image.new_from_file("resources/icons/bin.png") if thumbnl == None: # If no system icon, try stock file icon... @@ -62,21 +72,73 @@ class Icon: return thumbnl def nonImageOrVideoIcon(self, fullPath): - thumbPth = self.getSystemThumbnail(fullPath, self.systemIconImageWxH[0]) - return self.createIconImageBuffer(thumbPth, self.systemIconImageWxH) + if fullPath.lower().endswith( ('.desktop',) ): + return self.parseDesktopFiles(fullPath) + else: + thumbPth = self.getSystemThumbnail(fullPath, self.systemIconImageWxH[0]) + return self.createIconImageBuffer(thumbPth, self.systemIconImageWxH) def createIconImageBuffer(self, path, wxh): - pixbuf = None try: - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( - filename = path, - width = wxh[0], - height = wxh[1], - preserve_aspect_ratio = False) + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, wxh[0], wxh[1], False) return gtk.Image.new_from_pixbuf(pixbuf) except Exception as e: return gtk.Image.new_from_file("resources/icons/bin.png") + def parseDesktopFiles(self, fullPath): + xdgObj = DesktopEntry(fullPath) + icon = xdgObj.getIcon() + iconsDirs = "/usr/share/icons" + altIconPath = "" + + if "steam" in icon: # This whole thing is iffy at best but I want my damn icons!! + steamIconsDir = self.usrHome + "/.thumbnails/steam_icons/" + name = xdgObj.getName() + fileHash = hashlib.sha256(str.encode(name)).hexdigest() + + if isdir(steamIconsDir) == False: + os.mkdir(steamIconsDir) + + hashImgpth = steamIconsDir + fileHash + ".jpg" + if isfile(hashImgpth) == True: + return self.createIconImageBuffer(hashImgpth, self.systemIconImageWxH) + + execStr = xdgObj.getExec() + parts = execStr.split("steam://rungameid/") + id = parts[len(parts) - 1] + + url = "https://steamdb.info/app/" + id + "/" + request = urllib.Request(url, None, self.headers) + response = urllib.urlopen(request) + page = response.read().decode("utf8") + response.close() # its always safe to close an open connection + imageHTML = "" + imageLink = "" + + for line in page.split("\n"): + if "app-icon avatar" in line: + imageHTML = line.strip() + break + + srcPart = imageHTML.split() + srcPart = srcPart[3].split("\"") + imageLink = srcPart[1] + proc = subprocess.Popen(["wget", "-O", hashImgpth, imageLink]) + proc.wait() + + return self.createIconImageBuffer(hashImgpth, self.systemIconImageWxH) + elif os.path.exists(icon): + return self.createIconImageBuffer(icon, self.systemIconImageWxH) + else: + for (dirpath, dirnames, filenames) in os.walk(iconsDirs): + for file in filenames: + appNM = "application-x-" + icon + if appNM in file: + altIconPath = dirpath + "/" + file + break + + return self.createIconImageBuffer(altIconPath, self.systemIconImageWxH) + def getSystemThumbnail(self, filename, size): final_filename = "" if os.path.exists(filename):