Created
October 17, 2012 07:21
-
-
Save ajdiaz/3904191 to your computer and use it in GitHub Desktop.
Python standalone script to post message in 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 | |
# -*- encoding: utf-8 -*- | |
# vim:fenc=utf-8: | |
import urllib, urllib2, json, sys, os | |
import getpass | |
config_file = os.path.join(os.environ["HOME"],".postrc") | |
try: | |
username, password = tuple(map(lambda x:x.rstrip("\n"),file(config_file).readlines())) | |
except: | |
print "No configuration provided in ~/.postrc." | |
print "Falling back to interactive" | |
username = raw_input("Username: ") | |
password = getpass.getpass("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