Skip to content

Instantly share code, notes, and snippets.

@rtucker
Created July 30, 2010 00:11
Show Gist options
  • Select an option

  • Save rtucker/499562 to your computer and use it in GitHub Desktop.

Select an option

Save rtucker/499562 to your computer and use it in GitHub Desktop.
Queries Google Voice to determine voicemail counts, then creates dummy files for Asterisk's voicemail system
#!/usr/bin/python
import glob
import os
import sys
from googlevoice import Voice,util
mailboxroot = "/var/spool/asterisk/voicemail/default"
try:
extension = int(sys.argv[1])
except:
sys.stderr.write("usage: %s extensionnumber\n" % sys.argv[0])
sys.exit(1)
voice = Voice()
voice.login()
# get the numbers
voicemails = voice.voicemail()
total = voicemails['totalSize']
unread = voicemails['unreadCounts']['all']
# evaluate current mailbox status
mailboxpath = os.path.join(mailboxroot, str(extension))
inboxpath = os.path.join(mailboxpath, "INBOX")
oldpath = os.path.join(mailboxpath, "Old")
def quantifymailbox(path):
# returns dictionary: {filename: boolean indicating if it is ours}
resultdict = {}
for msgfile in glob.glob(os.path.join(path, "*.txt")):
rootname = os.path.splitext(msgfile)[0]
if len(glob.glob(os.path.join(rootname, "*"))) > 1:
resultdict[msgfile] = False
else:
resultdict[msgfile] = True
return resultdict
def setcount(path, count, filedict):
# sets count in a mailbox
currentcount = filedict.values().count(True)
filekeys = sorted(filedict.keys())
while currentcount > count:
target = filekeys.pop()
if filedict[target] == True:
os.unlink(target)
print "unlinking " + target
currentcount -= 1
while currentcount < count:
done = False
seq = 0
while not done:
candidate = os.path.join(path, "msg%.4i.txt" % seq)
if not os.path.isfile(candidate):
done = True
else:
seq += 1
open(candidate, 'w')
print "touching " + candidate
currentcount += 1
# handle new count
inboxstat = quantifymailbox(inboxpath)
setcount(inboxpath, unread, inboxstat)
# handle old count
oldstat = quantifymailbox(oldpath)
setcount(oldpath, total - unread, oldstat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment