1
0
mirror of https://github.com/thebaer/tildes.git synced 2018-07-20 07:15:21 +00:00

Merge branch 'master' of github.com:thebaer/tildes

This commit is contained in:
Matt Baer 2015-03-08 13:01:24 -05:00
commit 865d4a1b0a
3 changed files with 78 additions and 73 deletions

View File

@ -1,11 +1,11 @@
package store package store
import ( import (
"os"
"fmt"
"bufio" "bufio"
"strings" "fmt"
"io/ioutil" "io/ioutil"
"os"
"strings"
) )
type Row struct { type Row struct {
@ -24,7 +24,7 @@ func ReadData(path string) []byte {
func ReadRows(path, delimiter string) *[]Row { func ReadRows(path, delimiter string) *[]Row {
f, _ := os.Open(path) f, _ := os.Open(path)
defer f.Close() defer f.Close()
rows := []Row{} rows := []Row{}
@ -41,10 +41,12 @@ func ReadRows(path, delimiter string) *[]Row {
} }
func WriteData(path string, data []byte) { func WriteData(path string, data []byte) {
f, err := os.OpenFile(path, os.O_CREATE | os.O_RDWR | os.O_TRUNC, 0644) f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
// TODO: check for Close() errors
// https://github.com/ncw/swift/blob/master/swift.go#L170
defer f.Close() defer f.Close()
_, err = f.Write(data) _, err = f.Write(data)
@ -54,7 +56,7 @@ func WriteData(path string, data []byte) {
} }
func WriteRows(path string, rows *[]Row, delimeter string) { func WriteRows(path string, rows *[]Row, delimeter string) {
f, err := os.OpenFile(path, os.O_CREATE | os.O_RDWR, 0644) f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }

View File

@ -1,30 +1,30 @@
package main package main
import ( import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"fmt"
"time"
"flag"
"sort" "sort"
"bufio"
"strconv" "strconv"
"strings" "strings"
"io/ioutil"
"text/template" "text/template"
"time"
"github.com/thebaer/tildes/store" "github.com/thebaer/tildes/store"
) )
var ( var (
scoresPath = "/home/krowbar/Code/irc/tildescores.txt" scoresPath = "/home/krowbar/Code/irc/tildescores.txt"
jackpotPath = "/home/krowbar/Code/irc/tildejackpot.txt" jackpotPath = "/home/krowbar/Code/irc/tildejackpot.txt"
addictionData = "/home/karlen/bin/tilderoyale" addictionData = "/home/karlen/bin/tilderoyale"
) )
const ( const (
scoreDeltasPath = "/home/bear/scoredeltas.txt" scoreDeltasPath = "/home/bear/scoredeltas.txt"
deltaDelimiter = "+++" deltaDelimiter = "+++"
) )
func main() { func main() {
@ -41,7 +41,7 @@ func main() {
addictionData = "/home/bear/addicted.sh" addictionData = "/home/bear/addicted.sh"
} }
headers := []string{ "User", "Tildes", "Last Collected", "Addiction", "# Asks", "Avg.", "Last Amt." } headers := []string{"User", "Tildes", "Last Collected", "Addiction", "# Asks", "Avg.", "Last Amt."}
scoresData := store.ReadRows(scoresPath, "&^%") scoresData := store.ReadRows(scoresPath, "&^%")
updatesData := store.ReadRows(scoreDeltasPath, deltaDelimiter) updatesData := store.ReadRows(scoreDeltasPath, deltaDelimiter)
@ -54,21 +54,24 @@ func main() {
type table struct { type table struct {
Headers []string Headers []string
Rows []store.Row Rows []store.Row
} }
type By func(r1, r2 *store.Row) bool type By func(r1, r2 *store.Row) bool
func (by By) Sort(rows []store.Row) { func (by By) Sort(rows []store.Row) {
rs := &rowSorter { rs := &rowSorter{
rows: rows, rows: rows,
by: by, by: by,
} }
sort.Sort(rs) sort.Sort(rs)
} }
type rowSorter struct { type rowSorter struct {
rows []store.Row rows []store.Row
by func(r1, r2 *store.Row) bool by func(r1, r2 *store.Row) bool
} }
func (r *rowSorter) Len() int { func (r *rowSorter) Len() int {
return len(r.rows) return len(r.rows)
} }
@ -117,20 +120,20 @@ func niceTime(sec int) string {
if sec < 60 { if sec < 60 {
return fmt.Sprintf("%dsec", sec) return fmt.Sprintf("%dsec", sec)
} else if sec < 3600 { } else if sec < 3600 {
return fmt.Sprintf("%smin", trimTrailingZerosShort(float64(sec) / 60.0)) return fmt.Sprintf("%smin", trimTrailingZerosShort(float64(sec)/60.0))
} else if sec < 86400 { } else if sec < 86400 {
return fmt.Sprintf("%shr", trimTrailingZerosShort(float64(sec) / 3600.0)) return fmt.Sprintf("%shr", trimTrailingZerosShort(float64(sec)/3600.0))
} }
return fmt.Sprintf("%sdy", trimTrailingZerosShort(float64(sec) / 86400.0)) return fmt.Sprintf("%sdy", trimTrailingZerosShort(float64(sec)/86400.0))
} }
type LastScore struct { type LastScore struct {
LastUpdate int LastUpdate int
LastScore int LastScore int
LastIncrement int LastIncrement int
Times int Times int
ScoreOffset int ScoreOffset int
Addiction int Addiction int
} }
func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row { func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
@ -150,7 +153,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
times, _ := strconv.Atoi(r.Data[4]) times, _ := strconv.Atoi(r.Data[4])
so, _ := strconv.Atoi(r.Data[5]) so, _ := strconv.Atoi(r.Data[5])
users[r.Data[0]] = LastScore{ LastScore: score, LastUpdate: update, LastIncrement: inc, Times: times, ScoreOffset: so } users[r.Data[0]] = LastScore{LastScore: score, LastUpdate: update, LastIncrement: inc, Times: times, ScoreOffset: so}
} }
// Fetch IRC log data // Fetch IRC log data
@ -175,7 +178,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
u, exists := users[uname] u, exists := users[uname]
if !exists { if !exists {
u = LastScore{ ScoreOffset: 0 } u = LastScore{ScoreOffset: 0}
} }
u.ScoreOffset = 0 u.ScoreOffset = 0
u.Times = asks u.Times = asks
@ -197,7 +200,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
// Fill in any missing users // Fill in any missing users
if !exists { if !exists {
u = LastScore{ LastScore: score, LastIncrement: -1, LastUpdate: update, Times: 0, ScoreOffset: score, Addiction: 0 } u = LastScore{LastScore: score, LastIncrement: -1, LastUpdate: update, Times: 0, ScoreOffset: score, Addiction: 0}
users[r.Data[0]] = u users[r.Data[0]] = u
} }
@ -210,7 +213,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
} }
r.Data = append(r.Data, niceTime(u.Addiction)) r.Data = append(r.Data, niceTime(u.Addiction))
var asksStr string var asksStr string
if u.Times > 0 { if u.Times > 0 {
asksStr = strconv.Itoa(u.Times) asksStr = strconv.Itoa(u.Times)
@ -221,7 +224,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
var avgStr string var avgStr string
if u.Times > 0 { if u.Times > 0 {
avg := float64(score - u.ScoreOffset) / float64(u.Times) avg := float64(score-u.ScoreOffset) / float64(u.Times)
avgStr = trimTrailingZeros(avg) avgStr = trimTrailingZeros(avg)
} else { } else {
avgStr = "-" avgStr = "-"
@ -241,7 +244,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
} }
// Write deltas // Write deltas
f, err := os.OpenFile(scoreDeltasPath, os.O_CREATE | os.O_RDWR, 0644) f, err := os.OpenFile(scoreDeltasPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
@ -285,11 +288,11 @@ func getFile(path string) string {
} }
type Page struct { type Page struct {
Title string Title string
Table table Table table
Updated string Updated string
UpdatedForHumans string UpdatedForHumans string
Jackpot int Jackpot int
} }
func add(x, y int) int { func add(x, y int) int {
@ -303,10 +306,10 @@ func generate(title, jackpot string, table *table, outputFile string) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer f.Close() defer f.Close()
funcMap := template.FuncMap { funcMap := template.FuncMap{
"add": add, "add": add,
} }

View File

@ -1,21 +1,21 @@
package main package main
import ( import (
"encoding/json"
"strings"
"bufio" "bufio"
"os"
"io/ioutil"
"os/exec"
"fmt"
"regexp"
"net/http"
"flag"
"time"
"text/template"
"crypto/hmac" "crypto/hmac"
"crypto/sha256" "crypto/sha256"
"encoding/base64" "encoding/base64"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"text/template"
"time"
"github.com/thebaer/geo" "github.com/thebaer/geo"
"github.com/thebaer/tildes/store" "github.com/thebaer/tildes/store"
@ -56,22 +56,22 @@ func main() {
} }
type user struct { type user struct {
Name string `json:"name"` Name string `json:"name"`
IP string `json:"ip"` IP string `json:"ip"`
Region string `json:"region"` Region string `json:"region"`
Country string `json:"country"` Country string `json:"country"`
CurrentTime string `json:"current_time"` CurrentTime string `json:"current_time"`
Latitude float64 `json:"lat"` Latitude float64 `json:"lat"`
Longitude float64 `json:"lng"` Longitude float64 `json:"lng"`
Public bool Public bool
Anonymous bool Anonymous bool
} }
type publicUser struct { type publicUser struct {
Name string `json:"name"` Name string `json:"name"`
Region string `json:"region"` Region string `json:"region"`
Country string `json:"country"` Country string `json:"country"`
Latitude float64 `json:"lat"` Latitude float64 `json:"lat"`
Longitude float64 `json:"lng"` Longitude float64 `json:"lng"`
} }
@ -116,7 +116,7 @@ func who() []user {
for ip, name := range ips { for ip, name := range ips {
users[i] = user{Name: name, IP: ip, Public: true, Anonymous: false} users[i] = user{Name: name, IP: ip, Public: true, Anonymous: false}
// Get user permissions, marking if they're not opted-in with a // Get user permissions, marking if they're not opted-in with a
// `.here` file in their $HOME dir. // `.here` file in their $HOME dir.
if _, err := os.Stat("/home/" + name + "/.here"); os.IsNotExist(err) { if _, err := os.Stat("/home/" + name + "/.here"); os.IsNotExist(err) {
users[i].Public = false users[i].Public = false
@ -185,10 +185,10 @@ func getGeo(u *user) {
} }
func computeHmac256(message string) string { func computeHmac256(message string) string {
key := []byte(hashSecret) key := []byte(hashSecret)
h := hmac.New(sha256.New, key) h := hmac.New(sha256.New, key)
h.Write([]byte(message)) h.Write([]byte(message))
return base64.StdEncoding.EncodeToString(h.Sum(nil)) return base64.StdEncoding.EncodeToString(h.Sum(nil))
} }
func getFuzzyCoords(u *user, apiKey string) { func getFuzzyCoords(u *user, apiKey string) {
@ -254,8 +254,8 @@ func prettyLocation(region, country string) string {
} }
type page struct { type page struct {
Users []user Users []user
Updated string Updated string
UpdatedForHumans string UpdatedForHumans string
} }
@ -266,13 +266,13 @@ func generate(users []user, outputFile string) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer f.Close() defer f.Close()
funcMap := template.FuncMap { funcMap := template.FuncMap{
"Location": prettyLocation, "Location": prettyLocation,
} }
w := bufio.NewWriter(f) w := bufio.NewWriter(f)
template, err := template.New("").Funcs(funcMap).ParseFiles("../templates/where.html") template, err := template.New("").Funcs(funcMap).ParseFiles("../templates/where.html")
if err != nil { if err != nil {