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

153 lines
3.5 KiB
Go
Raw Normal View History

2016-02-06 16:40:53 +03:00
package main
import (
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"
"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"))
adjectives = strings.Split(string(adjectivesText), ",")
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 = ""
for {
animal := randomAnimal()
adjective := randomAdjective()
if animal[0] == adjective[0] {
combo = adjective + animal
break
}
}
return
}
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-10 17:13:21 +03:00
func getImportantVersions(p WikiData) []versionsInfo {
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)
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)
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})
}
return r
}
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})
}
return r
}
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"
}