forked from ghewgill/ljdump
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ljdump.py
executable file
·406 lines (369 loc) · 14.7 KB
/
ljdump.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ljdump.py - livejournal archiver
# Greg Hewgill <greg@hewgill.com> https://hewgill.com/
# Version 1.5.1
#
# 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-2010 Greg Hewgill and contributors
import argparse, codecs, os, pickle, pprint, re, shutil, sys, urllib2, xml.dom.minidom, xmlrpclib
from getpass import getpass
import urllib
from xml.sax import saxutils
from datetime import *
MimeExtensions = {
"image/gif": ".gif",
"image/jpeg": ".jpg",
"image/png": ".png",
}
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(server, username, password):
"""Log in with password and get session cookie."""
qs = "mode=sessiongenerate&user=%s&auth_method=clear&password=%s" % (urllib.quote(username), urllib.quote(password))
r = urllib2.urlopen(server+"/interface/flat", qs)
response = flatresponse(r)
r.close()
return response['ljsession']
def dumpelement(f, name, e):
f.write("<%s>\n" % name)
for k in e.keys():
if isinstance(e[k], {}.__class__):
dumpelement(f, k, e[k])
else:
try:
s = unicode(str(e[k]), "UTF-8")
except UnicodeDecodeError:
# fall back to Latin-1 for old entries that aren't UTF-8
s = unicode(str(e[k]), "cp1252")
f.write("<%s>%s</%s>\n" % (k, saxutils.escape(s), k))
f.write("</%s>\n" % name)
def writedump(fn, event):
f = codecs.open(fn, "w", "UTF-8")
f.write("""<?xml version="1.0"?>\n""")
dumpelement(f, "event", event)
f.close()
def writelast(journal, lastsync, lastmaxid):
f = open("%s/.last" % journal, "w")
f.write("%s\n" % lastsync)
f.write("%s\n" % lastmaxid)
f.close()
def createxml(doc, name, map):
e = doc.createElement(name)
for k in map.keys():
me = doc.createElement(k)
me.appendChild(doc.createTextNode(map[k]))
e.appendChild(me)
return e
def gettext(e):
if len(e) == 0:
return ""
return e[0].firstChild.nodeValue
def ljdump(Server, Username, Password, Journal, verbose=True):
m = re.search("(.*)/interface/xmlrpc", Server)
if m:
Server = m.group(1)
if Username != Journal:
authas = "&authas=%s" % Journal
else:
authas = ""
if verbose:
print("Fetching journal entries for: %s" % Journal)
try:
os.mkdir(Journal)
print "Created subdirectory: %s" % Journal
except:
pass
ljsession = getljsession(Server, Username, Password)
server = xmlrpclib.ServerProxy(Server+"/interface/xmlrpc")
def authed(params):
"""Transform API call params to include authorization."""
return dict(auth_method='clear', username=Username, password=Password, **params)
newentries = 0
newcomments = 0
errors = 0
lastsync = ""
lastmaxid = 0
try:
f = open("%s/.last" % Journal, "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(authed({
'ver': 1,
'getpickws': 1,
'getpickwurls': 1,
}))
userpics = dict(zip(map(str, r['pickws']), r['pickwurls']))
if r['defaultpicurl']:
userpics['*'] = r['defaultpicurl']
while True:
r = server.LJ.XMLRPC.syncitems(authed({
'ver': 1,
# 'lastsync': lastsync, # this one is not helpful when you want update existing stuff
'usejournal': Journal,
}))
if len(r['syncitems']) == 0:
break
for item in r['syncitems']:
if item['item'][0] == 'L':
print "Fetching journal entry %s (%s)" % (item['item'], item['action'])
try:
e = server.LJ.XMLRPC.getevents(authed({
'ver': 1,
'selecttype': "one",
'itemid': item['item'][2:],
'usejournal': Journal,
}))
if e['events']:
ev = e['events'][0]
newentries += 1
# Process the event
pprint.pprint(ev)
ev['event'] = re.sub('http://(edu.|staff.|)mmcs.sfedu.ru/~ulysses',
'https://a-pelenitsyn.github.io/Files',
str(ev['event']))
# Store locally
writedump("%s/%s" % (Journal, item['item']), ev)
# Write back the event to server
d = datetime.strptime(ev['eventtime'], '%Y-%m-%d %H:%M:%S')
ev1 = dict(lineendings="pc", year=d.year, mon=d.month, day=d.day,
hour=d.hour, min=d.minute, **ev)
r1 = server.LJ.XMLRPC.editevent(authed(ev1))
else:
print "Unexpected empty item: %s" % item['item']
errors += 1
except xmlrpclib.Fault, x:
print "Error getting item: %s" % item['item']
pprint.pprint(x)
errors += 1
lastsync = item['time']
writelast(Journal, lastsync, lastmaxid)
print "Good now, bye!"
os._exit(os.EX_OK)
# The following code doesn't work because the server rejects our repeated calls.
# https://www.livejournal.com/doc/server/ljp.csp.xml-rpc.getevents.html
# contains the statement "You should use the syncitems selecttype in
# conjuntions [sic] with the syncitems protocol mode", but provides
# no other explanation about how these two function calls should
# interact. Therefore we just do the above slow one-at-a-time method.
#while True:
# r = server.LJ.XMLRPC.getevents(authed({
# 'ver': 1,
# 'selecttype': "syncitems",
# 'lastsync': lastsync,
# }))
# pprint.pprint(r)
# if len(r['events']) == 0:
# break
# for item in r['events']:
# writedump("%s/L-%d" % (Journal, item['itemid']), item)
# newentries += 1
# lastsync = item['eventtime']
if verbose:
print("Fetching journal comments for: %s" % Journal)
try:
f = open("%s/comment.meta" % Journal)
metacache = pickle.load(f)
f.close()
except:
metacache = {}
try:
f = open("%s/user.map" % Journal)
usermap = pickle.load(f)
f.close()
except:
usermap = {}
maxid = lastmaxid
while True:
try:
try:
r = urllib2.urlopen(urllib2.Request(Server+"/export_comments.bml?get=comment_meta&startid=%d%s" % (maxid+1, authas), headers = {'Cookie': "ljsession="+ljsession}))
meta = xml.dom.minidom.parse(r)
except Exception, x:
print "*** Error fetching comment meta, possibly not community maintainer?"
print "***", x
break
finally:
try:
r.close()
except AttributeError: # r is sometimes a dict for unknown reasons
pass
for c in meta.getElementsByTagName("comment"):
id = int(c.getAttribute("id"))
metacache[id] = {
'posterid': c.getAttribute("posterid"),
'state': c.getAttribute("state"),
}
if id > maxid:
maxid = id
for u in meta.getElementsByTagName("usermap"):
usermap[u.getAttribute("id")] = u.getAttribute("user")
if maxid >= int(meta.getElementsByTagName("maxid")[0].firstChild.nodeValue):
break
f = open("%s/comment.meta" % Journal, "w")
pickle.dump(metacache, f)
f.close()
f = open("%s/user.map" % Journal, "w")
pickle.dump(usermap, f)
f.close()
newmaxid = maxid
maxid = lastmaxid
while True:
try:
try:
r = urllib2.urlopen(urllib2.Request(Server+"/export_comments.bml?get=comment_body&startid=%d%s" % (maxid+1, authas), headers = {'Cookie': "ljsession="+ljsession}))
meta = xml.dom.minidom.parse(r)
except Exception, x:
print "*** Error fetching comment body, possibly not community maintainer?"
print "***", x
break
finally:
r.close()
for c in meta.getElementsByTagName("comment"):
id = int(c.getAttribute("id"))
jitemid = c.getAttribute("jitemid")
comment = {
'id': str(id),
'parentid': c.getAttribute("parentid"),
'subject': gettext(c.getElementsByTagName("subject")),
'date': gettext(c.getElementsByTagName("date")),
'body': gettext(c.getElementsByTagName("body")),
'state': metacache[id]['state'],
}
if usermap.has_key(c.getAttribute("posterid")):
comment["user"] = usermap[c.getAttribute("posterid")]
try:
entry = xml.dom.minidom.parse("%s/C-%s" % (Journal, jitemid))
except:
entry = xml.dom.minidom.getDOMImplementation().createDocument(None, "comments", None)
found = False
for d in entry.getElementsByTagName("comment"):
if int(d.getElementsByTagName("id")[0].firstChild.nodeValue) == id:
found = True
break
if found:
print "Warning: downloaded duplicate comment id %d in jitemid %s" % (id, jitemid)
else:
entry.documentElement.appendChild(createxml(entry, "comment", comment))
f = codecs.open("%s/C-%s" % (Journal, jitemid), "w", "UTF-8")
entry.writexml(f)
f.close()
newcomments += 1
if id > maxid:
maxid = id
if maxid >= newmaxid:
break
lastmaxid = maxid
writelast(Journal, lastsync, lastmaxid)
if Username == Journal:
if verbose:
print("Fetching userpics for: %s" % Username)
f = open("%s/userpics.xml" % Username, "w")
print >>f, """<?xml version="1.0"?>"""
print >>f, "<userpics>"
for p in userpics:
print >>f, """<userpic keyword="%s" url="%s" />""" % (p, userpics[p])
pic = urllib2.urlopen(userpics[p])
ext = MimeExtensions.get(pic.info()["Content-Type"], "")
picfn = re.sub(r'[*?\\/:<>"|]', "_", p)
try:
picfn = codecs.utf_8_decode(picfn)[0]
picf = open("%s/%s%s" % (Username, picfn, ext), "wb")
except:
# for installations where the above utf_8_decode doesn't work
picfn = "".join([ord(x) < 128 and x or "_" for x in picfn])
picf = open("%s/%s%s" % (Username, picfn, ext), "wb")
shutil.copyfileobj(pic, picf)
pic.close()
picf.close()
print >>f, "</userpics>"
f.close()
if verbose or (newentries > 0 or newcomments > 0):
if origlastsync:
print("%d new entries, %d new comments (since %s)" % (newentries, newcomments, origlastsync))
else:
print("%d new entries, %d new comments" % (newentries, newcomments))
if errors > 0:
print "%d errors" % errors
if __name__ == "__main__":
args = argparse.ArgumentParser(description="Livejournal archive utility")
args.add_argument("--quiet", "-q", action='store_false', dest='verbose',
help="reduce log output")
args = args.parse_args()
if os.access("ljdump.config", os.F_OK):
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_els = config.documentElement.getElementsByTagName("password")
if len(password_els) > 0:
password = password_els[0].childNodes[0].data
else:
password = getpass("Password: ")
journals = [e.childNodes[0].data for e in config.documentElement.getElementsByTagName("journal")]
if not journals:
journals = [username]
else:
print "ljdump - livejournal archiver"
print
default_server = "https://livejournal.com"
server = raw_input("Alternative server to use (e.g. 'https://www.dreamwidth.org'), or hit return for '%s': " % default_server) or default_server
print
print "Enter your Livejournal username and password."
print
username = raw_input("Username: ")
password = getpass("Password: ")
print
print "You may back up either your own journal, or a community."
print "If you are a community maintainer, you can back up both entries and comments."
print "If you are not a maintainer, you can back up only entries."
print
journal = raw_input("Journal to back up (or hit return to back up '%s'): " % username)
print
if journal:
journals = [journal]
else:
journals = [username]
for journal in journals:
ljdump(server, username, password, journal, args.verbose)
# vim:ts=4 et: