]> www.wagner.pp.ru Git - oss/ljdump.git/blob - convertdump.py
builds one big file now of all entries and comments for the user
[oss/ljdump.git] / convertdump.py
1 #!/usr/bin/python
2
3 import xml.dom.minidom 
4 import os
5 from time import strptime, strftime
6
7 def getNodeText(doc, nodename):
8     rc = ""
9
10     try:
11         nodelist = doc.getElementsByTagName(nodename)[0].childNodes
12     except:
13         return ""
14
15     for node in nodelist:
16         if node.nodeType == node.TEXT_NODE:
17             rc = rc + node.data
18
19     return rc
20
21 def appendTextNode(doc, parent, nodename, value):
22     element = doc.createElement(nodename)
23
24     if( value != "" ): 
25         textNode = doc.createTextNode(value)
26         element.appendChild(textNode)
27
28     parent.appendChild(element)
29
30
31 def addEntryForId(outDoc, username, id):
32     entryFile = open("%s/L-%s" % (username,id), "r")
33     inDoc = xml.dom.minidom.parse(entryFile)
34
35     # Create an entry element
36     entry = outDoc.createElement("entry")
37     ljElement.appendChild(entry)
38
39     # Create an itemid element
40     appendTextNode(outDoc, entry, "itemid", getNodeText(inDoc,"itemid"))
41
42     # Create an eventtime element
43     appendTextNode(outDoc, entry, "eventtime", getNodeText(inDoc, "eventtime"))
44
45     # Create an subject element
46     appendTextNode(outDoc, entry, "subject", getNodeText(inDoc, "subject"))
47
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"))
52
53     # Create an allowmask element (doesn't exist in pydump output if public)
54     try:
55         appendTextNode(outDoc, entry, "allowmask", 
56             getNodeText(inDoc, "allowmask"))
57     except:
58         appendTextNode(outDoc, entry, "allowmask", "0")
59
60     # Create a taglist element
61     appendTextNode(outDoc, entry, "taglist", getNodeText(inDoc, "taglist"))
62
63     # XXXSMG: make sure there is a comment file before trying to do anything
64     # with it
65     addCommentsForId(outDoc, entry, username, id)
66
67 def addCommentsForId(outDoc, entry, username, id):
68     try: 
69         commentFile = open("%s/C-%s" % (username,id), "r")
70     except:
71         # there are no comments for this entry
72         return
73
74     inDoc = xml.dom.minidom.parse(commentFile)
75
76     comments = inDoc.getElementsByTagName("comment")
77
78     for comment in comments:
79         outComment = outDoc.createElement("comment")
80         entry.appendChild(outComment)
81
82         # add the item id for the comment
83         appendTextNode(outDoc, outComment, "itemid", 
84             getNodeText(comment, "id"))
85
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)
92         else:
93             emptyTime = outDoc.createElement("eventtime")
94             outComment.appendChild(emptyTime)
95
96         # Create an subject element
97         appendTextNode(outDoc, outComment, "subject", 
98             getNodeText(comment, "subject"))
99
100         # Create an event element
101         appendTextNode(outDoc, outComment, "event", 
102             getNodeText(comment, "body"))
103
104         # Create the author element
105         author = outDoc.createElement("author")
106         outComment.appendChild(author)
107
108         try:
109             cUser = getNodeText(comment, "user")
110         except:
111             cUser = "anonymous"
112
113         appendTextNode(outDoc, author, "name", cUser)
114         appendTextNode(outDoc, author, "email", cUser + "@livejournal.com")
115         
116         # Create the parent_itemid
117         parentId = getNodeText(comment, "parentid")
118         if(parentId != ""): 
119             appendTextNode(outDoc, outComment, "parent_itemid", parentId)
120
121
122
123
124 # Create the minidom document
125 outDoc = xml.dom.minidom.Document()
126
127 # Create the <livejournal> base element
128 ljElement = outDoc.createElement("livejournal")
129 outDoc.appendChild(ljElement)
130
131 userDir = os.listdir("grahams")
132
133 highNum = -1
134 entryArray = []
135
136 # get the list of entries
137 for file in userDir:
138     if file.startswith("L-"):
139         entryNum = int(file.replace("L-",""))
140
141         entryArray.append(entryNum)
142
143         if( highNum < entryNum ):
144             highNum = entryNum
145
146 entryArray.sort()
147
148 # start processing entries
149 for entry in entryArray:
150     print entry
151     addEntryForId(outDoc, "grahams", entry)
152
153
154 # Print our newly created XML
155 print outDoc.toprettyxml(indent="  ")