result = [('哦豁', '心心念念'), ('哦豁', '翻翻看看'), ('哦豁', '等你等你'), ('哦豁', '1111'), ('哦豁', '1111')]
last_five_result = [('哦豁', '心心念念'), ('哦豁', '翻翻看看'), ('哦豁', '等你等你'), ('哦豁', '1111'), ('哦豁', '1111')]
msg_to_act = []
def pushnewmsg_to_msgtoact():
global result
global last_five_result
new_element = result
result = [('哦豁', '翻翻看看'), ('哦豁', '等你等你'), ('哦豁', '1111'), ('哦豁', '1111'), ('哦豁', '9988')]
old_element = last_five_result
try:
while len(old_element) != 0:
p = old_element.index(new_element[0])
tem_last_five_result = old_element[p:len(old_element)]
tem_result = new_element[0:len(old_element) - p]
if tem_last_five_result == tem_result:
del new_element[0:len(old_element) - p]
tem_result = []
tem_last_five_result = []
else:
del old_element[0:p + 1]
tem_result = []
tem_last_five_result = []
for i in new_element:
msg_to_act.append(i)
print('送入新消息', i)
except:
for j in new_element:
msg_to_act.append(j)
print('送入新消息', j)
print('push后result:', result)
pushnewmsg_to_msgtoact()
import pyautogui
import pandas as pd
import uiautomation as auto
from bs4 import BeautifulSoup
import time
from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.interval import IntervalTrigger
from helium import *
import requests
import pyperclip
def local_weather_shanghai():
try:
driver = start_firefox('http://www.weather.com.cn/weather1d/101020100.shtml', headless=True)
twd = driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/div[2]/div[1]/div/div[3]')
twd = twd.text
report_time = driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/div[2]/div[1]/div/p[1]/span')
report_time = report_time.text
weather_ = datetime.now().strftime("%Y-%m-%d")+'上海天气:' + '\n' + report_time + '温度: ' + twd + '\n'
driver.get('http://www.weather.com.cn/weather/101020100.shtml')
we = driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/div[2]/ul/li[1]/p[1]')
weather_ += '今天天气:' + we.get_attribute('title') + '\n'
for i in range(2, 8):
we = driver.find_element_by_xpath('/html/body/div[5]/div[1]/div[1]/div[2]/ul/li[' + str(i) + ']/p[1]')
weather_ += ('第' + str(i) + '天天气:' + we.get_attribute('title') + '\n')
driver.quit()
f1 = open('WeatherRecord.txt', 'w')
f1.write(weather_)
f1.close()
except:
print('执行失败'+datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
pass
def local_weather_get():
f1 = open('WeatherRecord.txt', 'r')
f = f1.read()
print('已读取weather文件内容')
return f
def get_html_text(url):
try:
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
r = requests.get(url,timeout = 30,headers=headers)
r.raise_for_status()
r.encoding = 'utf-8'
return r.text
except:
return ""
def weibo_hot_get( ):
num = 20
lst = []
url = 'https://tophub.today/'
soup = BeautifulSoup(get_html_text(url), 'html.parser')
lst1 = soup.find_all('span', class_='t')
weibo_hot = '微博热搜榜:' + '\n'
f1 = open('WeiboHot.txt', 'w')
for i in range(num):
weibo_hot += (str(i+1)+" "+lst1[i].string+'\n')
f1.write(weibo_hot)
f1.close()
def send_msg_comfort():
tem_ = local_weather_get()
send_msg(tem_)
def send_msg_morning_weather():
tem_ = '现在是:'+time.strftime("%H", time.localtime())+'点,注意休息~'
send_msg(tem_)
def weibo_hot_send( ):
weibo_hot = open('WeiboHot.txt', 'r')
weibo_hot = weibo_hot.read()
print('已读取WeiboHot.txt文件内容')
return weibo_hot
def ttestt_():
return '=1'
def judge_if_new_msg_come():
global result
global last_five_result
print('开始前result:', result)
print('开始前last:', last_five_result)
last_five_result = result
result = []
max_i = -20
for i in range(-1, max_i, -1):
message = messages.GetChildren()[i]
content = message.Name
if content in ["查看更多消息", "以下为新消息"]:
continue
try:
details = message.GetChildren()[0].GetChildren()
except:
continue
if len(details) == 0:
continue
nickname, detail, me = details
name = nickname.Name
if me.Name:
continue
if not (content == "[图片]" or content.startswith("[语音]")):
details = detail.GetChildren()
if len(details) == 0:
continue
detail = details[-1].GetChildren()[0].GetChildren()[0].GetChildren()[0]
details = detail.GetChildren()
if len(details) != 0:
link_title = details[0].Name
link_content = details[1].Name
content += f"{link_title}\n{link_content}"
result.append((name, content.strip()))
if len(result) >= 5:
break
result = result[::-1]
print('开始后result:', result)
if result == last_five_result:
print('没有新消息' + time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()))
return True
else:
return False
def send_msg(msg_):
pyperclip.copy(msg_)
edit.SetFocus()
edit.SendKeys('{Ctrl}v')
edit.SendKeys('{Enter}')
def pushnewmsg_to_msgtoact():
new_element = result
old_element = last_five_result
try:
while len(old_element) != 0:
p = old_element.index(new_element[0])
tem_last_five_result = old_element[p:len(old_element)]
tem_result = new_element[0:len(old_element) - p]
if tem_last_five_result == tem_result:
del new_element[0:len(old_element) - p]
tem_result = []
tem_last_five_result = []
else:
del old_element[0:p + 1]
tem_result = []
tem_last_five_result = []
for i in new_element:
msg_to_act.append(i)
print('送入新消息', i)
except:
for j in new_element:
msg_to_act.append(j)
print('送入新消息', j)
print('push后result:', result)
def react_to_new_msg():
global last_five_result
if judge_if_new_msg_come():
return
else:
pushnewmsg_to_msgtoact()
while len(msg_to_act) != 0:
if msg_to_act[0][1] in func_dic:
tem_ = func_dic[msg_to_act[0][1]]()
send_msg(tem_)
else:
print('没有此命令',msg_to_act[0][1])
del msg_to_act[0]
print(msg_to_act)
func_dic = {
'1111': ttestt_,
'天气': local_weather_get,
'微博': weibo_hot_send
}
wechatWindow = auto.WindowControl(searchDepth=1, Name="微信", ClassName='WeChatMainWndForPC')
wechatWindow.SetFocus()
messages = wechatWindow.ListControl(Name='消息')
edit = wechatWindow.EditControl(Name='输入')
result = []
last_five_result = []
msg_to_act = []
sched = BlockingScheduler(timezone="Asia/Shanghai")
onesecondtrigger = IntervalTrigger(seconds=1)
foursecondtrigger = IntervalTrigger(seconds=4)
crontrigger_get_weather = CronTrigger(minute='15,45')
crontrigger_get_weibo_hot = CronTrigger(minute='20,50')
crontrigger_send_comfort_msg = CronTrigger(week='1-5', hour='9-20', minute=0)
crontrigger_send_morning_weather = CronTrigger(hour=7, minute=0)
crontrigger_send_weibo_hot = CronTrigger(hour='9,14', minute=2)
sched.add_job(react_to_new_msg, onesecondtrigger)
sched.add_job(local_weather_shanghai, crontrigger_get_weather)
sched.add_job(weibo_hot_get, crontrigger_get_weibo_hot)
sched.add_job(send_msg_comfort, crontrigger_send_comfort_msg)
sched.add_job(send_msg_morning_weather, crontrigger_send_morning_weather)
sched.add_job(weibo_hot_send, crontrigger_send_weibo_hot)
sched.start()