Displaying desktop notification after getting a new message python tkinter

397 views Asked by At

I wanted to make a notification system for StackOverflow using tkinter, when an item is unread in my inbox i want it to show the notification(but only once), which it does. But its not efficient. In some ways it shows notification once, and when i click on it, it takes some time for the notification to be identified as read by the API, So new notifications keeps on popping up, causing GUI to crash(as im running it inside of a after()). So i wanted to know if the notification part could be made more efficient in some way.

Code:

import tkinter as tk
import add
from time import ctime
import datetime
from stackapi import StackAPI
from win10toast import ToastNotifier
import threading
import webbrowser

root = tk.Tk()
root.title('Stack Notifier')

def authorise():
    print('auth')
    global end, acc_key, start, inbox, auth_button
    if required:
        add.run() #selenium for automation
        acc_key = add.acc_key_dum #accesskey passed from another module
        start = datetime.datetime.now()
        hours_added = datetime.timedelta(hours=24)
        end = start + hours_added #time for reaunthentication
        site.key = add.key #access key from another module
        site.access_token = acc_key #access token from another module
        site.max_pages = 1
        inbox = site.fetch('users/13382000/inbox')
        auth_button['state'] = tk.DISABLED
        threading.Thread(target=first).start()
    
    else:
        inbox = site.fetch('users/13382000/inbox')
        threading.Thread(target=first).start()

def first():
    global current, required, inbox, auth_button

    def popup():
        webbrowser.open(link,new=0)

    inbox = site.fetch('users/13382000/inbox')
    print('chc')
    current = datetime.datetime.now()
    if start < current < end:
        required = False
    
    else:
        required = True
        auth_button['state'] = tk.NORMAL 

    if not required:
        print('first')
        is_unread = inbox['items'][0]['is_unread']
        if is_unread:
            title = inbox['items'][0]['title']
            item_type = inbox['items'][0]['item_type']
            link = inbox['items'][0]['link']
            creation_date = ctime(inbox['items'][0]['creation_date'])
            noti = ToastNotifier()
            noti.show_toast(f'StackOveflow - {item_type}',f'{title} - {creation_date}',duration=5,callback_on_click=popup,threaded=True)
            print('yes')
   
    else:
        threading.Thread(target=authorise).start()
    
    root.after(1000,threading.Thread(target=first).start)

required = True #a reference to whether authentication is required again, after 24 hrs
label_login = tk.Label(root,text='Click To authorise with StackOverflow',font=('helvatica',16))
label_login.grid(row=0,columnspan=3)

auth_button = tk.Button(root,text='Authorise',command=authorise)
auth_button.grid(row=1,column=1)

site = StackAPI('stackoverflow')

root.mainloop()

I know this is not the best of codes, but im open to ideas and suggestions.

PS: Im using selenium for automation and ask for user credentials. and it requires giving permission every 24 hours, so i need to boot up selenium every 24 hours too, which is being finely done, i guess.

Thanks in advance :D

0

There are 0 answers