Created
July 10, 2014 20:09
-
-
Save jfinstrom/57a49991320acdc7dcb9 to your computer and use it in GitHub Desktop.
Dump Asterisk Phonebook to json as an interchange format
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
#!/usr/bin/env python | |
import subprocess | |
import json | |
callerid = {} | |
speeddial = {} | |
p = subprocess.Popen(["asterisk", "-rx", "database show"], stdout=subprocess.PIPE) | |
out = p.communicate() | |
out = out[0].splitlines() | |
for line in out: | |
if line.startswith('/cidname'): | |
key,value = line.split(':') | |
key = key.strip() | |
key = key.split('/')[2] | |
value = value.strip() | |
callerid[key] = value | |
if line.startswith('/sysspeeddials'): | |
key,value = line.split(':') | |
key = key.strip() | |
key = key.split('/')[2] | |
value = value.strip() | |
speeddial[value] = key | |
output = [] | |
for number,name in callerid.items(): | |
output.append({"entry" : {"full name" : name, "phone":number, "speeddial" : speeddial.get(number)}}) | |
jsonout = json.dumps(output) | |
print jsonout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment