Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Created June 25, 2012 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lovasoa/2989176 to your computer and use it in GitHub Desktop.
Save lovasoa/2989176 to your computer and use it in GitHub Desktop.
Ophriends : statistics on the Android SMS database (mmssms.db) to know how unbalanced your relationships are.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sqlite3, sys, re
con = sqlite3.connect("mmssms.db")
cur = con.cursor()
req = """
SELECT
DISTINCT address,
COUNT(body) as nbr
FROM sms
GROUP BY address
ORDER BY nbr DESC"""
try:
result = cur.execute(req)
except sqlite3.Error, e:
print("An error occured: %s." % (e.args[0]))
sys.exit(1)
addresses = result.fetchall()
stats = {}
for addrAndNbrMesssages in addresses:
#addrAndNbrMesssages is a tuple of the form (phone number, total number of messages)
addr = addrAndNbrMesssages[0]
#Display the phone number nicely
addrDisplay = re.sub(r"^\+\d\d","0",addr) #Supprime le +33 au début des numéros
addrDisplay = addrDisplay.replace(' ', '')
if len(addrDisplay) is 10:
addrDisplay = re.sub(r"(\d\d)", "\\1 ", addrDisplay)
sent = cur.execute("SELECT COUNT(body) FROM sms WHERE address=? AND type=2", (addr,)).fetchone()[0]
received = addrAndNbrMesssages[1]-sent
if stats.has_key(addrDisplay):
sent += stats[addrDisplay][0]
received += stats[addrDisplay][1]
stats[addrDisplay] = (sent, received)
stats = sorted(stats.items(), key= lambda (x):x[1], reverse=True)
for stat in stats:
print("%s :"%stat[0])
sent, received = stat[1]
if sent+received is not 0:
print("""
Envoyés : %d
Reçus : %d
Pourcentage de reçus: %.1f %%.
"""% (sent, received, 100.*received/(sent+received) ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment