python livejournal
felipeДнес реших да потърся начин да публикувам в блога си програмно чрез Python. След тестване на различни кодове успях да модифицирам този – http://hewgill.com/ljdump/ и се получи следното приложенийце, с което публикувах предишния запис:
#!/usr/bin/python
#
# ljdump.py – livejournal archiver
# Greg Hewgill <greg@hewgill.com> http://hewgill.com
# Version 1.2
#
# $Id: ljdump.py 17 2006-09-08 08:46:56Z greg $
#
# This program reads the journal entries from a livejournal (or compatible)
# blog site and archives them in a subdirectory named after the journal name.
#
# The configuration is read from “ljdump.config”. A sample configuration is
# provided in “ljdump.config.sample”, which should be copied and then edited.
# The configuration settings are:
#
# server – The XMLRPC server URL. This should only need to be changed
# if you are dumping a journal that is livejournal-compatible
# but is not livejournal itself.
#
# username – The livejournal user name. A subdirectory will be created
# with this same name to store the journal entries.
#
# password – The account password. This password is never sent in the
# clear; the livejournal “challenge” password mechanism is used.
#
# This program may be run as often as needed to bring the backup copy up
# to date. Both new and updated items are downloaded.
#
# LICENSE
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the author be held liable for any damages
# arising from the use of this software.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
#
# Copyright (c) 2005-2006 Greg Hewgill
import codecs, md5, os, pickle, pprint, re, sys, urllib2, xml.dom.minidom, xmlrpclib
from xml.sax import saxutils
def calcchallenge(challenge, password):
return md5.new(challenge+md5.new(password).hexdigest()).hexdigest()
def flatresponse(response):
r = {}
while True:
name = response.readline()
if len(name) == 0:
break
if name[-1] == '\n':
name = name[:len(name)-1]
value = response.readline()
if value[-1] == '\n':
value = value[:len(value)-1]
r[name] = value
return r
def getljsession(username, password):
r = urllib2.urlopen(Server+”/interface/flat”, “mode=getchallenge”)
response = flatresponse(r)
r.close()
r = urllib2.urlopen(Server+”/interface/flat”, “mode=sessiongenerate&user=%s&auth_method=challenge&auth_challenge=%s&auth_response=%s” % (username, response['challenge'], calcchallenge(response['challenge'], password)))
response = flatresponse(r)
r.close()
return response['ljsession']
def dochallenge(params, password):
challenge = server.LJ.XMLRPC.getchallenge()
params.update({
'auth_method': “challenge”,
'auth_challenge': challenge['challenge'],
'auth_response': calcchallenge(challenge['challenge'], password)
})
return params
config = xml.dom.minidom.parse(“ljdump.config”)
Server = config.documentElement.getElementsByTagName(“server”)[0].childNodes[0].data
Username = config.documentElement.getElementsByTagName(“username”)[0].childNodes[0].data
Password = config.documentElement.getElementsByTagName(“password”)[0].childNodes[0].data
m = re.search(“(.*)/interface/xmlrpc”, Server)
if m:
Server = m.group(1)
print “Fetching journal entries for: %s” % Username
try:
os.mkdir(Username)
print “Created subdirectory: %s” % Username
except:
pass
ljsession = getljsession(Username, Password)
server = xmlrpclib.ServerProxy(Server+”/interface/xmlrpc”)
newentries = 0
newcomments = 0
errors = 0
lastsync = “”
lastmaxid = 0
try:
f = open(“%s/.last” % Username, “r”)
lastsync = f.readline()
if lastsync[-1] == '\n':
lastsync = lastsync[:len(lastsync)-1]
lastmaxid = f.readline()
if len(lastmaxid) > 0 and lastmaxid[-1] == '\n':
lastmaxid = lastmaxid[:len(lastmaxid)-1]
if lastmaxid == “”:
lastmaxid = 0
else:
lastmaxid = int(lastmaxid)
f.close()
except:
pass
origlastsync = lastsync
r = server.LJ.XMLRPC.login(dochallenge({
'username': Username,
'ver': 1,
'getpickws': 1,
'getpickwurls': 1,
}, Password))
userpics = dict(zip(r['pickws'], r['pickwurls']))
userpics['*'] = r['defaultpicurl']
r = server.LJ.XMLRPC.syncitems(dochallenge({
'username': Username,
'ver': 1,
'lastsync': lastsync,
}, Password))
#pprint.pprint(r)
e = server.LJ.XMLRPC.postevent(dochallenge({
'username': Username,
'ver': 1,
'selecttype': “one”,
'event': 'please work',
'subject':'test',
'year':'2008',
'mon':'8',
'day':'26',
'hour':'12',
'min':'8'
}, Password))






