Last active
June 24, 2019 02:34
-
-
Save pindlebot/bc96457f39b930fd3fe153e4cc4700c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io" | |
"log" | |
"math/rand" | |
"os" | |
"os/exec" | |
"os/user" | |
"path" | |
"strconv" | |
"strings" | |
"time" | |
) | |
type Url struct { | |
Id int64 | |
Url string | |
Title string | |
VisitCount int64 | |
TypedCount int64 | |
Timestamp int64 | |
Hidden int64 | |
} | |
func parseInt(str string) int64 { | |
n, _ := strconv.ParseInt(str, 10, 64) | |
return n | |
} | |
func copy(src string, dst string) { | |
from, err := os.Open(src) | |
if err != nil { | |
panic(err) | |
} | |
defer from.Close() | |
to, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE, 0666) | |
if err != nil { | |
panic(err) | |
} | |
defer to.Close() | |
_, err = io.Copy(to, from) | |
if err != nil { | |
panic(err) | |
} | |
} | |
var src = rand.NewSource(time.Now().UnixNano()) | |
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
const ( | |
letterIdxBits = 6 // 6 bits to represent a letter index | |
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits | |
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits | |
) | |
func RandStringBytesMaskImprSrcSB(n int) string { | |
sb := strings.Builder{} | |
sb.Grow(n) | |
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters! | |
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; { | |
if remain == 0 { | |
cache, remain = src.Int63(), letterIdxMax | |
} | |
if idx := int(cache & letterIdxMask); idx < len(letterBytes) { | |
sb.WriteByte(letterBytes[idx]) | |
i-- | |
} | |
cache >>= letterIdxBits | |
remain-- | |
} | |
return sb.String() | |
} | |
func main() { | |
usr, err := user.Current() | |
if err != nil { | |
log.Fatal(err) | |
} | |
history := path.Join(usr.HomeDir, "/Library/Application Support/Google/Chrome/Default/History") | |
key := RandStringBytesMaskImprSrcSB(10) | |
dst := path.Join("/tmp/", key) | |
copy(history, dst) | |
res, _ := exec.Command("sqlite3", dst, "-separator", ",", "select * from urls limit").Output() | |
str := string(res) | |
raw := strings.Split(str, "\n") | |
rows := raw[0 : len(raw)-1] | |
var url Url | |
var urls []Url | |
for index, row := range rows { | |
columns := strings.Split(row, ",") | |
if index != 0 { | |
url.Id = parseInt(columns[0]) | |
url.Url = columns[1] | |
url.Title = columns[2] | |
url.VisitCount = parseInt(columns[3]) | |
url.TypedCount = parseInt(columns[4]) | |
url.Timestamp = parseInt(columns[5]) | |
url.Hidden = parseInt(columns[6]) | |
urls = append(urls, url) | |
} | |
} | |
jsonData, err := json.Marshal(urls) | |
if err != nil { | |
panic(err) | |
} | |
jsonFile, err := os.Create("./data.json") | |
if err != nil { | |
fmt.Println(err) | |
} | |
defer jsonFile.Close() | |
jsonFile.Write(jsonData) | |
jsonFile.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment