-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
131 lines (112 loc) · 2.92 KB
/
main.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* wp-import imports posts from WordPress into Write.as / WriteFreely.
* Copyright © 2019 A Bunch Tell LLC.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
package main
import (
"bytes"
"fmt"
"github.com/frankbille/go-wxr-import"
"github.com/writeas/go-writeas/v2"
"github.com/writeas/godown"
"io/ioutil"
"log"
"os"
"regexp"
)
var (
commentReg = regexp.MustCompile("(?m)<!-- ([^m ]|m[^o ]|mo[^r ]|mor[^e ])+ -->\n?")
)
func main() {
if len(os.Args) < 2 {
//errQuit("usage: wp-import https://write.as filename.xml")
errQuit("usage: wp-import filename.xml")
}
//instance := os.Args[1]
instance := "https://write.as"
fname := os.Args[1]
// TODO: load user config from same func as writeas-cli
t := ""
if t == "" {
errQuit("not authenticated. run: writeas auth <username>")
}
cl := writeas.NewClientWith(writeas.Config{
URL: instance + "/api",
Token: t,
})
// An application key is required on Write.as to skip API rate-limiting
appKey := ""
cl.SetApplicationKey(appKey)
log.Printf("Reading %s...\n", fname)
raw, _ := ioutil.ReadFile(fname)
log.Println("Parsing...")
d := wxr.ParseWxr(raw)
log.Printf("Found %d channels.\n", len(d.Channels))
postsCount := 0
for _, ch := range d.Channels {
log.Printf("Channel: %s\n", ch.Title)
// Create the blog
c := &writeas.CollectionParams{
Title: ch.Title,
Description: ch.Description,
}
log.Printf("Creating %s...\n", ch.Title)
coll, err := cl.CreateCollection(c)
if err != nil {
errQuit(err.Error())
}
log.Printf("Done!\n")
log.Printf("Found %d items.\n", len(ch.Items))
for _, wpp := range ch.Items {
if wpp.PostType != "post" {
continue
}
// Convert to Markdown
b := bytes.NewBufferString("")
r := bytes.NewReader([]byte(wpp.Content))
err = godown.Convert(b, r, nil)
con := b.String()
// Remove unneeded WordPress comments that take up space, like <!-- wp:paragraph -->
con = commentReg.ReplaceAllString(con, "")
// Append tags
tags := ""
sep := ""
for _, cat := range wpp.Categories {
if cat.Domain != "post_tag" {
continue
}
tags += sep + "#" + cat.DisplayName
sep = " "
}
if tags != "" {
con += "\n\n" + tags
}
p := &writeas.PostParams{
Title: wpp.Title,
Slug: wpp.PostName,
Content: con,
Created: &wpp.PostDateGmt,
Updated: &wpp.PostDateGmt,
Font: "norm",
Language: &ch.Language,
Collection: coll.Alias,
}
log.Printf("Creating %s", p.Title)
_, err = cl.CreatePost(p)
if err != nil {
errQuit(fmt.Sprintf("create post: %s\n", err))
}
postsCount++
}
}
log.Printf("Created %d posts.\n", postsCount)
}
func errQuit(m string) {
fmt.Fprintf(os.Stderr, m+"\n")
os.Exit(1)
}