wp2git/wp2git.d

71 lines
2.3 KiB
D
Raw Normal View History

2010-06-15 16:17:10 -05:00
import std.stdio;
import std.process;
import std.stream;
import std.string;
import std.file;
2010-06-15 18:09:41 -05:00
import std.conv;
2010-06-15 16:17:10 -05:00
import litexml;
int main(string[] args)
{
if (args.length != 2)
{
2010-06-15 16:57:05 -05:00
fwritefln(stderr, "Usage: %s Article_name", args[0]);
2010-06-15 16:17:10 -05:00
return 1;
}
string name = args[1];
if (name.length>=2 && name[0]=='"' && name[$-1]=='"')
name = name[1..$-1]; // strip quotes
2010-06-15 16:56:46 -05:00
if (spawnvp(P_WAIT, "curl", ["curl", "-d", "\"\"", "http://en.wikipedia.org/w/index.php?title=Special:Export&pages=" ~ name, "-o", "history.xml"]))
2010-06-15 16:17:10 -05:00
throw new Exception("curl error");
2010-06-15 16:57:05 -05:00
fwritefln(stderr, "Loading history...");
2010-06-15 16:57:25 -05:00
string xmldata = cast(string) read("history.xml");
2010-06-15 18:10:33 -05:00
std.file.remove("history.xml");
2010-06-15 16:57:25 -05:00
auto xml = new XmlDocument(new MemoryStream(xmldata));
2010-06-15 16:17:10 -05:00
2010-06-15 17:49:48 -05:00
string data = "reset refs/heads/master\n";
2010-06-15 16:17:10 -05:00
foreach (child; xml[0]["page"])
if (child.tag=="revision")
{
2010-06-15 18:26:01 -05:00
string id = child["id"].text;
2010-06-15 16:55:24 -05:00
string summary = child["comment"] ? child["comment"].text : null;
2010-06-15 17:04:12 -05:00
string committer = child["contributor"]["username"] ? child["contributor"]["username"].text : child["contributor"]["ip"].text;
2010-06-15 16:55:24 -05:00
string text = child["text"].text;
2010-06-15 18:26:01 -05:00
fwritefln(stderr, "Revision %s by %s: %s", id, committer, summary);
summary ~= "\n\nhttp://en.wikipedia.org/w/index.php?oldid=" ~ id;
2010-06-15 16:17:10 -05:00
data ~=
2010-06-15 17:50:02 -05:00
"commit refs/heads/master\n" ~
2010-06-15 18:09:41 -05:00
"committer " ~ committer ~ " <" ~ committer ~ "@en.wikipedia.org> " ~ ISO8601toRFC2822(child["timestamp"].text) ~ "\n" ~
2010-06-15 16:17:10 -05:00
"data " ~ .toString(summary.length) ~ "\n" ~
summary ~ "\n" ~
"M 644 inline " ~ name ~ ".txt\n" ~
"data " ~ .toString(text.length) ~ "\n" ~
text ~ "\n" ~
"\n";
}
write("fast-import-data", data);
2010-06-15 17:05:13 -05:00
if (exists(".git"))
throw new Exception("A git repository already exists here!");
system("git init");
2010-06-15 18:09:41 -05:00
system("git fast-import --date-format=rfc2822 < fast-import-data");
2010-06-15 18:10:33 -05:00
std.file.remove("fast-import-data");
system("git reset --hard");
2010-06-15 17:05:13 -05:00
2010-06-15 16:55:24 -05:00
return 0;
2010-06-15 16:17:10 -05:00
}
2010-06-15 18:09:41 -05:00
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
// 2010-06-15T19:28:44Z
// Feb 6 11:22:18 2007 -0500
string ISO8601toRFC2822(string s)
{
return monthNames[.toInt(s[5..7])-1] ~ " " ~ s[8..10] ~ " " ~ s[11..13] ~ ":" ~ s[14..16] ~ ":" ~ s[17..19] ~ " " ~ s[0..4] ~ " +0000";
}