bookie-python/xml_import.py

50 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3.4
2014-11-06 01:39:33 +00:00
import os, re
from flask.ext.mongoengine import MongoEngine
import datetime
import base64
from bs4 import BeautifulSoup as BS
from xml.sax.saxutils import unescape
from bookie import *
app.config["MONGODB_DB"] = "bookie"
db = MongoEngine(app)
date_marker = datetime.datetime.now()
count = 0
Bookmark.objects.all().delete()
Tag.objects.all().delete()
ArchivedText.objects.all().delete()
ArchivedImage.objects.all().delete()
with open("import.xml", "r") as importfile:
data = importfile.read()
soup = BS(data)
count = 0
for i in soup(["bookmark"]):
b = Bookmark()
b.title = unescape(i.find("title").get_text())
b.short = unescape(i.find("short").get_text())
b.url = unescape(i.find("url").get_text())
b.note = i.find("note").get_text()
b.created_at = datetime.datetime(*map(int, re.split('[^\d]', unescape(i.find("created_at").get_text()))[:-1]))
if i.find("unread").get_text() == "True":
b.unread = True
else:
b.unread = False
b.tags = []
for t in i.find("tags").get_text().split(" "):
tag = Tag.objects.get_or_create(name=t)[0].save()
b.tags.append(tag)
count += 1
print(str(count)+" - "+b.short)
print(b.title)
print(b.url)
2014-12-29 17:52:55 +00:00
b.factor = b.get_factor()
b.save()