Fix encoding issue with server titles.

This commit is contained in:
A. Svensson 2016-02-20 19:55:23 +01:00
parent 0fbc127ec6
commit 9723c63912
3 changed files with 20 additions and 10 deletions

View File

@ -67,8 +67,6 @@ Todo
- Update static files to newer versions. - Update static files to newer versions.
- Use unicode for server names in the templates.
- Fix and clean up the tooltips in the server details template. - Fix and clean up the tooltips in the server details template.
- Use the same format for the verbose timestamps. - Use the same format for the verbose timestamps.

View File

@ -2,10 +2,15 @@ package ss13
import ( import (
"fmt" "fmt"
"io"
"net/http"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time"
"golang.org/x/text/encoding/charmap"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
) )
@ -20,21 +25,26 @@ func ScrapePage() []*RawServerData {
} }
func download_data() *goquery.Document { func download_data() *goquery.Document {
var ( var r io.Reader
doc *goquery.Document
err error
)
if IsDebugging() { if IsDebugging() {
fmt.Println("Scraper data source: ./dump.html") fmt.Println("Scraper data source: ./dump.html")
f, err := os.Open("./tmp/dump.html") f, err := os.Open("./tmp/dump.html")
check_error(err) check_error(err)
defer f.Close() defer f.Close()
doc, err = goquery.NewDocumentFromReader(f) r = charmap.Windows1252.NewDecoder().Reader(f)
check_error(err)
} else { } else {
doc, err = goquery.NewDocument("http://www.byond.com/games/exadv1/spacestation13") client := &http.Client{
check_error(err) Timeout: time.Duration(1) * time.Minute,
}
resp, e := client.Get("http://www.byond.com/games/exadv1/spacestation13")
check_error(e)
defer resp.Body.Close()
// Yep, Byond serve's it's pages with Windows-1252 encoding...
r = charmap.Windows1252.NewDecoder().Reader(resp.Body)
} }
doc, e := goquery.NewDocumentFromReader(r)
check_error(e)
return doc return doc
} }
@ -57,6 +67,7 @@ func parse_server_data(raw *goquery.Selection) *RawServerData {
t = t.Find("b").First() t = t.Find("b").First()
} }
title := strings.TrimSpace(t.Text()) title := strings.TrimSpace(t.Text())
//title = toUtf8([]byte(title))
title = strings.Replace(title, "\n", "", -1) title = strings.Replace(title, "\n", "", -1)
if len(title) < 1 { if len(title) < 1 {
// Yes, someone has made a public server without a server name at least once // Yes, someone has made a public server without a server name at least once

View File

@ -11,6 +11,7 @@ import (
func (i *Instance) Init() { func (i *Instance) Init() {
InitSchema(i.DB) InitSchema(i.DB)
SetDebug(i.Debug) // TODO: get rid of this stupid debug thing
} }
func (i *Instance) Serve(addr string) error { func (i *Instance) Serve(addr string) error {