Created
February 13, 2019 21:08
-
-
Save ryanquinlan/6e2b68ec1265ca9bb11134008a91ab32 to your computer and use it in GitHub Desktop.
Spool CMS CDR messages to a SQLite database
This file contains 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 app import app | |
from flask_admin import Admin | |
from flask_admin.contrib.peewee import ModelView | |
admin = Admin(app, name='cdr-admin', template_mode='bootstrap3') | |
admin.add_view(ModelView(Record, db.session)) | |
app.run(host="0.0.0.0", port=8081) |
This file contains 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 flask import Flask, request | |
import xmltodict | |
import json | |
import models | |
from pprint import pprint | |
import json | |
from dbinterface import Record | |
app = Flask(__name__) | |
@app.route('/cdr', methods=['POST']) | |
def post(): | |
try: | |
cdr = xmltodict.parse(request.data) | |
if isinstance(cdr['records']['record'], list): | |
for record in cdr['records']['record']: | |
instance = getattr(models, str(record['@type']))(record) | |
Record.create(data=json.dumps(instance.__dict__)) | |
else: | |
instance = getattr(models, str(cdr['records']['record']['@type']))(cdr['records']['record']) | |
Record.create(data=json.dumps(instance.__dict__)) | |
return "" | |
except: | |
raise | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=8080, debug=True) |
This file contains 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 peewee import * | |
from playhouse.sqlite_ext import SqliteExtDatabase | |
import datetime | |
db = SqliteExtDatabase('cdr.db') | |
class BaseModel(Model): | |
class Meta: | |
database = db | |
class Record(BaseModel): | |
created_date = DateTimeField(default=datetime.datetime.now) | |
data = TextField() | |
db.connect() | |
if __name__ == '__main__': | |
db.create_tables([Record]) |
This file contains 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 collections import OrderedDict | |
class Cdr(object): | |
"""Base CDR class""" | |
def __init__(self, *cdr): | |
for d in cdr: | |
for key in d: | |
setattr(self, key, d[key]) | |
def __str__(self): | |
return "I am a {} message".format(type(self).__name__) | |
class callStart(Cdr): | |
"""CDR call start object""" | |
def __init__(self, *cdr): | |
super(callStart, self).__init__(*cdr) | |
class callLegStart(Cdr): | |
"""CDR call leg start object""" | |
def __init__(self, *cdr): | |
super(callLegStart, self).__init__(*cdr) | |
class callLegUpdate(Cdr): | |
"""CDR call leg update object""" | |
def __init__(self, *cdr): | |
super(callLegUpdate, self).__init__(*cdr) | |
class callLegEnd(Cdr): | |
"""CDR call leg end object""" | |
def __init__(self, *cdr): | |
super(callLegEnd, self).__init__(*cdr) | |
class callEnd(Cdr): | |
"""CDR call end object""" | |
def __init__(self, *cdr): | |
super(callEnd, self).__init__(*cdr) | |
class recordingStart(Cdr): | |
"""CDR recording start object""" | |
def __init__(self, *cdr): | |
super(recordingStart, self).__init__(*cdr) | |
class recordingEnd(Cdr): | |
"""CDR recording end object""" | |
def __init__(self, *cdr): | |
super(recordingEnd, self).__init__(*cdr) | |
class streamingStart(Cdr): | |
"""CDR streaming start object""" | |
def __init__(self, *cdr): | |
super(callStart, self).__init__(*cdr) | |
class streamingEnd(Cdr): | |
"""CDR streaming end object""" | |
def __init__(self, *cdr): | |
super(streamingEnd, self).__init__(*cdr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment