Commits

Arno Hautala committed 600c375

updates to mirror_sms to retrieve from the inbox and archive after (murty)

Comments (0)

Files changed (1)

examples/mirror_sms.py

-#!/usr/bin/env python
-
+#!/usr/bin/env python
+# Download sms messages and archive them if present in inbox
+#
+# NOTE: Set download_type below:
+# 'inbox': Download SMS messages in inbox only and archive them
+# 'sms': Download all SMS messages and archive them. Set this for initial run
+download_type = 'inbox'
+#download_type = 'sms'
+# Download directory for SMS messages
+outputDir = 'downloadedsms'
 from googlevoice import Voice
 import re
 import sys
 import json
+import time
 import os
 import os.path
-
+import glob
+import csv
 def getNames(html):
     import BeautifulSoup
     Names = {}
         for row in rows:
             fromname = ' '.join(row.findAll(text=True)).strip().replace(':','')
             fromnames.append(fromname)
-        Names[conv['id']] = fromnames
+        if fromnames:
+            Names[conv['id']] = fromnames
     return Names
-
 def ensureDirectory(path):
     if not os.path.isdir(path):
         try:
         except OSError, e:
             if e.errno != errno.EEXIST:
                 raise
-
-outputDir = 'downloadedsms'
-ensureDirectory(outputDir)
+jsondir = outputDir + '/json'
+csvdir = outputDir + '/csv'
+ensureDirectory(jsondir)
+ensureDirectory(csvdir)
 voice = Voice()
 voice.login()
-
 conversations = []
 page = 0
-archived_sms_reached = False
-while not archived_sms_reached:
+while True:
     page += 1
-    voice.sms(terms={ 'page':('p%d' % page) })
-    jsonData = voice.sms.data
-    pagenames = getNames(voice.sms.html)
+    getattr(voice,download_type)(terms={ 'page':('p%d' % page) })
+    value = getattr(voice,download_type)
+    jsonData = value.data
+    pagenames = getNames(value.html)
     pageconversations = []
     if 'messages' not in jsonData or len(jsonData['messages']) < 1:
         break
-    # need to sort jsonData['messages'] by startTime, probably create messageID list and sort that
     for message in jsonData['messages']:
+        if 'sms' not in jsonData['messages'][message]['labels']:
+            continue 
         getattr(voice,'message')(terms={ 'messageId':jsonData['messages'][message]['id'] })
-        
-        if "inbox" in jsonData['messages'][message]['labels']:
-            voice.archive(jsonData['messages'][message]['id'])
-        else:
-            archived_sms_reached = True
-            break
-            
         pageconversations.append(getattr(voice,'message').data['messages'])
     for i in pageconversations:
         for j in i:
             if 'children' not in i[j]:
-                next
+                continue
             if j not in pagenames:
-                next
+                continue
             c = 0
             maxc = len(pagenames[j])
             for conversation in i[j]['children']:
                 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')
+        head = '/' + conv[key]['phoneNumber'].replace('+','') + '.'
+        timestamp = time.strftime('%Y%m%d.%H%M%S',time.localtime(int(conv[key]['startTime'])/1000))
+        tail = '.' + key
+        jsonname = jsondir + head + timestamp + tail + '.json'
+        csvname = csvdir + head + timestamp + tail + '.csv'
+        for oldfile in glob.glob(jsondir + head + '*' + tail + '.json'):
+            if oldfile != jsonname:
+                os.unlink(oldfile)
+        for oldfile in glob.glob(csvdir + head + '*' + tail + '.csv'):
+            if oldfile != csvname:
+                os.unlink(oldfile)
+        f = open(jsonname,'wb')
         f.write(json.dumps(conv,sort_keys=True, indent=4))
         f.close()
-
-print len(conversations), ' conversations written to directory ', outputDir
+        f = csv.writer(open(csvname,'wb'))
+        for i in conv[key]['children']:
+            f.writerow([i['fromName'].encode('utf-8'),i['displayStartDateTime'].encode('utf-8'),i['message'].encode('utf-8')])
+        if 'inbox' in conv[key]['labels']:
+            voice.archive(key)
+print len(conversations), 'conversations written to directory', outputDir
+print 'Any conversations in inbox are now archived'