6 from time import strptime, strftime
8 def getNodeText(doc, nodename):
12 nodelist = doc.getElementsByTagName(nodename)[0].childNodes
17 if node.nodeType == node.TEXT_NODE:
22 def appendTextNode(doc, parent, nodename, value):
25 # make sure value is properly encoded
27 bytes = nodeValue.encode("UTF-8")
29 bytes = nodeValue.encode("cp1252")
30 nodeValue = unicode(bytes, "UTF-8")
32 element = doc.createElement(nodename)
34 if( nodeValue != "" ):
35 textNode = doc.createTextNode(nodeValue)
36 element.appendChild(textNode)
38 parent.appendChild(element)
41 def addEntryForId(outDoc, username, id):
42 entryFile = open("%s/L-%s" % (username,id), "r")
43 inDoc = xml.dom.minidom.parse(entryFile)
45 # Create an entry element
46 entry = outDoc.createElement("entry")
47 ljElement.appendChild(entry)
49 # Create an itemid element
50 appendTextNode(outDoc, entry, "itemid", getNodeText(inDoc,"itemid"))
52 # Create an eventtime element
53 appendTextNode(outDoc, entry, "eventtime", getNodeText(inDoc, "eventtime"))
55 # Create an subject element
56 appendTextNode(outDoc, entry, "subject", getNodeText(inDoc, "subject"))
58 # Create an event node (special case because for some reason there are two
59 # 'event' elements in the pydump output, which is probably LJ's fault)
60 event = inDoc.getElementsByTagName("event")[0]
61 appendTextNode(outDoc, entry, "event", getNodeText(event, "event"))
63 # Create an allowmask element (doesn't exist in pydump output if public)
64 maskText = getNodeText(inDoc, "allowmask")
67 appendTextNode(outDoc, entry, "allowmask", maskText)
69 appendTextNode(outDoc, entry, "allowmask", "0")
71 # Create a taglist element
72 appendTextNode(outDoc, entry, "taglist", getNodeText(inDoc, "taglist"))
74 # XXXSMG: make sure there is a comment file before trying to do anything
76 addCommentsForId(outDoc, entry, username, id)
78 def addCommentsForId(outDoc, entry, username, id):
80 commentFile = open("%s/C-%s" % (username,id), "r")
81 except IOError: # there are no comments for this entry
84 inDoc = xml.dom.minidom.parse(commentFile)
86 comments = inDoc.getElementsByTagName("comment")
88 for comment in comments:
89 outComment = outDoc.createElement("comment")
90 entry.appendChild(outComment)
92 # add the item id for the comment
93 appendTextNode(outDoc, outComment, "itemid",
94 getNodeText(comment, "id"))
96 # convert the time string
97 timeString = getNodeText(comment, "date")
98 if( timeString != "" ):
99 inDate = strptime(timeString, "%Y-%m-%dT%H:%M:%SZ")
100 outDate = strftime("%Y-%m-%d %H:%M:%S", inDate)
101 appendTextNode(outDoc, outComment, "eventtime", outDate)
103 emptyTime = outDoc.createElement("eventtime")
104 outComment.appendChild(emptyTime)
106 # Create an subject element
107 appendTextNode(outDoc, outComment, "subject",
108 getNodeText(comment, "subject"))
110 # Create an event element
111 appendTextNode(outDoc, outComment, "event",
112 getNodeText(comment, "body"))
114 # Create the author element
115 author = outDoc.createElement("author")
116 outComment.appendChild(author)
119 cUser = getNodeText(comment, "user")
123 appendTextNode(outDoc, author, "name", cUser)
124 appendTextNode(outDoc, author, "email", cUser + "@livejournal.com")
126 # Create the parent_itemid
127 parentId = getNodeText(comment, "parentid")
129 appendTextNode(outDoc, outComment, "parent_itemid", parentId)
134 userDir = os.listdir("grahams")
139 # get the list of entries
141 if file.startswith("L-"):
142 entryNum = int(file.replace("L-",""))
144 entryArray.append(entryNum)
146 if( highNum < entryNum ):
152 # Create the minidom document
153 outDoc = xml.dom.minidom.Document()
155 # Create the <livejournal> base element
156 ljElement = outDoc.createElement("livejournal")
157 outDoc.appendChild(ljElement)
162 # start processing entries
163 for entry in entryArray:
164 addEntryForId(outDoc, "grahams", entry)
166 currentFileEntry += 1
168 if( currentFileEntry == breakup ):
170 f = open("grahams - %s.xml" % entry, "w")
171 tempXML = outDoc.toxml("UTF-8")
176 # Create the minidom document
177 outDoc = xml.dom.minidom.Document()
179 # Create the <livejournal> base element
180 ljElement = outDoc.createElement("livejournal")
181 outDoc.appendChild(ljElement)