# -*- 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("平頂山市")