view bitsyblog/blogme.py @ 45:c228832db770

fix blogme
author k0s <k0scist@gmail.com>
date Tue, 24 Nov 2009 19:20:47 -0500
parents 0e7f56709c90
children 66bfcceea3b4
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
from utils import datestamp

# 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('-f', '--file')
    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)

    if options.file:
        # get the blog
        if args:
            msg = ' '.join(args)
        else:
            msg = tmpbuffer()
        options.file = os.path.join(options.file, 'entries')
        for opt in 'private', 'secret':
            if getattr(options, opt):
                options.file = os.path.join(options.file, opt)
                break
        else:
            options.file = os.path.join(options.file, 'public')
        options.file = os.path.join(options.file, datestamp())
        f = file(options.file, 'w')
        print >> f, msg
        f.close()
        sys.exit(0)

    # parse dotfile config
    config = ConfigParser()
    _config = {} # config to write out
    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)

    # get default host, if available
    if options.host:
        if config.has_option('DEFAULTS', 'host'):
            print "Make %s the default host? (Y/n): ",
            answer = raw_input()
            if not anwer.lower().startswith('n'):
                _config['default'] = options.host
    else:
        if config.has_option('DEFAULTS', 'host'):
            options.host = config.get('DEFAULTS', 'host')
        else:
            print "Enter URL of host: ",
            host = raw_input()
            _config['default'] = host

    # determine user name and password
    fields = [ 'user', 'password' ]
    for field in fields:
        if hasattr(options, field):
            if (not config.has_option(options.host, field)) or (config.get(options.host, field) != getattr(options, field)):
                _config[field] = getattr(options, field)
        else:
            print "%s: " % field, 
            setattr(options, field, raw_input())
            _config[field] = getattr(options, field)

    # write the dotfile if it doesn't exist
    if _config:
        if 'default' in _config:
            if not config.has_section('DEFAULTS'):
                config.add_section('DEFAULTS')
            config.set('DEFAULTS', 'host', options.host)
        if not config.has_section(options.host):
            config.add_section(options.host)
        for field in fields:
            if not config.has_option(options.host, field):
                config.set(options.host, field, getattr(options, field))
        config.write()

    # 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, options.user, options.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()