# -*- coding=utf-8 -*-
import sys
sys.path.append("/home/doingming/rob")
from component.source.AbstractPlugin import AbstractPlugin
import difflib #判断字符串相似程度
import requests
import json
import random
class Plugin(AbstractPlugin): #便于识别动态加载插件,继承
def handle(self,query):
url = 'https://free-api.heweather.net/s6/weather/'
type = 'forecast' #now实况天forecast 3-10天预报hourly逐小时预报lifestyle生活指数
location = "平顶山市"
print(query,"已经传入weather")
parm = {"location":location,"key":'37f7479267064be899c53d03094803fd' }
url =url+type+'?'
answer = requests.get(url,parm)
answer.encoding = 'utf-8'
# print("天气结果::::",answer.text)
try:
results = answer.json()['HeWeather6'][0]['daily_forecast']
res = '{}的天气:'.format(location)
day_label=['今天','明天','后天']
i = 0
for result in results:
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 += 1
print("res天气结果:",res)
self.say(res,True)
# return res
except Exception as e:
print("解析失败:", e)
# return '天气查询失败'
self.say("天气插件解析失败:",True)
def isValid(self,query):
return difflib.SequenceMatcher(None, '天气', query).quick_ratio()>0.60 or difflib.SequenceMatcher(None, '明天的天气', query).quick_ratio()>0.85 #如果返回true执行他
#'天气' in query
#difflib.SequenceMatcher(None, '天气', query).quick_ratio()>0.50 :
if __name__=="__main__":
p = Plugin(12) ###12仅仅用于平衡con
p.handle("平顶山市")