diff --git a/utils.go b/utils.go index 7e600def..0fdd17b4 100644 --- a/utils.go +++ b/utils.go @@ -27,6 +27,7 @@ import ( "math/rand" "net/http" "net/url" + "regexp" "strings" "time" "unicode/utf8" @@ -397,6 +398,12 @@ func ParseFeed(c appengine.Context, contentType, origUrl, fetchUrl string, body } if enc != encoding.Nop { cr = nilCharsetReader + preview := string(body[:128]) // + r := regexp.MustCompile(`<\?xml.*encoding="(.*)".*\?>`) + rr := r.FindStringSubmatch(preview) + if len(rr) > 1 { + enc, _ = charset.Lookup(rr[1]) + } body, err = ioutil.ReadAll(transform.NewReader(bytes.NewReader(body), enc.NewDecoder())) if err != nil { return nil, nil, err