Importing Blog Posts from Typepad

Templates Scripts and Step by Step Instructions To Port Typepad Blog Posts into Blogs Package

Created by: Anonymous, Last modification: 12 May 2007 (22:30 UTC) by Will
Be sure you make a backup of your site and test this importer on a non-live site before running this importer on your live site.

TypePad won't let you do a database dump of your blog posts, but it will let you dump all your posts and related comments via a custom index template. For this example, we start with a blog by a single author and we create a custom template that generates a text document which we can easily convert into a php hash, which we will then process to push the posts into our bitweaver install.

Step 1. The TypePad Template

First thing to do is in TypePad create a new index template. This index file will generate a text file that looks similar to a php hash. Note that because we can't escape quotes in our blog posts text we don't use quotes for our hash, we use %QUOTE%. In the next step we will escape all quotes in our content and then convert the %QUOTE% tags to real quotes. Also worth nothing, in this example, we push TypePad categories into Tags. This assumes you are using the TagsPackage. You could use a similar technique with PigeonholesPackage.

Typepad template to export data to php hash


<?php
$typepadHash 
= array(<MTBlogIfArchives archive_type="Individual"><MTEntries all="1">
    array(
        %
QUOTE%title%QUOTE% => %QUOTE%<$MTEntryTitle encode_xml="0"$>%QUOTE%,
        %
QUOTE%date%QUOTE% => %QUOTE%<MTIfNonEmpty tag="MTEntryDate" format="%a, %d %b %Y %H:%M:%S %z" language="en_US"><$MTEntryDate format="%a, %d %b %Y %H:%M:%S %z" language="en_US"$></MTIfNonEmpty>%QUOTE%,
        %
QUOTE%data%QUOTE% => %QUOTE%<MTWeblogIfFullRSSContent><MTWeblogOwnerHas field="feeds_full_content"><$MTEntryBody encode_xml="0"$></MTWeblogOwnerHas></MTWeblogIfFullRSSContent>%QUOTE%,
        %
QUOTE%comments%QUOTE% => array(
        <
MTComments>
            array(
            %
QUOTE%anon_name%QUOTE% => %QUOTE%<$MTCommentAuthor$>%QUOTE%,
            %
QUOTE%date%QUOTE% => %QUOTE%<$MTCommentDate format="%a, %d %b %Y %H:%M:%S %z" language="en_US">%QUOTE%,
            %
QUOTE%data%QUOTE% => %QUOTE%<$MTCommentBody$>%QUOTE%,
            ),
        </
MTComments>
        ),
        %
QUOTE%tags%QUOTE% => %QUOTE%<MTEntryCategories><$MTCategoryLabel encode_xml="0"$></MTEntryCategories>%QUOTE%,
    ),</
MTEntries></MTBlogIfArchives>
);
?>


Once you have saved this template publish the content and then copy paste the results to a new file called output.php

Step 2. Clean Up Text From TypePad

The next thing we need to do is escape quotes in our content and then convert those tags we used in step one back to real quotes. Using command line run the following:

Fix typepad output


<?php
sed 
"s/'/\\\\'/g" output.php sed "s/%QUOTE%/'/g" typepad.php
?>


You now have a new file typepad.php. This should now be a valid php hash of blog post data to store. You can test the file is valid by running the command $ php -l typepad.php

Step 3. Configure The Installer.

The following code is an installer script which will push each blog post into your bitweaver database. There are a couple of values to set. At the top is $user_id, this is the id of the bitweaver author you would like to assign all the posts to. The second value to set is $blog_content_id, this is optional. All the posts will be stored as the user's posts. If you also want the posts crossposted to a blog on your site, then you should assign the content id for that blog. If you don't to crosspost to any blog leave the value as = 0.

Import script


<?php
// import configuration
// the user these posts belong to
$user_id 2;

// the blog these posts should be inserted into
$blog_content_id 0;

// trackback for _all_ imported posts
$trackback '';


// no need to touch any of the stuff below unless you want to change the
// import behaviour
require_once( 'bit_setup_inc.php' );
require_once( 
LIBERTY_PKG_PATH.'LibertyComment.php' );
if( 
$gBitSystem->isPackageActive'tags' )) {
    require_once( 
TAGS_PKG_PATH.'LibertyTag.php' );
}

$gBitSystem->verifyPermission'p_admin' );

if( 
$gBitSystem->isPackageActive'blogs' )) {
    require_once( 
BLOGS_PKG_PATH.'BitBlogPost.php' );

    
// include the list of posts
    
require_once( 'typepad.php' );

    
$pattern[] = "/( \s*)+/";
    
$replace[] = " ";
    
$pattern[] = "/\btarget=\"[a-zA-Z_-]*\"/";
    
$replace[] = 'class="external"';

    foreach( 
$typepadHash as $post ) {
        
$bp = new BitBlogPost();

        
$feedback "Importing: ".$post['title'];

        
$post['data']            = preg_replace$pattern$replace$post['data'] );
        
$post['user_id']         = $user_id;
        
$post['blog_content_id'] = $blog_content_id;
        
$post['trackback']       = $trackback;
        
$post['format_guid']     = 'bithtml';
        
$post['publish_date']    = $post['created'] = $post['last_modified'] = strtotime$post['date'] );

        if( 
$bp->store$post )) {
            
$feedback .= "\n- Post: SUCCESS - content_id: ".$post['content_id'];

            if( !empty( 
$post['comments'] )) {
                foreach( 
$post['comments'] as $comment ) {
                    
$comment['edit']        = preg_replace$pattern$replace$comment['data'] );
                    
$comment['title']       = '';
                    
$comment['format_guid'] = 'bithtml';
                    
$comment['root_id']     = $bp->mContentId;
                    
$comment['parent_id']   = $bp->mContentId;
                    
$comment['user_id']     = ANONYMOUS_USER_ID;
                    
$comment['created']     = $comment['last_modified'] = strtotime$comment['date'] );
                    
$lc = new LibertyComment();
                    if( 
$lc->storeComment$comment )) {
                        
$feedback .= "\n- Comment: SUCCESS - comment added";
                    } else {
                        
$feedback .= "\n- Comment: ERROR:\n --- ".implode"\n --- "$lc->mErrors );
                    }
                }
            }

            if( 
$gBitSystem->isPackageActive'tags' )) {
                
tags_content_store$bp$post );
            }
        } else {
            
$feedback .= "\n- Post: ERROR:\n --- ".implode"\n --- "$bp->mErrors );
        }

        
//vd(strtotime($post['date'])." - ".date("r", strtotime($post['date']))." - ".$post['date']);
        
vd$feedback );
    }
}
?>


Save this file as something like import_typepad.php.

Step 4. Run The Importer

All that is left is to run the importer. You need to put import_typepad.php and typepad.php in your root directory. Then just load the import file in your browser. Depending on how many records you are storing the importing could take several minutes. Be sure you make a backup of your site and test this importer on a non-live site before running this importer on your live site.