Question

I use Skype4Py for my Skype Bot I've been working on.
I was wondering how I could order what the APIs respond.
For example, if I wanted to get the weather, I'd type !weather
and it would respond with:

Gathering weather information. Please wait...
Weather: { "data": { "current_condition": [ {"cloudcover": "0", "humidity": "39", "observation_time": "11:33 AM", "precipMM": "0.0", "pressure": "1023", "temp_C": "11", "temp_F": "51", "visibility": "16", "weatherCode": "113", "weatherDesc": [ {"value": "Clear" } ], "weatherIconUrl": [ {"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png" } ], "winddir16Point": "N", "winddirDegree": "0", "windspeedKmph": "0", "windspeedMiles": "0" } ], "request": [ {"query": "90210", "type": "Zipcode" } ] }}

and I would like to have it be more like:

Weather:
Current Temp: 51 F | 22 C
Humidity: 39%
Wind Speed: 0 MPH

or something cleanly ordered out like that.
That way it looks less ugly in Skype, and looks more professional.


I should've added the code:

    def weather(zip):
try:
    return urllib2.urlopen('http://api.worldweatheronline.com/free/v1/weather.ashx?q='+zip+'&format=json&num_of_days=1&fx=no&cc=yes&key=r8nkqkdsrgskdqa9spp8s4hx' ).read()
except:
    return False

That is my functions.py

This is my commands.py:

                        elif msg.startswith('!weather '):
                    debug.action('!weather command executed.')
                    send(self.nick + 'Gathering weather information. Please wait...')
                    zip = msg.replace('!weather ', '', 1);
                    current = functions.weather(zip)
                    if 4 > 2:
                        send('Weather: ' + current)
                    else:
                        send('Weather: ' + current)

Like I stated, I'm using Skype4Py, and yeah.

Was it helpful?

Solution

Your API is returning json, which you need to parse:

import requests

url = 'http://api.worldweatheronline.com/free/v1/weather.ashx'
params = {'format': 'json',
          'num_of_days': 1, 'fx': 'no', 'cc': 'yes', 'key': 'sekret'}
params['zip'] = 90210

r = requests.get(url, params=params)
if r.status_code == 200:
   results = r.json()

print('Weather:
Current Temp: {0[temp_f]} F | {0[temp_c]} C
Humidity: {0[humidity]}%
Wind Speed: {0[windspeedMiles]} MPH'.format(results[0]))

I'm using the excellent requests library

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top