Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
NoobHumiliator committed Jan 5, 2024
0 parents commit 0ff215b
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 TechShrimp技术爬爬虾

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 免费微信推送
原理:申请免费的测试版公众号,可获得免费的模板推送功能。

### 视频教程

https://www.bilibili.com/video/BV1Ng4y1r7EP/

作者 **技术爬爬虾** 全网同名,转载请注明作者

### 申请公众号测试账户

使用微信扫码即可
https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

进入页面以后我们来获取到这四个值
#### appID appSecret openId template_id
![image](https://github.com/tech-shrimp/FreeWechatPush/assets/154193368/bdb27abd-39cb-4e77-9b89-299afabc7330)

想让谁收消息,谁就用微信扫二维码,然后出现在用户列表,获取微信号(openId)
![image](https://github.com/tech-shrimp/FreeWechatPush/assets/154193368/1327c6f5-5c92-4310-a10b-6f2956c1dd75)

新增测试模板获得 template_id(模板ID)
![image](https://github.com/tech-shrimp/FreeWechatPush/assets/154193368/ec689f4d-6c0b-44c4-915a-6fd7ada17028)

模板标题随便填,模板内容如下,可以根据需求自己定制

模板一 天气预报:
```copy
今天:{{date.DATA}}
地区:{{region.DATA}}
天气:{{weather.DATA}}
气温:{{temp.DATA}}
风向:{{wind_dir.DATA}}
对你说的话:{{today_note.DATA}}
```

模板二 课程提醒:
```copy
消息:{{message.DATA}}
```

### 配置代码

将上面获得的几个值填入代码这几行
![image](https://github.com/tech-shrimp/FreeWechatPush/assets/154193368/fe5a78ad-b4eb-45f8-a271-eda55f33a617)
### 配置定时任务

修改这几行
![image](https://github.com/tech-shrimp/FreeWechatPush/assets/154193368/58b7c58c-ac22-4a1a-b3e8-2eacc01b7329)

### 安装python依赖,启动项目
```copy
pip3 install -r requirements.txt
python main.py
```
166 changes: 166 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# 安装依赖 pip3 install requests html5lib bs4 schedule

import time
import requests
import json
import schedule
from bs4 import BeautifulSoup


# 从测试号信息获取
appID = ""
appSecret = ""
#收信人ID即 用户列表中的微信号,见上文
openId = ""
# 天气预报模板ID
weather_template_id = ""
# 时间表模板ID
timetable_template_id = ""


def get_weather(my_city):
urls = ["http://www.weather.com.cn/textFC/hb.shtml",
"http://www.weather.com.cn/textFC/db.shtml",
"http://www.weather.com.cn/textFC/hd.shtml",
"http://www.weather.com.cn/textFC/hz.shtml",
"http://www.weather.com.cn/textFC/hn.shtml",
"http://www.weather.com.cn/textFC/xb.shtml",
"http://www.weather.com.cn/textFC/xn.shtml"
]
for url in urls:
resp = requests.get(url)
text = resp.content.decode("utf-8")
soup = BeautifulSoup(text, 'html5lib')
div_conMidtab = soup.find("div", class_="conMidtab")
tables = div_conMidtab.find_all("table")
for table in tables:
trs = table.find_all("tr")[2:]
for index, tr in enumerate(trs):
tds = tr.find_all("td")
# 这里倒着数,因为每个省会的td结构跟其他不一样
city_td = tds[-8]
this_city = list(city_td.stripped_strings)[0]
if this_city == my_city:

high_temp_td = tds[-5]
low_temp_td = tds[-2]
weather_type_day_td = tds[-7]
weather_type_night_td = tds[-4]
wind_td_day = tds[-6]
wind_td_day_night = tds[-3]

high_temp = list(high_temp_td.stripped_strings)[0]
low_temp = list(low_temp_td.stripped_strings)[0]
weather_typ_day = list(weather_type_day_td.stripped_strings)[0]
weather_type_night = list(weather_type_night_td.stripped_strings)[0]

wind_day = list(wind_td_day.stripped_strings)[0] + list(wind_td_day.stripped_strings)[1]
wind_night = list(wind_td_day_night.stripped_strings)[0] + list(wind_td_day_night.stripped_strings)[1]

# 如果没有白天的数据就使用夜间的
temp = f"{low_temp}——{high_temp}摄氏度" if high_temp != "-" else f"{low_temp}摄氏度"
weather_typ = weather_typ_day if weather_typ_day != "-" else weather_type_night
wind = f"{wind_day}" if wind_day != "--" else f"{wind_night}"
return this_city, temp, weather_typ, wind


def get_access_token():
# 获取access token的url
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}' \
.format(appID.strip(), appSecret.strip())
response = requests.get(url).json()
print(response)
access_token = response.get('access_token')
return access_token


def get_daily_love():
# 每日一句情话
url = "https://api.lovelive.tools/api/SweetNothings/Serialization/Json"
r = requests.get(url)
all_dict = json.loads(r.text)
sentence = all_dict['returnObj'][0]
daily_love = sentence
return daily_love


def send_weather(access_token, weather):
# touser 就是 openID
# template_id 就是模板ID
# url 就是点击模板跳转的url
# data就按这种格式写,time和text就是之前{{time.DATA}}中的那个time,value就是你要替换DATA的值

import datetime
today = datetime.date.today()
today_str = today.strftime("%Y年%m月%d日")

body = {
"touser": openId.strip(),
"template_id": weather_template_id.strip(),
"url": "https://weixin.qq.com",
"data": {
"date": {
"value": today_str
},
"region": {
"value": weather[0]
},
"weather": {
"value": weather[2]
},
"temp": {
"value": weather[1]
},
"wind_dir": {
"value": weather[3]
},
"today_note": {
"value": get_daily_love()
}
}
}
url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(access_token)
print(requests.post(url, json.dumps(body)).text)


def send_timetable(access_token, message):
body = {
"touser": openId,
"template_id": timetable_template_id.strip(),
"url": "https://weixin.qq.com",
"data": {
"message": {
"value": message
},
}
}
url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(access_token)
print(requests.post(url, json.dumps(body)).text)


def weather_report(city):
# 1.获取access_token
access_token = get_access_token()
# 2. 获取天气
weather = get_weather(city)
print(f"天气信息: {weather}")
# 3. 发送消息
send_weather(access_token, weather)


def timetable(message):
# 1.获取access_token
access_token = get_access_token()
# 3. 发送消息
send_timetable(access_token, message)


if __name__ == '__main__':
weather_report("青岛")
# timetable("第二教学楼十分钟后开始英语课")

# schedule.every().day.at("18:30").do(weather_report, "南京")
# schedule.every().monday.at("13:50").do(timetable, "第二教学楼十分钟后开始英语课")
#while True:
# schedule.run_pending()
# time.sleep(1)
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
requests
schedule
bs4
html5lib

0 comments on commit 0ff215b

Please sign in to comment.