1

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

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
  • ~ I saw that you wanted me to take a look at this, but I am unfamiliar with the main modules you are using. I'm afraid I wouldn't be much help. – OneMadGypsy Sep 14 '20 at 06:35
  • @MichaelGuidry oh its fine, but do you have any general idea on how you would proceed if you had to show the notification from an api just once? I just need a general concept so maybe i could apply it here, im sure this method is pretty trash as it keeps on popping new notification up till the item is marked as read by the api(late by 1 min). – Delrius Euphoria Sep 14 '20 at 06:39
  • How are you trying to acknowledge that user has read the message? @CoolCloud – Karthik Sep 14 '20 at 06:46
  • @Karthik Its basically like the api no longer has the data marked as un read. So its no longer in the list of those list of unread messages. – Delrius Euphoria Sep 14 '20 at 06:49
  • 1
    What i used to do in website dev is give a checkbox to user. If user ticks the check box the message will be stored as 1 along with name and if he doesnt ticks it it will be stored as 0 and name. But dont know how would you show an checkbox in notification – Karthik Sep 14 '20 at 06:56
  • @Karthik I see, once the person clicks the notification here, it takes them to the link which shows notification. – Delrius Euphoria Sep 14 '20 at 06:59
  • @Karthik Oh okay, ill try keeping a reference to if the message is read. – Delrius Euphoria Sep 14 '20 at 07:09

0 Answers0