Skip to content

Instantly share code, notes, and snippets.

@NdYAG
Created November 22, 2014 14:52
Show Gist options
  • Save NdYAG/bd28aa7e01f4fd258b32 to your computer and use it in GitHub Desktop.
Save NdYAG/bd28aa7e01f4fd258b32 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# This script read the latest
# Beijing air quality from stateair
# and send notification to Pushbullet
import json
import urllib2
from xml.dom import minidom
def basic_authorization(user, password):
s = user + ":" + password
return "Basic " + s.encode("base64").rstrip()
def sendNotification(title, content):
url = 'https://api.pushbullet.com/v2/pushes'
access_token = 'access_token_from_pushbullet'
headers = {
'Authorization': basic_authorization(access_token, ''),
'Content-Type': 'application/json'
}
data = ({
"type": "note",
"title": title,
"body": content
})
req = urllib2.Request(url, json.dumps(data), headers)
urllib2.urlopen(req)
def main():
url = 'http://www.stateair.net/web/rss/1/1.xml'
xmldoc = minidom.parse(urllib2.urlopen(url))
firstItem = xmldoc.getElementsByTagName('item')[0]
time = firstItem.getElementsByTagName('title')[0].firstChild.nodeValue
aqi = firstItem.getElementsByTagName('AQI')[0].firstChild.nodeValue
if int(aqi) > 50:
content = 'PM2.5 %s' % aqi
sendNotification(time, content)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment