5 from time import strptime, strftime
7 def getNodeText(doc, nodename):
11 nodelist = doc.getElementsByTagName(nodename)[0].childNodes
16 if node.nodeType == node.TEXT_NODE:
21 def appendTextNode(doc, parent, nodename, value):
22 element = doc.createElement(nodename)
25 textNode = doc.createTextNode(value)
26 element.appendChild(textNode)
28 parent.appendChild(element)
31 def addEntryForId(outDoc, username, id):
32 entryFile = open("%s/L-%s" % (username,id), "r")
33 inDoc = xml.dom.minidom.parse(entryFile)
35 # Create an entry element
36 entry = outDoc.createElement("entry")
37 ljElement.appendChild(entry)
39 # Create an itemid element
40 appendTextNode(outDoc, entry, "itemid", getNodeText(inDoc,"itemid"))
42 # Create an eventtime element
43 appendTextNode(outDoc, entry, "eventtime", getNodeText(inDoc, "eventtime"))
45 # Create an subject element
46 appendTextNode(outDoc, entry, "subject", getNodeText(inDoc, "subject"))
48 # Create an event node (special case because for some reason there are two
49 # 'event' elements in the pydump output, which is probably LJ's fault)
50 event = inDoc.getElementsByTagName("event")[0]
51 appendTextNode(outDoc, entry, "event", getNodeText(event, "event"))
53 # Create an allowmask element (doesn't exist in pydump output if public)
55 appendTextNode(outDoc, entry, "allowmask",
56 getNodeText(inDoc, "allowmask"))
58 appendTextNode(outDoc, entry, "allowmask", "0")
60 # Create a taglist element
61 appendTextNode(outDoc, entry, "taglist", getNodeText(inDoc, "taglist"))
63 # XXXSMG: make sure there is a comment file before trying to do anything
65 addCommentsForId(outDoc, entry, username, id)
67 def addCommentsForId(outDoc, entry, username, id):
69 commentFile = open("%s/C-%s" % (username,id), "r")
71 # there are no comments for this entry
74 inDoc = xml.dom.minidom.parse(commentFile)
76 comments = inDoc.getElementsByTagName("comment")
78 for comment in comments:
79 outComment = outDoc.createElement("comment")
80 entry.appendChild(outComment)
82 # add the item id for the comment
83 appendTextNode(outDoc, outComment, "itemid",
84 getNodeText(comment, "id"))
86 # convert the time string
87 timeString = getNodeText(comment, "date")
88 if( timeString != "" ):
89 inDate = strptime(timeString, "%Y-%m-%dT%H:%M:%SZ")
90 outDate = strftime("%Y-%m-%d %H:%M:%S", inDate)
91 appendTextNode(outDoc, outComment, "eventtime", outDate)
93 emptyTime = outDoc.createElement("eventtime")
94 outComment.appendChild(emptyTime)
96 # Create an subject element
97 appendTextNode(outDoc, outComment, "subject",
98 getNodeText(comment, "subject"))
100 # Create an event element
101 appendTextNode(outDoc, outComment, "event",
102 getNodeText(comment, "body"))
104 # Create the author element
105 author = outDoc.createElement("author")
106 outComment.appendChild(author)
109 cUser = getNodeText(comment, "user")
113 appendTextNode(outDoc, author, "name", cUser)
114 appendTextNode(outDoc, author, "email", cUser + "@livejournal.com")
116 # Create the parent_itemid
117 parentId = getNodeText(comment, "parentid")
119 appendTextNode(outDoc, outComment, "parent_itemid", parentId)
124 # Create the minidom document
125 outDoc = xml.dom.minidom.Document()
127 # Create the <livejournal> base element
128 ljElement = outDoc.createElement("livejournal")
129 outDoc.appendChild(ljElement)
131 userDir = os.listdir("grahams")
136 # get the list of entries
138 if file.startswith("L-"):
139 entryNum = int(file.replace("L-",""))
141 entryArray.append(entryNum)
143 if( highNum < entryNum ):
148 # start processing entries
149 for entry in entryArray:
151 addEntryForId(outDoc, "grahams", entry)
154 # Print our newly created XML
155 print outDoc.toprettyxml(indent=" ")