分類
程序

python查詢aqicn的空氣質量數據

#!/data/data/com.termux/files/usr/bin/python
# -*- coding: utf8 -*-

#aqicn空氣質量 #https://aqicn.org/api/
#v2 同時指定多個監測站,已防止單個站點很久不更新數據
import json,requests,sys,time

#token從https://aqicn.org/data-platform/token/獲取,只需郵箱即可
token = 'YourToken'
#預設為當前IP位置,可自行指定基站,幫助 --help
stationList = ["@5851","@5855","@5860"]

def getAqi(station):    
    aqiUrl = "https://api.waqi.info/feed/"+station+"/?token="+token
    try:
        res = requests.get(aqiUrl)
        a = res.json()
        if a['status']=="ok":            
            if time.time() - a['data']['time']['v'] < 86400:
                return a
            else:
                return {"status":a['data']['city']['name']+"的數據已過期。"}
        else:
            res = requests.get(aqiUrl)
            a = res.json()
            return a
    except:
        return {"status":"net error"}

def searchStation(keyword):
    searchUrl = "https://api.waqi.info/search/?token="+token+"&keyword="+keyword
    try:
        res = requests.get(searchUrl)
        a = res.json()
        res = []
        if a['status']=="ok":
            for s in a['data']:
                resT={}
                resT['uid']=s['uid']
                resT['name']=s['station']['name']
                resT['aqi']=s['aqi']+" ("+s['time']['stime']+")"
                res.append(resT)
            return res
        else:
            return a
    except:
        return {"status":"net error"}
def processAqi(aqiJson):
    s=aqiJson
#     print(json.dumps(s, indent=4))
    res={}
    if s['status']=="ok":
        res['cityName']=s['data']['city']['name']
        res['time']=s['data']['time']['s']
        res['aqi']=s['data']['aqi']
        try:
            res['pm25']=s['data']['iaqi']['pm25']['v']
        except:
            res['pm25']="N/A"
        try:
            res['pm10']=s['data']['iaqi']['pm10']['v']
        except:
            res['pm10']="N/A"
        try:
            res['temp']=s['data']['iaqi']['t']['v']
        except:
            res['temp']="N/A"
        try:
            res['humidity']=s['data']['iaqi']['h']['v']
        except:
            res['humidity']="N/A"
        try:
            res['wind']=s['data']['iaqi']['w']['v']
        except:
            res['wind']="N/A"
    else:
        res=s
    return res

def main():
    global station
    if len(sys.argv)==1:
        for station in stationList:
            res = processAqi(getAqi(station))
            print(json.dumps(res, indent=2, ensure_ascii=False))
    elif len(sys.argv)==2:
        station = str(sys.argv[1])
        if station == "--help":
            res={"@5851":"根據觀測點編號查詢","Nanyang":"根據城市名稱查詢",
                 "here":"根據IP查詢","s shenzhen":"查詢深圳觀測點",}
        else:
            res = processAqi(getAqi(station))
        print(json.dumps(res, indent=2, ensure_ascii=False))
    elif len(sys.argv)==3:
        res = searchStation(str(sys.argv[2]))
        print(json.dumps(res, indent=2, ensure_ascii=False))
    
if __name__ == '__main__':
    main()

前天迎來如秋後的第一場中度霧霾。

本文更新於 2018/11/08。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *