import urllib2, urllib, time, datetime from xml.dom import minidom from pprint import pprint def login(delicious_user, delicious_pass): """Takes a username and password and logs into delicious""" password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() password_manager.add_password( None, 'https://api.del.icio.us/', delicious_user, delicious_pass ) auth_handler = urllib2.HTTPBasicAuthHandler(password_manager) opener = urllib2.build_opener(auth_handler) urllib2.install_opener(opener) def urlencode(d): for key, value in d.items(): if isinstance(value, unicode): d[key] = value.encode('utf-8') return urllib.urlencode(d) def post_URL(url, title, notes, tags): """function takes a title, url, description and tags and posts to delicious""" postUrl = urlencode({ 'url': url, 'description': title, 'extended': notes, 'tags': tags, 'replace': 'no' }) return minidom.parseString(urllib2.urlopen('https://api.del.icio.us/v1/posts/add?'+postUrl).read()).getElementsByTagName('result')[0].getAttribute('code') def get_user_links(username): """given a username return the most recent links for that user as a minidom""" return minidom.parseString(urllib2.urlopen('http://del.icio.us/rss/'+username).read()).getElementsByTagName('item') def read_in_file(): """Reads in the config files, returns the username and password of the main account and list of dictionarys of the subname acconts""" fp = open('/home/natbat/natbat.net/code/python/doubleposter/config.txt','r') lines = fp.readlines() fp.close() accounts = [] for line in lines: if line.startswith('#') or not line.strip(): continue if line.startswith('-'): line = line.lstrip(" -") master_username, master_password = map(lambda x: x.strip(), line.split(',')) else: username, tags, fortag = map(lambda x: x.strip(), line.split(',')) accounts.append({ 'username' : username, 'tags' : tags, 'fortag' : fortag }) return accounts, master_username, master_password def parse_date(s): """Takes a date in the format YYYY-MM-DDTHH:mm:ssZ""" return datetime.datetime(*time.strptime(s,"%Y-%m-%dT%H:%M:%SZ")[0:6]) def syncronise_account(): """Kickstarts the whole kaboodle""" linkQ = [] print 'starting sync' accounts, master_username, master_password = read_in_file() print 'logging in with username %s and password %s' % (master_username, master_password) login(master_username, master_password) for account in accounts: userLinks = get_user_links(account['username']) for link in userLinks: tagElements = link.getElementsByTagName('dc:subject') if len(tagElements) > 0: tags = tagElements[0].firstChild.data.split() else: tags = [] tempDesc = link.getElementsByTagName('description') if len(tempDesc) > 0: desc = tempDesc[0].firstChild.data else: desc = '' if account['fortag'] in tags or len(account['fortag']) == 0: tags.extend(account['tags'].split()) linkQ.append({ 'url' : link.getElementsByTagName('link')[0].firstChild.data, 'title' : link.getElementsByTagName('title')[0].firstChild.data, 'tags' : ' '.join(tags), 'desc' : desc, 'date' : parse_date(link.getElementsByTagName('dc:date')[0].firstChild.data) }) linkQ.sort(lambda x,y: cmp(x['date'], y['date'])) for linkInQ in linkQ: pprint(linkInQ) # posting in the form url title notes tags post_URL(linkInQ['url'],linkInQ['title'],linkInQ['desc'],linkInQ['tags']) time.sleep(2) if __name__ == '__main__': syncronise_account()