| 167 | logger.warning("No WXR namespace found, assuming 1.0") |
| 168 | |
| 169 | def insert(self, thread): |
| 170 | url = urlparse(thread.find("link").text) |
| 171 | path = url.path |
| 172 | |
| 173 | if url.query: |
| 174 | path += "?" + url.query |
| 175 | |
| 176 | self.db.threads.new(path, thread.find("title").text.strip()) |
| 177 | |
| 178 | comments = list(map(self.Comment, thread.findall(self.ns + "comment"))) |
| 179 | comments.sort(key=lambda k: k["id"]) |
| 180 | |
| 181 | remap = {} |
| 182 | ids = set(c["id"] for c in comments) |
| 183 | |
| 184 | self.count += len(ids) |
| 185 | |
| 186 | while comments: |
| 187 | for i, item in enumerate(comments): |
| 188 | if item["parent"] in ids: |
| 189 | continue |
| 190 | |
| 191 | item["parent"] = remap.get(item["parent"], None) |
| 192 | rv = self.db.comments.add(path, item) |
| 193 | remap[item["id"]] = rv["id"] |
| 194 | |
| 195 | ids.remove(item["id"]) |
| 196 | comments.pop(i) |
| 197 | |
| 198 | break |
| 199 | else: |
| 200 | # should never happen, but... it's WordPress. |
| 201 | return |
| 202 | |
| 203 | def migrate(self): |
| 204 | tree = ElementTree.parse(self.xmlfile) |