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