from BeautifulSoup import BeautifulSoup, SoupStrainer
import libgmail
import sys

def getConfirmationLink(emailAddress, password, partOfConfirmationUrl):
    ga = libgmail.GmailAccount(emailAddress, password)
    ga.login()
    folder = ga.getMessagesByFolder('inbox')
    
    for thread in folder:
        for msg in thread:
            if msg.source.count(partOfConfirmationUrl) > 0:
                msg = msg.source.replace('\n', ' ')
                href = filter(lambda word: word.count(partOfConfirmationUrl) > 0, [word for word in msg.split(' ')])
                if href:
                    href = href[0]
                    if href.count('href='):
                        href = href.replace("'", '''"''')
                        href = href[href.find('''"''') + 1:]
                        href = href[:href.find('''"''')]
                    return href
    return None
