-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
132 lines (113 loc) · 4.29 KB
/
main.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import json
import bottle
from bottle import Bottle, run, request, response, abort, hook, HTTPResponse
from bottle.ext.mongo import MongoPlugin
from truckpad.bottle.cors import CorsPlugin, enable_cors
from bson.json_util import dumps
from bson.objectid import ObjectId
app = Bottle()
with open("config.json") as f:
app.config.load_dict(json.load(f))
plugin = MongoPlugin(uri="mongodb://{}".format(app.config['db.host']), db=app.config['db.name'], json_mongo=True)
app.install(plugin)
ENTITY_COLLECTION_MAP = {
'channels': 'station',
'recordings': 'recording',
'fragments': 'fragment'
}
def raise_error(message, code=400):
raise HTTPResponse(
body=json.dumps({ "message": message }),
status=code,
headers={ 'Content-type': 'application/json' }
)
def check_path(entity):
if not entity in ENTITY_COLLECTION_MAP:
raise_error('{} not found'.format(entity), code=404)
@enable_cors
@app.route('/<entity>/')
def channels(mongodb, entity):
check_path(entity)
response.content_type = 'application/json'
for k in request.query:
if request.query[k].startswith('{'):
request.query[k] = json.loads(request.query[k])
return dumps(mongodb[ENTITY_COLLECTION_MAP[entity]].find(request.query))
@enable_cors
@app.route('/<entity>/<id>')
def channel(mongodb, entity, id):
check_path(entity)
if len(id) == 24:
obj = mongodb[ENTITY_COLLECTION_MAP[entity]].find_one({'_id': ObjectId(id)})
else:
obj = mongodb[ENTITY_COLLECTION_MAP[entity]].find_one({'_id': id})
if obj:
return obj
else:
raise_error('{} not found'.format(ENTITY_COLLECTION_MAP[entity]), code=404)
@enable_cors
@app.route('/<entity>/', method='POST')
def post_channel(mongodb, entity):
check_path(entity)
obj = request.json
if not obj:
raise_error('No data received')
if entity == 'recordings':
station = mongodb['station'].find_one({'station_name': obj['station_name']})
if not station:
raise_error('Station does not exist')
obj['station_id'] = station['_id']
#entity = json.loads(data)
try:
mongodb[ENTITY_COLLECTION_MAP[entity]].save(obj)
except Exception as e:
raise_error(str(e))
@enable_cors
@app.route('/<entity>/<id>', method='PUT')
def put_channel(mongodb, entity, id):
check_path(entity)
if len(id) == 24:
obj = mongodb[ENTITY_COLLECTION_MAP[entity]].find_one({'_id': ObjectId(id)})
else:
obj = mongodb[ENTITY_COLLECTION_MAP[entity]].find_one({'_id': id})
if not request.json:
raise_error('No data received')
if obj:
updated_obj = request.json
if entity == 'recordings':
station = None
if 'station_name' in updated_obj:
station = mongodb['station'].find_one({ 'station_name': updated_obj['station_name'] })
elif 'station_id' in updated_obj:
if len(updated_obj['station_id']) == 24:
station = mongodb['station'].find_one({'_id': ObjectId(updated_obj['station_id'])})
else:
station = mongodb['station'].find_one({'_id': updated_obj['station_id']})
if station:
obj['station_id'] = station['_id']
obj.update(updated_obj)
mongodb[ENTITY_COLLECTION_MAP[entity]].update_one(
{ '_id': obj['_id'] },
{ "$set": obj }
)
return obj
else:
raise_error('{} not found'.format(ENTITY_COLLECTION_MAP[entity]), code=404)
@enable_cors
@app.route('/<entity>/<id>', method='DELETE')
def put_channel(mongodb, entity, id):
check_path(entity)
if len(id) == 24:
obj = mongodb[ENTITY_COLLECTION_MAP[entity]].find_one({'_id': ObjectId(id)})
else:
obj = mongodb[ENTITY_COLLECTION_MAP[entity]].find_one({'_id': id})
if obj:
mongodb[ENTITY_COLLECTION_MAP[entity]].delete_one({ '_id': obj['_id'] })
return { "message": "Successfully deleted object" }
else:
raise_error('{} not found'.format(ENTITY_COLLECTION_MAP[entity]), code=404)
app.install(CorsPlugin(origins=['*']))
if __name__ == '__main__':
run(app, host=app.config['server.host'], port=app.config['server.port'], reloader=True)
else:
application = app