-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathopencellid.py
31 lines (24 loc) · 889 Bytes
/
opencellid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from bottle import Bottle, abort, request
from bottle.ext.sqlalchemy import SQLAlchemyPlugin
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.schema import Table
Base = declarative_base()
engine = create_engine('sqlite:///cells.sqlite')
class Cell(Base):
__table__ = Table('cell', Base.metadata, autoload=True, autoload_with=engine)
@property
def json(self):
return {'lon': self.lon, 'lat': self.lat, 'range': self.range}
app = Bottle()
app.install(SQLAlchemyPlugin(engine))
@app.get('/')
def get(db):
criterion = {
'mcc': request.query.get('mcc'),
'mnc': request.query.get('mnc'),
'lac': request.query.get('lac'),
'cellid': request.query.get('cellid'),
}
cell = db.query(Cell).filter_by(**criterion).first()
return cell.json if cell else abort(404)