1
0
Fork 0
qurl/load.go

87 lines
1.5 KiB
Go
Raw Normal View History

2018-11-11 16:56:30 -09:00
package main
import (
"encoding/json"
"fmt"
"net"
"os"
"git.binarythought.com/cdramey/qurl/storage"
"git.binarythought.com/cdramey/qurl/obj"
2018-11-11 16:56:30 -09:00
"time"
)
type qurljson struct {
ID uint64 `json:"id"`
IP string `json:"ip"`
Browser string `json:"browser"`
URL string `json:"url"`
Date struct {
Date int64 `json:"$date"`
} `json:"date"`
}
func loadjson(stor storage.Storage, filename string) error {
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("Error opening %s: %s", filename, err.Error())
}
defer file.Close()
var qj []qurljson
decoder := json.NewDecoder(file)
err = decoder.Decode(&qj)
if err != nil {
return fmt.Errorf("Error parsing %s: %s", filename, err.Error())
}
fmt.Printf("Parsing %d qurls.. \n", len(qj))
var max uint64 = 0
var count uint64 = 0
for _, e := range qj {
if e.ID > max {
max = e.ID
}
q := &obj.QURL{
2018-11-11 16:56:30 -09:00
ID: e.ID,
URL: e.URL,
}
if e.Date.Date > 0 {
q.Created = time.Unix((e.Date.Date / 1000), 0)
2018-11-11 16:56:30 -09:00
}
if e.IP != "" {
q.IP = net.ParseIP(e.IP)
2018-11-11 16:56:30 -09:00
}
if e.Browser != "" {
q.Browser = e.Browser
2018-11-11 16:56:30 -09:00
}
err := q.CheckValid()
2018-11-11 16:56:30 -09:00
if err != nil {
fmt.Printf("\n%s\nValidation failure: %s\n", q.URL, err.Error())
2018-11-17 07:47:39 -09:00
continue
2018-11-11 16:56:30 -09:00
}
err = stor.AddQURL(q)
if err != nil {
return fmt.Errorf("AddQURL() Database error: %s", err.Error())
}
2018-11-11 16:56:30 -09:00
count++
if (count % 100) == 0 {
fmt.Printf("*")
}
}
err = stor.SetQURLSequence(max)
if err != nil {
return fmt.Errorf("Error setting sequence: %s", err.Error())
}
fmt.Printf("\nDone!\n")
return nil
}