Demo

This is my recent play-for-fun, a tiny trick developing a weatherman robot on SNS.

E.g.




This robot will update the HK local weather everyday at 20:00 EDT and post it on my SinaWeibo account.

Checklist

Pipeline

WWO API

Use urllib to run GET request to WWO, get back weather information in json

import urllib2
url = 'http://api.worldweatheronline.com/premium/v1/weather.ashx?key=*******************&q=HongKong&format=json&num_of_days=1&date=today&mca=no&fx24=yes&showlocaltime=yes'
response = urllib2.urlopen(url).read()

json decode and extraction

In python one can easily use json library for json decoding, then extract information interested.

import json
js = json.loads(response)
w = 'Good morning! Weather robot online. Current Query: ' + js['data']['request'][0]['query'] + '.\n'
w += 'Current weather condition: ' + js['data']['current_condition'][0]['weatherDesc'][0]['value'] + ', '
w += 'Feels like ' + js['data']['current_condition'][0]['FeelsLikeC'] + ' degree Celsius' + '.'

Weibo post

Use Weibo API to publish a post.

client.post('statuses/update', status=w)

to post. Make sure to figure out the OAuth2.0 authorization before using this API.

Server

Deploy the script on the Linux server and use

crontab -e

to set the schedule.