Import Textpattern to Jekyll

Obsolete. Here only to prevent link rot.

Created: by Pradeep GowdaUpdated:Oct 31, 2023Tagged: python · web · textpattern · jekyll .

A python script to import blog posts from textpattern to Jekyll

Requires: web.py

Update 2023-10-31: copied code from gist to inline. No other changes.

import os
import web
import codecs

db = web.database(dbn=os.environ.get('DATABASE_ENGINE', 'mysql'),
                          db='textpatterndb', user="root", passwd="BANANA")
def main():
    QUERY = """SELECT Title, id,
                    url_title,
                    Posted,
                    Body,
                    concat(Category1, case when Category2 <> '' then concat(' ', Category2) else '' end) as Categories, \
                    comments_count
             FROM textpattern
             WHERE Status = '4' OR
                   Status = '5';
    """
    results = db.query(QUERY)

    for r in results:
        content = r.Body
        slug = r.url_title
        name = '-'.join([r.Posted.strftime("%Y-%m-%d"), slug]) + ".textile"
        os.makedirs('/tmp/_posts')

        f = codecs.open('/tmp/_posts/%s' % name, 'w', "utf-8")
        f.write('---\n')
        f.write('layout: post\n')
        f.write('title: %s\n' % r.Title.replace('/', '-'))
        f.write('categories: %s\n' % r.Categories.replace('/', '-'))
        f.write('permalink: %s\n' % slug)
        f.write('---\n\n')
        f.write(content)
        f.close()

if __name__ ==  '__main__':
    main()