Write to me: 1613161916 to discuss python and distribute python learning materials!
Main Ideas
1. Get a list of friends from wxpy
2. Create timers
3. Timer trigger function
4. Function execution, traversing the list of friends
5. Friend object executes a parametric function with the parameter of the Friend City.
6. The function requests Baidu weather interface, gets the weather data corresponding to the friend, parses and processes the data, sends weather information, and completes the sending of the object.
7. End of traversal, end of sending
Defect: Launch failed after packaging as exe file because the timer could not find trigger. To solve this problem, you need to check Apscheduler information.
Solution: Change a timer.
Perform normally on the compiler.
When packaged as exe, it can be easily distributed to others. Every morning at 5:30 a.m., the weather forecast in the effect map will be sent automatically to all the friends after scanning the code.
Complete code
from wxpy import *
import requests
from datetime import datetime
import time
from apscheduler.schedulers.blocking import BlockingScheduler#Timing Framework
bot = Bot(cache_path=True)
#tuling = Tuling(api_key=Your api')#Robot api
def send_weather(location):
#Get ready url address
path ='http://api.map.baidu.com/telematics/v3/weather?location=%s&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'
url = path % location
response = requests.get(url)
result = response.json()
#If the city is wrong, send the weather according to Puyang
if result['error'] !=0:
location ='Puyang'
url = path % location
response = requests.get(url)
result = response.json()
str0 = (' Good morning! This is today's weather forecast!... Robot: PyChatBot\n')
results = result['results']
# Remove Data Dictionary
data1 = results[0]
# Get out of the city
city = data1['currentCity']
str1 =' Your City: %s\n' % city
# take out pm2.5 value
pm25 = data1['pm25']
str2 =' Pm value : %s\n' % pm25
# Converting strings to integers Otherwise, you can't compare sizes.
if pm25 =='':
pm25 =0
pm25 =int(pm25)
# adopt pm2.5 Judging pollution index by value
if 0 <= pm25 <35:
pollution ='excellent'
elif 35 <= pm25 <75:
pollution ='good'
elif 75 <= pm25 <115:
pollution ='Mild pollution'
elif 115 <= pm25 <150:
pollution ='Moderate pollution'
elif 150 <= pm25 <250:
pollution ='Heavy pollution'
elif pm25 >=250:
pollution ='Serious pollution'
str3 =' pollution index: %s\n' % pollution
result1 = results[0]
weather_data = result1['weather_data']
data = weather_data[0]
temperature_now = data['date']
str4 =' Current temperature: %s\n' % temperature_now
wind = data['wind']
str5 =' wind direction : %s\n' % wind
weather = data['weather']
str6 =' weather : %s\n' % weather
str7 =' temperature : %s\n' % data['temperature']
message = data1['index']
str8 =' Dress : %s\n' % message[0]['des']
str9 =' I'm very considerate.: %s\n' % message[2]['des']
str10 =' motion : %s\n' % message[3]['des']
str11 =' Ultraviolet rays : %s\n' % message[4]['des']
str = str0 + str1 + str2 + str3 + str4 + str5 + str6 + str7 + str8 + str9 + str10 + str11
return str
#Friends List
my_friends = []
my_friends = bot.friends()
my_friends.pop(0)
#Sender function
def send_message():
#Send it to all your friends
for friend in my_friends:
friend.send(send_weather(friend.city))
#Send me a successful notification
bot.file_helper.send(send_weather('Puyang'))
bot.file_helper.send('Send out')
#timer
print('star')
sched = BlockingScheduler()
sched.add_job(send_message,'cron',month='1-12',day='1-31',hour=5,minute =30)
sched.start()