Created
December 11, 2010 20:59
-
-
Save bmuller/737651 to your computer and use it in GitHub Desktop.
Python script to post to http://identi.ca
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import urllib, urllib2, json, sys | |
| username = "username" | |
| password = "password" | |
| def formatAtom(atom): | |
| atom = atom.strip() | |
| if atom.startswith('http://') or atom.startswith('https://'): | |
| data = urllib.urlopen("http://ur.ly/new.json?href=%s" % urllib.quote(atom)).read() | |
| return "http://ur.ly/%s" % json.loads(data)['code'] | |
| return atom | |
| if len(sys.argv) == 1: | |
| print "Usage: %s <your message>" % sys.argv[0] | |
| sys.exit(1) | |
| # join all parts if command line option was not quoted | |
| msg = " ".join(sys.argv[1:]) | |
| # get all of the atoms in the string | |
| atoms = filter(lambda x: len(x.strip()) > 0, msg.split()) | |
| # format the atoms and join then back together | |
| msg = " ".join(map(formatAtom, atoms)) | |
| if len(msg) > 140: | |
| print "Your message was %i characters too long." % (len(msg) - 140) | |
| sys.exit(1) | |
| url = "http://identi.ca/api/statuses/update.xml" | |
| passman = urllib2.HTTPPasswordMgrWithDefaultRealm() | |
| passman.add_password(None, url, username, password) | |
| authhandler = urllib2.HTTPBasicAuthHandler(passman) | |
| opener = urllib2.build_opener(authhandler) | |
| opener.open(urllib2.Request(url, urllib.urlencode({'status': msg}))) | |
| print "Posted: %s" % msg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment