mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
bab1a7601b
Former-commit-id: 3476ccae25481d014b4911e38fc6daeca311cc93 [formerly 8a4604fb2d666c861b7c2e4bba9683fb373317af] [formerly 96a5897a881a4886a93bf69dffb1c2400033fa70 [formerly 1f327dbf2d
]]
Former-commit-id: 4afcc7a63445a30744bc95600b6102f4ef069b85 [formerly f975a9c6127f93624dd4b34a781a1b4457f4a072]
Former-commit-id: 6cad6d35666351f9b3dd7190adb30cc0dd2b1bb0
153 lines
3.5 KiB
Go
153 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"log"
|
|
"math/rand"
|
|
"path"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/sergi/go-diff/diffmatchpatch"
|
|
)
|
|
|
|
var animals []string
|
|
var adjectives []string
|
|
var robotsTxt string
|
|
var aboutPageText string
|
|
|
|
type versionsInfo struct {
|
|
VersionDate string
|
|
VersionNum int
|
|
}
|
|
|
|
func init() {
|
|
rand.Seed(time.Now().Unix())
|
|
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)
|
|
}
|
|
|
|
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"
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func timeTrack(start time.Time, name string) {
|
|
elapsed := time.Since(start)
|
|
log.Printf("%s took %s", name, elapsed)
|
|
}
|
|
|
|
func getImportantVersions(p WikiData) []versionsInfo {
|
|
// defer timeTrack(time.Now(), "getImportantVersions")
|
|
m := map[int]int{}
|
|
lastTime := time.Now().AddDate(0, -1, 0)
|
|
for i := range p.Diffs {
|
|
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
|
|
}
|
|
|
|
// Sort in order of decreasing diff times
|
|
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)
|
|
}
|
|
sort.Sort(sort.Reverse(sort.IntSlice(a)))
|
|
|
|
// Get the top 4 biggest diff times
|
|
var importantVersions []int
|
|
var r []versionsInfo
|
|
for _, k := range a {
|
|
for _, s := range n[k] {
|
|
if s != 0 && s != len(n) {
|
|
// fmt.Printf("%d, %d\n", s, k)
|
|
importantVersions = append(importantVersions, s)
|
|
if len(importantVersions) > 10 {
|
|
sort.Ints(importantVersions)
|
|
for _, nn := range importantVersions {
|
|
r = append(r, versionsInfo{p.Timestamps[nn], nn})
|
|
}
|
|
return r
|
|
}
|
|
}
|
|
}
|
|
}
|
|
sort.Ints(importantVersions)
|
|
for _, nn := range importantVersions {
|
|
r = append(r, versionsInfo{p.Timestamps[nn], nn})
|
|
}
|
|
return r
|
|
}
|
|
|
|
func rebuildTextsToDiffN(p WikiData, n int) string {
|
|
dmp := diffmatchpatch.New()
|
|
lastText := ""
|
|
for i, diff := range p.Diffs {
|
|
seq1, _ := dmp.DiffFromDelta(lastText, diff)
|
|
textsLinemode := diffRebuildtexts(seq1)
|
|
rebuilt := textsLinemode[len(textsLinemode)-1]
|
|
if i == n {
|
|
return rebuilt
|
|
}
|
|
lastText = rebuilt
|
|
}
|
|
return "ERROR"
|
|
}
|