]> www.wagner.pp.ru Git - oss/ljdump.git/blob - convertdump.py
Fixed handling of security/allowmask tags
[oss/ljdump.git] / convertdump.py
1 #!/usr/bin/python
2
3 # Copyright 2009, Sean M. Graham (www.sean-graham.com)
4 # All rights reserved.
5
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
9
10 # - Redistributions of source code must retain the above copyright notice,
11 #   this list of conditions and the following disclaimer.
12
13 # - Redistributions in binary form must reproduce the above copyright notice,
14 #   this list of conditions and the following disclaimer in the documentation
15 #   and/or other materials provided with the distribution.
16
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 # EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
27
28 import xml.dom.minidom 
29 import os
30 import codecs
31 import sys
32
33 from time import strptime, strftime
34
35 def getNodeText(doc, nodename):
36     rc = ""
37
38     try:
39         nodelist = doc.getElementsByTagName(nodename)[0].childNodes
40     except:
41         return ""
42
43     for node in nodelist:
44         if node.nodeType == node.TEXT_NODE:
45             rc = rc + node.data
46
47     return rc
48
49 def appendTextNode(doc, parent, nodename, value):
50     nodeValue = value
51
52     # make sure value is properly encoded
53     try:
54         bytes = nodeValue.encode("UTF-8")
55     except:
56         bytes = nodeValue.encode("cp1252")
57         nodeValue = unicode(bytes, "UTF-8")
58
59     element = doc.createElement(nodename)
60
61     if( nodeValue != "" ): 
62         textNode = doc.createTextNode(nodeValue)
63         element.appendChild(textNode)
64
65     parent.appendChild(element)
66
67
68 def addEntryForId(outDoc, element, username, id, includeSecure):
69     entryFile = open("%s/L-%s" % (username,id), "r")
70     inDoc = xml.dom.minidom.parse(entryFile)
71
72     # Create an entry element
73     entry = outDoc.createElement("entry")
74
75     # Create an itemid element
76     appendTextNode(outDoc, entry, "itemid", getNodeText(inDoc,"itemid"))
77
78     # Create an eventtime element
79     appendTextNode(outDoc, entry, "eventtime", getNodeText(inDoc, "eventtime"))
80
81     # Create an subject element
82     appendTextNode(outDoc, entry, "subject", getNodeText(inDoc, "subject"))
83
84     # Create an event node (special case because for some reason there are two
85     # 'event' elements in the pydump output, which is probably LJ's fault)
86     event = inDoc.getElementsByTagName("event")[0]
87     appendTextNode(outDoc, entry, "event", getNodeText(event, "event"))
88
89     security = getNodeText(inDoc, "security")
90
91     if(security != ""):
92         # don't append this entry unless the user provided the argument
93         if(includeSecure == False):
94             print("omitting secure entry: L-%s" % id)
95             return 
96         else:
97             if(security == "usemask"):
98                 print("including allowmask entry: L-%s" % id)
99
100                 # Create an allowmask element 
101                 maskText = getNodeText(inDoc, "allowmask")
102
103                 if(maskText != ""):
104                     appendTextNode(outDoc, entry, "allowmask", maskText)
105                 else:
106                     appendTextNode(outDoc, entry, "allowmask", "0")
107             else:
108                 print("including private entry: L-%s" % id)
109
110         appendTextNode(outDoc, entry, "security", security)
111
112     # Create a taglist element
113     appendTextNode(outDoc, entry, "taglist", getNodeText(inDoc, "taglist"))
114
115     # XXXSMG: make sure there is a comment file before trying to do anything
116     # with it
117     addCommentsForId(outDoc, entry, username, id)
118
119     element.appendChild(entry)
120
121 def addCommentsForId(outDoc, entry, username, id):
122     try: 
123         commentFile = open("%s/C-%s" % (username,id), "r")
124     except IOError:  # there are no comments for this entry
125         return
126
127     inDoc = xml.dom.minidom.parse(commentFile)
128
129     comments = inDoc.getElementsByTagName("comment")
130
131     for comment in comments:
132         outComment = outDoc.createElement("comment")
133         entry.appendChild(outComment)
134
135         # add the item id for the comment
136         appendTextNode(outDoc, outComment, "itemid", 
137             getNodeText(comment, "id"))
138
139         # convert the time string
140         timeString = getNodeText(comment, "date")
141         if( timeString != "" ):
142             inDate = strptime(timeString, "%Y-%m-%dT%H:%M:%SZ")
143             outDate = strftime("%Y-%m-%d %H:%M:%S", inDate)
144             appendTextNode(outDoc, outComment, "eventtime", outDate)
145         else:
146             emptyTime = outDoc.createElement("eventtime")
147             outComment.appendChild(emptyTime)
148
149         # Create an subject element
150         appendTextNode(outDoc, outComment, "subject", 
151             getNodeText(comment, "subject"))
152
153         # Create an event element
154         appendTextNode(outDoc, outComment, "event", 
155             getNodeText(comment, "body"))
156
157         # Create the author element
158         author = outDoc.createElement("author")
159         outComment.appendChild(author)
160
161         try:
162             cUser = getNodeText(comment, "user")
163         except:
164             cUser = "anonymous"
165
166         appendTextNode(outDoc, author, "name", cUser)
167         appendTextNode(outDoc, author, "email", cUser + "@livejournal.com")
168         
169         # Create the parent_itemid
170         parentId = getNodeText(comment, "parentid")
171         if(parentId != ""): 
172             appendTextNode(outDoc, outComment, "parent_itemid", parentId)
173
174 def main(argv): 
175     username = ""
176     entryLimit = 250
177     includeSecure = False;
178     
179     if( len(argv) < 2 ):
180         print( "Usage: convertdump.py <username> <entrylimit>" )
181         return
182     else:
183         username = argv[0]
184         entryLimit = int(argv[1])
185
186         try:
187             includeSecure = bool(argv[2])
188         except IndexError:
189             includeSecure = False
190
191     if(includeSecure == True):
192         print( "Warning:  Including secure entries in XML output" )
193
194     userDir = os.listdir(username)
195
196     highNum = -1
197     entryArray = []
198
199     # get the list of entries
200     for file in userDir:
201         if file.startswith("L-"):
202             entryNum = int(file.replace("L-",""))
203
204             entryArray.append(entryNum)
205
206             if( highNum < entryNum ):
207                 highNum = entryNum
208
209     entryArray.sort()
210
211     # Create the minidom document
212     outDoc = xml.dom.minidom.Document()
213
214     # Create the <livejournal> base element
215     ljElement = outDoc.createElement("livejournal")
216     outDoc.appendChild(ljElement)
217
218     currentFileEntry = 0
219
220     # start processing entries
221     for entry in entryArray:
222         addEntryForId(outDoc, ljElement, username, entry, includeSecure)
223
224         currentFileEntry += 1
225
226         if( currentFileEntry == entryLimit or entry == entryArray[-1] ):
227
228             f = open("%s - %s.xml" % (username, entry), "w")
229             tempXML = outDoc.toxml("UTF-8")
230             f.write(tempXML)
231             
232             currentFileEntry = 0
233
234             # Create the minidom document
235             outDoc = xml.dom.minidom.Document()
236
237             # Create the <livejournal> base element
238             ljElement = outDoc.createElement("livejournal")
239             outDoc.appendChild(ljElement)
240
241 if __name__ == "__main__":
242     main(sys.argv[1:])
243