import requests
import json

def format_str (weather,temperature,wind) :
        f_weather = '{:>5}'.format(weather)
        print(f_weather)
        f_temperature = '{:>10}'.format(temperature)
        f_wind = '{:>10}'.format(wind)
        return '%s %s %s' % (f_weather,f_temperature,f_wind)
        
def query (query) :
        res = requests.get("http://api.map.baidu.com/telematics/v3/weather?location=" + query + "&ak=8yoeZq9BzQGxKQMvpUyKNZuZ&output=json")

        data = json.loads(res.content.decode())['results'][0]
        index = data['index']
        weather = data['weather_data']
        results = []
        
        count = 0
        for i in weather :
            if count == 0:
                descript = index[0]
                results.append({
                "Title": "[今日]  %s     %s      %s" % (i['weather'],i['temperature'],i['wind']),
                "SubTitle": "%s" % descript['des'],
                "IcoPath": "Images/app.png"
                })
            else:
                f_str = format_str(i['weather'] ,i['temperature'],i['wind'])
                print(f_str)
                results.append({
                    "Title": "[%s]  %s" % (i['date'],f_str),
                    "SubTitle": "",
                    "IcoPath": "Images/app.png"
                })
            count = count + 1

        return results
        
if __name__ == "__main__" :
    results = query('随州')
    