"""
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()

