-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
227 lines (202 loc) · 5.11 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"regexp"
"strings"
"sync"
"time"
"github.com/b4b4r07/go-crowi"
)
type Qiita struct {
Articles []Articles `json:"articles"`
Groups []interface{} `json:"groups"`
Projects []Projects `json:"projects"`
Version string `json:"version"`
}
type Articles struct {
RenderedBody string `json:"rendered_body"`
Body string `json:"body"`
Coediting bool `json:"coediting"`
CreatedAt time.Time `json:"created_at"`
Group interface{} `json:"group"`
ID string `json:"id"`
Private bool `json:"private"`
Tags []struct {
Name string `json:"name"`
Versions []interface{} `json:"versions"`
} `json:"tags"`
Title string `json:"title"`
UpdatedAt time.Time `json:"updated_at"`
URL string `json:"url"`
User struct {
ID string `json:"id"`
PermanentID int `json:"permanent_id"`
ProfileImageURL string `json:"profile_image_url"`
} `json:"user"`
Comments []interface{} `json:"comments"`
}
type Projects struct {
RenderedBody string `json:"rendered_body"`
Archived bool `json:"archived"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
ID int `json:"id"`
Name string `json:"name"`
UpdatedAt time.Time `json:"updated_at"`
User struct {
ID string `json:"id"`
PermanentID int `json:"permanent_id"`
ProfileImageURL string `json:"profile_image_url"`
} `json:"user"`
}
var (
img = regexp.MustCompile(`<img .* ?src="(https?://.*?)"`)
ch = make(chan int, 4)
urlSafe = strings.NewReplacer(
`^`, `^`, // for Crowi's regexp
`$`, `$`,
`*`, `*`,
`%`, `%`, // query
`?`, `?`,
`/`, `/`, // Prevent unexpected stratification
)
)
var (
accessToken = flag.String("access-token", "", "Crowi's access token")
crowiUrl = flag.String("crowi-url", "", "Your Crowi base URL")
pagePath = flag.String("page-path", "/qiita", "Default page path")
)
func main() {
flag.Parse()
var q Qiita
dec := json.NewDecoder(os.Stdin)
if err := dec.Decode(&q); err != nil {
log.Printf("[ERROR] local json syntax error: %s", err.Error())
os.Exit(1)
}
wg := sync.WaitGroup{}
errNum := 0
client, err := crowi.NewClient(*crowiUrl, *accessToken)
if err != nil {
log.Printf("[ERROR] %s", err.Error())
os.Exit(1)
}
for _, article := range q.Articles {
wg.Add(1)
go func(article Articles) {
err := qiita2crowi(client, article)
if err != nil {
log.Printf("[ERROR] %s", err.Error())
errNum++
}
wg.Done()
}(article)
}
wg.Wait()
if errNum > 0 {
log.Printf("Failures %d/%d pages", errNum, len(q.Articles))
os.Exit(1)
}
log.Printf("Finished.")
}
func qiita2crowi(client *crowi.Client, article Articles) error {
ch <- 1
defer func() { <-ch }()
pagePath := getTitlePath(*pagePath, article.Title)
if !path.IsAbs(pagePath) {
return fmt.Errorf("%s: invalid page path", pagePath)
}
pageBody := fmt.Sprintf("<!-- Imported by\n%s\n-->\n\n%s",
strings.TrimLeft(article.URL, "https://"),
article.Body,
)
res, err := client.PagesCreate(pagePath, pageBody)
if err != nil {
return err
}
if !res.OK {
return errors.New(res.Error)
}
// Download images in the Qiita text
// then upload to Crowi
pageId := res.Page.ID
matched := img.FindAllStringSubmatch(article.RenderedBody, -1)
if len(matched) > 0 {
for _, urls := range matched {
for i := 1; i < len(urls); i++ {
file, err := downloadImage(urls[i])
if err != nil {
return err
}
res, err := client.AttachmentsAdd(pageId, file)
if err != nil {
return err
}
if !res.OK {
return errors.New(res.Error)
}
pageBody = strings.Replace(pageBody, urls[i], res.Filename, -1)
}
}
// Update image's links in the Crowi page
res, err = client.PagesUpdate(pageId, pageBody)
if err != nil {
return err
}
if !res.OK {
return errors.New(res.Error)
}
}
// If there are comments, add those at the end of the body
if len(article.Comments) > 0 {
pageBody += "# Comments by Qiita:Team\n"
for _, comment := range article.Comments {
pageBody += fmt.Sprintf("## %s\n", comment.(map[string]interface{})["user"].(map[string]interface{})["id"].(string))
pageBody += comment.(map[string]interface{})["body"].(string)
}
res, err = client.PagesUpdate(pageId, pageBody)
if err != nil {
return err
}
if !res.OK {
return errors.New(res.Error)
}
}
return nil
}
func getSaftyPath(path string) string {
return urlSafe.Replace(path)
}
func getTitlePath(basePath, titlePath string) string {
return path.Clean(path.Join(
basePath,
getSaftyPath(titlePath),
))
}
func downloadImage(url string) (filename string, err error) {
response, err := http.Get(url)
if err != nil {
return
}
// response.Status
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return
}
_, filename = path.Split(url)
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return
}
defer file.Close()
file.Write(body)
return
}