-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (89 loc) · 1.98 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
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
)
var (
inputPath = flag.String("in", "", "input json file path")
)
type Dataset struct {
Title string `json:"title"`
Body string `json:"body"`
Tags []Tag `json:tags`
Comments []Comment `json:comments`
}
type Tag struct {
Name string `json:"name"`
Versions []string `json:versions`
}
type User struct {
Id string `json:id`
PermanentId int `json:permanent_id`
ProfileImageUrl string `profile_image_url`
}
type Comment struct {
Body string `json:body`
CreatedAt string `json:created_at`
UpdateAt string `json:updated_at`
User User `json:user`
}
type Article struct {
Results []Dataset `json:"articles"`
}
func main() {
flag.Parse()
if !Exsits(*inputPath) {
fmt.Printf("not exist input file path : %s \n", *inputPath)
return
}
// Loading jsonfile
file, err := ioutil.ReadFile(*inputPath)
if err != nil {
panic(err)
}
var article Article
jsonErr := json.Unmarshal(file, &article)
if jsonErr != nil {
panic(jsonErr)
}
for _, result := range article.Results {
fileName := strings.Replace(result.Title, "/", "", -1)
fw, err := os.Create("./output/" + fileName + ".md")
if err != nil {
panic(err)
}
w := bufio.NewWriter(fw)
fmt.Fprint(w, OutputValue(&result))
w.Flush()
fw.Close()
}
fmt.Println("処理が終了しました")
}
func Exsits(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func OutputValue(result *Dataset) string {
var buffer bytes.Buffer
buffer.WriteString("#" + result.Title)
buffer.WriteString("\n")
buffer.WriteString(result.Body)
buffer.WriteString("\n")
if len(result.Comments) > 0 {
buffer.WriteString("##Comments")
buffer.WriteString("\n")
for _, val := range result.Comments {
buffer.WriteString(val.Body)
buffer.WriteString("\n")
buffer.WriteString(val.User.Id)
buffer.WriteString("\n")
}
}
return buffer.String()
}