Snippets
Created by
Edelberto Mania
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 | #!/usr/bin/env python
# script to generate SBC extentions to be included in extension.conf
# source for DIDs are in csv hosted by Google Doc, or any http server
# csv columns: CONTEXT, DID, ISOCC, NOTES
# Edelberto Mania <ed@zenoradio.com>
# 20151113 EST
# reqs:
# (CentOS6) yum install python-beautifulsoup4 python-requests
login='THIS_IS_THE_LOGIN@zenoradio.com'
password='THIS_IS_THE_PASSWORD'
config_include='/tmp/_dids-201511v.conf'
## URLs
login_url="https://accounts.google.com/ServiceLogin"
auth_url="https://accounts.google.com/ServiceLoginAuth"
did_url='https://docs.google.com/a/zenoradio.com/spreadsheets/d/1Kl1YGThqUET4Wg3atMtYcc2kOSByWaIz25coO5k13kk/pub?gid=0&single=true&output=csv'
format_exten=""";; ISOCC: {0}, STATION: {3} ({4})
exten=>_{1},1,NoOp(${{EXTEN}} to {2})
same=>n,GoTo({2},${{EXTEN}},1)
"""
format_exten_plus_us_local=""";; ISOCC: {0}, STATION: {4} ({5})
exten=>_{1},1,NoOp(${{EXTEN}} to {2})
same=>n,GoTo({2},${{EXTEN}},1)
; just in case CLEC is sending us 10-digit DID
exten=>_{3},1,NoOp(${{EXTEN}} to {2})
same=>n,GoTo({2},1${{EXTEN}},1)
"""
import requests,bs4,csv,os,sys
from io import StringIO,BytesIO
## on version python2, we sometimes encounter 'InsecurePlatformWarning'
## uncomment below to disable the warning
#if sys.version[0]=='2':
# import requests.packages.urllib3
# requests.packages.urllib3.disable_warnings()
class GoogleSession:
def __init__(self,login_url,auth_url,login,password):
self.s=requests.session()
login_html=self.s.get(login_url)
soup_login=bs4.BeautifulSoup(login_html.content,'html.parser').find('form').find_all('input')
d={}
for u in soup_login:
if u.has_attr('value'):d[u['name']]=u['value']
d['Email']=login
d['Passwd']=password
self.s.post(auth_url,data=d)
def get(self,URL):return self.s.get(URL)
print('Generating configuration, please wait... ')
session=GoogleSession(login_url,auth_url,login,password)
## some checks
if session.get(did_url).status_code!=requests.codes.ok:
print('AUTH FAILED or {0} is not reachable'.format(login_url))
sys.exit(1)
if 'CONTEXT' not in session.get(did_url).text:
print('Opss! Authentication failed.')
sys.exit(1)
did_file=StringIO(session.get(did_url).text)
## fetch the DIDs and create a list
with did_file as fh:did_list=list(csv.reader(fh.readlines()))
## delete old config, and create empty one
## ignore erros for now
try:os.unlink(config_include)
except:pass
try:os.mknod(config_include)
except:pass
try:
with open(config_include,'a+') as ch:
for line in did_list[1:]:
source_context=line[0].lower().strip()
source_did=line[1].strip()
source_isocc=line[2].strip()
source_station_name=line[3].strip()
source_note=line[4].strip()
if '' not in [source_context,source_did]:
## select proper context here
## 3.4 -> cpe-100
## anning -> ANNING-178
## ... and so on.
if source_context=='3.4':s_context='cpe-11-100-62-137'
elif source_context=='release-3.4':s_context='release-3.4'
elif source_context=='development':s_context='telx-dev-cpe'
elif source_context=='pbx':s_context='ny-pbx'
elif source_context=='stage':s_context='telx-cpe-staging32'
elif source_context=='anning':s_context='ANNING-178'
elif source_context=='twi2':s_context='twi2'
elif source_context=='twi3':s_context='twi3'
elif source_context=='twi4':s_context='twi4'
elif source_context=='amantel':s_context='amantel'
elif source_context=='default':s_context='default'
elif source_context=='default-logic':s_context='default-logic'
elif source_context=='default-v3-1':s_context='default-v3-1'
elif source_context=='highvolumedids':s_context='highvolumedids'
elif source_context=='cpe-107':s_context='telx-cpe-107'
elif source_context=='filter':s_context='filter-did'
## if US/CA, create two exten lines - with prefix 1 and without
if source_isocc in ['us','ca'] or source_did.startswith('1'):
ch.write(format_exten_plus_us_local.format(source_isocc,source_did,s_context,source_did[1:],source_station_name,source_note))
else:
ch.write(format_exten.format(source_isocc,source_did,s_context,source_station_name,source_note))
print('Done! Check the file {0} for inclusion in the main extension.conf asterisk configuration.'.format(config_include))
except Exception(e):
print('Error: {0}'.format(e))
finally:
fh.close()
sys.exit(1)
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.