Created
January 18, 2016 14:58
-
-
Save cfjedimaster/6b458d44dc5474d2fed6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var input = fs.readFileSync('./2016-01-03.xml').toString(); | |
var olddataraw = JSON.parse(fs.readFileSync('./oldposts.json').toString()); | |
var olddata = olddataraw.map(function(item) { | |
var date = new Date(item.posted); | |
item.year = date.getFullYear(); | |
item.month = pad(date.getMonth() + 1,2); | |
item.day = pad(date.getDate(),2); | |
return item; | |
}); | |
var skipped = 0; | |
function pad(n, width, z) { | |
z = z || '0'; | |
n = n + ''; | |
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; | |
} | |
var parseString = require('xml2js').parseString; | |
parseString(input, function(err, result) { | |
/* | |
var keys = Object.keys(result.rss.channel); | |
console.dir(keys); | |
for(var key in result.rss.channel[0]) { | |
console.log(key); | |
//console.log(result.rss.channel); | |
} | |
*/ | |
var items = result.rss.channel[0].item; | |
var done=0; | |
items.forEach(function(item) { | |
/* | |
for now, skip ones with no proper guid | |
*/ | |
var linkParts = item.link[0].split('/'); | |
var year = linkParts[3]; | |
var month = linkParts[4]; | |
var day = linkParts[5]; | |
var guid = item['wp:post_id'][0]; | |
var title = item.title[0]; | |
title = title.replace(/"/g,'\\"'); | |
var oldurl = ''; | |
if(linkParts.length === 6) { | |
olddata.forEach(function(old) { | |
if( ( | |
old.title == title || | |
old.title.replace(/"/g,'\\"') == title || | |
(title == 'Hal Helms\' Occasional Newsletter' && old.title == 'Hal Helms Occasional Newsletter') | |
) && old.year == year && old.month == month && old.day == day) { | |
linkParts[6] = old.id; | |
guid = ""; | |
} | |
}); | |
if(linkParts.length === 6) { | |
console.log('ERROR'); | |
console.log(title); | |
console.dir(item); | |
console.dir(linkParts); | |
throw("Couldn't find match."); | |
} | |
//make an old url | |
oldurl = 'http://www.raymondcamden.com/'+year+'/'+month.replace(/0/,'')+'/'+day.replace(/0/,'')+'/'+linkParts[6]; | |
} | |
done++; | |
//if(done > 5) process.exit(); | |
//console.log('--- '+item.title[0]+' --- '+item.link[0]); | |
var slug = linkParts[6]; | |
// var fileName = year+'-'+pad(month,2)+'-'+pad(day,2)+'-'+slug+'.md'; | |
var fileName = slug+'.md'; | |
var content = item['content:encoded'][0]; | |
// var date = item['wp:post_date'][0]; | |
//2015-06-24T13:50:46+02:00 | |
var date = new Date(item['pubDate'][0]); | |
var formattedDate = date.getFullYear() + '-' + pad((date.getMonth()+1),2) + '-' + pad(date.getDate(),2); | |
formattedDate += 'T' + pad(date.getHours(),2) + ':' + pad(date.getMinutes(),2) + ':' + pad(date.getSeconds(),2) + '+06:00'; | |
var cats = []; | |
var tags = []; | |
/* | |
<category domain="category" nicename="Development"><![CDATA[Development]]></category> | |
<category domain="post_tag" nicename="ionic"><![CDATA[Ionic]]></category> | |
<category domain="category" nicename="JavaScript"><![CDATA[JavaScript]]></category> | |
<category domain="category" nicename="Mobile"><![CDATA[Mobile]]></category> | |
*/ | |
if(item.category) { | |
for(var i=0;i<item.category.length;i++) { | |
//console.dir(item.category[i]['$']); | |
var catItem = item.category[i]['$']; | |
if(catItem.domain === 'category') cats.push(catItem.nicename); | |
if(catItem.domain === 'post_tag') tags.push(catItem.nicename); | |
} | |
} | |
if(cats.length === 0) cats[0] = "Misc"; | |
var frontMatter = { | |
title:title, | |
categories:cats, | |
tags:tags, | |
date:formattedDate, | |
url:"/"+year+"/"+month+"/"+day+"/"+slug | |
}; | |
if(guid != '') { | |
frontMatter.guid = guid; | |
} | |
if(oldurl != '') { | |
frontMatter.oldurl = oldurl; | |
} | |
content = JSON.stringify(frontMatter,null,'\t')+'\n\n'+content; | |
/* | |
new logic to support year/month/day | |
*/ | |
var path = '../hugo_dynamic/content/post/'+year+'/'; | |
//console.log(path); | |
try { | |
fs.accessSync(path,fs.F_OK); | |
} catch(e) { | |
if(e.errno === -2) { | |
fs.mkdirSync(path); | |
} | |
} | |
path = '../hugo_dynamic/content/post/'+year+'/'+pad(month,2)+'/'; | |
try { | |
fs.accessSync(path,fs.F_OK); | |
} catch(e) { | |
if(e.errno === -2) { | |
fs.mkdirSync(path); | |
} | |
} | |
path = '../hugo_dynamic/content/post/'+year+'/'+pad(month,2)+'/'+pad(day,2)+'/'; | |
try { | |
fs.accessSync(path,fs.F_OK); | |
} catch(e) { | |
if(e.errno === -2) { | |
fs.mkdirSync(path); | |
} | |
} | |
fs.writeFileSync('../hugo_dynamic/content/post/'+year+'/'+pad(month,2)+'/'+pad(day,2)+'/'+fileName, content,'utf8'); | |
//console.log(fileName); | |
//console.dir(item); | |
}); | |
console.log('Total done: '+done); | |
console.log('Skipped: '+skipped); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment