156 lines
2.7 KiB
Go
156 lines
2.7 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"html/template"
|
||
|
"math/rand"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const statsPath = "/home/vilmibm/bin/townstats"
|
||
|
|
||
|
// TODO const statsPath = "/town/bin/stats"
|
||
|
|
||
|
type user struct {
|
||
|
Username string `json:"username"` // Username of user
|
||
|
PageTitle string `json:"title"` // Title of user's HTML page, if they have one
|
||
|
DefaultPage bool `json:"default"` // Whether or not user has updated their default index.html
|
||
|
}
|
||
|
|
||
|
type tildeData struct {
|
||
|
Users []*user `json:"users"`
|
||
|
}
|
||
|
|
||
|
func _main() error {
|
||
|
data, err := stats()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
users := []*user{}
|
||
|
|
||
|
for _, u := range data.Users {
|
||
|
if !u.DefaultPage {
|
||
|
users = append(users, u)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
hypertextDocument := getHeader()
|
||
|
|
||
|
rand.Seed(time.Now().UTC().UnixNano())
|
||
|
|
||
|
for i := range rand.Perm(len(users)) {
|
||
|
link, err := renderUserLink(users[i])
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
hypertextDocument += link
|
||
|
}
|
||
|
|
||
|
hypertextDocument += getFooter()
|
||
|
|
||
|
fmt.Println(hypertextDocument)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func renderUserLink(u *user) (string, error) {
|
||
|
title := u.PageTitle
|
||
|
if title == "" {
|
||
|
title = "~" + u.Username
|
||
|
}
|
||
|
pt := rand.Intn(20)
|
||
|
pb := rand.Intn(30)
|
||
|
pl := rand.Intn(25)
|
||
|
pr := rand.Intn(20)
|
||
|
opacity := rand.Intn(50) + 50
|
||
|
h := rand.Intn(5)
|
||
|
t, err := template.New("userlink").Parse(`<h{{.H}} style="padding-top: {{.Pt}}px;padding-left: {{.Pl}}px; padding-right:{{.Pr}}px; padding-bottom:{{.Pb}}px; opacity: {{.Opacity}}%; float:left">
|
||
|
<a href="https://tilde.town/~{{.Username}}">{{.Title}}</a>
|
||
|
</h{{.H}}>`)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
out := bytes.Buffer{}
|
||
|
err = t.Execute(&out, struct {
|
||
|
Username string
|
||
|
Pt int
|
||
|
Pb int
|
||
|
Pl int
|
||
|
Pr int
|
||
|
Opacity int
|
||
|
H int
|
||
|
Title string
|
||
|
}{
|
||
|
Pt: pt, Pb: pb, Pl: pl, Pr: pr,
|
||
|
Opacity: opacity,
|
||
|
H: h,
|
||
|
Username: u.Username, Title: title,
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
return out.String(), nil
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
err := _main()
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "error: %s\n", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func stats() (*tildeData, error) {
|
||
|
sout := bytes.Buffer{}
|
||
|
cmd := exec.Command(statsPath)
|
||
|
cmd.Stdout = &sout
|
||
|
|
||
|
err := cmd.Run()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
data := tildeData{}
|
||
|
|
||
|
err = json.Unmarshal(sout.Bytes(), &data)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &data, nil
|
||
|
}
|
||
|
|
||
|
func getHeader() string {
|
||
|
return `
|
||
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>web pages of tilde town</title>
|
||
|
<meta charset="UTF-8">
|
||
|
<link rel="icon" href="/favicon.ico">
|
||
|
<link rel="stylesheet" href="style.css"
|
||
|
<style>
|
||
|
body {
|
||
|
background-color: black;
|
||
|
}
|
||
|
</style>
|
||
|
</head>
|
||
|
<body>
|
||
|
`
|
||
|
}
|
||
|
|
||
|
func getFooter() string {
|
||
|
return `
|
||
|
</body>
|
||
|
</html>`
|
||
|
}
|