]> www.wagner.pp.ru Git - oss/ljdump.git/blob - convertdump.py
Accepted patch from Carl Johan Crafoord <mail@carljohancrafoord.se>
[oss/ljdump.git] / convertdump.py
1 #!/usr/bin/python
2
3 import xml.dom.minidom 
4 import os
5 import codecs
6 import sys
7
8 from time import strptime, strftime
9
10 def getNodeText(doc, nodename):
11     rc = ""
12
13     try:
14         nodelist = doc.getElementsByTagName(nodename)[0].childNodes
15     except:
16         return ""
17
18     for node in nodelist:
19         if node.nodeType == node.TEXT_NODE:
20             rc = rc + node.data
21
22     return rc
23
24 def appendTextNode(doc, parent, nodename, value):
25     nodeValue = value
26
27     # make sure value is properly encoded
28     try:
29         bytes = nodeValue.encode("UTF-8")
30     except:
31         bytes = nodeValue.encode("cp1252")
32         nodeValue = unicode(bytes, "UTF-8")
33
34     element = doc.createElement(nodename)
35
36     if( nodeValue != "" ): 
37         textNode = doc.createTextNode(nodeValue)
38         element.appendChild(textNode)
39
40     parent.appendChild(element)
41
42
43 def addEntryForId(outDoc, element, username, id):
44     entryFile = open("%s/L-%s" % (username,id), "r")
45     inDoc = xml.dom.minidom.parse(entryFile)
46
47     # Create an entry element
48     entry = outDoc.createElement("entry")
49     element.appendChild(entry)
50
51     # Create an itemid element
52     appendTextNode(outDoc, entry, "itemid", getNodeText(inDoc,"itemid"))
53
54     # Create an eventtime element
55     appendTextNode(outDoc, entry, "eventtime", getNodeText(inDoc, "eventtime"))
56
57     # Create an subject element
58     appendTextNode(outDoc, entry, "subject", getNodeText(inDoc, "subject"))
59
60     # Create an event node (special case because for some reason there are two
61     # 'event' elements in the pydump output, which is probably LJ's fault)
62     event = inDoc.getElementsByTagName("event")[0]
63     appendTextNode(outDoc, entry, "event", getNodeText(event, "event"))
64
65     # Create an allowmask element (doesn't exist in pydump output if public)
66     maskText = getNodeText(inDoc, "allowmask")
67
68     # XXXSMG: consult L-1411 and L-976 for examples of security and
69     # allowmask use
70     if(maskText != ""):
71         appendTextNode(outDoc, entry, "allowmask", maskText)
72     else:
73         appendTextNode(outDoc, entry, "allowmask", "0")
74
75     # Create a taglist element
76     appendTextNode(outDoc, entry, "taglist", getNodeText(inDoc, "taglist"))
77
78     # XXXSMG: make sure there is a comment file before trying to do anything
79     # with it
80     addCommentsForId(outDoc, entry, username, id)
81
82 def addCommentsForId(outDoc, entry, username, id):
83     try: 
84         commentFile = open("%s/C-%s" % (username,id), "r")
85     except IOError:  # there are no comments for this entry
86         return
87
88     inDoc = xml.dom.minidom.parse(commentFile)
89
90     comments = inDoc.getElementsByTagName("comment")
91
92     for comment in comments:
93         outComment = outDoc.createElement("comment")
94         entry.appendChild(outComment)
95
96         # add the item id for the comment
97         appendTextNode(outDoc, outComment, "itemid", 
98             getNodeText(comment, "id"))
99
100         # convert the time string
101         timeString = getNodeText(comment, "date")
102         if( timeString != "" ):
103             inDate = strptime(timeString, "%Y-%m-%dT%H:%M:%SZ")
104             outDate = strftime("%Y-%m-%d %H:%M:%S", inDate)
105             appendTextNode(outDoc, outComment, "eventtime", outDate)
106         else:
107             emptyTime = outDoc.createElement("eventtime")
108             outComment.appendChild(emptyTime)
109
110         # Create an subject element
111         appendTextNode(outDoc, outComment, "subject", 
112             getNodeText(comment, "subject"))
113
114         # Create an event element
115         appendTextNode(outDoc, outComment, "event", 
116             getNodeText(comment, "body"))
117
118         # Create the author element
119         author = outDoc.createElement("author")
120         outComment.appendChild(author)
121
122         try:
123             cUser = getNodeText(comment, "user")
124         except:
125             cUser = "anonymous"
126
127         appendTextNode(outDoc, author, "name", cUser)
128         appendTextNode(outDoc, author, "email", cUser + "@livejournal.com")
129         
130         # Create the parent_itemid
131         parentId = getNodeText(comment, "parentid")
132         if(parentId != ""): 
133             appendTextNode(outDoc, outComment, "parent_itemid", parentId)
134
135 def main(argv): 
136     username = ""
137     entryLimit = 250
138     
139
140     if( len(argv) != 2 ):
141         print( "Usage: convertdump.py <username> <entrylimit>" )
142         return
143     else:
144         username = argv[0]
145         entryLimit = int(argv[1])
146
147     userDir = os.listdir(username)
148
149     highNum = -1
150     entryArray = []
151
152     # get the list of entries
153     for file in userDir:
154         if file.startswith("L-"):
155             entryNum = int(file.replace("L-",""))
156
157             entryArray.append(entryNum)
158
159             if( highNum < entryNum ):
160                 highNum = entryNum
161
162     entryArray.sort()
163
164
165     # Create the minidom document
166     outDoc = xml.dom.minidom.Document()
167
168     # Create the <livejournal> base element
169     ljElement = outDoc.createElement("livejournal")
170     outDoc.appendChild(ljElement)
171
172     currentFileEntry = 0
173
174     # start processing entries
175     for entry in entryArray:
176         addEntryForId(outDoc, ljElement, username, entry)
177
178         currentFileEntry += 1
179
180         if( currentFileEntry == entryLimit or entry == entryArray[-1] ):
181
182             f = open("%s - %s.xml" % (username, entry), "w")
183             tempXML = outDoc.toxml("UTF-8")
184             f.write(tempXML)
185             
186             currentFileEntry = 0
187
188             # Create the minidom document
189             outDoc = xml.dom.minidom.Document()
190
191             # Create the <livejournal> base element
192             ljElement = outDoc.createElement("livejournal")
193             outDoc.appendChild(ljElement)
194
195 if __name__ == "__main__":
196     main(sys.argv[1:])
197