Gmail Driven Web Service with Python
March 12, 2010 2 Comments
I had a basic task: upload video files to a content server. The videos are received by email/gmail and I have to do a lot of manual work to put them up. So I looked to automate this task with my friend Python.
After some research I found libgmail, but the current version is out of date and no longer works with gmail. You will get an error: libgmail.GmailLoginFailure: ‘Login failed. (Wrong username/password?)’). I found a patch for libgmail.py here. Update: this links seems to be broken, but Google still has it here.
Sample code follows:
#!/usr/bin/env pythonimport libgmailimport osga = libgmail.GmailAccount("USERNAME", "PASSWORD")ga.login()folder = ga.getMessagesByLabel("video")for thread in folder:print "Processing thread:", thread.id, "Subject:", thread.subjectfor msg in thread:print "Message:", msg.authorfor file in msg.attachments:print "Saving file: " + file.filenameout = open(file.filename, "w")out.write(file._getContent())out.close()print "Uploading the file: " + file.filenameos.system("scp " + file.filename + " user@server:")print "Sending a response to: ", msg.senderresponse=libgmail.GmailComposedMessage(msg.sender, thread.subject, "Done")ga.sendMessage(response)print "Archiving message: " + thread.idthread.removeLabel("video");thread.addLabel("video-archive");