Skip to content

Instantly share code, notes, and snippets.

@brianrusso
Created May 25, 2016 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianrusso/a3b01b8abd694249cd1e5aa4fdce95b6 to your computer and use it in GitHub Desktop.
Save brianrusso/a3b01b8abd694249cd1e5aa4fdce95b6 to your computer and use it in GitHub Desktop.
import json
import geojson
import requests
class ClavinClient(object):
def __init__(self, clavin_endpoint=None):
if clavin_endpoint is None:
#self.clavin_endpoint = "http://54.186.35.170:9090/api/v0/geotag"
self.clavin_endpoint = "http://127.0.0.1:9090/api/v0/geotag"
# TODO: Remove this eventually.. replace with our public endpoint when it's on 24/7?
# Even then.. come from a config file somewhere that's user-overridable.
@staticmethod
def uniq(list):
return {}.fromkeys(list).keys()
def geocode_dynadocument(self, doc):
feature = geojson.Feature()
headers = {'Content-Type': 'text/plain'}
# Send document.contents via POST to CLAVIN
try:
resp_json = requests.post(self.clavin_endpoint, headers=headers, data=doc.contents.encode('utf-8'))
except AttributeError:
return None
# Load from JSON
clavinresp = json.loads(resp_json._content, "utf-8")
# Get resolved locations
places = list()
point_list = list()
for location in clavinresp['resolvedLocations']:
try:
places.append(location['matchedName'])
coords = (location['geoname']['longitude'], location['geoname']['latitude'])
point_list.append(coords)
except:
pass
# FIXME: hardcoded
kvasir_prefix = "http://127.0.0.1:5000/articles/"
# TODO: Extend this to support some notion of weighting vice just uniquing the places.
# E.g. if it mentions a place 10 times it's probably a stronger relation
point_list = self.uniq(point_list)
places = self.uniq(places)
if len(point_list) > 1:
feature.geometry = geojson.MultiPoint(point_list)
elif len(point_list) == 1:
feature.geometry = geojson.MultiPoint([point_list[0]])
else:
feature.geometry = geojson.MultiPoint([geojson.Point(coordinates=(0, 0))])
doc_url = kvasir_prefix + doc.dockey
feature.properties['title'] = doc.title
feature.properties['url'] = doc_url
feature.properties['date'] = unicode(doc.date)
feature.properties['dskey'] = doc.dskey
feature.properties['places'] = ", ".join(places)
print feature.properties['places']
#feature.properties = {'title': document.title, 'link': document.url, 'places': places,
# 'name': '<A href=\"%s\">%s</A>' % (document.url, document.title)}
return feature
def geocode_document(self, document):
feature = geojson.Feature()
feature.properties = list()
headers = {'Content-Type': 'text/plain'}
# Send document.contents via POST to CLAVIN
resp_json = requests.post(self.clavin_endpoint, headers=headers, data=document.contents)
# Load from JSON
clavinresp = json.loads(resp_json._content, "utf-8")
# Get resolved locations
places = list()
point_list = list()
for location in clavinresp['resolvedLocations']:
try:
places.append(location['matchedName'])
coords = (location['geoname']['longitude'], location['geoname']['latitude'])
point_list.append(coords)
except:
pass
# TODO: Extend this to support some notion of weighting vice just uniquing the places.
# E.g. if it mentions a place 10 times it's probably a stronger relation
point_list = self.uniq(point_list)
places = self.uniq(places)
if point_list:
feature.geometry = geojson.MultiPoint(point_list)
else:
feature.geometry = geojson.Point(coordinates=(0, 0))
feature.properties = {'title': document.title, 'link': document.link, 'places': places,
'name': '<A href=\"%s\">%s</A>' % (document.link, document.title)}
return feature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment