Initial commit
This commit is contained in:
80
load.go
Normal file
80
load.go
Normal file
@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"qurl/qurl"
|
||||
"qurl/storage"
|
||||
"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
|
||||
}
|
||||
|
||||
qurl := &qurl.QURL{
|
||||
ID: e.ID,
|
||||
URL: e.URL,
|
||||
}
|
||||
|
||||
if e.Date.Date > 0 {
|
||||
qurl.Created = time.Unix((e.Date.Date / 1000), 0)
|
||||
}
|
||||
|
||||
if e.IP != "" {
|
||||
qurl.IP = net.ParseIP(e.IP)
|
||||
}
|
||||
|
||||
if e.Browser != "" {
|
||||
qurl.Browser = e.Browser
|
||||
}
|
||||
|
||||
err := stor.AddQURL(qurl)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error adding qurl: %s", err.Error())
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user