Skip to content

Instantly share code, notes, and snippets.

@lobatt
Last active December 18, 2016 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lobatt/59fcaf438ebca0d6ecca to your computer and use it in GitHub Desktop.
Save lobatt/59fcaf438ebca0d6ecca to your computer and use it in GitHub Desktop.
Go program to migrate blog posts to Hugo via RSS feed xml (specially made for micolog)
//Usage:
// First set your blog feed generate to dump all feed entries
// $ wget http://yourdomain.com/feed -O feed.xml
// OR you can just export your data as rss 2.0 feed.
// Then:
// $ go run feed2md.go
// It will generate a bunch of .md files, one for each blog item.
// Now move those items to your content dir, and you are done.
// This program will generate archetypes for hugo engine, inlcuding aliases for the old URL pattern
// The content, however, will stay as HTML code, but it is ok, Hugo just handle them nicely.
package main
import (
"flag"
"encoding/xml"
"io/ioutil"
"log"
"path"
"os"
"fmt"
"time"
"strings"
"net/url"
)
type RSS struct {
XMLName xml.Name `xml:"rss"`
Channel Channel `xml:"channel"`
}
type Channel struct {
Items []Item `xml:"item"`
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
PubDate string `xml:"pubDate"`
Category string `xml:"category"`
Content string `xml:"content"`
}
func main() {
var input = flag.String("f", "feed.xml", "Feed xml input for the converter")
flag.Parse()
dat, err := ioutil.ReadFile(*input)
if err != nil {
log.Panicf("Can not read input file %s", *input)
}
feed := &RSS{}
if e := xml.Unmarshal(dat, feed); e != nil {
log.Panicf("Can not unmarhsal data %s", e.Error())
}
for _, item := range feed.Channel.Items {
link := item.Link
aliasUrl, _ := url.Parse(link)
log.Printf("%v\n",item.Link)
//log.Printf("%v\n",item.Content)
filename := path.Base(link)
f, _ := os.Create(strings.Split(filename,".")[0] + ".md")
f.WriteString("+++\n")
//archetype
f.WriteString(fmt.Sprintf("title = \"%s\"\n", item.Title))
date, _ := time.Parse(time.RFC1123Z, item.PubDate)
f.WriteString(fmt.Sprintf("date = \"%s\"\n", date.Format(time.RFC3339)))
//f.WriteString(fmt.Sprintf("category = %v\n", strings.Split(item.Category,",")))
f.WriteString(fmt.Sprintf("aliases = [\"%s\"]\n", aliasUrl.RequestURI()))
f.WriteString("+++\n")
f.WriteString(item.Content)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment