Last active
October 4, 2017 18:05
-
-
Save sidmutha/cf0e9d463d95f82f7b75e07425a94171 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from src import app | |
from flask import jsonify, request | |
import requests | |
import json | |
import redis | |
R = redis.Redis( | |
host='session-redis.hasura', | |
decode_responses=True | |
) | |
@app.route("/") | |
def hello(): | |
return json.dumps({"message":"Autobots Assemble"}) | |
@app.route("/<process_topic>", methods=['POST']) | |
def process_url(process_topic): | |
topic = process_topic[8:] # remove 'process-' and get topic | |
topic = topic.replace("-", "_") | |
process(request.json, topic) | |
return ('', 204) | |
def process(dataIn, table): | |
print("[{}] processing {}".format(table, dataIn)) | |
dataToInsert = processHeaders(dataIn) | |
dataToInsert = addGeoData(dataToInsert) | |
insertData(table, dataToInsert) | |
def processHeaders(dataIn): | |
headers = dataIn["headers"] | |
ip = headers["X-Real-IP"][0] | |
user_agent = headers["User-Agent"][0] | |
timestamp = dataIn["timestamp"] | |
dataToInsert = dataIn["data"] | |
dataToInsert["created"] = timestamp | |
dataToInsert["ip"] = ip | |
dataToInsert["user_agent"] = user_agent | |
return dataToInsert | |
def addGeoData(dataToInsert): | |
ip = dataToInsert['ip'] | |
geo = checkAndGetGeoData(ip) | |
if geo: | |
dataToInsert["city"] = geo.get("city") | |
dataToInsert["country"] = geo.get("country") | |
dataToInsert["ip_metadata"] = geo | |
return dataToInsert | |
def checkAndGetGeoData(ip): | |
geo = R.hget("geo", ip) | |
if not geo: | |
geo = getGeoData(ip) | |
R.hset("geo", ip, json.dumps(geo)) | |
else: | |
geo = json.loads(geo) | |
return geo | |
def getGeoData(ip): | |
r = requests.get("http://extreme-ip-lookup.com/json/"+ip) | |
j = {} | |
if r.status_code == 200: | |
j = r.json() | |
return j | |
""" | |
r = { | |
"type" : "select", | |
"args" : { | |
"table" : "geo_data", | |
"columns" : [ | |
"geo" | |
], | |
"where": { | |
"ip" : ip | |
} | |
} | |
} | |
""" | |
def insertData(table, obj): | |
r = { | |
"type" : "insert", | |
"args" : { | |
"table" : table, | |
"objects" : [ | |
obj | |
] | |
} | |
} | |
h = { | |
"X-Hasura-Role" : "admin", | |
"X-Hasura-User-Id" : "1" | |
} | |
print("[{}] inserting {}".format(table, obj)) | |
resp = requests.post("http://data.hasura/v1/query", json=r, headers=h) | |
print("{} {}".format(resp.status_code, resp.json())) | |
return resp.status_code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment