码迷,mamicode.com
首页 > 微信 > 详细

企业微信机器人 获取当前天气

时间:2020-02-22 00:22:32      阅读:872      评论:0      收藏:0      [点我收藏+]

标签:form   经纬   div   format   %s   空气质量   get   key   int   

import requests
import json
import time

# address = input()  # 输入要查询的地址
address = ‘湘潭市湖南科技大学‘
# 获取输入地址的经纬度
map_key = ‘腾讯地图API key‘
map_url = ‘https://apis.map.qq.com/ws/geocoder/v1/?address=‘ + address + ‘&key=‘ + map_key
map_response = requests.get(map_url)
map_json = json.loads(map_response.text)
latitude = str(map_json[‘result‘][‘location‘][‘lat‘])
longitude = str(map_json[‘result‘][‘location‘][‘lng‘])
# print(address + longitude + latitude)

# 根据经纬度查询天气
api_key = ‘彩云天气API key‘  # api秘钥
json_url = ‘https://api.caiyunapp.com/v2/‘ + api_key + ‘/‘ + longitude + ‘,‘ + latitude + ‘/realtime.json‘
json_response = requests.get(json_url)
json_str = json.loads(json_response.text)
timeStamp = json_str[‘server_time‘]
localTime = time.localtime(timeStamp)
formatTime = time.strftime("%Y{y}%m{m}%d{d} %H{c}%M{mi}%S{s}").format(y=‘年‘, m=‘月‘, d=‘日‘, c=‘点‘, mi=‘分‘, s=‘秒‘)
temperature = str(json_str[‘result‘][‘temperature‘])  # 气温
humidity = json_str[‘result‘][‘humidity‘]  # 湿度
skyconndition = str(json_str[‘result‘][‘skycon‘])  # 天气状况
weatherdic = {‘CLEAR_DAY‘: ‘晴‘, ‘CLEAR_NIGHT‘: ‘晴‘, ‘PARTLY_CLOUDY_DAY‘: ‘多云‘, ‘PARTLY_CLOUDY_NIGHT‘: ‘多云‘,
              ‘CLOUDY‘: ‘阴‘, ‘WIND‘: ‘阴‘, ‘HAZE‘: ‘雾霾‘, ‘RAIN‘: ‘雨‘, ‘SNOW‘: ‘雪‘}
skyconndition = weatherdic[skyconndition]  # 匹配代码与文字
aqi = str(json_str[‘result‘][‘aqi‘])  # 空气质量指数
if int(aqi) >= 200:
    aqi = ‘200 \n雾霾天要少出去玩哦‘
# pm25 = str(json_str[‘result‘][‘pm25‘])
real_humidity = str(humidity * 100)
current_weather = address + ‘\n北纬‘ + str(latitude) + ‘ 东经‘ + str(
    longitude) + ‘\n实时天气:\n气温:‘ + temperature + ‘℃\n湿度:‘ + real_humidity + ‘%\n天气:‘ + skyconndition + ‘\nAQI:‘ + aqi + ‘\n时间:‘ + formatTime
print(current_weather)


def weather():
    headers = {"Content-Type": "text/plain"}

    # 苏宁接口获取时间
    # timeUrl = ‘http://quan.suning.com/getSysTime.do‘
    # timeResponse = requests.get(timeUrl)
    # timeJson = json.loads(timeResponse.text)
    # severTime = timeJson[‘sysTime2‘]  # 2020-02-21 12:26:12

    s = current_weather
    data = {
        "msgtype": "text",
        "text": {
            "content": s,
        }
    }
    r = requests.post(
        url=‘http://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=企业微信机器人key‘,  # webhook地址
        headers=headers, json=data)
    print(r.text)


weather()

  

企业微信机器人 获取当前天气

标签:form   经纬   div   format   %s   空气质量   get   key   int   

原文地址:https://www.cnblogs.com/loneykids/p/12343658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!