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

Fix formatting

This commit is contained in:
Matt Baer 2015-02-23 19:06:28 -05:00
parent ff37899e2b
commit 98cfe89b05
3 changed files with 78 additions and 73 deletions

View File

@ -1,11 +1,11 @@
package store
import (
"os"
"fmt"
"bufio"
"strings"
"fmt"
"io/ioutil"
"os"
"strings"
)
type Row struct {
@ -41,10 +41,12 @@ func ReadRows(path, delimiter string) *[]Row {
}
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 {
fmt.Println(err)
}
// TODO: check for Close() errors
// https://github.com/ncw/swift/blob/master/swift.go#L170
defer f.Close()
_, err = f.Write(data)
@ -54,7 +56,7 @@ func WriteData(path string, data []byte) {
}
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 {
fmt.Println(err)
}

View File

@ -1,30 +1,30 @@
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"fmt"
"time"
"flag"
"sort"
"bufio"
"strconv"
"strings"
"io/ioutil"
"text/template"
"time"
"github.com/thebaer/tildes/store"
)
var (
scoresPath = "/home/krowbar/Code/irc/tildescores.txt"
jackpotPath = "/home/krowbar/Code/irc/tildejackpot.txt"
scoresPath = "/home/krowbar/Code/irc/tildescores.txt"
jackpotPath = "/home/krowbar/Code/irc/tildejackpot.txt"
addictionData = "/home/karlen/bin/tilderoyale"
)
const (
scoreDeltasPath = "/home/bear/scoredeltas.txt"
deltaDelimiter = "+++"
deltaDelimiter = "+++"
)
func main() {
@ -41,7 +41,7 @@ func main() {
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, "&^%")
updatesData := store.ReadRows(scoreDeltasPath, deltaDelimiter)
@ -54,21 +54,24 @@ func main() {
type table struct {
Headers []string
Rows []store.Row
Rows []store.Row
}
type By func(r1, r2 *store.Row) bool
func (by By) Sort(rows []store.Row) {
rs := &rowSorter {
rs := &rowSorter{
rows: rows,
by: by,
by: by,
}
sort.Sort(rs)
}
type rowSorter struct {
rows []store.Row
by func(r1, r2 *store.Row) bool
by func(r1, r2 *store.Row) bool
}
func (r *rowSorter) Len() int {
return len(r.rows)
}
@ -117,20 +120,20 @@ func niceTime(sec int) string {
if sec < 60 {
return fmt.Sprintf("%dsec", sec)
} 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 {
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 {
LastUpdate int
LastScore int
LastUpdate int
LastScore int
LastIncrement int
Times int
ScoreOffset int
Addiction int
Times int
ScoreOffset int
Addiction int
}
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])
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
@ -175,7 +178,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
u, exists := users[uname]
if !exists {
u = LastScore{ ScoreOffset: 0 }
u = LastScore{ScoreOffset: 0}
}
u.ScoreOffset = 0
u.Times = asks
@ -197,7 +200,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
// Fill in any missing users
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
}
@ -221,7 +224,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
var avgStr string
if u.Times > 0 {
avg := float64(score - u.ScoreOffset) / float64(u.Times)
avg := float64(score-u.ScoreOffset) / float64(u.Times)
avgStr = trimTrailingZeros(avg)
} else {
avgStr = "-"
@ -241,7 +244,7 @@ func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
}
// 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 {
fmt.Println(err)
}
@ -285,11 +288,11 @@ func getFile(path string) string {
}
type Page struct {
Title string
Table table
Updated string
Title string
Table table
Updated string
UpdatedForHumans string
Jackpot int
Jackpot int
}
func add(x, y int) int {
@ -306,7 +309,7 @@ func generate(title, jackpot string, table *table, outputFile string) {
defer f.Close()
funcMap := template.FuncMap {
funcMap := template.FuncMap{
"add": add,
}

View File

@ -1,21 +1,21 @@
package main
import (
"encoding/json"
"strings"
"bufio"
"os"
"io/ioutil"
"os/exec"
"fmt"
"regexp"
"net/http"
"flag"
"time"
"text/template"
"crypto/hmac"
"crypto/sha256"
"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/tildes/store"
@ -56,22 +56,22 @@ func main() {
}
type user struct {
Name string `json:"name"`
IP string `json:"ip"`
Region string `json:"region"`
Country string `json:"country"`
CurrentTime string `json:"current_time"`
Latitude float64 `json:"lat"`
Longitude float64 `json:"lng"`
Public bool
Anonymous bool
Name string `json:"name"`
IP string `json:"ip"`
Region string `json:"region"`
Country string `json:"country"`
CurrentTime string `json:"current_time"`
Latitude float64 `json:"lat"`
Longitude float64 `json:"lng"`
Public bool
Anonymous bool
}
type publicUser struct {
Name string `json:"name"`
Region string `json:"region"`
Country string `json:"country"`
Latitude float64 `json:"lat"`
Name string `json:"name"`
Region string `json:"region"`
Country string `json:"country"`
Latitude float64 `json:"lat"`
Longitude float64 `json:"lng"`
}
@ -185,10 +185,10 @@ func getGeo(u *user) {
}
func computeHmac256(message string) string {
key := []byte(hashSecret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
key := []byte(hashSecret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func getFuzzyCoords(u *user, apiKey string) {
@ -254,8 +254,8 @@ func prettyLocation(region, country string) string {
}
type page struct {
Users []user
Updated string
Users []user
Updated string
UpdatedForHumans string
}
@ -269,7 +269,7 @@ func generate(users []user, outputFile string) {
defer f.Close()
funcMap := template.FuncMap {
funcMap := template.FuncMap{
"Location": prettyLocation,
}