Update: Added favicon function to Kumo

It used regex method, but favicons are always moving target.
it is a goo try to get the favicon.

Signed-off-by: debianpepper <pdpdebdevuan@protonmail.com>
This commit is contained in:
debianpepper 2023-12-08 22:32:21 +09:00
parent 22068c9298
commit e1db9eed2a
1 changed files with 57 additions and 18 deletions

View File

@ -7,11 +7,14 @@
"""
import os
import re
from urllib.parse import urljoin
from tkinter import filedialog
import sqlite3
import tkinter as tk
import requests
import ttkbootstrap as ttk
import bsconf
from tkinter import filedialog
# setup the window
@ -26,6 +29,7 @@ pwin.title('Peppermint Kumo (SSB Manager)')
gusr = os.getlogin()
spath = "/home/" + gusr + "/.local/share/pmostools/peptools"
dpath = "/home/" + gusr + "/.local/share/applications/"
ipath = "/home/" + gusr + "/Pictures/"
# Set the window icon
pwin.tk.call('wm', 'iconphoto', pwin,
tk.PhotoImage(
@ -39,6 +43,40 @@ pcur.execute(""" CREATE TABLE IF NOT EXISTS kumoapp (id integer PRIMARY
KEY AUTOINCREMENT, ssbname text, lnk text);"""
)
def download_favicon(url, output_folder=ipath, request_timeout=3):
"""
This function will try a regex to find and locate the favicon
of a website.Depending on the website it may not find the favicon.
the goals is to try and stay within the python stndard library
"""
# Send a GET request to the website
response = requests.get(url, timeout=request_timeout)
response.raise_for_status() # Raise an error for bad responses
# Use a regular expression to find the favicon URL in the HTML
match = re.search(
r'<link[^>]*?rel=["\']?icon["\']?[^>]*?href=["\'](.*?)["\']',
response.text,
re.IGNORECASE
)
if match:
favicon_url = match.group(1)
favicon_url = urljoin(url, favicon_url)
# Download the favicon
response = requests.get(favicon_url,timeout=request_timeout, stream=True)
response.raise_for_status()
if not os.path.exists(output_folder):
os.makedirs(output_folder)
filename = os.path.join(output_folder,
os.path.basename(favicon_url)
)
with open(filename, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return filename
return None
def delete_ssb():
""" Delete the ssb that is selected """
@ -52,7 +90,7 @@ def delete_ssb():
del_path = os.path.join(dpath, dfile)
os.remove(del_path)
pwin.destroy()
os.system('python3 refresh.py')
os.system('python3 refresh.py')
def center_screen():
@ -96,11 +134,11 @@ def add_new_ssb():
ssb_address = urladdr_value.get("1.0", 'end-1c')
sql_insert = """ INSERT INTO kumoapp(ssbname,lnk) VALUES(?,?);"""
pcur.execute(sql_insert, (ssb_name, ssb_address,))
dcon.commit()
dcon.commit()
ssb_value.delete('1.0', tk.END)
urladdr_value.delete('1.0', tk.END)
pwin.destroy()
os.system('python3 ' + spath + '/refresh.py')
os.system('python3 ' + spath + '/refresh.py')
pwin.destroy()
os.system('python3 refresh.py')
@ -122,8 +160,8 @@ def fill_url_address():
use_address = pcur.fetchone()
runaddr_value.delete('1.0', tk.END)
runaddr_value.insert("end-1c", use_address)
def run_url_address():
""" Run the ssb with the run button"""
run_addrs = runaddr_value.get("1.0", 'end-1c')
@ -132,9 +170,11 @@ def run_url_address():
def select_icon():
"""
Select the icon to be used for the SSB in the system
The starting folder is the pixmaps
The starting folder is the home pictures folder
"""
initial_dir = "/usr/share/pixmaps/"
ssb_address = urladdr_value.get("1.0", 'end-1c')
output_filename = download_favicon(ssb_address)
initial_dir = ipath
file_path = filedialog.askopenfilename(
title="Select Icon",
initialdir=initial_dir,
@ -145,8 +185,7 @@ def select_icon():
if file_path:
icon_value.delete(1.0, tk.END)
icon_value.insert(tk.END, file_path)
### Create SSB side objects used
### Title
@ -161,9 +200,9 @@ ssb_value = tk.Text(pwin, height=1, width=25)
ssb_value.place(x=10, y=80)
lblcmbomenu = ttk.Label(pwin, text="Menu Location:")
lblcmbomenu.place(x=10, y=113)
categories = { 'AudioVideo' : 'AudioVideo', 'Audio':'Audio', 'Video':'Video',
categories = { 'AudioVideo' : 'AudioVideo', 'Audio':'Audio', 'Video':'Video',
'Development':'Development', 'Education':'Education',
'Game':'Game', 'Graphics':'Graphics', 'Network':'Internet',
'Game':'Game', 'Graphics':'Graphics', 'Network':'Internet',
'Office':'Office', 'Settings':'Settings','System':'System',
'Utility':'Utility'
}
@ -174,17 +213,17 @@ cmbomenu['values'] = list(categories.values())
icon_default_text = 'Set the icon with the "Icon" button'
icon_value = tk.Text(pwin, height=1, width=36)
icon_value.insert(tk.END, icon_default_text)
icon_value.place(x=10, y=172)
icon_value.place(x=10, y=280)
icon_button = ttk.Button(pwin, text="Icon", width=7, cursor="hand2",
bootstyle="light-outline", command=select_icon
)
icon_button.place(x=232, y=210)
icon_button.place(x=232, y=324)
url_default_text = "example: https://www.example.com"
urladdr_label = ttk.Label(pwin, text="Enter the Url:")
urladdr_label.place(x=10, y=224)
urladdr_label.place(x=10, y=172)
urladdr_value = tk.Text(pwin, height=3, width=36)
urladdr_value.insert(tk.END, url_default_text)
urladdr_value.place(x=10, y=246)
urladdr_value.place(x=10, y=200)
btnsv = ttk.Button(
pwin,
@ -208,7 +247,7 @@ lblcmbo = ttk.Label(pwin, text="Select SSB to Manage:")
lblcmbo.place(x=330, y=55)
cmbo = ttk.Combobox(pwin)
cmbo.place(x=330, y=80)
cmbo['values'] = (fill_dropdown())
cmbo['values'] = fill_dropdown()
runaddr_label = ttk.Label(pwin, text="Url Address:")
runaddr_label.place(x=330, y=125)
runaddr_value = tk.Text(pwin, height=4, width=36)
@ -239,7 +278,7 @@ def set_state(event):
Function that managse the state of the buttons and the Url address
for the Manage SSBs section, this is an event function
"""
selected_value = cmbo.get()
if selected_value == "":
btndelete["state"] = "disabled"