Created
August 31, 2018 01:23
-
-
Save LeNarvalo/de51f98d85c14f168fa1e4b17541bcb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf8 -*- | |
#!/usr/bin/env python | |
# Module : SysTrayIcon.py | |
# Synopsis : Windows System tray icon. | |
# Programmer : Simon Brunning - simon@brunningonline.net | |
# Date : 11 April 2005 | |
# Notes : Based on (i.e. ripped off from) Mark Hammond's | |
# win32gui_taskbar.py and win32gui_menu.py demos from PyWin32 | |
'''TODO | |
For now, the demo at the bottom shows how to use it...''' | |
import os | |
import sys | |
import win32api | |
import win32con | |
import win32gui_struct | |
try: | |
import winxpgui as win32gui | |
except ImportError: | |
import win32gui | |
import ctypes | |
def getpid(process_name): | |
import os | |
return [item.split()[1] for item in os.popen('tasklist').read().splitlines()[4:] if process_name in item.split()] | |
current_pid = os.getpid() | |
process_pid = getpid("systrayico.exe") | |
for pid in process_pid: | |
if int(pid) != current_pid: | |
os.kill(int(pid),21) | |
class SysTrayIcon(object): | |
'''TODO''' | |
QUIT = 'QUIT' | |
SPECIAL_ACTIONS = [QUIT] | |
FIRST_ID = 1023 | |
def __init__(self, | |
icon, | |
hover_text, | |
menu_options, | |
on_quit=None, | |
default_menu_index=None, | |
window_class_name=None,): | |
global selfe | |
self.icon = icon | |
self.hover_text = hover_text | |
self.on_quit = on_quit | |
menu_options = menu_options + (('Quitter', None, self.QUIT),) | |
self._next_action_id = self.FIRST_ID | |
self.menu_actions_by_id = set() | |
self.menu_options = self._add_ids_to_menu_options(list(menu_options)) | |
self.menu_actions_by_id = dict(self.menu_actions_by_id) | |
del self._next_action_id | |
#self.sub_menu_id = 0 | |
self.default_menu_index = (default_menu_index or 0) | |
self.window_class_name = window_class_name or "SysTrayIconPy" | |
message_map = {win32gui.RegisterWindowMessage("TaskbarCreated"): self.restart, | |
win32con.WM_DESTROY: self.destroy, | |
win32con.WM_COMMAND: self.command, | |
win32con.WM_USER+20 : self.notify,} | |
# Register the Window class. | |
window_class = win32gui.WNDCLASS() | |
hinst = window_class.hInstance = win32gui.GetModuleHandle(None) | |
window_class.lpszClassName = self.window_class_name | |
window_class.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW; | |
window_class.hCursor = win32gui.LoadCursor(0, win32con.IDC_ARROW) | |
window_class.hbrBackground = win32con.COLOR_WINDOW | |
window_class.lpfnWndProc = message_map # could also specify a wndproc. | |
classAtom = win32gui.RegisterClass(window_class) | |
# Create the Window. | |
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU | |
self.hwnd = win32gui.CreateWindow(classAtom, | |
self.window_class_name, | |
style, | |
0, | |
0, | |
win32con.CW_USEDEFAULT, | |
win32con.CW_USEDEFAULT, | |
0, | |
0, | |
hinst, | |
None) | |
win32gui.UpdateWindow(self.hwnd) | |
self.notify_id = None | |
self.refresh_icon() | |
win32gui.PumpMessages() | |
selfe = self | |
def _add_ids_to_menu_options(self, menu_options,subMenu=0): | |
global selfe | |
result = [] | |
for menu_option in menu_options: | |
option_text, option_icon, option_action = menu_option | |
if subMenu: | |
#print('HERRRRRRRRE') | |
id = titreArt.index(option_text.encode('utf8')) | |
self.menu_actions_by_id.add((id, option_action)) | |
#print('HERE-2') | |
result.append(menu_option + (id,)) | |
#print(str(id)+" : "+option_text.encode('utf8')) | |
#self.sub_menu_id+=1 | |
elif callable(option_action) or option_action in self.SPECIAL_ACTIONS: | |
self.menu_actions_by_id.add((self._next_action_id, option_action)) | |
result.append(menu_option + (self._next_action_id,)) | |
elif non_string_iterable(option_action): #sub_menu | |
result.append((option_text, | |
option_icon, | |
self._add_ids_to_menu_options(option_action,subMenu=1), | |
self._next_action_id)) | |
else: | |
print 'Unknown item', option_text, option_icon, option_action | |
self._next_action_id += 1 | |
selfe = self | |
return result | |
def refresh_icon(self): | |
global selfe | |
# Try and find a custom icon | |
hinst = win32gui.GetModuleHandle(None) | |
if os.path.isfile(self.icon): | |
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE | |
hicon = win32gui.LoadImage(hinst, | |
self.icon, | |
win32con.IMAGE_ICON, | |
0, | |
0, | |
icon_flags) | |
else: | |
print "Can't find icon file - using default." | |
hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) | |
if self.notify_id: message = win32gui.NIM_MODIFY | |
else: message = win32gui.NIM_ADD | |
self.notify_id = (self.hwnd, | |
0, | |
win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP, | |
win32con.WM_USER+20, | |
hicon, | |
self.hover_text) | |
win32gui.Shell_NotifyIcon(message, self.notify_id) | |
selfe=self | |
def restart(self, hwnd, msg, wparam, lparam): | |
global selfe | |
self.refresh_icon() | |
selfe = self | |
def destroy(self, hwnd, msg, wparam, lparam): | |
global selfe | |
if self.on_quit: self.on_quit(self) | |
nid = (self.hwnd, 0) | |
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) | |
win32gui.PostQuitMessage(0) # Terminate the app. | |
selfe = self | |
def notify(self, hwnd, msg, wparam, lparam): | |
global selfe | |
try: | |
if lparam==win32con.WM_LBUTTONDBLCLK: | |
self.execute_menu_option(self.default_menu_index + self.FIRST_ID) | |
elif lparam==win32con.WM_RBUTTONUP: | |
self.show_menu() | |
elif lparam==win32con.WM_LBUTTONUP: | |
self.show_menu() | |
except: | |
ctypes.windll.user32.MessageBoxW(0, u"Error-NOTIFY", u"Error", 0) | |
selfe =self | |
return True | |
def show_menu(self): | |
global selfe | |
menu = win32gui.CreatePopupMenu() | |
'''-------------------------DEBUT------------------------''' | |
menu_options = (('Aller sur le site', icon, hello), | |
(str(articleAVoir)+" nouvel(aux) article(s)",icon,tuple(sub_menu)), | |
('Modifier le rafraichissement',icon,dialogue), | |
(activ+' les notifs',icon,desactiv), | |
#('A sub-menu', icons.next(), (('Say Hello to Simon', icon, simon), | |
# ('Switch Icon', icon, switch_icon), | |
# )) | |
) | |
menu_options = menu_options + (('Quitter', None, self.QUIT),) | |
self._next_action_id = self.FIRST_ID | |
self.menu_actions_by_id = set() | |
self.menu_options = self._add_ids_to_menu_options(list(menu_options)) | |
#print("TEST-23") | |
self.menu_actions_by_id = dict(self.menu_actions_by_id) | |
#print("TEST-24") | |
'''*************************FIN**************************''' | |
self.create_menu(menu, self.menu_options) | |
#win32gui.SetMenuDefaultItem(menu, 1000, 0) | |
pos = win32gui.GetCursorPos() | |
# See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp | |
win32gui.SetForegroundWindow(self.hwnd) | |
win32gui.TrackPopupMenu(menu, | |
win32con.TPM_LEFTALIGN, | |
pos[0], | |
pos[1], | |
0, | |
self.hwnd, | |
None) | |
win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0) | |
selfe= self | |
def create_menu(self, menu, menu_options): | |
global selfe | |
for option_text, option_icon, option_action, option_id in menu_options[::-1]: | |
if option_icon: | |
option_icon = self.prep_menu_icon(option_icon) | |
if option_id in self.menu_actions_by_id: | |
item, extras = win32gui_struct.PackMENUITEMINFO(text=option_text, | |
hbmpItem=option_icon, | |
wID=option_id) | |
win32gui.InsertMenuItem(menu, 0, 1, item) | |
else: | |
submenu = win32gui.CreatePopupMenu() | |
self.create_menu(submenu, option_action) | |
item, extras = win32gui_struct.PackMENUITEMINFO(text=option_text, | |
hbmpItem=option_icon, | |
hSubMenu=submenu) | |
win32gui.InsertMenuItem(menu, 0, 1, item) | |
selfe=self | |
def prep_menu_icon(self, icon): | |
global selfe | |
# First load the icon. | |
ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) | |
ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) | |
hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE) | |
hdcBitmap = win32gui.CreateCompatibleDC(0) | |
hdcScreen = win32gui.GetDC(0) | |
hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y) | |
hbmOld = win32gui.SelectObject(hdcBitmap, hbm) | |
# Fill the background. | |
brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) | |
win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush) | |
# unclear if brush needs to be feed. Best clue I can find is: | |
# "GetSysColorBrush returns a cached brush instead of allocating a new | |
# one." - implies no DeleteObject | |
# draw the icon | |
win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL) | |
win32gui.SelectObject(hdcBitmap, hbmOld) | |
win32gui.DeleteDC(hdcBitmap) | |
selfe=self | |
return hbm | |
def command(self, hwnd, msg, wparam, lparam): | |
global selfe | |
id = win32gui.LOWORD(wparam) | |
self.execute_menu_option(id) | |
selfe=self | |
def execute_menu_option(self, id): | |
global selfe | |
menu_action = self.menu_actions_by_id[id] | |
if menu_action == self.QUIT: | |
win32gui.DestroyWindow(self.hwnd) | |
elif id < 20: | |
'''SUBMENU!!!''' | |
print('SUBMENU!!!') | |
menu_action(id) | |
else: | |
menu_action(self) | |
selfe=self | |
def notif(self, title, msg): | |
global selfe | |
flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP | |
nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, self.hover_text) | |
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid) | |
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, (self.hwnd, 0, win32gui.NIF_INFO, | |
win32con.WM_USER + 20, | |
hicon, title, msg, 200, | |
"NOTIFICATION KOREUS :")) | |
selfe=self | |
def non_string_iterable(obj): | |
try: | |
iter(obj) | |
except TypeError: | |
return False | |
else: | |
return not isinstance(obj, basestring) | |
# Minimal self test. You'll need a bunch of ICO files in the current working | |
# directory in order for this to work... | |
if __name__ == '__main__': | |
import itertools, glob | |
import urllib | |
import os | |
from win10toast import ToastNotifier | |
import time | |
import threading | |
from Tkinter import * | |
import winsound | |
global koreus | |
chemin = os.path.expanduser('~\Koreus') | |
if not os.path.exists(chemin): | |
os.mkdir(chemin) | |
pageTest= urllib.urlopen('https://www.koreus.com/modules/news/') | |
strpageTest=pageTest.read() | |
prefix = "https://www.koreus.com/modules/news/article" | |
suffix = ".html" | |
indexx = 0 | |
tape = 0 | |
articleAVoir = 0 | |
refreshTime = 5 | |
show = 1 | |
icon = chemin+'\\koreus.ico' | |
hover_text = "Notif Koreus" | |
activ = u'Désactiver' | |
VRAI = 1 | |
sub_menu = [] | |
def hello(sysTrayIcon): | |
global articleAVoir, sub_menu | |
sub_menu = [] | |
os.startfile("https://www.koreus.com/modules/news/") | |
articleAVoir = 0 | |
def testo(id): | |
global articleAVoir, sub_menu | |
#ubid = 0 | |
#or sub in sub_menu: | |
# if titreArt[id] == sub[0]: | |
# print(sub[0]) | |
# subid+=1 | |
txt = titreArt[id] | |
print('EN/DECODE BUG?') | |
print(txt.decode('utf8')) | |
c = 0 | |
for m in sub_menu: | |
if txt.decode('utf8') == m[0]: | |
ind = c | |
c+=1 | |
try: | |
sub_menu = sub_menu[:ind]+sub_menu[ind+1:] | |
except: | |
print("bug") | |
os.startfile(lienArt[id]) | |
print("testo : id:"+str(id)) | |
articleAVoir -= 1 | |
def badType(): | |
winsound.PlaySound('SystemExit', winsound.SND_ALIAS) | |
def getTime(): | |
global refreshTime, VRAI | |
try: | |
refreshTime = float(entreeTime.get()) | |
if refreshTime < 0.15: | |
sound_thread = threading.Thread(target=badType) | |
sound_thread.daemon = True | |
sound_thread.start() | |
timeVar.set("Minimum : 0.15 min") | |
return | |
else: | |
file = open(chemin+"\\Timer.txt","w") | |
file.write(str(refreshTime)) | |
file.close() | |
window.destroy() | |
except: | |
sound_thread = threading.Thread(target=badType) | |
sound_thread.daemon = True | |
sound_thread.start() | |
timeVar.set("Erreur!") | |
return | |
VRAI = 0 | |
wait_thread = threading.Thread(target=waitBeforeRelaunch) | |
wait_thread.daemon = True | |
wait_thread.start() | |
def waitBeforeRelaunch(): | |
global VRAI | |
time.sleep(5) | |
VRAI = 1 | |
launch_th = threading.Thread(target=launch) | |
launch_th.daemon = True | |
launch_th.start() | |
def escape(): | |
window.destroy() | |
def dialogue(sysTrayIcon): | |
global entreeTime, timeVar, window | |
window = Tk() | |
window.deiconify() | |
window.overrideredirect(True) | |
labelRac = Label(window, text="CONFIGURATION:") | |
labelRac.grid(row=0,column = 0, columnspan = 2) | |
time_label = Label(window, text="Tapez un temps en minute :") | |
time_label.grid(row=1,column=0, sticky=W) | |
timeVar=StringVar() | |
entreeTime= Entry(window,textvariable=timeVar) | |
entreeTime.grid(row=1,column = 1, sticky=W) | |
entreeTime.focus_set() | |
OK = Button(window, overrelief=GROOVE, text ='OK', command=getTime) | |
OK.grid(row=3,column=0) | |
ESC = Button(window, overrelief=GROOVE, text ='Quitter', command=escape) | |
ESC.grid(row=3,column=1) | |
window.mainloop() | |
def desactiv(sysTrayIcon): | |
global show, activ | |
show = 1 - show | |
if not show: | |
activ = "Activer" | |
sound_thread = threading.Thread(target=badType) | |
sound_thread.daemon = True | |
sound_thread.start() | |
else: | |
activ = u"Désactiver" | |
winsound.Beep(1500,50) | |
winsound.Beep(2500,50) | |
winsound.Beep(3500,50) | |
winsound.Beep(4500,50) | |
file = open(chemin+"\\Show.txt","w") | |
file.write(str(show)) | |
file.close() | |
####################### | |
#Archive | |
####################### | |
if not os.path.exists(chemin+"\\Archive.txt"): | |
file = open(chemin+"\\Archive.txt","w") | |
file.write("Num:00000\n") | |
file.write("Titre:XYZ\n") | |
file.write('Lien:https://www.koreus.com/video/XYZ.html\n') | |
file.write("Img:https://koreus.cdn.li/thumbs/201808/XYZ.jpg\n") | |
file.close() | |
if not os.path.exists(chemin+"\\Timer.txt"): | |
file = open(chemin+"\\Timer.txt","w") | |
file.write("5.0") | |
file.close() | |
if not os.path.exists(chemin+"\\Show.txt"): | |
file = open(chemin+"\\Show.txt","w") | |
file.write("1") | |
file.close() | |
def get_archive(): | |
global nums, titres, liens, imgs | |
file = open(chemin+"\\Archive.txt","r") | |
archive = file.read() | |
file.close() | |
nums = [] | |
titres = [] | |
liens = [] | |
imgs = [] | |
for l in archive.split(): | |
try: | |
nums.append(l[l.index("Num:")+4:]) | |
except: | |
None | |
try: | |
titres.append(l[l.index("Titre:")+6:]) | |
except: | |
None | |
try: | |
liens.append(l[l.index("Lien:")+5:]) | |
except: | |
None | |
try: | |
imgs.append(l[l.index("Img:")+4:]) | |
except: | |
None | |
def get_refreshTime(): | |
global refreshTime | |
file = open(chemin+"\\Timer.txt","r") | |
refreshTime = float(file.read()) | |
file.close() | |
def get_desactiv(): | |
global show | |
file = open(chemin+"\\Show.txt","r") | |
show = int(file.read()) | |
file.close() | |
####################### | |
get_desactiv() | |
if not show: | |
activ = "Activer" | |
menu_options = (('Aller sur le site', icon, hello), | |
(str(articleAVoir)+" nouvel(aux) article(s)",icon,hello), | |
('Modifier le rafraichissement',icon,dialogue), | |
(activ+' les notifs',icon,desactiv), | |
#('A sub-menu', icons.next(), (('Say Hello to Simon', icon, simon), | |
# ('Switch Icon', icon, switch_icon), | |
# )) | |
) | |
def compare(): | |
global articleAVoir, menu_options, show, sub_menu, indexx, lienArt, titreArt | |
#print('3333333333') | |
split1 = strpageTest.split('<span class="itemAdminLink"></span> <span class="itemPermaLink"><a href="'+prefix) | |
numArt = [] #Contient le numero des articles disponibles sur la premiere page | |
for l in split1[1:]: | |
num = l[:l.index(suffix)] | |
numArt.append(num) | |
#print('444444444') | |
split2 = strpageTest.split('<div class="itemHead"><h1 class="itemTitle">') | |
titreArt = [] #Contient le titre des articles... | |
count = 0 | |
for l in split2[1:]: | |
link = "<a href='"+prefix+numArt[count]+suffix+"'>" | |
noc = len(link) | |
titre = l[l.index(link)+noc:l.index('</a></h1></div>')] | |
titreArt.append(titre) | |
count+=1 | |
#print('55555555555') | |
split3 = strpageTest.split('<p class="itemText"><a href="') | |
lienArt=[] | |
imgArt=[] | |
for l in split3[1:]: | |
lien = l[:l.index('">')] | |
lienArt.append(lien) | |
img = l[l.index('src="')+5:l.index('.jpg')] | |
imgArt.append(img+'.jpg') | |
#print('66666666666') | |
get_archive() | |
#print('777777777777') | |
nbOfNvArt = 0 | |
for n in numArt: | |
if n not in nums: | |
nbOfNvArt += 1 | |
sub_menu.append((titreArt[numArt.index(n)].decode('utf8'),icon,testo)) | |
if nbOfNvArt > 0: | |
#print('8888') | |
tape = 0 | |
if show: | |
if int(nums[0])==0: | |
toaster = ToastNotifier() | |
toaster.show_toast("Notif Koreus!!!", | |
"Bienvenue!\nInitialisation en cours...", | |
icon_path=icon, | |
duration=10, | |
threaded=True) | |
else: | |
toaster = ToastNotifier() | |
toaster.show_toast("Notif Koreus!!!", | |
str(nbOfNvArt)+" nouvel(aux) article(s)\n", | |
icon_path=icon, | |
duration=10, | |
threaded=True) | |
else: | |
print('KOREUS NOTIF TEST') | |
#NOUVELLE IDEE :FAIRE UNE FENETRE AVEC TKINTER! OU PYGAME | |
#koreus.notif("Notif Koreus 2!!!", str(nbOfNvArt)+" nouvel(aux) article(s)\n") | |
articleAVoir = nbOfNvArt | |
file = open(chemin+"\\Archive.txt","w") | |
count = 0 | |
for num in numArt: | |
file.write("Num:"+str(num)+"\n") | |
file.write("Titre:"+titreArt[count]+"\n") | |
file.write("Lien:"+lienArt[count]+"\n") | |
file.write("Img:"+imgArt[count]+"\n") | |
count += 1 | |
file.close() | |
def bye(sysTrayIcon): print 'Bye, then.' | |
def launch(): | |
while VRAI: | |
#print("tTTTt") | |
get_refreshTime() | |
get_desactiv() | |
compare() | |
t = 0 | |
while t < refreshTime*60 and VRAI: | |
#print("22222222") | |
t+=1 | |
time.sleep(1) | |
launch_th = threading.Thread(target=launch) | |
launch_th.daemon = True | |
launch_th.start() | |
koreus = SysTrayIcon(icon, hover_text, menu_options, on_quit=bye, default_menu_index=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment