Mercurial > hg > bitsyblog
view bitsyblog/blogme.py @ 44:0e7f56709c90
cleanup of blogme, not yet finished
author | k0s <k0scist@gmail.com> |
---|---|
date | Wed, 11 Nov 2009 19:39:27 -0500 |
parents | 8b9aa9f40fa5 |
children | c228832db770 |
line wrap: on
line source
#!/usr/bin/env python """ command line blogger """ import optparse import os import subprocess import sys import tempfile import urllib2 from ConfigParser import ConfigParser # global variable CONFIG = '.blogme' def tmpbuffer(editor=None): """open an editor and retreive the resulting editted buffer""" if not editor: editor = os.environ['EDITOR'] tmpfile = tempfile.mktemp(suffix='.txt') cmdline = editor.split() cmdline.append(tmpfile) edit = subprocess.call(cmdline) buffer = file(tmpfile).read().strip() os.remove(tmpfile) return buffer def main(args=sys.argv[1:]): # parse command line options parser = optparse.OptionParser() parser.add_option('-c', '--config') parser.add_option('-H', '--host') parser.add_option('-u', '--user') parser.add_option('-p', '--password') parser.add_option('--private', action='store_true', default=False) parser.add_option('--secret', action='store_true', default=False) options, args = parser.parse_args() # sanity check if options.private and options.secret: print "post can't be secret and private!" sys.exit(1) # parse dotfile config config = ConfigParser() if not options.config: home = os.environ['HOME'] options.config = os.path.join(home, CONFIG) if os.path.exists(options.config): config.read(options.config) # determine user name and password fields = [ 'user', 'password' ] for field in fields: globals()[field] = prefs.get(field) optval = getattr(options, field) if optval: password = None # needed to ensure prompting for pw from command line globals()[field] = optval if globals()[field] is None: globals()[field] = raw_input('%s: ' % field) assert user is not None assert password is not None # write the dotfile if it doesn't exist # if not os.path.exists(dotfile): # preffile = file(dotfile, 'w') # print >> preffile, 'user: %s' % user # print >> preffile, 'password: %s' % password # preffile.close() # os.chmod(dotfile, 0600) # get the blog if args: msg = ' '.join(args) else: msg = tmpbuffer() # open the url url = options.host url += '?auth=digest' # specify authentication method if options.private: url += '&privacy=private' if options.secret: url += '&privacy=secret' authhandler = urllib2.HTTPDigestAuthHandler() authhandler.add_password('bitsyblog', url, user, password) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener) try: url = urllib2.urlopen(url, data=msg) print url.url # print the blog post's url except urllib2.HTTPError, e: print e if __name__ == '__main__': main()