]> www.wagner.pp.ru Git - oss/ljdump.git/blob - ljdump.py
add +x
[oss/ljdump.git] / ljdump.py
1 #!/usr/bin/python
2 #
3 # ljdump.py - livejournal archiver
4 # Greg Hewgill <greg@hewgill.com> http://hewgill.com
5 # Version 1.0.2
6 #
7 # $Id$
8 #
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.
11 #
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:
15 #
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.
19 #
20 #   username - The livejournal user name. A subdirectory will be created
21 #              with this same name to store the journal entries.
22 #
23 #   password - The account password. This password is never sent in the
24 #              clear; the livejournal "challenge" password mechanism is used.
25 #
26 # This program may be run as often as needed to bring the backup copy up
27 # to date. Only new items are downloaded.
28 #
29 # LICENSE
30 #
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.
34 #
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:
38 #
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.
46 #
47 # Copyright (c) 2005 Greg Hewgill
48
49 import codecs, md5, os, pprint, sys, xml.dom.minidom, xmlrpclib
50 from xml.sax import saxutils
51
52 def dochallenge(params, password):
53     challenge = server.LJ.XMLRPC.getchallenge()
54     params.update({
55         'auth_method': "challenge",
56         'auth_challenge': challenge['challenge'],
57         'auth_response': md5.new(challenge['challenge']+md5.new(password).hexdigest()).hexdigest()
58     })
59     return params
60
61 def dumpelement(f, name, e):
62     f.write("<%s>\n" % name)
63     for k in e.keys():
64         if isinstance(e[k], {}.__class__):
65             dumpelement(f, k, e[k])
66         else:
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)
70
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)
75     f.close()
76
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
81
82 print "Fetching journal entries for: %s" % Username
83 try:
84     os.mkdir(Username)
85     print "Created subdirectory: %s" % Username
86 except:
87     pass
88
89 server = xmlrpclib.ServerProxy(Server)
90
91 total = 0
92 fetched = 0
93 errors = 0
94
95 last = ""
96 while True:
97     r = server.LJ.XMLRPC.syncitems(dochallenge({
98         'username': Username,
99         'ver': 1,
100         'lastsync': last,
101     }, Password))
102     #pprint.pprint(r)
103     if len(r['syncitems']) == 0:
104         break
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']
110                 try:
111                     e = server.LJ.XMLRPC.getevents(dochallenge({
112                         'username': Username,
113                         'ver': 1,
114                         'selecttype': "one",
115                         'itemid': item['item'][2:],
116                     }, Password))
117                     writedump(fn, e['events'][0])
118                     fetched += 1
119                 except xmlrpclib.Fault, x:
120                     print "Error getting item: %s" % item['item']
121                     pprint.pprint(x)
122                     errors += 1
123             total += 1
124         last = item['time']
125 print "%d total entries" % total
126 print "%d fetched entries" % fetched
127 if errors > 0:
128     print "%d errors" % errors