/
ljp.py
executable file
·148 lines (111 loc) · 5.4 KB
/
ljp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#! /usr/bin/python
# -*- coding: utf-8 -*-
'''Submit a new log entry to the server.
Given all of the required information on a post will create a new
entry. The URL for the newly added entry is printed on the screen
for further peruse.
'''
__author__ = 'Mikhail Sobolev'
__copyright__ = 'Copyright (c) 2002, 2003, 2004, 2005 Mikhail Sobolev'
__version__ = '$Revision$'
__date__ = '$Date$'
if __name__ == '__main__':
import sys
from locale import getdefaultlocale
from livejournal import LiveJournal, list2list, list2mask, Config, evalue
from livejournal.config import std_parser
from livejournal.convert import args2text, text2args
lang, defaultenc = getdefaultlocale ()
parser = std_parser (usage = '%prog [options] [message text with spaces]')
parser.add_option ('-e', '--encoding', type='string', dest='encoding', default = None,
help = 'specify character encoding',
metavar = 'ENCODING')
parser.add_option ('-j', '--journal', type='string', dest='journal', default = None,
help = 'specify the journal to post to',
metavar = 'JOURNAL')
parser.add_option ('-S', '--security',
action = 'store', type = 'string', dest = 'security', default = 'public',
help = 'restrict access to the item', metavar = 'SECURITY')
parser.add_option ('-o', '--options',
action = 'store', type = 'string', dest = 'options', default = None,
help = 'list of options for the entry')
parser.add_option ('-s', '--subject',
action = 'store', type = 'string', dest = 'subject', default = None,
help = 'specify a subject for the event',
metavar = 'SUBJECT')
parser.add_option ('-m', '--mood',
action = 'store', type = 'string', dest = 'mood', default = None,
help = 'tell them what mood you are in',
metavar = 'MOOD')
parser.add_option ('-M', '--music',
action = 'store', type = 'string', dest = 'music', default = None,
help = 'tell them what music you are listening to',
metavar = 'MUSIC')
parser.add_option ('-b', '--batch',
action = 'store_true', dest = 'batch', default = None,
help = 'use in a batch mode: the text will be read from the standard input and posted right away')
parser.add_option ('-i', '--include',
action = 'store', dest = 'draft', default = None,
help = 'specify the file, which contains the draft of the message (this option is avilable only in non-batch mode)',
metavar = 'FILE')
options, args = parser.parse_args ()
# MSS: i may want to be able to specify the encoding from the configuration file
encoding = evalue (defaultenc, options.encoding)
subject = options.subject
if len (args) > 0:
body = ' '.join (args)
else:
body = ''
props = {}
if options.options is not None:
for option in list2list (options.options):
if option in [ 'preformatted', 'nocomments', 'backdated', 'noemail' ]:
props['opt_%s' % option] = 1
else:
print 'Ignoring unknown option:', option
if options.mood is not None:
props['current_mood'] = options.mood
if options.music is not None:
props['current_music'] = options.music
config = Config ()
config.load (evalue ('~/.ljrc', options.config))
server = getattr (config, options.server)
ljp = config.ljp
username = evalue (server.username, options.username)
password = evalue (server.password, options.password)
if username is None or password is None:
print "You must provide both user name and password"
sys.exit (2)
usejournal = evalue (None, options.journal)
lj = LiveJournal (config.misc.version)
info = lj.login (username, password)
security = list2mask (options.security, info.friendgroups)
if evalue (None, ljp.batch, options.batch):
body = body + '\n' + sys.stdin.read ()
else:
# At this point we have the following variables holding useful information:
# -- body
# -- subject
# -- props
# -- usejournal
# -- security
if 0:
text = args2text (info = info, event = body, usejournal = usejournal, subject = subject, props = props, security = options.security)
pass # process the text
# subject, usejournal, body, props, security = text2args (info, text)
print 'Sorry, non-batch mode is not supported yet'
sys.exit (1)
body = unicode (body, encoding)
if subject is not None:
subject = unicode (subject, encoding)
if props.has_key ('current_mood'):
props['current_mood'] = unicode (props['current_mood'], encoding)
if props.has_key ('current_music'):
props['current_music'] = unicode (props['current_music'], encoding)
entry = lj.postevent (body,
usejournal = usejournal,
subject = subject,
props = props,
security = security)
print 'Posted.\nLink to the post: http://www.livejournal.com/talkread.bml?journal=%s&itemid=%s' % (usejournal or server.username, entry.itemid*256 + entry.anum)
# vim:ts=2:sw=2:noet