Created
May 2, 2015 15:17
-
-
Save jpmens/1d8f451f115ad4849280 to your computer and use it in GitHub Desktop.
mqttwarn functions for slack whereis slash-command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import json | |
import time | |
import urllib2 | |
# --- slack_whereis | |
def _isloc(s): | |
data = json.loads(s) | |
if type(data) == dict: | |
if '_type' in data and data['_type'] == 'location': | |
if 'lat' in data and 'lon' in data: | |
return data | |
return None | |
def slack_whereis_filter(topic, payload): | |
''' return True if this message should be suppressed ''' | |
data = _isloc(payload) | |
if data is None: | |
return True | |
return False | |
def slack_whereis(topic, data, srv=None): | |
geo = "?" | |
data = _isloc(data['payload']) | |
if data is not None: | |
lat = data['lat'] | |
lon = data['lon'] | |
try: | |
jsondata = json.load(urllib2.urlopen('http://maps.googleapis.com/maps/api/geocode/json?latlng='+str(lat)+','+str(lon)+'&sensor=true')) | |
geo = jsondata['results'][0]['formatted_address'] | |
except: | |
geo = 'reverse lookup error' | |
return dict(geo=geo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment