Fix source errors

* Dilbert Image
* GoComics List
This commit is contained in:
Brian Buller 2018-12-27 08:37:44 -06:00
parent 5b76117cf8
commit 5096f3522e
10 changed files with 81 additions and 27 deletions

1
.gitignore vendored
View File

@ -13,3 +13,4 @@ rssfeed.xml
# vim-restconsole # vim-restconsole
console.rest console.rest
vrc_cookie_jar

View File

@ -31,12 +31,12 @@ func handleApiComicsCall(w http.ResponseWriter, req *http.Request) {
return return
default: default:
http.Error(w, "You did a bad", 400) userError(w)
return return
} }
default: default:
http.Error(w, "You did a bad", 400) userError(w)
return return
} }
} }
@ -45,7 +45,7 @@ func handleApiListComics(w http.ResponseWriter) {
var res []byte var res []byte
var err error var err error
if res, err = json.Marshal(m.Comics); err != nil { if res, err = json.Marshal(m.Comics); err != nil {
http.Error(w, "I did a bad", 500) serverError(w)
return return
} }
fmt.Fprint(w, string(res)) fmt.Fprint(w, string(res))
@ -62,7 +62,7 @@ func handleApiSearchComic(nm string, w http.ResponseWriter) {
var res []byte var res []byte
var err error var err error
if res, err = json.Marshal(cs); err != nil { if res, err = json.Marshal(cs); err != nil {
http.Error(w, "I did a bad", 500) serverError(w)
return return
} }
fmt.Fprint(w, string(res)) fmt.Fprint(w, string(res))
@ -73,17 +73,17 @@ func handleApiShowComic(cid string, w http.ResponseWriter) {
var c *Comic var c *Comic
pts := strings.Split(cid, ";") pts := strings.Split(cid, ";")
if len(pts) != 2 { if len(pts) != 2 {
http.Error(w, "You did a bad", 400) userError(w)
return return
} }
if c, err = m.GetComic(pts[0], pts[1]); err != nil { if c, err = m.GetComic(pts[0], pts[1]); err != nil {
http.Error(w, err.Error(), 400) userError(w)
return return
} }
var res []byte var res []byte
if res, err = json.Marshal(c); err != nil { if res, err = json.Marshal(c); err != nil {
http.Error(w, err.Error(), 400) userError(w)
return return
} }
fmt.Fprint(w, string(res)) fmt.Fprint(w, string(res))

View File

@ -22,13 +22,15 @@ func handleApiUsersCall(w http.ResponseWriter, req *http.Request) {
return return
} }
switch fn { switch fn {
case "byname":
handleApiShowUserByName(uid, w)
default: default:
handleApiShowUser(uid, w) handleApiShowUser(uid, w)
} }
case "POST", "PUT": case "POST", "PUT":
if !uidok { if !uidok {
http.Error(w, "You did a bad", 400) userError(w)
return return
} }
if !fnok { if !fnok {
@ -36,7 +38,7 @@ func handleApiUsersCall(w http.ResponseWriter, req *http.Request) {
var u *User var u *User
var err error var err error
if u, err = m.GetUserByName(uid); err == nil { if u, err = m.GetUserByName(uid); err == nil {
http.Error(w, "You did a bad", 400) userError(w)
return return
} }
u = NewUser(uid) u = NewUser(uid)
@ -49,18 +51,18 @@ func handleApiUsersCall(w http.ResponseWriter, req *http.Request) {
case "slugs": case "slugs":
slug, slugok := vars["slug"] slug, slugok := vars["slug"]
if !slugok { if !slugok {
http.Error(w, "You did a bad", 400) userError(w)
return return
} }
handleApiSubUser(uid, slug, w) handleApiSubUser(uid, slug, w)
default: default:
http.Error(w, "You did a bad", 400) userError(w)
return return
} }
default: default:
http.Error(w, "You did a bad", 400) userError(w)
return return
} }
@ -70,7 +72,7 @@ func handleApiListUsers(w http.ResponseWriter) {
var res []byte var res []byte
var err error var err error
if res, err = json.Marshal(m.Users); err != nil { if res, err = json.Marshal(m.Users); err != nil {
http.Error(w, "I did a bad", 500) serverError(w)
return return
} }
fmt.Fprint(w, string(res)) fmt.Fprint(w, string(res))
@ -80,13 +82,30 @@ func handleApiShowUser(uid string, w http.ResponseWriter) {
var err error var err error
var u *User var u *User
if u, err = m.GetUser(uid); err != nil { if u, err = m.GetUser(uid); err != nil {
http.Error(w, err.Error(), 400) userError(w)
return return
} }
var res []byte var res []byte
if res, err = json.Marshal(u); err != nil { if res, err = json.Marshal(u); err != nil {
http.Error(w, err.Error(), 400) userError(w)
return
}
fmt.Fprint(w, string(res))
return
}
func handleApiShowUserByName(uname string, w http.ResponseWriter) {
var err error
var u *User
if u, err = m.GetUserByName(uname); err != nil {
userError(w)
return
}
var res []byte
if res, err = json.Marshal(u); err != nil {
userError(w)
return return
} }
fmt.Fprint(w, string(res)) fmt.Fprint(w, string(res))
@ -98,23 +117,23 @@ func handleApiSubUser(uid string, slug string, w http.ResponseWriter) {
var u *User var u *User
var err error var err error
if u, err = m.GetUser(uid); err != nil { if u, err = m.GetUser(uid); err != nil {
http.Error(w, err.Error(), 400) userError(w)
return return
} }
pts := strings.Split(slug, ";") pts := strings.Split(slug, ";")
if len(pts) != 2 { if len(pts) != 2 {
http.Error(w, err.Error(), 400) userError(w)
return return
} }
_, err = m.GetComic(pts[0], pts[1]) _, err = m.GetComic(pts[0], pts[1])
if err != nil { if err != nil {
http.Error(w, err.Error(), 400) userError(w)
return return
} }
u.SubSlugs = append(u.SubSlugs, slug) u.SubSlugs = append(u.SubSlugs, slug)
err = m.SaveUser(u) err = m.SaveUser(u)
if err != nil { if err != nil {
http.Error(w, err.Error(), 400) userError(w)
return return
} }
m.LoadUsers() m.LoadUsers()

View File

@ -64,5 +64,5 @@ func getDilbertFeedDesc(date time.Time) (string, error) {
if !exists { if !exists {
return "", errors.New("Couldn't find image source") return "", errors.New("Couldn't find image source")
} }
return "<img src=\"" + src + "\" />", nil return "<img src=\"https:" + src + "\" />", nil
} }

View File

@ -15,19 +15,22 @@ func downloadGoComicsList() []Comic {
lstUrl := "http://www.gocomics.com/comics/a-to-z" lstUrl := "http://www.gocomics.com/comics/a-to-z"
res, err := http.Get(lstUrl) res, err := http.Get(lstUrl)
if err != nil { if err != nil {
fmt.Println("Error getting gocomics list:", err.Error())
return ret return ret
} }
defer res.Body.Close() defer res.Body.Close()
if res.StatusCode != 200 { if res.StatusCode != 200 {
fmt.Println("Error getting gocomics list:", res.StatusCode)
return ret return ret
} }
// Load the HTML document // Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body) doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil { if err != nil {
fmt.Println("Error loading gocomics list:", err.Error())
return ret return ret
} }
doc.Find("a.amu-media-item-link").Each(func(i int, s *goquery.Selection) { doc.Find(".gc-blended-link").Each(func(i int, s *goquery.Selection) {
// For each item found, get the band and title // For each item found, get the band and title
slug, exists := s.Attr("href") slug, exists := s.Attr("href")
if exists { if exists {
@ -35,9 +38,15 @@ func downloadGoComicsList() []Comic {
if len(pts) > 2 { if len(pts) > 2 {
slug = pts[1] slug = pts[1]
} }
name := s.Find("h4.media-heading").Text() inner := strings.TrimSpace(s.Text())
author := s.Find("h6.media-subheading").Text() pts = strings.Split(inner, "\n")
name := strings.TrimSpace(pts[0])
var author string
if len(pts) > 1 {
author = strings.TrimSpace(pts[1])
}
author = strings.TrimPrefix(author, "By ") author = strings.TrimPrefix(author, "By ")
author = strings.Replace(author, "\u0026", "&", -1)
ret = append(ret, *NewComic(slug, name, author, "gocomics")) ret = append(ret, *NewComic(slug, name, author, "gocomics"))
} }
}) })

View File

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"net/http"
"strings" "strings"
"time" "time"
) )
@ -67,7 +68,7 @@ func buildRssFeed(uid string) (string, error) {
"<rss version=\"2.0\">", "<rss version=\"2.0\">",
" <channel>", " <channel>",
" <title>BCW Comic Feed</title>", " <title>BCW Comic Feed</title>",
" <link>http://comics.bullercodeworks.com/edit/" + uid + "</link>", " <link>http://ribbit.bullercodeworks.com/edit/" + uid + "</link>",
" <description>Comic feed for " + usr.Username + "</description>", " <description>Comic feed for " + usr.Username + "</description>",
" <language>en-us</language>", " <language>en-us</language>",
" <lastBuildDate>" + time.Now().Format(time.RFC1123) + "</lastBuildDate>", " <lastBuildDate>" + time.Now().Format(time.RFC1123) + "</lastBuildDate>",
@ -100,3 +101,11 @@ func addStringIfUnique(st string, sl []string) []string {
} }
return append(sl, st) return append(sl, st)
} }
func userError(w http.ResponseWriter) {
http.Error(w, "You did a bad", 400)
}
func serverError(w http.ResponseWriter) {
http.Error(w, "I did a bad", 500)
}

View File

@ -64,8 +64,7 @@ func main() {
} else if len(os.Args) > 1 { } else if len(os.Args) > 1 {
switch os.Args[1] { switch os.Args[1] {
case "--test": case "--test":
//d, _ := getXKCDFeedDesc(time.Now()) fmt.Println(buildRssFeed("30f14e57-6500-443c-8c77-f352788eacb0"))
fmt.Println(buildRssFeed("09af5fda-43dc-416e-93ad-cc050e0c098a"))
done() done()
case "--list-comics": case "--list-comics":
comics := m.GetAllComics() comics := m.GetAllComics()
@ -115,7 +114,7 @@ func main() {
http.Handle("/", r) http.Handle("/", r)
chain := alice.New(loggingHandler).Then(r) chain := alice.New(loggingHandler).Then(r)
// Save changes to the DB every 5 minutes // Refresh the DB at 2 AM
go func() { go func() {
for { for {
if m.Site.LastSave.IsZero() || (time.Now().Day() != m.Site.LastSave.Day() && time.Now().Hour() == 2) { if m.Site.LastSave.IsZero() || (time.Now().Day() != m.Site.LastSave.Day() && time.Now().Hour() == 2) {

View File

@ -98,3 +98,10 @@ func (m *model) UpdateAllUserFeeds() {
} }
} }
} }
type Source interface {
downloadList() []Comic
getRssItem(slug string) (string, error)
getUrl(slug string, dt time.Time) (string, error)
getDescription(slug string, dt time.Time) (string, error)
}

View File

@ -91,6 +91,14 @@ func (m *model) SaveComic(c *Comic) error {
if err = m.bolt.SetTimestamp(bkt, "lastupdate", c.LastUpdate); err != nil { if err = m.bolt.SetTimestamp(bkt, "lastupdate", c.LastUpdate); err != nil {
return err return err
} }
// Add it to the cached list
for i, v := range m.Comics {
if v.Source == c.Source && v.Slug == c.Slug {
m.Comics[i] = *c
return nil
}
}
m.Comics = append(m.Comics, *c)
return nil return nil
} }

View File

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"fmt"
"strings" "strings"
"github.com/pborman/uuid" "github.com/pborman/uuid"
@ -69,6 +70,7 @@ func (m *model) GetUser(uid string) (*User, error) {
bkt := []string{"users", uid} bkt := []string{"users", uid}
ret.Uuid = uid ret.Uuid = uid
if ret.Username, err = m.bolt.GetValue(bkt, "username"); err != nil { if ret.Username, err = m.bolt.GetValue(bkt, "username"); err != nil {
fmt.Println("Error getting username value:", err.Error())
return nil, err return nil, err
} }
var subs string var subs string