Adding voicemail support to the session and making it possible to save it
[gc-dialer] / src / backends / gvoice / gvoice.py
index 3f2069f..f73600e 100755 (executable)
@@ -414,20 +414,21 @@ class GVoiceBackend(object):
 
                return json
 
-       def download(self, messageId, adir):
+       def recording_url(self, messageId):
+               url = self._downloadVoicemailURL+messageId
+               return url
+
+       def download(self, messageId, targetPath):
                """
                Download a voicemail or recorded call MP3 matching the given ``msg``
                which can either be a ``Message`` instance, or a SHA1 identifier. 
-               Saves files to ``adir`` (defaults to current directory). 
                Message hashes can be found in ``self.voicemail().messages`` for example. 
                @returns location of saved file.
                @blocks
                """
-               page = self._get_page(self._downloadVoicemailURL, {"id": messageId})
-               fn = os.path.join(adir, '%s.mp3' % messageId)
-               with open(fn, 'wb') as fo:
+               page = self._get_page(self.recording_url(messageId))
+               with open(targetPath, 'wb') as fo:
                        fo.write(page)
-               return fn
 
        def is_valid_syntax(self, number):
                """
@@ -1013,6 +1014,21 @@ def grab_debug_info(username, password):
                )
 
 
+def grab_voicemails(username, password):
+       cookieFile = os.path.join(".", "raw_cookies.txt")
+       try:
+               os.remove(cookieFile)
+       except OSError:
+               pass
+
+       backend = GVoiceBackend(cookieFile)
+       backend.login(username, password)
+       voicemails = list(backend.get_voicemails())
+       for voicemail in voicemails:
+               print voicemail.id
+               backend.download(voicemail.id, ".")
+
+
 def main():
        import sys
        logging.basicConfig(level=logging.DEBUG)
@@ -1022,6 +1038,7 @@ def main():
                password = args[2]
 
        grab_debug_info(username, password)
+       grab_voicemails(username, password)
 
 
 if __name__ == "__main__":