blob: 4c48bb0bda571f7eddd6a1e35100a1abcffe183f (
plain) (
blame)
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
|
package importer
import (
// "bufio"
"encoding/json"
//"fmt"
"io"
"log"
"adammathes.com/neko/models/feed"
"adammathes.com/neko/models/item"
"os"
)
type IItem struct {
Title string `json:"title"`
Url string `json:"url"`
Description string `json:"description"`
ReadState bool `json:"read"`
Starred bool `json:"starred"`
Date *IDate `json:"date"`
Feed *IFeed `json:"feed"`
}
type IFeed struct {
Url string `json:"url"`
Title string `json:"title"`
WebUrl string `json:"web_url"`
}
type IDate struct {
Date string `json:"$date"`
}
func ImportJSON(filename string) {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
dec := json.NewDecoder(f)
for {
var ii IItem
if err := dec.Decode(&ii); err == io.EOF {
break
} else if err != nil {
log.Println(err)
} else {
InsertIItem(&ii)
}
}
}
func InsertIItem(ii *IItem) {
var f feed.Feed
if ii.Feed == nil {
return
}
err := f.ByUrl(ii.Feed.Url)
if err != nil {
f.Url = ii.Feed.Url
f.Title = ii.Feed.Title
f.Create()
}
var i item.Item
i.FeedId = f.Id
i.Title = ii.Title
i.Url = ii.Url
i.Description = ii.Description
i.PublishDate = ii.Date.Date
err = i.Create()
log.Printf("inserted %s\n", i.Url)
if err != nil {
log.Println(err)
}
}
|