Commits

Arno Hautala committed f0b7f3e

added mirror_sms.py as submitted by murty (originally as an updated getsms.py)

  • Participants
  • Parent commits 51c4701

Comments (0)

Files changed (1)

File examples/mirror_sms.py

+#!/usr/bin/env python
+
+from googlevoice import Voice
+import re
+import sys
+import json
+import os
+import os.path
+
+def getNames(html):
+    import BeautifulSoup
+    Names = {}
+    tree = BeautifulSoup.BeautifulSoup(html)
+    convBlock = tree.findAll('div', attrs={'id' : True}, recursive=False)
+    for conv in convBlock:
+        fromnames = []
+        rows = conv.findAll(attrs={'class' : 'gc-message-sms-from'})
+        for row in rows:
+            fromname = ' '.join(row.findAll(text=True)).strip().replace(':','')
+            fromnames.append(fromname)
+        Names[conv['id']] = fromnames
+    return Names
+
+def ensureDirectory(path):
+    if not os.path.isdir(path):
+        try:
+            os.makedirs(path)
+        except OSError, e:
+            if e.errno != errno.EEXIST:
+                raise
+
+outputDir = 'downloadedsms'
+ensureDirectory(outputDir)
+voice = Voice()
+voice.login()
+
+conversations = []
+page = 0
+while True:
+    page += 1
+    voice.sms(terms={ 'page':('p%d' % page) })
+    jsonData = voice.sms.data
+    pagenames = getNames(voice.sms.html)
+    pageconversations = []
+    if 'messages' not in jsonData or len(jsonData['messages']) < 1:
+        break
+    for message in jsonData['messages']:
+        getattr(voice,'message')(terms={ 'messageId':jsonData['messages'][message]['id'] })
+        pageconversations.append(getattr(voice,'message').data['messages'])
+    for i in pageconversations:
+        for j in i:
+            if "children" not in i[j]:
+                next
+            if j not in pagenames:
+                next
+            c = 0
+            maxc = len(pagenames[j])
+            for conversation in i[j]["children"]:
+                if c == maxc:
+                    break
+                if not conversation["fromName"]:
+                    if pagenames[j][c]:
+                        conversation["fromName"] = pagenames[j][c]
+                c += 1
+    conversations += pageconversations
+    print 'page %d' % page
+
+for conv in conversations:
+    for key in conv:
+        mynumber = conv[key]["phoneNumber"].replace('+','')
+        mystamp = conv[key]["displayStartDateTime"].replace(' ','_').replace('/','-')
+        suffix = key + '.txt'
+        name = outputDir + '/' + mynumber + '.' + mystamp + '.' + suffix
+        f = open(name, 'w')
+        f.write(json.dumps(conv,sort_keys=True, indent=4))
+        f.close()
+
+print len(conversations), ' conversations written to directory ', outputDir