# -*- coding: UTF-8 -*-
import win32gui
import win32con
import time
import requests
from bs4 import BeautifulSoup
import logging
import webbrowser
class TestTaskbarIcon:
def __init__(self):
wc = win32gui.WNDCLASS()
hinst = wc.hInstance = win32gui.GetModuleHandle(None)
wc.lpszClassName = "PythonTaskbarDemo"
wc.lpfnWndProc = {win32con.WM_DESTROY: self.OnDestroy, }
classAtom = win32gui.RegisterClass(wc)
style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
self.hwnd = win32gui.CreateWindow(classAtom, "Taskbar Demo", style,
0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,
0, 0, hinst, None)
hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
nid = (self.hwnd, 0, win32gui.NIF_ICON, win32con.WM_USER + 20, hicon, "Demo")
win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)
def showMsg(self, title, msg):
nid = (self.hwnd, # 句柄
0, # 托盘图标ID
win32gui.NIF_INFO, # 标识
0, # 回调消息ID
0, # 托盘图标句柄
"TestMessage", # 图标字符串
msg, # 气球提示字符串
0, # 提示的显示时间
title, # 提示标题
win32gui.NIIF_INFO # 提示用到的图标
)
win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid)
def OnDestroy(self):
nid = (self.hwnd, 0)
win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)
win32gui.PostQuitMessage(0) # Terminate the app.
def GetInfo(AimUrl):
res = requests.get(AimUrl)
html = res.text
html = html.encode("ISO-8859-1")
html = html.decode("utf-8")
soups = BeautifulSoup(html, 'html.parser')
return soups
def Remind():
t = TestTaskbarIcon()
t.showMsg("您关注的内容有了更新", "有人疑似要发锭了!")
time.sleep(5)
win32gui.DestroyWindow(t.hwnd)
def log():
# 创建一个logging对象
logger = logging.getLogger()
# 创建一个文件对象 创建一个文件对象,以UTF-8 的形式写入 标配版.log 文件中
fh = logging.FileHandler('AllLog.log', encoding='utf-8')
# 创建一个屏幕对象
sh = logging.StreamHandler()
# 配置显示格式 可以设置两个配置格式 分别绑定到文件和屏幕上
formatter = logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s')
fh.setFormatter(formatter) # 将格式绑定到两个对象上
sh.setFormatter(formatter)
logger.addHandler(fh) # 将两个句柄绑定到logger
logger.addHandler(sh)
logger.setLevel(10) # 总开关
fh.setLevel(10) # 写入文件的从10开始
sh.setLevel(30) # 在屏幕显示的从30开始
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
if __name__ == '__main__':
log()
print('请输入刷新时效(整数,越小刷新越快,但过可能会错过。建议5左右)')
PutTime = input('>>>')
aim_url = 'http://zhannei.baidu.com/cse/site?q=%E9%94%AD&s=&cc=mcbbs.net&sti=' + str(PutTime)
print('0000 INFO: 系统初始化完成..')
print('0001 INFO: 正在暗中观察发锭选手..')
print('0002 INFO: 作者:mcbbs [雨韵] uid:1617821')
i = 3
while 1:
soup = GetInfo(aim_url)
divs = soup.find_all("h3", class_='c-title')
if len(divs) < 1:
print(str(i).zfill(4) + ' INFO: 暂无新帖')
i = i + 1
else:
i = i + 1
for r in divs:
url = r.a['href']
print(str(i).zfill(4) + ' INFO: 发现新的帖子地址 ' + url)
webbrowser.open(url)
Remind()
exit()
time.sleep(2)
|