-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (84 loc) · 2.17 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
package main
import (
"encoding/csv"
"encoding/json"
"flag"
"log"
"os"
"strings"
)
type PinboardBookmark struct {
Href string `json:"href"`
Description string `json:"description"`
Extended string `json:"extended"`
Time string `json:"time"`
ToRead string `json:"toread"`
Tags string `json:"tags"`
}
var (
input string
output string
defaultFolder string
unreadFolder string
)
func main() {
flag.StringVar(&input, "input", "", "JSON export of Pinboard bookmarks")
flag.StringVar(&output, "output", "", "CSV file to write to")
flag.StringVar(&defaultFolder, "default-folder", "Imported", "Folder to put imported bookmarks in")
flag.StringVar(&unreadFolder, "unread-folder", "To Read", "Folder to put unread imported bookmarks in")
flag.Parse()
if input == "" {
log.Fatal("Must set -input")
}
if output == "" {
log.Fatal("Must set -output")
}
if err := run(input, output); err != nil {
log.Fatal(err)
}
}
// run takes a input file and output file and returns an error if conversion
// from pinboard bookmarks.json to csv fails.
func run(input, output string) error {
f, err := os.Open(input)
if err != nil {
return err
}
defer f.Close()
var bookmarks []PinboardBookmark
if err = json.NewDecoder(f).Decode(&bookmarks); err != nil {
return err
}
var records [][]string
for _, bookmark := range bookmarks {
records = append(records, convert(bookmark))
}
o, err := os.Create(output)
if err != nil {
return err
}
defer o.Close()
w := csv.NewWriter(o)
if err = w.Write([]string{"url", "folder", "title", "description", "tags", "created"}); err != nil {
return err
}
if err = w.WriteAll(records); err != nil {
return err
}
log.Println("✅ Converted", len(records), "bookmarks")
return nil
}
// convert converts a PinboardBookmark to a CSV record to be imported on
// Raindrop.io
func convert(b PinboardBookmark) []string {
url := b.Href
folder := defaultFolder
if b.ToRead == "yes" {
folder = unreadFolder
}
title := b.Description
description := b.Extended
tags := strings.Join(strings.Split(b.Tags, " "), ", ")
created := b.Time
return []string{url, folder, title, description, tags, created}
}