Convertir RSS2 a Movable Type

Últimamente he estado probando Movable Type, y la verdad es que me gusta, así que he decidido migrar este blog a dicha plataforma. El problema es que para importar los posts del blog antiguo, la gente de Six Apart no proporciona ninguna utilidad para convertir los posts de otro blog, sino que éstos deben introducirse con un formato especial de Movable Type. Además, tengo pendiente encontrar algún diseño que me guste, o adaptar alguno.

Después de buscar algún conversor, deduciendo al fin que no había ninguno hecho, decidí hacer uno. Convierte los posts exportados en RSS2 (diría que todas las plataformas de blogging permiten exportar todos los posts en RSS2), al formato específico de MovableType.

Está hecho en Java (1.5) y os lo podéis descargar aquí:
Ejecutable RSS2MT (.class) - para ejecutar: java RSS2MT feed.rss posts.mt
Código fuente RSS2MT - para compilar: javac RSS2MT.java
(Adjunto el código fuente también en el cuerpo del artículo.)

Además, me ha servido para conocer una serie de clases muy interesantes de Java para manipular fechas (Date, DateFormat y SimpleDateFormat).

Por un lado, se crea un objeto SimpleDateFormat con el formato de fecha en la que tengamos el String (i.e: Web, 12 Apr 2006 19:24:50 +02:00 sería EEE, dd MMM yyyy HH:mm:ss Z)
SimpleDateFormat dateParser = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Se lee la fecha contenida en el String date
Date formattedDate = dateParser.parse(date);
Se aplica el nuevo pattern (04/12/2006 19:24:50)
dateParser.applyPattern("MM/dd/yyyy HH:mm:ss");
Y se escribe la fecha previamente leída con el nuevo formato
out.println("DATE: " + dateParser.format(formattedDate));



import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.text.*;
import java.util.regex.Pattern;

/**
*
* @author Daniel (http://www.gra2.com)
*/
public class RSS2MT {

/** Creates a new instance of Main */
public RSS2MT(String rss2, String dest) {
try {
BufferedReader in = new BufferedReader(new FileReader(rss2));

FileOutputStream fos = new FileOutputStream(dest);
DataOutputStream fout = new DataOutputStream(fos);
PrintStream out = new PrintStream(fout);

String line = in.readLine();
while(line!=null) {
System.out.println(line);
//Post starts after <item>
while(!line.startsWith("<item>")) {
line = in.readLine();
//Reached end of RSS file.
if(line.startsWith("</rss>")) return;
}

while(!line.startsWith("<title>")) { line = in.readLine(); }
//Got post title.

String title = line.substring("<title>".length());
title = title.substring(0, title.length()-"</title>".length());
out.println("TITLE: " + parseHTMLTags(title));

while(!line.startsWith("<pubDate>")) { line = in.readLine(); }
//Got date.

String date = line.substring("<pubDate>".length());
date = date.substring(0, date.length()-"</pubDate>".length());

//Convert RSS date format to MT
SimpleDateFormat dateParser = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date formattedDate = dateParser.parse(date);
dateParser.applyPattern("MM/dd/yyyy HH:mm:ss");

out.println("DATE: " + dateParser.format(formattedDate));

while(!line.startsWith("<dc:subject>")) { line = in.readLine(); }
//Got category.

String text = line.substring("<dc:subject>".length());
out.println("CATEGORY: " + parseHTMLTags(text.substring(0, text.length()-"</dc:subject>".length())));
out.println("-----");

while(!line.startsWith("<description>")) { line = in.readLine(); }
//Got body of post.

text = line.substring("<description>".length());
//System.out.println(text);

out.println("BODY:");
if(!text.contains("</description>")) {
//Post is multi-lined

out.println(parseHTMLTags(text)); //First line will not contain a </description> tag.
line = in.readLine();
while(!line.contains("</description>")) {
out.println(parseHTMLTags(line));
line = in.readLine();
}

out.println(parseHTMLTags(line.substring(0, line.length()-"</description>".length())));

} else {
//Post has a single line.
out.println("BODY:");
out.println(parseHTMLTags(text.substring(0, text.length()-"</description>".length())));
}
out.println("--------");

}
in.close();
out.close();

}catch(Exception e) {
e.printStackTrace();
}
}

public String parseHTMLTags(String line) {
line = line.replace("&lt;", "<");
line = line.replace("&gt;", ">");
line = line.replace("&lt;", "<");
line = line.replace("&quot;", "'");
line = line.replace("á", "&aacute;");
line = line.replace("é", "&eacute;");
line = line.replace("í", "&iacute;");
line = line.replace("ó", "&oacute;");
line = line.replace("ú", "&uacute;");

line = line.replace("Á", "&Aacute;");
line = line.replace("É", "&Eacute;");
line = line.replace("Í", "&Iacute;");
line = line.replace("Ó", "&Oacute;");
line = line.replace("Ú", "&Uacute;");

line = line.replace("à", "&agrave;");
line = line.replace("è", "&egrave;");
line = line.replace("ì", "&igrave;");
line = line.replace("ò", "&ograve;");
line = line.replace("ù", "&ugrave;");

line = line.replace("À", "&Agrave;");
line = line.replace("È", "&Egrave;");
line = line.replace("Ì", "&Igrave;");
line = line.replace("Ò", "&Ograve;");
line = line.replace("Ù", "&Ugrave;");

return line;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if(args.length<2) System.out.println("Syntax: java rss2mt RSS2File OutputFile");
else new RSS2MT(args[0], args[1]);
}

}




 


Trackback

Trackback URL for this entry:
http://www.gra2.com/trackback.php/convertir-rss2-a-movable-type

No trackback comments for this entry.

Comments

Post a comment

Search



About

newton.gra2.com is a blog about technology, opinion and random thoughts written by Daniel Alvarez, a computer engineer currently living in Zurich, Switzerland.

Topics

News (20/0)
Manuals (24/0)
Security (7/0)
Music (3/0)
Weeklog (1/0)
Personal (34/0)
Photos (3/0)
Opinion (14/0)
Windows (5/0)

Blogroll

Pros i contres (Jordi)
Entrepa de fusta (Oriol)
Spaghetti Code (Isaac)
Made in net (Eric)
Nogare (Juan)
Blog de Isaac Jimenez
Web d'en Jaume Benet
Montcada Wireless (Fran)
Blog d'en Ricard Forniol
Angela Fabregues
in.solit.us

Libertad Digital
FOX News
The Wall Street Journal
The Washington Times
The Jerusalem Post

Michelle Malkin
Eurabian News
Nihil Obstat
Barcepundit
Expose the left
Davids Medienkritik
Johan Norberg
Ayaan Hirsi Ali

User Functions

:

:


Lost your password?

Latest posts

Stories

No new stories

Comments last 2 days


Trackbacks last 2 days

No new trackback comments

Links last 2 weeks

No recent new links