aboutsummaryrefslogtreecommitdiffstats
path: root/importer
diff options
context:
space:
mode:
authorAdam Mathes <adam@trenchant.org>2017-01-23 20:04:03 -0800
committerAdam Mathes <adam@trenchant.org>2017-01-23 20:04:03 -0800
commit93d6d36eb697cd9452eb4aab446151a1a33ed245 (patch)
treec9cb88718d03a6964f6d3705066f11d356257d37 /importer
downloadneko-93d6d36eb697cd9452eb4aab446151a1a33ed245.tar.gz
neko-93d6d36eb697cd9452eb4aab446151a1a33ed245.tar.bz2
neko-93d6d36eb697cd9452eb4aab446151a1a33ed245.zip
neko v2 initial commit
Diffstat (limited to 'importer')
-rw-r--r--importer/importer.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/importer/importer.go b/importer/importer.go
new file mode 100644
index 0000000..f746d72
--- /dev/null
+++ b/importer/importer.go
@@ -0,0 +1,80 @@
+package importer
+
+import (
+ // "bufio"
+ "encoding/json"
+ //"fmt"
+ "io"
+ "log"
+ "neko/models/feed"
+ "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)
+ }
+}