comparison blogme.py @ 0:e3823be6a423

initial commit of bitsyblog, from https://svn.openplans.org/svn/standalone/bitsyblog/trunk/
author k0s <k0scist@gmail.com>
date Sat, 12 Sep 2009 16:06:57 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e3823be6a423
1 #!/usr/bin/env python
2
3 import optparse
4 import os
5 import subprocess
6 import sys
7 import tempfile
8 import urllib2
9
10 # global variables
11
12 EDITOR='emacs -nw'
13 SERVER='http://bitsyblog.biz'
14 dotfile='.blogme'
15
16 parser = optparse.OptionParser()
17 parser.add_option('-s', '--server', default=SERVER)
18 parser.add_option('-u', '--user')
19 parser.add_option('-p', '--password')
20 parser.add_option('--private', action='store_true', default=False)
21 parser.add_option('--secret', action='store_true', default=False)
22
23 options, args = parser.parse_args()
24
25 if options.private and options.secret:
26 print "post can't be secret and private!"
27 sys.exit(1)
28
29 # parse dotfile
30
31 home = os.environ.get('HOME')
32 if home:
33 dotfile = os.path.join(home, dotfile)
34 if os.path.exists(dotfile):
35 prefs = file(dotfile).read().split('\n')
36 prefs = [ i for i in prefs if i.strip() ]
37 prefs = [ [ j.strip() for j in i.split(':', 1) ] for i in prefs
38 if ':' in i] # probably not necessary
39 prefs = dict(prefs)
40 else:
41 prefs = {}
42
43 # determine user name and password
44 fields = [ 'user', 'password' ]
45 for field in fields:
46 globals()[field] = prefs.get(field)
47
48 optval = getattr(options, field)
49 if optval:
50 password = None # needed to ensure prompting for pw from command line
51 globals()[field] = optval
52
53 if globals()[field] is None:
54 globals()[field] = raw_input('%s: ' % field)
55 assert user is not None
56 assert password is not None
57
58 # write the dotfile if it doesn't exist
59 if not os.path.exists(dotfile):
60 preffile = file(dotfile, 'w')
61 print >> preffile, 'user: %s' % user
62 print >> preffile, 'password: %s' % password
63 preffile.close()
64 os.chmod(dotfile, 0600)
65
66 def tmpbuffer(editor=EDITOR):
67 """open an editor and retreive the resulting editted buffer"""
68 tmpfile = tempfile.mktemp(suffix='.txt')
69 cmdline = editor.split()
70 cmdline.append(tmpfile)
71 edit = subprocess.call(cmdline)
72 buffer = file(tmpfile).read().strip()
73 os.remove(tmpfile)
74 return buffer
75
76 # get the blog
77
78 if args:
79 msg = ' '.join(args)
80 else:
81 msg = tmpbuffer()
82
83 # open the url
84
85 url = '/'.join((options.server, user))
86 url += '?auth=digest' # specify authentication method
87
88 if options.private:
89 url += '&privacy=private'
90 if options.secret:
91 url += '&privacy=secret'
92
93 authhandler = urllib2.HTTPDigestAuthHandler()
94 authhandler.add_password('bitsyblog', url, user, password)
95 opener = urllib2.build_opener(authhandler)
96 urllib2.install_opener(opener)
97
98 try:
99 url = urllib2.urlopen(url, data=msg)
100 print url.url # print the blog post's url
101 except urllib2.HTTPError:
102 pass