Skip to content

Instantly share code, notes, and snippets.

@jhaus
Created December 3, 2010 07:17
Show Gist options
  • Save jhaus/726676 to your computer and use it in GitHub Desktop.
Save jhaus/726676 to your computer and use it in GitHub Desktop.
import re
import urllib2
import urllib
import simplejson as json
# add my.ooma.com credentials here
username = ''
password = ''
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.25 Safari/532.0')]
urllib2.install_opener(opener)
# set 'per_page' param to no. of records to return
auth_url = 'https://my.ooma.com/home/login'
call_logs_url = 'https://my.ooma.com/call_logs?page=1&per_page=25'
login_page_contents = opener.open(auth_url).read()
match = re.search('<input name="authenticity_token"[^<]+value="(.*?)"', login_page_contents)
if match:
auth_token = match.group(1)
else:
auth_token = ""
login_params = urllib.urlencode( {
'username' : username,
'password' : password,
'authenticity_token' : auth_token
})
opener.open(auth_url, login_params)
call_logs_html = opener.open(call_logs_url).read()
match_call_table = re.search('<table class="calls"[^<]+>.*?</table>', call_logs_html, re.DOTALL)
if match_call_table:
calls_html = match_call_table.group()
else:
call= ""
calls = []
for matches in re.finditer('(?s)<tr class="([^<]+)">(.*?)</tr>', calls_html):
call = {}
call_type = matches.group(1)
call_info = matches.group(2)
call_type = re.sub(r"(?s)(blue|white)\s+", "", call_type)
call['calltype'] = call_type
match_phone = re.search(r"<td>\s+((\(\d+\) [\d\-]+)|\d+).*?</td>\s+<td>\s+(.*?)&nbsp;\s+</td>", call_info, re.DOTALL)
if match_phone:
phone = match_phone.group(1)
incoming_cid = match_phone.group(3)
else:
phone = ""
incoming_cid = ""
call['number'] = phone
call['callerid'] = incoming_cid
match_date = re.search("<span[^<]+>(.*)</span>", call_info, re.DOTALL)
if match_date:
call_timestamp = match_date.group(1)
else:
call_timestamp = ""
call['timestamp'] = call_timestamp
calls.append(call)
ooma_data = {'calls': calls }
print json.dumps(ooma_data)
# via https://sites.google.com/a/byubyu.com/projects/home/scrapingoomacalllogswithpython
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment