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