Design sketch
Main idea
1. Get the friends list from wxpy
2. Create timer
3. Timer trigger function
4. Function execution, traversing the friends list
5. The friend object performs the function with parameters, and the parameter is the friend city.
6. In the function, request the weather interface of Baidu, get the weather data corresponding to the friend, analyze and process the data, send the weather information, and complete the sending of the object.
7. End of traversal and sending
Defect: failed to start after packing as exe file because timer could not find trigger. To solve this problem, you need to check Apscheduler related information.
Solution: change timer.
Normal execution on compiler.
Packaged as exe, it can be easily sent to others for use. The weather forecast in the rendering will be automatically sent to all friends at 5:30 every morning after code scanning and login.
Complete code
from wxpy import * import requests from datetime import datetime import time from apscheduler.schedulers.blocking import BlockingScheduler#Timed frame bot = Bot(cache_path=True) # tuling = Tuling(api_key=Your api')#Robot api def send_weather(location): # Prepare 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'] # Take out data dictionary data1 = results[0] # Take out 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 # Convert string to integer otherwise size cannot be compared if pm25 =='': pm25 =0 pm25 =int(pm25) # Judging pollution index by the value of pm2.5 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 ='Severe 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 =' Dressing : %s\n' % message[0]['des'] str9 =' I am very intimate.: %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 Python Resource sharing qun 784758214 ,There is an installation package inside. PDF,Learning video, here is Python Learners' gathering place, zero foundation, advanced, welcome
Friends list
my_friends = [] my_friends = bot.friends() my_friends.pop(0)
Sending function
def send_message():
Send to all friends
for friend in my_friends: friend.send(send_weather(friend.city))
Send success notification to me
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()