1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00
cowyo/utils.go

219 lines
5.4 KiB
Go
Raw Normal View History

2016-02-06 16:40:53 +03:00
package main
import (
2016-02-14 17:05:56 +03:00
"encoding/binary"
2016-02-11 01:03:00 +03:00
"io/ioutil"
2016-02-10 23:38:55 +03:00
"log"
2016-02-06 16:40:53 +03:00
"math/rand"
2016-02-11 01:03:00 +03:00
"path"
2016-02-10 01:52:52 +03:00
"sort"
2016-02-06 16:40:53 +03:00
"strings"
"time"
2016-02-14 17:05:56 +03:00
"github.com/boltdb/bolt"
"github.com/sergi/go-diff/diffmatchpatch"
2016-02-06 16:40:53 +03:00
)
var animals []string
var adjectives []string
2016-02-10 17:21:24 +03:00
var robotsTxt string
var aboutPageText string
2016-02-06 16:40:53 +03:00
2016-02-10 02:38:25 +03:00
type versionsInfo struct {
VersionDate string
VersionNum int
}
2016-02-06 16:40:53 +03:00
func init() {
rand.Seed(time.Now().Unix())
2016-02-11 01:03:00 +03:00
animalsText, _ := ioutil.ReadFile(path.Join(RuntimeArgs.SourcePath, "static/text/animals"))
animals = strings.Split(string(animalsText), ",")
adjectivesText, _ := ioutil.ReadFile(path.Join(RuntimeArgs.SourcePath, "static/text/adjectives"))
2016-02-14 16:18:00 +03:00
adjectives = strings.Split(string(adjectivesText), "\n")
2016-02-11 01:03:00 +03:00
robotsTxtFile, _ := ioutil.ReadFile(path.Join(RuntimeArgs.SourcePath, "static/text/robots.txt"))
robotsTxt = string(robotsTxtFile)
2016-02-06 16:40:53 +03:00
}
func randomAnimal() string {
return strings.Replace(strings.Title(animals[rand.Intn(len(animals)-1)]), " ", "", -1)
}
func randomAdjective() string {
return strings.Replace(strings.Title(adjectives[rand.Intn(len(adjectives)-1)]), " ", "", -1)
}
func randomAlliterateCombo() (combo string) {
combo = ""
2016-02-14 17:14:01 +03:00
// first determine which names are taken from program data
2016-02-14 17:05:56 +03:00
takenNames := []string{}
err := db.View(func(tx *bolt.Tx) error {
// Assume bucket exists and has keys
b := tx.Bucket([]byte("programdata"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
takenNames = append(takenNames, strings.ToLower(string(v)))
}
return nil
})
if err != nil {
panic(err)
}
2016-02-14 17:14:01 +03:00
// generate random alliteration thats not been used
2016-02-06 16:40:53 +03:00
for {
animal := randomAnimal()
adjective := randomAdjective()
2016-02-14 17:05:56 +03:00
if animal[0] == adjective[0] && stringInSlice(strings.ToLower(adjective+animal), takenNames) == false {
2016-02-06 16:40:53 +03:00
combo = adjective + animal
break
}
}
return
}
2016-02-14 17:14:01 +03:00
// is there a string in a slice?
2016-02-14 17:05:56 +03:00
func stringInSlice(s string, strings []string) bool {
for _, k := range strings {
if s == k {
return true
}
}
return false
}
// itob returns an 8-byte big endian representation of v.
func itob(v int) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(v))
return b
}
func contentType(filename string) string {
switch {
case strings.Contains(filename, ".css"):
return "text/css"
case strings.Contains(filename, ".jpg"):
return "image/jpeg"
case strings.Contains(filename, ".png"):
return "image/png"
case strings.Contains(filename, ".js"):
return "application/javascript"
}
return "text/html"
}
2016-02-07 19:05:38 +03:00
func diffRebuildtexts(diffs []diffmatchpatch.Diff) []string {
text := []string{"", ""}
for _, myDiff := range diffs {
if myDiff.Type != diffmatchpatch.DiffInsert {
text[0] += myDiff.Text
}
if myDiff.Type != diffmatchpatch.DiffDelete {
text[1] += myDiff.Text
}
}
return text
}
2016-02-10 23:38:55 +03:00
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%s took %s", name, elapsed)
}
2016-02-11 17:26:02 +03:00
func getImportantVersions(p WikiData) ([]versionsInfo, time.Duration) {
2016-02-10 23:41:24 +03:00
// defer timeTrack(time.Now(), "getImportantVersions")
2016-02-10 01:52:52 +03:00
m := map[int]int{}
lastTime := time.Now().AddDate(0, -1, 0)
totalTime := time.Now().Sub(time.Now())
2016-02-11 01:03:00 +03:00
for i := range p.Diffs {
2016-02-10 01:52:52 +03:00
parsedTime, _ := time.Parse(time.ANSIC, p.Timestamps[i])
duration := parsedTime.Sub(lastTime)
if duration.Minutes() < 3 {
totalTime += duration
}
2016-02-10 01:52:52 +03:00
m[i] = int(duration.Seconds())
if i > 0 {
m[i-1] = m[i]
}
// On to the next one
lastTime = parsedTime
}
2016-02-10 02:38:25 +03:00
// Sort in order of decreasing diff times
2016-02-10 01:52:52 +03:00
n := map[int][]int{}
var a []int
for k, v := range m {
n[v] = append(n[v], k)
}
for k := range n {
a = append(a, k)
}
2016-02-10 01:52:52 +03:00
sort.Sort(sort.Reverse(sort.IntSlice(a)))
2016-02-10 02:38:25 +03:00
// Get the top 4 biggest diff times
var importantVersions []int
var r []versionsInfo
2016-02-10 01:52:52 +03:00
for _, k := range a {
for _, s := range n[k] {
if s != 0 && s != len(n) {
2016-02-10 23:16:29 +03:00
// fmt.Printf("%d, %d\n", s, k)
2016-02-10 02:38:25 +03:00
importantVersions = append(importantVersions, s)
2016-02-10 02:47:08 +03:00
if len(importantVersions) > 10 {
2016-02-10 02:38:25 +03:00
sort.Ints(importantVersions)
for _, nn := range importantVersions {
r = append(r, versionsInfo{p.Timestamps[nn], nn})
}
2016-02-11 17:26:02 +03:00
return r, totalTime
2016-02-10 02:38:25 +03:00
}
2016-02-10 01:52:52 +03:00
}
}
}
2016-02-10 02:38:25 +03:00
sort.Ints(importantVersions)
for _, nn := range importantVersions {
r = append(r, versionsInfo{p.Timestamps[nn], nn})
}
2016-02-11 17:26:02 +03:00
return r, totalTime
2016-02-10 02:38:25 +03:00
}
2016-02-10 01:52:52 +03:00
2016-02-10 17:13:21 +03:00
func rebuildTextsToDiffN(p WikiData, n int) string {
2016-02-10 02:38:25 +03:00
dmp := diffmatchpatch.New()
lastText := ""
for i, diff := range p.Diffs {
seq1, _ := dmp.DiffFromDelta(lastText, diff)
2016-02-11 01:03:00 +03:00
textsLinemode := diffRebuildtexts(seq1)
rebuilt := textsLinemode[len(textsLinemode)-1]
2016-02-10 02:38:25 +03:00
if i == n {
return rebuilt
}
lastText = rebuilt
}
return "ERROR"
}
2016-02-11 05:22:21 +03:00
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
)
2016-02-11 17:26:02 +03:00
// RandStringBytesMaskImprSrc prints a random string
2016-02-11 05:22:21 +03:00
func RandStringBytesMaskImprSrc(n int) string {
b := make([]byte, 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) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}