#!/usr/bin/python """get lists from tadalist""" import sys, os, re, pwd import urllib, urllib2, cookielib, urlparse from BeautifulSoup import BeautifulSoup website = 'jcomeauictx.tadalist.com' def loginform(url): loginform = 'no login form found' page = fetch(url) forms = page.findAll('form') for form in forms: if form.get('id') and 'login' in form.get('id').lower(): loginform = form break return loginform def makepost(inputs): postdata = {} for input in inputs: postdata[input.get('name') or 'none'] = input.get('value') or '' return postdata def login(): pattern = '^machine\s+%s\s+login\s+(\S+)\s+password\s+(.*\S)\s*$' % website login = filter(re.compile(pattern).match, open(os.path.join( pwd.getpwuid(os.geteuid())[5], '.netrc')).readlines()) login = re.compile(pattern).match(login[-1]).groups() # use last one in list username, password = login url = 'http://%s/session/new' % website form = loginform(url) url = urlparse.urljoin(https(url), form.get('action')) inputs = form.findAll('input') postdata = makepost(inputs) postdata['username'], postdata['password'] = username, password debug('logging in...') page = post(url, urllib.urlencode(postdata)).prettify() if username in page: debug('login successful') else: print page raise Exception, 'failed to log in; did you set up $HOME/.netrc correctly?' return True def https(url): parsed = list(urlparse.urlsplit(url)) parsed[0] = 'https' return urlparse.urlunsplit(parsed) def init(): cookiejar = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) urllib2.install_opener(opener) login() def fetch(url): socket = urllib2.urlopen(url) page = BeautifulSoup(socket.read()) socket.close() return page def post(url, data): debug('sending data "%s" to url "%s"' % (data, url)) socket = urllib2.urlopen(url, data) page = BeautifulSoup(socket.read()) socket.close() return page def getlists(): page = fetch('http://%s/lists' % website) atags = page.findAll('a') for tag in atags: href = tag.get('href') if href.startswith('/lists/') and not href.endswith('/new'): url = 'http://%s%s/email' % (website, href) post(url, '') def debug(message): if True: print >>sys.stderr, message init() # no matter what, we have to login and set up handler first if __name__ == '__main__': getlists() else: debug('ready for testing: %s' % dir())