当前位置: 首页 > 后端技术 > Python

plugins1_weather

时间:2023-03-26 13:24:39 Python

#-*-coding=utf-8-*-importsyssys.path.append("/home/doingming/rob")fromcomponent.source.AbstractPluginimportAbstractPluginimportdifflib#判断字符串相似度importrequestsimportjsonimportrandomclassPlugin(AbstractPlugin):#方便识别动态加载插件,继承defhandle(self,query):url='https://free-api.heweather.net/s6/weather/'type='forecast'#nowlivedayforecast3-10天预报每小时每小时预报生活方式生活指数location="平顶山市"print(query,"weatherhasbeenpassedin")parm={"location":location,"key":'37f7479267064be899c53d03094803fd'}url=url+输入+'?'answer=requests.get(url,parm)answer.encoding='utf-8'#print("Weatherresults::::",answer.text)try:results=answer.json()['HeWeather6'][0]['daily_forecast']res='{}'sweather:'.format(location)day_label=['今天','明天','后天']i=0forresultinresults:tmp_min,tmp_max,cond_txt_d,cond_txt_n=\result['tmp_min'],result['tmp_max'],result['cond_txt_d'],result['cond_txt_n']res+='{}:白天{},夜间{},温度从{}到{}度摄氏度。'.format(day_label[i],cond_txt_d,cond_txt_n,tmp_min,tmp_max)i+=1print("resweatherresult:",res)self.say(res,True)#returnresexceptExceptionase:print("解析失败:",e)#return'Weatherqueryfailed'self.say("Weatherpluginparsedfailed:",True)defisValid(self,query):returndifflib.SequenceMatcher(None,'weather',query).quick_ratio()>0.60ordifflib.SequenceMatcher(None,'明天的天气',query).quick_ratio()>0.85#如果返回true,执行他#'weather'inquery#difflib.SequenceMatcher(None,'weather'',query).quick_ratio()>0.50:if__name__=="__main__":p=Plugin(12)###12仅用于余额控制p.handle("平顶山市")

最新推荐
猜你喜欢