3 # ljdump.py - livejournal archiver
4 # Greg Hewgill <greg@hewgill.com> http://hewgill.com
9 # This program reads the journal entries from a livejournal (or compatible)
10 # blog site and archives them in a subdirectory named after the journal name.
12 # The configuration is read from "ljdump.config". A sample configuration is
13 # provided in "ljdump.config.sample", which should be copied and then edited.
14 # The configuration settings are:
16 # server - The XMLRPC server URL. This should only need to be changed
17 # if you are dumping a journal that is livejournal-compatible
18 # but is not livejournal itself.
20 # username - The livejournal user name. A subdirectory will be created
21 # with this same name to store the journal entries.
23 # password - The account password. This password is never sent in the
24 # clear; the livejournal "challenge" password mechanism is used.
26 # This program may be run as often as needed to bring the backup copy up
27 # to date. Only new items are downloaded.
31 # This software is provided 'as-is', without any express or implied
32 # warranty. In no event will the author be held liable for any damages
33 # arising from the use of this software.
35 # Permission is granted to anyone to use this software for any purpose,
36 # including commercial applications, and to alter it and redistribute it
37 # freely, subject to the following restrictions:
39 # 1. The origin of this software must not be misrepresented; you must not
40 # claim that you wrote the original software. If you use this software
41 # in a product, an acknowledgment in the product documentation would be
42 # appreciated but is not required.
43 # 2. Altered source versions must be plainly marked as such, and must not be
44 # misrepresented as being the original software.
45 # 3. This notice may not be removed or altered from any source distribution.
47 # Copyright (c) 2005 Greg Hewgill
49 import codecs, md5, os, pprint, sys, xml.dom.minidom, xmlrpclib
50 from xml.sax import saxutils
52 def dochallenge(params, password):
53 challenge = server.LJ.XMLRPC.getchallenge()
55 'auth_method': "challenge",
56 'auth_challenge': challenge['challenge'],
57 'auth_response': md5.new(challenge['challenge']+md5.new(password).hexdigest()).hexdigest()
61 def dumpelement(f, name, e):
62 f.write("<%s>\n" % name)
64 if isinstance(e[k], {}.__class__):
65 dumpelement(f, k, e[k])
67 s = unicode(str(e[k]), "UTF-8")
68 f.write("<%s>%s</%s>\n" % (k, saxutils.escape(s), k))
69 f.write("</%s>\n" % name)
71 def writedump(fn, event):
72 f = codecs.open(fn, "w", "UTF-8")
73 f.write("""<?xml version="1.0"?>\n""")
74 dumpelement(f, "event", event)
77 config = xml.dom.minidom.parse("ljdump.config")
78 Server = config.documentElement.getElementsByTagName("server")[0].childNodes[0].data
79 Username = config.documentElement.getElementsByTagName("username")[0].childNodes[0].data
80 Password = config.documentElement.getElementsByTagName("password")[0].childNodes[0].data
82 print "Fetching journal entries for: %s" % Username
85 print "Created subdirectory: %s" % Username
89 server = xmlrpclib.ServerProxy(Server)
97 r = server.LJ.XMLRPC.syncitems(dochallenge({
103 if len(r['syncitems']) == 0:
105 for item in r['syncitems']:
106 if item['item'][0] == 'L':
107 fn = "%s/%s" % (Username, item['item'])
108 if not os.access(fn, os.F_OK):
109 print "Fetching journal entry %s" % item['item']
111 e = server.LJ.XMLRPC.getevents(dochallenge({
112 'username': Username,
115 'itemid': item['item'][2:],
117 writedump(fn, e['events'][0])
119 except xmlrpclib.Fault, x:
120 print "Error getting item: %s" % item['item']
125 print "%d total entries" % total
126 print "%d fetched entries" % fetched
128 print "%d errors" % errors