# encoding: utf-8
#!/usr/bin/python
import os
import time
import MySQLdb
#设置目录
timearray = time.localtiime(time.time())
timedir = time.strftime("%Y/%m/%d/",timearray)
monitordir = "/var/spool/asterisk/monitor/"+timedir
vi conv.p
# 打开数据库连接
db = MySQLdb.connect("localhost","root","Elastixdb","asteriskcdrdb" )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
sql = "SELECT recordingfile from cdr where recordingfile like '%.wav';"
try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
recordingfile = row[0]
if os.path.exists(monitordir+recordingfile):
print "转换:%s" % monitordir+recordingfile
os.system("sox "+monitordir+recordingfile+" "+monitordir+recordingfile.replace('wav','gsm'))
os.remove(monitordir+recordingfile.replace('gsm','wav'))
sql = "update cdr set recordingfile=replace(recordingfile,'wav','gsm') where recordingfile='"+recordingfile+"';"
cursor.execute(sql)
db.commit()
else:
print "找不到文件:%s" % monitordir+recordingfile
except:
print "Error: unable to fecth data"
# 关闭数据库连接
db.close()