more cleanup
This commit is contained in:
parent
57343f8be2
commit
5f0a27d041
1 changed files with 144 additions and 145 deletions
277
main.go
277
main.go
|
|
@ -10,10 +10,9 @@ import (
|
||||||
"image"
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/png"
|
"image/png"
|
||||||
"iter"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -27,16 +26,24 @@ func main() {
|
||||||
flag.StringVar(&listen, "l", ":7878", "listen addr")
|
flag.StringVar(&listen, "l", ":7878", "listen addr")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
if len(cityList) != len(fileList) {
|
||||||
|
fatal(errors.New("city and file list length differ"))
|
||||||
|
}
|
||||||
|
|
||||||
ic := &ImageCrawler{
|
ic := &ImageCrawler{
|
||||||
url: "https://uvi.bfs.de/Tagesgrafiken",
|
url: "https://uvi.bfs.de/Tagesgrafiken",
|
||||||
c: &http.Client{},
|
c: &http.Client{},
|
||||||
m: make(map[string]*Image),
|
m: make(map[int]*Image),
|
||||||
}
|
}
|
||||||
http.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" {
|
if r.Method == "POST" {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
loc := r.PostForm.Get("loc")
|
loc := r.PostForm.Get("loc")
|
||||||
if _, ok := locations[loc]; ok {
|
_, ok := locFromString(loc)
|
||||||
|
if !ok {
|
||||||
|
http.Redirect(w, r, "/", http.StatusMovedPermanently)
|
||||||
|
return
|
||||||
|
}
|
||||||
c := &http.Cookie{
|
c := &http.Cookie{
|
||||||
Name: "loc",
|
Name: "loc",
|
||||||
Path: "/",
|
Path: "/",
|
||||||
|
|
@ -49,7 +56,6 @@ func main() {
|
||||||
http.Redirect(w, r, "/", http.StatusMovedPermanently)
|
http.Redirect(w, r, "/", http.StatusMovedPermanently)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
w.Write([]byte(locPage))
|
w.Write([]byte(locPage))
|
||||||
})
|
})
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
@ -68,14 +74,25 @@ func main() {
|
||||||
} else {
|
} else {
|
||||||
c = cc[0]
|
c = cc[0]
|
||||||
}
|
}
|
||||||
loc := c.Value
|
num, ok := locFromString(c.Value)
|
||||||
img, mod, err := ic.getImage(loc)
|
if !ok {
|
||||||
|
fmt.Fprintf(w, indexPage,
|
||||||
|
"000000",
|
||||||
|
"Error",
|
||||||
|
"Error",
|
||||||
|
time.Now().Format(time.RFC1123),
|
||||||
|
"Error",
|
||||||
|
version,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
img, mod, err := ic.getImage(num)
|
||||||
mod = mod.Local()
|
mod = mod.Local()
|
||||||
location := selectListReverse[loc]
|
location := cityList[num]
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, indexPage,
|
fmt.Fprintf(w, indexPage,
|
||||||
"000000",
|
"000000",
|
||||||
"Err",
|
"Error",
|
||||||
mod.Format(time.RFC1123),
|
mod.Format(time.RFC1123),
|
||||||
time.Now().Format(time.RFC1123),
|
time.Now().Format(time.RFC1123),
|
||||||
location,
|
location,
|
||||||
|
|
@ -87,7 +104,7 @@ func main() {
|
||||||
if len(vals) == 0 {
|
if len(vals) == 0 {
|
||||||
fmt.Fprintf(w, indexPage,
|
fmt.Fprintf(w, indexPage,
|
||||||
"000000",
|
"000000",
|
||||||
"Err",
|
"Error",
|
||||||
mod.Format(time.RFC1123),
|
mod.Format(time.RFC1123),
|
||||||
time.Now().Format(time.RFC1123),
|
time.Now().Format(time.RFC1123),
|
||||||
location,
|
location,
|
||||||
|
|
@ -111,11 +128,18 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fatal(err error) {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type ImageCrawler struct {
|
type ImageCrawler struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
url string
|
url string
|
||||||
c *http.Client
|
c *http.Client
|
||||||
m map[string]*Image
|
m map[int]*Image
|
||||||
}
|
}
|
||||||
|
|
||||||
type Image struct {
|
type Image struct {
|
||||||
|
|
@ -128,11 +152,8 @@ type Image struct {
|
||||||
lastGet time.Time
|
lastGet time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ic *ImageCrawler) getImage(loc string) (img image.Image, mod time.Time, err error) {
|
func (ic *ImageCrawler) getImage(loc int) (img image.Image, mod time.Time, err error) {
|
||||||
file, ok := locations[loc]
|
file := fileList[loc]
|
||||||
if !ok {
|
|
||||||
return nil, time.Time{}, errors.New("invalid location")
|
|
||||||
}
|
|
||||||
ic.mu.RLock()
|
ic.mu.RLock()
|
||||||
if i, ok := ic.m[loc]; ok {
|
if i, ok := ic.m[loc]; ok {
|
||||||
ic.mu.RUnlock()
|
ic.mu.RUnlock()
|
||||||
|
|
@ -362,134 +383,112 @@ func isStart(col color.Color) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func fatal(err error) {
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultCity is Muenchen
|
// DefaultCity is Muenchen
|
||||||
|
// Array index 24
|
||||||
const DefaultCity = "24"
|
const DefaultCity = "24"
|
||||||
|
|
||||||
var locations = map[string]string{
|
var fileList = []string{
|
||||||
"0": "EEr_Andernach_today.png",
|
"EEr_Andernach_today.png",
|
||||||
"1": "EEr_Berlin_Potsdamer_Platz_today.png",
|
"EEr_Berlin_Potsdamer_Platz_today.png",
|
||||||
"2": "EEr_Bonn_today.png",
|
"EEr_Bonn_today.png",
|
||||||
"3": "EEr_Boesel_B_today.png",
|
"EEr_Boesel_B_today.png",
|
||||||
"4": "EEr_Chieming_today.png",
|
"EEr_Chieming_today.png",
|
||||||
"5": "EEr_Cuxhaven_B_today.png",
|
"EEr_Cuxhaven_B_today.png",
|
||||||
"6": "EEr_Dortmund_today.png",
|
"EEr_Dortmund_today.png",
|
||||||
"7": "EEr_Duderstadt_B_today.png",
|
"EEr_Duderstadt_B_today.png",
|
||||||
"8": "EEr_Eckernfoerde_today.png",
|
"EEr_Eckernfoerde_today.png",
|
||||||
"9": "EEr_Fichtelberg_today.png",
|
"EEr_Fichtelberg_today.png",
|
||||||
"10": "EEr_Friedrichshafen_today.png",
|
"EEr_Friedrichshafen_today.png",
|
||||||
"11": "EEr_Genthin_today.png",
|
"EEr_Genthin_today.png",
|
||||||
"12": "EEr_Giessen-Wettenberg_today.png",
|
"EEr_Giessen-Wettenberg_today.png",
|
||||||
"13": "EEr_Goerlitz_today.png",
|
"EEr_Goerlitz_today.png",
|
||||||
"14": "EEr_Groemitz_today.png",
|
"EEr_Groemitz_today.png",
|
||||||
"15": "EEr_Hamburg_today.png",
|
"EEr_Hamburg_today.png",
|
||||||
"16": "EEr_Hohenpeissenberg_today.png",
|
"EEr_Hohenpeissenberg_today.png",
|
||||||
"17": "EEr_Kassel_today.png",
|
"EEr_Kassel_today.png",
|
||||||
"18": "EEr_Klippeneck_today.png",
|
"EEr_Klippeneck_today.png",
|
||||||
"19": "EEr_Kulmbach_today.png",
|
"EEr_Kulmbach_today.png",
|
||||||
"20": "EEr_Langen_today.png",
|
"EEr_Langen_today.png",
|
||||||
"21": "EEr_Lindenberg_today.png",
|
"EEr_Lindenberg_today.png",
|
||||||
"22": "EEr_Lueneburg_today.png",
|
"EEr_Lueneburg_today.png",
|
||||||
"23": "EEr_Melpitz_today.png",
|
"EEr_Melpitz_today.png",
|
||||||
"24": "EEr_Muenchen_today.png",
|
"EEr_Muenchen_today.png",
|
||||||
"25": "EEr_Norderney_today.png",
|
"EEr_Norderney_today.png",
|
||||||
"26": "EEr_Belm-Osnabrueck_today.png",
|
"EEr_Belm-Osnabrueck_today.png",
|
||||||
"27": "EEr_Salzgitter_today.png",
|
"EEr_Salzgitter_today.png",
|
||||||
"28": "EEr_Sankt Augustin_today.png",
|
"EEr_Sankt Augustin_today.png",
|
||||||
"29": "EEr_Schauinsland_today.png",
|
"EEr_Schauinsland_today.png",
|
||||||
"30": "EEr_Schweinfurt_Wasserlosen_today.png",
|
"EEr_Schweinfurt_Wasserlosen_today.png",
|
||||||
"31": "EEr_Stuttgart_today.png",
|
"EEr_Stuttgart_today.png",
|
||||||
"32": "EEr_Sylt_Tinnum_today.png",
|
"EEr_Sylt_Tinnum_today.png",
|
||||||
"33": "EEr_Tholey_today.png",
|
"EEr_Tholey_today.png",
|
||||||
"34": "EEr_Todendorf_today.png",
|
"EEr_Todendorf_today.png",
|
||||||
"35": "EEr_Waldhof_Falkenstein_today.png",
|
"EEr_Waldhof_Falkenstein_today.png",
|
||||||
"36": "EEr_Waldmuenchen_today.png",
|
"EEr_Waldmuenchen_today.png",
|
||||||
"37": "EEr_Wasserkuppe bei Fulda_today.png",
|
"EEr_Wasserkuppe bei Fulda_today.png",
|
||||||
"38": "EEr_Weissenburg_today.png",
|
"EEr_Weissenburg_today.png",
|
||||||
"39": "EEr_Wurmberg_B_today.png",
|
"EEr_Wurmberg_B_today.png",
|
||||||
"40": "EEr_Zingst_today.png",
|
"EEr_Zingst_today.png",
|
||||||
"41": "EEr_Zirchow_today.png",
|
"EEr_Zirchow_today.png",
|
||||||
"42": "EEr_Schneefernerhaus_today.png",
|
"EEr_Schneefernerhaus_today.png",
|
||||||
}
|
}
|
||||||
|
|
||||||
var selectList = map[string]string{
|
func locFromString(s string) (i int, ok bool) {
|
||||||
"Andernach": "0",
|
i, err := strconv.Atoi(s)
|
||||||
"Berlin": "1",
|
if err != nil {
|
||||||
"Bonn": "2",
|
|
||||||
"Bösel": "3",
|
|
||||||
"Chieming": "4",
|
|
||||||
"Cuxhaven": "5",
|
|
||||||
"Dortmund": "6",
|
|
||||||
"Duderstadt": "7",
|
|
||||||
"Eckernförde": "8",
|
|
||||||
"Fichtelberg": "9",
|
|
||||||
"Friedrichshafen": "10",
|
|
||||||
"Genthin": "11",
|
|
||||||
"Gießen": "12",
|
|
||||||
"Görlitz": "13",
|
|
||||||
"Grömitz": "14",
|
|
||||||
"Hamburg": "15",
|
|
||||||
"Hohenpeißenberg": "16",
|
|
||||||
"Kassel": "17",
|
|
||||||
"Klippeneck": "18",
|
|
||||||
"Kulmbach": "19",
|
|
||||||
"Langen": "20",
|
|
||||||
"Lindenberg": "21",
|
|
||||||
"Lüneburg": "22",
|
|
||||||
"Melpitz": "23",
|
|
||||||
"München": "24",
|
|
||||||
"Norderney": "25",
|
|
||||||
"Osnabrück": "26",
|
|
||||||
"Salzgitter": "27",
|
|
||||||
"Sankt Augustin": "28",
|
|
||||||
"Schauinsland": "29",
|
|
||||||
"Schweinfurt": "30",
|
|
||||||
"Stuttgart": "31",
|
|
||||||
"Sylt (Westerland)": "32",
|
|
||||||
"Tholey": "33",
|
|
||||||
"Todendorf": "34",
|
|
||||||
"Waldhof": "35",
|
|
||||||
"Waldmünchen": "36",
|
|
||||||
"Wasserkuppe": "37",
|
|
||||||
"Weißenburg": "38",
|
|
||||||
"Wurmberg": "39",
|
|
||||||
"Zingst": "40",
|
|
||||||
"Zirchow": "41",
|
|
||||||
"Zugspitze (Schneefernerhaus)": "42",
|
|
||||||
}
|
|
||||||
|
|
||||||
var selectListReverse = reverseMap(selectList)
|
|
||||||
|
|
||||||
func reverseMap[K, V comparable](v map[K]V) map[V]K {
|
|
||||||
m := make(map[V]K)
|
|
||||||
for k, v := range v {
|
|
||||||
m[v] = k
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
||||||
func sortedMap[K cmp.Ordered, V any](m map[K]V) iter.Seq2[K, V] {
|
|
||||||
a := make([]K, len(m))
|
|
||||||
i := 0
|
|
||||||
for k := range m {
|
|
||||||
a[i] = k
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
slices.Sort(a)
|
|
||||||
return func(yield func(K, V) bool) {
|
|
||||||
for _, k := range a {
|
|
||||||
v := m[k]
|
|
||||||
if !yield(k, v) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
return i, validLoc(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validLoc(loc int) bool {
|
||||||
|
return loc >= 0 && loc < len(cityList)
|
||||||
|
}
|
||||||
|
|
||||||
|
var cityList = []string{
|
||||||
|
"Andernach",
|
||||||
|
"Berlin",
|
||||||
|
"Bonn",
|
||||||
|
"Bösel",
|
||||||
|
"Chieming",
|
||||||
|
"Cuxhaven",
|
||||||
|
"Dortmund",
|
||||||
|
"Duderstadt",
|
||||||
|
"Eckernförde",
|
||||||
|
"Fichtelberg",
|
||||||
|
"Friedrichshafen",
|
||||||
|
"Genthin",
|
||||||
|
"Gießen",
|
||||||
|
"Görlitz",
|
||||||
|
"Grömitz",
|
||||||
|
"Hamburg",
|
||||||
|
"Hohenpeißenberg",
|
||||||
|
"Kassel",
|
||||||
|
"Klippeneck",
|
||||||
|
"Kulmbach",
|
||||||
|
"Langen",
|
||||||
|
"Lindenberg",
|
||||||
|
"Lüneburg",
|
||||||
|
"Melpitz",
|
||||||
|
"München",
|
||||||
|
"Norderney",
|
||||||
|
"Osnabrück",
|
||||||
|
"Salzgitter",
|
||||||
|
"Sankt Augustin",
|
||||||
|
"Schauinsland",
|
||||||
|
"Schweinfurt",
|
||||||
|
"Stuttgart",
|
||||||
|
"Sylt (Westerland)",
|
||||||
|
"Tholey",
|
||||||
|
"Todendorf",
|
||||||
|
"Waldhof",
|
||||||
|
"Waldmünchen",
|
||||||
|
"Wasserkuppe",
|
||||||
|
"Weißenburg",
|
||||||
|
"Wurmberg",
|
||||||
|
"Zingst",
|
||||||
|
"Zirchow",
|
||||||
|
"Zugspitze (Schneefernerhaus)",
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateLocPage() string {
|
func generateLocPage() string {
|
||||||
|
|
@ -503,8 +502,8 @@ func generateLocPage() string {
|
||||||
<h5 style="font-size:4em;">Change Location</h5>
|
<h5 style="font-size:4em;">Change Location</h5>
|
||||||
<form action="/set" method="post">
|
<form action="/set" method="post">
|
||||||
<select name="loc" style="font-size:5em;margin-bottom:50px;">`
|
<select name="loc" style="font-size:5em;margin-bottom:50px;">`
|
||||||
for k, v := range sortedMap(selectList) {
|
for i, v := range cityList {
|
||||||
loc += `<option value="` + v + `">` + k + `</option>`
|
loc += `<option value="` + strconv.Itoa(i) + `">` + v + `</option>`
|
||||||
}
|
}
|
||||||
loc += `</select><br><input type="submit" style="font-size:5em;" value="Change"></input></form></center></body></html>`
|
loc += `</select><br><input type="submit" style="font-size:5em;" value="Change"></input></form></center></body></html>`
|
||||||
return loc
|
return loc
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue