Skip to content

Instantly share code, notes, and snippets.

@mmas
Created July 4, 2017 23:50
Show Gist options
  • Save mmas/1fb7c382eff403b2d486890d4d746a6f to your computer and use it in GitHub Desktop.
Save mmas/1fb7c382eff403b2d486890d4d746a6f to your computer and use it in GitHub Desktop.
import os
import json
from tornado.web import RequestHandler
from pygeoip import GeoIP
import dateutil.parser
from app.models import Visit
from app import settings
class VisitController(RequestHandler):
def post(self):
self.set_status(204)
data = json.loads(self.request.body)
ip = self.request.remote_ip
if ip:
data['ip'] = ip
else:
return self.finish()
geo_db = GeoIP(os.path.join(settings.DATA_ROOT, 'GeoLiteCity.dat'))
try:
city = geo_db.record_by_addr(ip)
except:
return self.finish()
else:
data.update({'city': city['city'],
'country_code': city['country_code'],
'country_name': city['country_name'],
'continent': city['continent']})
data['date'] = dateutil.parser.parse(data['date'])
try:
Visit.create(**data)
except:
pass
return self.finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment