1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| import requests import json import time import csv import linecache import random
city = ''
models = 'qpeaRHwuXys96O3WE2zysh4AWwn-i4DGb0BIOS79NBw'
填写上面复制的
appid = '' secret = ''
填写用户id
user = ' '
csv_file = csv.reader(open('city.csv', 'r',encoding='UTF-8') ) def yys(): for i in range(1,2): a = random.randrange(1, 9) theline = linecache.getline(r'poem.txt', a) print (theline) return theline
def read_city(my_city): for line in csv_file: if line[5] == my_city: return line[4]
def nowtime(): now = time.strftime('%Y年%m月%d日 %H:%M:%S', time.localtime(time.time()+28800)) return now
def date_diff(date1, date2): date1 = time.strptime(date1, "%Y-%m-%d") date2 = time.strptime(date2, "%Y-%m-%d") date1 = time.mktime(date1) date2 = time.mktime(date2) return int((date1 - date2) / 86400)
def get_token(): url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + \ appid+'&secret='+secret res = requests.get(url) token = res.json()['access_token'] return token
def send(weather, hw, dw, date): url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='+get_token() data = { "touser": user, "template_id": models, "url": "http://blog.jhx.asia", "topcolor": "#FF0000", "data": { "weather": { "value": weather, "color": "#000000" }, "date": { "value": date, "color": "#FFC0CB" }, "hw": { "value": hw, "color": "#B22222" }, "nowtime": { "value": nowtime(), "color": "#173177" }, "dizhi": { "value": city, "color": "#7B68EE" }, "yiyan": { "value": yys(), "color": "#FA8072" }, "dw": { "value": dw, "color": "#1E90FF" } } }
res = requests.post(url, data=json.dumps(data)) print(res.text)
def main(): citys = read_city(city) url = 'https://api.map.baidu.com/weather/v1/?district_id=' +citys+'&data_type=all&output=json&ak=' res = requests.get(url) weather = res.json()['result']['now']['text'] hw = res.json()['result']['forecasts'][0]['high'] dw = res.json()['result']['forecasts'][0]['low'] date = date_diff(time.strftime('%Y-%m-%d', time.localtime(time.time()+28800)), '2022-06-29') send(weather, hw, dw, date)
def main_handler(event, context): print("Received event: " + json.dumps(event, indent = 2)) print("Received context: " + str(context)) print("Hello world") return("Hello World")
main()
|