You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.4 KiB
81 lines
1.4 KiB
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 {
|
|
fmt.Printf("\nError adding qurl: %s\n", err.Error())
|
|
continue
|
|
}
|
|
|
|
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
|
|
}
|