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:
parent
22068c9298
commit
e1db9eed2a
|
@ -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 """
|
||||
|
@ -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,
|
||||
|
@ -147,7 +187,6 @@ def select_icon():
|
|||
icon_value.insert(tk.END, file_path)
|
||||
|
||||
|
||||
|
||||
### Create SSB side objects used
|
||||
### Title
|
||||
new_label = ttk.Label(pwin, text="Create new SSBs",
|
||||
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue