comparison 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
comparison
equal deleted inserted replaced
43:8b9aa9f40fa5 44:0e7f56709c90
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2
3 """
4 command line blogger
5 """
2 6
3 import optparse 7 import optparse
4 import os 8 import os
5 import subprocess 9 import subprocess
6 import sys 10 import sys
7 import tempfile 11 import tempfile
8 import urllib2 12 import urllib2
9 13
14 from ConfigParser import ConfigParser
15
10 # global variable 16 # global variable
11 dotfile='.blogme' 17 CONFIG = '.blogme'
12 18
13 def tmpbuffer(editor=None): 19 def tmpbuffer(editor=None):
14 """open an editor and retreive the resulting editted buffer""" 20 """open an editor and retreive the resulting editted buffer"""
15 if not editor: 21 if not editor:
16 editor = os.environ['EDITOR'] 22 editor = os.environ['EDITOR']
22 os.remove(tmpfile) 28 os.remove(tmpfile)
23 return buffer 29 return buffer
24 30
25 def main(args=sys.argv[1:]): 31 def main(args=sys.argv[1:]):
26 32
27 # create the option parser 33 # parse command line options
28 parser = optparse.OptionParser() 34 parser = optparse.OptionParser()
29 parser.add_option('-s', '--server') 35 parser.add_option('-c', '--config')
36 parser.add_option('-H', '--host')
30 parser.add_option('-u', '--user') 37 parser.add_option('-u', '--user')
31 parser.add_option('-p', '--password') 38 parser.add_option('-p', '--password')
32 parser.add_option('--private', action='store_true', default=False) 39 parser.add_option('--private', action='store_true', default=False)
33 parser.add_option('--secret', action='store_true', default=False) 40 parser.add_option('--secret', action='store_true', default=False)
34
35 options, args = parser.parse_args() 41 options, args = parser.parse_args()
36 42
37 # sanity check 43 # sanity check
38 if options.private and options.secret: 44 if options.private and options.secret:
39 print "post can't be secret and private!" 45 print "post can't be secret and private!"
40 sys.exit(1) 46 sys.exit(1)
41 47
42 # parse dotfile 48 # parse dotfile config
43 home = os.environ.get('HOME') 49 config = ConfigParser()
44 if home: 50 if not options.config:
45 dotfile = os.path.join(home, dotfile) 51 home = os.environ['HOME']
46 if os.path.exists(dotfile): 52 options.config = os.path.join(home, CONFIG)
47 prefs = file(dotfile).read().split('\n') 53 if os.path.exists(options.config):
48 prefs = [ i for i in prefs if i.strip() ] 54 config.read(options.config)
49 prefs = [ [ j.strip() for j in i.split(':', 1) ] for i in prefs
50 if ':' in i] # probably not necessary
51 prefs = dict(prefs)
52 else:
53 prefs = {}
54 55
55 # determine user name and password 56 # determine user name and password
56 fields = [ 'user', 'password' ] 57 fields = [ 'user', 'password' ]
57 for field in fields: 58 for field in fields:
58 globals()[field] = prefs.get(field) 59 globals()[field] = prefs.get(field)
66 globals()[field] = raw_input('%s: ' % field) 67 globals()[field] = raw_input('%s: ' % field)
67 assert user is not None 68 assert user is not None
68 assert password is not None 69 assert password is not None
69 70
70 # write the dotfile if it doesn't exist 71 # write the dotfile if it doesn't exist
71 if not os.path.exists(dotfile): 72 # if not os.path.exists(dotfile):
72 preffile = file(dotfile, 'w') 73 # preffile = file(dotfile, 'w')
73 print >> preffile, 'user: %s' % user 74 # print >> preffile, 'user: %s' % user
74 print >> preffile, 'password: %s' % password 75 # print >> preffile, 'password: %s' % password
75 preffile.close() 76 # preffile.close()
76 os.chmod(dotfile, 0600) 77 # os.chmod(dotfile, 0600)
77 78
78 79
79 # get the blog 80 # get the blog
80 if args: 81 if args:
81 msg = ' '.join(args) 82 msg = ' '.join(args)
82 else: 83 else:
83 msg = tmpbuffer() 84 msg = tmpbuffer()
84 85
85 # open the url 86 # open the url
86 url = '/'.join((options.server, user)) 87 url = options.host
87 url += '?auth=digest' # specify authentication method 88 url += '?auth=digest' # specify authentication method
88 if options.private: 89 if options.private:
89 url += '&privacy=private' 90 url += '&privacy=private'
90 if options.secret: 91 if options.secret:
91 url += '&privacy=secret' 92 url += '&privacy=secret'
92 authhandler = urllib2.HTTPDigestAuthHandler() 93 authhandler = urllib2.HTTPDigestAuthHandler()
93 authhandler.add_password('bitsyblog', url, user, password) 94 authhandler.add_password('bitsyblog', url, user, password)
94 opener = urllib2.build_opener(authhandler) 95 opener = urllib2.build_opener(authhandler)
95 urllib2.install_opener(opener) 96 urllib2.install_opener(opener)
96
97 try: 97 try:
98 url = urllib2.urlopen(url, data=msg) 98 url = urllib2.urlopen(url, data=msg)
99 print url.url # print the blog post's url 99 print url.url # print the blog post's url
100 except urllib2.HTTPError: 100 except urllib2.HTTPError, e:
101 pass 101 print e
102 102
103 if __name__ == '__main__': 103 if __name__ == '__main__':
104 main() 104 main()