Created
September 12, 2014 09:11
-
-
Save c93614/7ab97a43b8f944983820 to your computer and use it in GitHub Desktop.
dnspod_watcher.py
This file contains hidden or 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
# -*- encoding: utf-8 -*- | |
# Creates a task-bar icon with balloon tip. Run from Python.exe to see the | |
# messages printed. Right click for balloon tip. Double click to exit. | |
# original version of this demo available at http://www.itamarst.org/software/ | |
import win32api, win32con, win32gui | |
import webbrowser | |
import time, urllib2, threading, re | |
import logging | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) | |
opener.addheaders = [ | |
('User-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'), | |
('Accept', 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash,' | |
+ 'application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*'), | |
('Accept-Language','zh-cn'), | |
] | |
urllib2.install_opener(opener) | |
class Taskbar: | |
def __init__(self): | |
self.visible = 0 | |
message_map = { | |
win32con.WM_DESTROY: self.onDestroy, | |
win32con.WM_USER+20 : self.onTaskbarNotify, | |
} | |
# Register the Window class. | |
wc = win32gui.WNDCLASS() | |
hinst = wc.hInstance = win32api.GetModuleHandle(None) | |
wc.lpszClassName = "DNSPodInvite" | |
wc.style = win32con.CS_VREDRAW | win32con.CS_HREDRAW; | |
wc.hCursor = win32gui.LoadCursor( 0, win32con.IDC_ARROW ) | |
wc.hbrBackground = win32con.COLOR_WINDOW | |
wc.lpfnWndProc = message_map # could also specify a wndproc. | |
classAtom = win32gui.RegisterClass(wc) | |
# Create the Window. | |
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU | |
self.hwnd = win32gui.CreateWindow( classAtom, "DNSPod Invite Fetcher", style, \ | |
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, \ | |
0, 0, hinst, None) | |
win32gui.UpdateWindow(self.hwnd) | |
def setIcon(self, hicon, tooltip=None): | |
self.hicon = hicon | |
self.tooltip = tooltip | |
def show(self): | |
"""Display the taskbar icon""" | |
flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | |
if self.tooltip is not None: | |
flags |= win32gui.NIF_TIP | |
nid = (self.hwnd, 0, flags, win32con.WM_USER+20, self.hicon, self.tooltip) | |
else: | |
nid = (self.hwnd, 0, flags, win32con.WM_USER+20, self.hicon) | |
if self.visible: | |
self.hide() | |
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid) | |
self.visible = 1 | |
def hide(self): | |
"""Hide the taskbar icon""" | |
if self.visible: | |
nid = (self.hwnd, 0) | |
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid) | |
self.visible = 0 | |
def onDestroy(self, hwnd, msg, wparam, lparam): | |
self.hide() | |
win32gui.PostQuitMessage(0) # Terminate the app. | |
def onTaskbarNotify(self, hwnd, msg, wparam, lparam): | |
if lparam == win32con.WM_LBUTTONUP: | |
self.onClick() | |
elif lparam == win32con.WM_LBUTTONDBLCLK: | |
self.onDoubleClick() | |
elif lparam == win32con.WM_RBUTTONUP: | |
self.onRightClick() | |
elif lparam == win32con.WM_USER+5: | |
self.onBalloonClick() | |
return 1 | |
def onClick(self): | |
"""Override in subclassess""" | |
pass | |
def onDoubleClick(self): | |
"""Override in subclassess""" | |
pass | |
def onRightClick(self): | |
"""Override in subclasses""" | |
pass | |
def onBalloonClick(self): | |
pass | |
if __name__=='__main__': | |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-5s %(message)s') | |
class DNSPodTaskbar(Taskbar): | |
def __init__(self): | |
Taskbar.__init__(self) | |
icon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION) | |
self.setIcon(icon) | |
self.show() | |
self.t = TWatcher() | |
self.t.taskbar = self | |
self.t.start() | |
def onClick(self): | |
self.quit() | |
def quit(self): | |
self.hide() | |
win32gui.PostQuitMessage(0) | |
self.t.stop = True | |
self.t.join() | |
def onDoubleClick(self): | |
self.quit() | |
def onRightClick(self): | |
self.quit() | |
def notify(self, msg): | |
msg = msg.replace(' ', ' ') | |
flags = win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_INFO | |
nid = (self.hwnd, 0, flags, win32con.WM_USER+20, self.hicon, "", msg.decode('utf-8'), 6, u"DNSPod微博通知", win32gui.NIIF_INFO) | |
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid) | |
def onBalloonClick(self): | |
webbrowser.open_new_tab('http://t.qq.com/p/t/' + self.tid) | |
class TWatcher(threading.Thread): | |
taskbar = None | |
init = False | |
stop = False | |
def run(self): | |
while not self.stop: | |
logging.debug(u'检测微博信息中...'.encode('gbk')) | |
while True: | |
try: | |
logging.debug(u'下载微博信息'.encode('gbk')) | |
content = urllib2.urlopen('http://e.t.qq.com/DNSPOD').read() | |
except: | |
logging.debug(u'读取微博失败'.encode('gbk')) | |
if retries > 5: | |
break | |
else: | |
time.sleep(1) | |
retries += 1 | |
else: | |
if not self.init: | |
logging.debug(u'初始化'.encode('gbk')) | |
newest = re.search('<li\s+id="(\d+)"\s+rel="\d+"', content, re.MULTILINE| re.DOTALL) | |
if newest: | |
self.init = newest.group(1) | |
else: | |
matches = re.findall('<li\s+id="(\d+)"\s+rel="\d+".+>(.+?)</li>', content, re.MULTILINE| re.DOTALL) | |
if matches and not matches[0][0] == self.init: | |
match = re.search('<div\s+class="msgCnt">(.+?)</div>', matches[0][1], re.MULTILINE| re.DOTALL) | |
if match: | |
self.taskbar.tid = matches[0][0] | |
self.taskbar.notify(re.sub('<.*?>','', match.group(1))) | |
self.init = matches[0][0] | |
break | |
time.sleep(1) | |
t = DNSPodTaskbar() | |
win32gui.PumpMessages() # start demo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment