Importing Radio posts to MT

A couple of people have asked for more pointers on exactly how I got my Radio posts into MT. So here’s some more detailed information.

Firstly, I ran into lots of problems with Radio seemingly caching macros. When I edit radio macro files and resave them, Radio doesn’t seem to notice the changes for about 5 minutes. Which is far ideal for the programming approach I use (particularly in a language I’m not that familiar with, such as UserTalk), which basically involves keeping the code running at all times (code a line, save it, test it still works). I would never survived as a programmer in the old days of coding ‘offline’.

I originally thought this was to do with my set up (I have my PC’s C: drive mounted via samba onto the linux box on which I do most of my programming, as I find it much easier to code in that environment), but using Notepad didn’t seem to make any difference, and there are a few scattered references to this problem littering the Userland noticeboards.

Anyway, I ended up having to code in the scratchpad instead. The script is below. It was cut-n-pasted from Radio’s outline editor so the formatting is a little strange, but it should do the trick. This can either be used as a macro, or, as I did, or by adding it to the scratchpad and calling <% workspace.showAll ()%> in a page (I just created a new page in my Radio Userland/www/ directory that had that in it.

This outputs all my posts in XML, with the titles and bodies wrapped in CDATA tags. I then wrote a simple Perl script to turn this into the MT input format. You then import this into MT following the instructions in the manual.

I’m sure this could all be tidied up some more, but hopefully it’s of some use to someone as is!

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
on showAll () {
 on showAll () {
  local (s = "", i);;
 
  on cData (text) {
    return "<![CDATA[" + text + "]]>";};
 
  on tag(tag, text) {
    return "<" + tag + ">" + text + "</" + tag + ">";};
 
  on postTitle (post, title = "") {
    if defined (post.title) {
      title = post.title};
    return tag("ptitle", title);};
 
  on postLink (post, link = "") {
    if defined (post.link) {
      link = cdata(post.link)};
    return tag("plink", link);};
 
  on postCategories (post, cats = "") {
    local (adrcats = post.categories);;
    for adr in @adrcats {
      local (name = nameOf(adr^));
      local (adrcat= @adrblog^.categories.[name]);;
      if (defined (adrcat) && defined (adrcat^.htmlUrl)) {
        cats = cats + tag("category", name);}};
    return tag("categories", cats);};
 
  on postText (post) {
    return tag("text", cData(string(post.text)))};
 
  local (adrblog = radio.weblog.init ());;
  local (adrposts = @adrblog^.posts);;
  for i = sizeof (adrposts^) downto 1 {
    local (adrpost = @adrposts^ [i]);;
    local (url); radio.weblog.getUrlForPost (adrpost, @url);;
    local (thisdate = adrpost^.when);;
    s = s + "<post id=\"" + i + "\">";;
    s = s + tag("date", thisdate) + postTitle(adrpost^) +
      postLink(adrpost^) + postCategories(adrpost^) + postText(adrpost^);;
    s = s + "</post>";};
  s = tag("allposts", s);;
  return (s);
}
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
#!/usr/bin/perl
 
 
=head1 NAME
 
radio2mt - turn Radio UserLand posts into Moveable Type posts
 
=head1 SYNOPSIS
 
	radio2mt filename > outputfile
 
=head1 DESCRIPTION
 
This script takes a Radio UserLand database that has been output to XML
(using the Radio macro available at http://www.tmtm.com/radioShowAll.txt)
and outputs a file suitable for importing into Moveable Type.
 
=head1 AUTHOR
 
Tony Bowden <tony@tmtm.com>
 
=head1 LICENSE
 
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
 
=cut
 
use strict;
use warnings;
use File::Slurp;
use XML::LibXML;
use Time::Piece;
 
my $filename = shift or die "Usage: $0 <filename>\n";
my $parser = XML::LibXML->new();
 
my $text = read_file($filename);
   $text = $1 if $text =~ m#(<allposts>.*</allposts>)#gs;
 
my $doc = $parser->parse_string($text);
 
foreach my $post (reverse $doc->findnodes('//post'));
  my $id    = $post->findvalue('@id');
  my $title = $post->findvalue("./ptitle");
  my $date  = $post->findvalue("./date");
  my $body  = $post->findvalue("./text");
  my $tp    = Time::Piece->strptime($date, "%d/%m/%Y; %H:%M:%S");
  my $link  = $post->findvalue("./plink");
  my @cats  = map $_->findvalue("."), $post->findnodes(".//category");
  my $pr_ct = shift @cats;
  print  "TITLE: $title\n" if $title;
  printf "DATE: %s\n", $tp->strftime("%m/%d/%Y %H:%M:%S");
  print  "PRIMARY CATEGORY: $pr_ct\n" if $pr_ct;
  print  "CATEGORY: $_\n" foreach @cats;
  print  "STATUS: publish\n";
  print  "-----\nBODY:\n$body\n-----\n";
  print  "--------\n";
}

Leave a Reply

Your email address will not be published. Required fields are marked *