Added quazi ok steam icon detection

This commit is contained in:
Maxim Stewart 2019-06-17 01:38:45 -05:00
parent a4c98450f3
commit f5464cd070
4 changed files with 148 additions and 25 deletions

View File

@ -8,5 +8,4 @@ Need python 3
<ul> <ul>
<li>Attach copy, cut, and paste signals to the controls menu.</li> <li>Attach copy, cut, and paste signals to the controls menu.</li>
<li>Add a settings file to store values.</li> <li>Add a settings file to store values.</li>
<li>Improve icon detection for Steam.</li>
</ul> </ul>

Binary file not shown.

View File

@ -7,11 +7,16 @@ gi.require_version('Gdk', '3.0')
from gi.repository import Gtk as gtk from gi.repository import Gtk as gtk
from gi.repository import Gio as gio from gi.repository import Gio as gio
from gi.repository import GdkPixbuf from gi.repository import GdkPixbuf
from xdg.DesktopEntry import DesktopEntry
# Python Imports
import os, subprocess, hashlib, threading import os, subprocess, hashlib, threading
import urllib.request as urllib
from os.path import isdir, isfile, join from os.path import isdir, isfile, join
def threaded(fn): def threaded(fn):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs).start() threading.Thread(target=fn, args=args, kwargs=kwargs).start()
@ -28,6 +33,10 @@ class Icon:
self.systemIconImageWxH = settings.returnSystemIconImageWH() self.systemIconImageWxH = settings.returnSystemIconImageWH()
self.viIconWxH = settings.returnVIIconWH() 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): def createIcon(self, dir, file):
fullPath = dir + "/" + file fullPath = dir + "/" + file
thumbnl = self.getIconImage(file, fullPath) thumbnl = self.getIconImage(file, fullPath)
@ -36,10 +45,10 @@ class Icon:
def getIconImage(self, file, fullPath): def getIconImage(self, file, fullPath):
thumbnl = gtk.Image() thumbnl = gtk.Image()
vidsList = ('.mkv', '.avi', '.flv', '.mov', '.m4v', '.mpg', '.wmv', '.mpeg', '.mp4', '.webm') vidsList = ('.mkv', '.avi', '.flv', '.mov', '.m4v', '.mpg', '.wmv', '.mpeg', '.mp4', '.webm')
imagesList = ('.png', '.jpg', '.jpeg', '.gif') imagesList = ('.png', '.jpg', '.jpeg', '.gif', '.ico', '.tga')
try: try:
if file.lower().endswith(vidsList): if file.lower().endswith(vidsList): # Video thumbnail
fileHash = hashlib.sha256(str.encode(fullPath)).hexdigest() fileHash = hashlib.sha256(str.encode(fullPath)).hexdigest()
hashImgpth = self.usrHome + "/.thumbnails/normal/" + fileHash + ".png" hashImgpth = self.usrHome + "/.thumbnails/normal/" + fileHash + ".png"
@ -47,11 +56,12 @@ class Icon:
self.generateVideoThumbnail(fullPath, hashImgpth) self.generateVideoThumbnail(fullPath, hashImgpth)
thumbnl = self.createIconImageBuffer(hashImgpth, self.viIconWxH) thumbnl = self.createIconImageBuffer(hashImgpth, self.viIconWxH)
elif file.lower().endswith(imagesList): elif file.lower().endswith(imagesList): # Image Icon
thumbnl = self.createIconImageBuffer(fullPath, self.viIconWxH) thumbnl = self.createIconImageBuffer(fullPath, self.viIconWxH)
else: else: # System icons
thumbnl = self.nonImageOrVideoIcon(fullPath) thumbnl = self.nonImageOrVideoIcon(fullPath)
except Exception as e: except Exception as e:
print(e)
return gtk.Image.new_from_file("resources/icons/bin.png") return gtk.Image.new_from_file("resources/icons/bin.png")
if thumbnl == None: # If no system icon, try stock file icon... if thumbnl == None: # If no system icon, try stock file icon...
@ -62,21 +72,73 @@ class Icon:
return thumbnl return thumbnl
def nonImageOrVideoIcon(self, fullPath): def nonImageOrVideoIcon(self, fullPath):
thumbPth = self.getSystemThumbnail(fullPath, self.systemIconImageWxH[0]) if fullPath.lower().endswith( ('.desktop',) ):
return self.createIconImageBuffer(thumbPth, self.systemIconImageWxH) return self.parseDesktopFiles(fullPath)
else:
thumbPth = self.getSystemThumbnail(fullPath, self.systemIconImageWxH[0])
return self.createIconImageBuffer(thumbPth, self.systemIconImageWxH)
def createIconImageBuffer(self, path, wxh): def createIconImageBuffer(self, path, wxh):
pixbuf = None
try: try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, wxh[0], wxh[1], False)
filename = path,
width = wxh[0],
height = wxh[1],
preserve_aspect_ratio = False)
return gtk.Image.new_from_pixbuf(pixbuf) return gtk.Image.new_from_pixbuf(pixbuf)
except Exception as e: except Exception as e:
return gtk.Image.new_from_file("resources/icons/bin.png") 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): def getSystemThumbnail(self, filename, size):
final_filename = "" final_filename = ""
if os.path.exists(filename): if os.path.exists(filename):

View File

@ -7,11 +7,16 @@ gi.require_version('Gdk', '3.0')
from gi.repository import Gtk as gtk from gi.repository import Gtk as gtk
from gi.repository import Gio as gio from gi.repository import Gio as gio
from gi.repository import GdkPixbuf from gi.repository import GdkPixbuf
from xdg.DesktopEntry import DesktopEntry
# Python Imports
import os, subprocess, hashlib, threading import os, subprocess, hashlib, threading
import urllib.request as urllib
from os.path import isdir, isfile, join from os.path import isdir, isfile, join
def threaded(fn): def threaded(fn):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
threading.Thread(target=fn, args=args, kwargs=kwargs).start() threading.Thread(target=fn, args=args, kwargs=kwargs).start()
@ -28,6 +33,10 @@ class Icon:
self.systemIconImageWxH = settings.returnSystemIconImageWH() self.systemIconImageWxH = settings.returnSystemIconImageWH()
self.viIconWxH = settings.returnVIIconWH() 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): def createIcon(self, dir, file):
fullPath = dir + "/" + file fullPath = dir + "/" + file
thumbnl = self.getIconImage(file, fullPath) thumbnl = self.getIconImage(file, fullPath)
@ -36,10 +45,10 @@ class Icon:
def getIconImage(self, file, fullPath): def getIconImage(self, file, fullPath):
thumbnl = gtk.Image() thumbnl = gtk.Image()
vidsList = ('.mkv', '.avi', '.flv', '.mov', '.m4v', '.mpg', '.wmv', '.mpeg', '.mp4', '.webm') vidsList = ('.mkv', '.avi', '.flv', '.mov', '.m4v', '.mpg', '.wmv', '.mpeg', '.mp4', '.webm')
imagesList = ('.png', '.jpg', '.jpeg', '.gif') imagesList = ('.png', '.jpg', '.jpeg', '.gif', '.ico', '.tga')
try: try:
if file.lower().endswith(vidsList): if file.lower().endswith(vidsList): # Video thumbnail
fileHash = hashlib.sha256(str.encode(fullPath)).hexdigest() fileHash = hashlib.sha256(str.encode(fullPath)).hexdigest()
hashImgpth = self.usrHome + "/.thumbnails/normal/" + fileHash + ".png" hashImgpth = self.usrHome + "/.thumbnails/normal/" + fileHash + ".png"
@ -47,11 +56,12 @@ class Icon:
self.generateVideoThumbnail(fullPath, hashImgpth) self.generateVideoThumbnail(fullPath, hashImgpth)
thumbnl = self.createIconImageBuffer(hashImgpth, self.viIconWxH) thumbnl = self.createIconImageBuffer(hashImgpth, self.viIconWxH)
elif file.lower().endswith(imagesList): elif file.lower().endswith(imagesList): # Image Icon
thumbnl = self.createIconImageBuffer(fullPath, self.viIconWxH) thumbnl = self.createIconImageBuffer(fullPath, self.viIconWxH)
else: else: # System icons
thumbnl = self.nonImageOrVideoIcon(fullPath) thumbnl = self.nonImageOrVideoIcon(fullPath)
except Exception as e: except Exception as e:
print(e)
return gtk.Image.new_from_file("resources/icons/bin.png") return gtk.Image.new_from_file("resources/icons/bin.png")
if thumbnl == None: # If no system icon, try stock file icon... if thumbnl == None: # If no system icon, try stock file icon...
@ -62,21 +72,73 @@ class Icon:
return thumbnl return thumbnl
def nonImageOrVideoIcon(self, fullPath): def nonImageOrVideoIcon(self, fullPath):
thumbPth = self.getSystemThumbnail(fullPath, self.systemIconImageWxH[0]) if fullPath.lower().endswith( ('.desktop',) ):
return self.createIconImageBuffer(thumbPth, self.systemIconImageWxH) return self.parseDesktopFiles(fullPath)
else:
thumbPth = self.getSystemThumbnail(fullPath, self.systemIconImageWxH[0])
return self.createIconImageBuffer(thumbPth, self.systemIconImageWxH)
def createIconImageBuffer(self, path, wxh): def createIconImageBuffer(self, path, wxh):
pixbuf = None
try: try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, wxh[0], wxh[1], False)
filename = path,
width = wxh[0],
height = wxh[1],
preserve_aspect_ratio = False)
return gtk.Image.new_from_pixbuf(pixbuf) return gtk.Image.new_from_pixbuf(pixbuf)
except Exception as e: except Exception as e:
return gtk.Image.new_from_file("resources/icons/bin.png") 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): def getSystemThumbnail(self, filename, size):
final_filename = "" final_filename = ""
if os.path.exists(filename): if os.path.exists(filename):