Waiting for E-mails

After about two days spent waiting for an e-mail from a certain someone I was really hoping to hear from, I couldn’t take it anymore. You know that feeling when you wrote an e-mail that you feel could be sort of – I don’t know – controversial, and you really want to hear back from the person to see how they respond? Yeah – it was that kind of e-mail. I managed to push it out of my mind, but I could feel it sitting back there, taunting me, making me sort of nervous. I couldn’t concentrate on my homework, and since I have exams coming up in a little over a week, this is a problem. So what did I do? I did what any responsible geek would do: I wrote a python script that checks my e-mails every minute and plays a sound if I get an e-mail. Now this would be useful, but it would also trigger a lot of false alarms, so I had the script search the “From” field of the e-mail for the e-mail address of the person in question. I used libgmail and pygame (for audio playback).

Here’s the code (sorry for the indentation, or lack thereof):


import libgmail, pygame, curses, time

#pygame code adapted from: http://code.activestate.com/recipes/521884/

def playmusic2(soundfile):
pygame.init()
pygame.mixer.music.load(soundfile)
pygame.mixer.music.set_endevent(pygame.constan ts.USEREVENT)
pygame.event.set_allowed(pygame.constants.USEREVENT)
pygame.mixer.music.play()
pygame.event.wait()


continuing = True
print "Starting pygame..."
pygame.init()
print "Logging into Gmail..."
ga = libgmail.GmailAccount("worldgnat@gmail.com", "my_tricky_password369") #that's really my password... no really...
ga.login()
print "Logged in to Gmail, checking messages every minute..."


while continuing:
msgs = ga.getUnreadMessages()
for msg in msgs:
if (msg._authors.find("i_never_check_my_e-mail@gmail.com") != -1):
print "You've got mail!"
playmusic2("some_funky_noise.wav")
continuing = False

if continuing: time.sleep(60)

If I had time to waste I would have made it a daemon, but since I didn’t have time, I didn’t make it a daemon (which leads one to wonder why I’m wasting time writing this blog post…). Pygame is fairly versatile when it comes to sound. You can play wav, ogg, and I’m pretty sure you can play mp3. This all may depend on what libraries you have installed and on what OS (I’m using Linux, so I’m awesome). If you wanted to play a bunch of sounds you could comment out the last line of the playmusic2 method, which would prevent the function from delaying before moving on to the next instruction. The sound would play itself out unless you tried to play another sound right after it.

The process itself (to my delight and surprise) takes up very little processing power. I’m running the script right now, and python is currently using 0.7% processing power, which is even less than my window manager (plasma). Libgmail likes to throw an exception every now and then, but you can just restart it and it’s fine. If this were a problem you could throw a try-catch (or whatever it’s called in python) in the while loop that would log back in to gmail in the catch statement (might fix it). But I’m lazy, so I won’t. At least not until after I do my calculus homework.

The shrewd blogonot will notice that I imported curses and never used it. This is because I intended to use curses.getch() to exit the program gracefully, but didn’t have time to implement it. However, I realized that ctrl+c works just fine (the last few scripts I’ve written used threads that didn’t want to die, and the only way to end them was sudo killall -SIGKILL python. Not pretty.)

Thus far the person in question still hasn’t written back, so either they haven’t checked their e-mails (very likely), or what I said was so horribly offensive that they never want to talk to me again (very unlikely considering I was asking a question about some vacation photos on Facebook).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s