Subject: Export (ODT)
Für eine einfache Archivierung eines Forums habe ich mir einen einfachen ODT-Export geschrieben. Es ist nicht perfekt (kein ABBC-Code wird aufgelöst, die Generierung des Inhaltsverzeichnisses muss in OOo angestossen werden) und hat sicher jede Menge Fehler aber vielleicht hilft es jemand weiter.
(abhängigkeit: odfpy, python-mysqldb)
(abhängigkeit: odfpy, python-mysqldb)
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import MySQLdb
- from odf.opendocument import OpenDocumentText
- from odf.teletype import WhitespaceText
- from odf.style import Style, TextProperties, ParagraphProperties
- from odf.text import H, P, Span, TableOfContent, TocMark
- dbhost = raw_input("db host[127.0.0.1]") or "127.0.0.1"
- prefix = raw_input("db prefix[unb_tg]: ") or "unb_tg"
- dbuser = raw_input("db user[yvesf]: ") or "yvesf"
- dbname = raw_input("db name[%s]: "%dbuser) or dbuser
- dbpass = raw_input("Password[]: ") or ""
- def debug(str):
- print str
- def recode(str):
- return unicode(str, 'cp1252')
- class Forum(object):
- def __init__(self,db):
- self.forums = {}
- self.db = db
- self._build_forum_tree(node=self.forums,id=0)
- def _build_forum_tree(self,node,id):
- debug("_build_forum_tree(id=%s)"%id)
- cur = self.db.cursor()
- cur.execute("SELECT ID, Parent,Name,Description FROM %s_Forums WHERE Parent = %s ORDER BY SORT" % (prefix,id))
- for row in cur.fetchall():
- node[row[0]] = {}
- self._build_forum_tree(node[row[0]], row[0])
- cur.close()
- @staticmethod
- def get_forum_info(db,forumID):
- cur = db.cursor()
- cur.execute("SELECT * FROM %s_Forums WHERE ID = %s" %(prefix,forumID))
- row = cur.fetchone()
- return {"ID" : row[0], "Parent" : row[1], "Name" : row[3], "Description" : row[5]}
- class Thread(object):
- def __init__(self,db,forumID):
- self.db = db
- self.forumID = forumID
- self.threads = []
- cur = self.db.cursor()
- cur.execute("SELECT ID, Forum, FROM_UNIXTIME(LastPostDate), Subject, t.Desc, FROM_UNIXTIME(Date) FROM %s_Threads t WHERE Forum = %s ORDER BY Date" % (prefix,forumID))
- for row in cur.fetchall():
- self.threads.append(row)
- cur.close()
- @staticmethod
- def get_posts(db,thread_id):
- cur = db.cursor()
- cur.execute("""SELECT u.Name, p.Subject, p.Msg, FROM_UNIXTIME(p.Date)
- FROM %s_Posts p INNER JOIN %s_Users u
- ON u.ID = p.User
- WHERE p.Thread = %s""" % (prefix, prefix, thread_id))
- return cur.fetchall()
- class Document(object):
- def __init__(self,db):
- self.db = db
- self.doc = OpenDocumentText()
- self.st_forum_description = Style(name="Forum Description", family="paragraph")
- self.st_forum_description.addElement(TextProperties(attributes={'fontsize':'16pt'}))
- self.doc.styles.addElement(self.st_forum_description)
- self.st_forum_title = Style(name="Forum Title", family="paragraph")
- self.st_forum_title.addElement(TextProperties(attributes={'fontsize':'23pt','fontweight':'bold'}))
- self.doc.styles.addElement(self.st_forum_title)
- self.st_thread_title = Style(name="Thread title", family="paragraph")
- self.st_thread_title.addElement(TextProperties(attributes={'fontsize':'15pt', 'fontweight':'bold'}))
- self.doc.styles.addElement(self.st_thread_title)
- self.st_thread_description = Style(name="Thread Description", family="paragraph")
- self.st_thread_description.addElement(TextProperties(attributes={'fontsize':'14pt'}))
- self.doc.styles.addElement(self.st_thread_description)
- self.st_post_header = Style(name="Post Header", family="paragraph")
- self.st_post_header.addElement(TextProperties(attributes={'fontweight':'bold'}))
- self.doc.styles.addElement(self.st_post_header)
- self.st_post_body = Style(name="Post Body", family="paragraph")
- self.st_post_body.addElement(TextProperties(attributes={'fontsize':'11pt'}))
- self.st_post_body.addElement(ParagraphProperties(attributes={'marginbottom':'1cm', 'margintop':'0.5cm'}))
- self.doc.styles.addElement(self.st_post_body)
- self.doc.text.addElement(TableOfContent(attributes={'name':'Inhalt'}))
- def line_break_escape(self,str):
- return str.replace(u'\n', u'<text:line-break/>').replace(u'\r', u'<text:line-break/>')
- def parse_forum_tree(self,tree,indent=0):
- for subforum_key in tree.keys():
- forum = Forum.get_forum_info(self.db,subforum_key)
- p = P(stylename=self.st_forum_title, text=u"%s> Forum %s" % (indent,recode(forum["Name"])))
- p.addElement(TocMark(attributes={'stringvalue':u'%s'%recode(forum["Name"]), 'outlinelevel':'%s'%indent}))
- self.doc.text.addElement(p)
- self.doc.text.addElement(P(stylename=self.st_forum_description, text=u"%s" % (recode(forum["Description"]))))
- for thread in Thread(self.db, subforum_key).threads:
- p=P(stylename=self.st_thread_title, text=u"Thread: %s" % (recode(thread[3])))
- p.addElement(TocMark(attributes={'stringvalue':u'%s'%recode(thread[3]), 'outlinelevel':(u'%s'%(indent+1))}))
- self.doc.text.addElement(p)
- self.doc.text.addElement(P(stylename=self.st_thread_description, text=u"Beschreibung: %s, Start: %s" % (recode(thread[4]) or u"<leer>",recode(thread[5].__str__()))))
- for post in Thread.get_posts(self.db, thread[0]):
- self.doc.text.addElement(P(stylename=self.st_post_header, text=u"%s schrieb am %s" % (recode(post[0]), recode(post[3].__str__()))))
- w = WhitespaceText()
- p = P(stylename=self.st_post_body)
- w.addTextToElement(p, recode(post[2]))
- self.doc.text.addElement(p)
- print "P",
- print "T",
- self.parse_forum_tree(tree[subforum_key],indent+1)
- def save(self,filename):
- self.doc.save(filename,True)
- con = MySQLdb.connect(
- host = dbhost,
- db = dbname,
- user = dbuser,
- passwd = dbpass)
- f = Forum(con)
- doc = Document(con)
- doc.parse_forum_tree(f.forums)
- doc.save("out")

YvesFischer
Show profile
Link to this post