Login

Movable Type Import

Author:
bretwalker
Posted:
December 17, 2007
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

This is a quick and dirty way to import blog entries and comments from Movable Type.

Sorry I don't have time to document it better. Hopefully it will help you get started if you're looking to move from MT to Django.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
mtimport.py

Imports Movable Type entries and comments.

File format:
http://www.sixapart.com/movabletype/docs/3.2/01_installation_and_upgrade/mt_export.html


"""

import os
import re
import sys
import datetime

from app.blog.models import Entry
from django.contrib.auth.models import User
from django.contrib.comments import models as comment_models
from django.contrib.contenttypes.models import ContentType
from django.db import models

def read_file():
    
    """ File to read"""
    f=open(os.getcwd() + '/movable_type_export.txt', 'r')
    
    """ Field names to look for """
    pattern = re.compile(r'^([A-Z|\s]+):\s*(.*)\n')
    
    body = ""
    comment = {}
    entry_attrs = {}
    comments = []
    commentbody = ""
    broken = 0
    i = 1
    
    """ Get line count """
    count_lines = len(f.readlines())
    """ Go back to beginning of file """
    f.seek(0)
    
    """ Iterate through the entire file """
    for line_num in range(0, count_lines):
        """ Read the current line """
        line = f.readline()
        
        """ If this line denotes the end of entry, then save it, else go through, getting relevant fields """
        if (line != "--------\n"):
            """ In case """
            try:
                search_results = pattern.search(line).groups()
                if (search_results[0] == "TITLE"):
                    entry_attrs['headline'] = search_results[1]
                if (search_results[0] == "DATE"):
                    try:
                        dt = search_results[1].split(' ')
                        date = dt[0].split('/')
                        time = dt[1].split(':')
                        if(dt[2] == 'PM' and int(time[0]) < 12):
                            time[0] = str(int(time[0]) + 12)
                        dt = date + time
                        dt_ro = [int(dt[2]), int(dt[0]), int(dt[1]), int(dt[3]), int(dt[4]), int(dt[5])]
                        entry_attrs['pub_date'] = datetime.datetime(*dt_ro)
                    except:
                        print sys.exc_info()
                        pass
                    
                if (search_results[0] == "BODY"):
                    while(1):
                        line = f.readline()
                        if (line == "-----\n"):
                            break
                        else:
                            body += line
                    try:
                        entry_attrs['body'] = body.decode('iso-8859-1')
                    except:
                        print sys.exc_info()
                if (search_results[0] == "COMMENT"):
                    while(1):
                        if(broken == 0):
                            line = f.readline()
                        try:
                            search_results2 = pattern.search(line).groups()
                        except:
                            pass
                        if (line == "-----\n" or broken == 1):
                            try:
                                comments.append(comment)
                            except:
                                print sys.exc_info()
                                pass
                            comment = {}
                            broken = 0
                            break
                        else:
                            if(search_results2[0] == "AUTHOR"):
                                comment['person_name'] = search_results2[1] 
                            elif(search_results2[0] == "DATE"):
                                dt = search_results2[1].split(' ')
                                date = dt[0].split('/')
                                time = dt[1].split(':')
                                if(dt[2] == 'PM' and int(time[0]) < 12):
                                    time[0] = str(int(time[0]) + 12)
                                dt = date + time
                                dt_ro = [int(dt[2]), int(dt[0]), int(dt[1]), int(dt[3]), int(dt[4]), int(dt[5])]
                                comment['submit_date'] = datetime.datetime(*dt_ro)
                                print comment['submit_date']
                                while(1):
                                    line = f.readline()
                                    if (line == "-----\n"):
                                        broken = 1
                                        break
                                    else:
                                        commentbody += line
                                comment['comment'] = commentbody.decode('iso-8859-1')
                                commentbody = ""
                            elif(search_results2[0] == "IP"):
                                comment['ip_address'] = search_results2[1]
                            elif(search_results2[0] == "EMAIL"):
                                pass
                            elif(search_results2[0] == "URL"):
                                pass
             
            except:
                #print sys.exc_info()
                pass
        else:
            
            """ Static Entry attrs """
            entry_attrs['slug'] = re.sub(r'[^a-zA-Z0-9]', '_', entry_attrs['headline'])
            entry_attrs['summary'] = "This entry came from my old site, and is password protected."
            entry_attrs['author'] = User(1)
            entry_attrs['enable_comments'] = True
            entry_attrs['is_published'] = True
            entry_attrs['is_private'] = True
            entry = Entry(**entry_attrs)
            entry.save()
            add_comments(entry, comments)
            body = ""
            comments = []
    
def add_comments(entryIn, comments):
    entry = ContentType.objects.get_for_model(Entry)
    for comment in comments:
        comment['object_id'] = entryIn.id
        comment['is_public'] = True
        comment['approved'] = True
        comment['site_id'] = 1
        comment['content_type'] = entry
        c = comment_models.FreeComment(**comment)
        c.save()
            
    
if __name__ == '__main__':
    read_file()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

patrickbeeson (on February 1, 2008):

Could you post some documentation on how to use this snippet?

I'm new to Django, and would like to import the entries from my Movable Type blog.

Thanks!

#

Please login first before commenting.