Fix source errors
* Dilbert Image * GoComics List
This commit is contained in:
parent
5b76117cf8
commit
5096f3522e
1
.gitignore
vendored
1
.gitignore
vendored
@ -13,3 +13,4 @@ rssfeed.xml
|
||||
|
||||
# vim-restconsole
|
||||
console.rest
|
||||
vrc_cookie_jar
|
||||
|
@ -31,12 +31,12 @@ func handleApiComicsCall(w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
|
||||
default:
|
||||
http.Error(w, "You did a bad", 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
|
||||
default:
|
||||
http.Error(w, "You did a bad", 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -45,7 +45,7 @@ func handleApiListComics(w http.ResponseWriter) {
|
||||
var res []byte
|
||||
var err error
|
||||
if res, err = json.Marshal(m.Comics); err != nil {
|
||||
http.Error(w, "I did a bad", 500)
|
||||
serverError(w)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, string(res))
|
||||
@ -62,7 +62,7 @@ func handleApiSearchComic(nm string, w http.ResponseWriter) {
|
||||
var res []byte
|
||||
var err error
|
||||
if res, err = json.Marshal(cs); err != nil {
|
||||
http.Error(w, "I did a bad", 500)
|
||||
serverError(w)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, string(res))
|
||||
@ -73,17 +73,17 @@ func handleApiShowComic(cid string, w http.ResponseWriter) {
|
||||
var c *Comic
|
||||
pts := strings.Split(cid, ";")
|
||||
if len(pts) != 2 {
|
||||
http.Error(w, "You did a bad", 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
if c, err = m.GetComic(pts[0], pts[1]); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
|
||||
var res []byte
|
||||
if res, err = json.Marshal(c); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, string(res))
|
||||
|
@ -22,13 +22,15 @@ func handleApiUsersCall(w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
switch fn {
|
||||
case "byname":
|
||||
handleApiShowUserByName(uid, w)
|
||||
default:
|
||||
handleApiShowUser(uid, w)
|
||||
}
|
||||
|
||||
case "POST", "PUT":
|
||||
if !uidok {
|
||||
http.Error(w, "You did a bad", 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
if !fnok {
|
||||
@ -36,7 +38,7 @@ func handleApiUsersCall(w http.ResponseWriter, req *http.Request) {
|
||||
var u *User
|
||||
var err error
|
||||
if u, err = m.GetUserByName(uid); err == nil {
|
||||
http.Error(w, "You did a bad", 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
u = NewUser(uid)
|
||||
@ -49,18 +51,18 @@ func handleApiUsersCall(w http.ResponseWriter, req *http.Request) {
|
||||
case "slugs":
|
||||
slug, slugok := vars["slug"]
|
||||
if !slugok {
|
||||
http.Error(w, "You did a bad", 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
handleApiSubUser(uid, slug, w)
|
||||
|
||||
default:
|
||||
http.Error(w, "You did a bad", 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
|
||||
default:
|
||||
http.Error(w, "You did a bad", 400)
|
||||
userError(w)
|
||||
return
|
||||
|
||||
}
|
||||
@ -70,7 +72,7 @@ func handleApiListUsers(w http.ResponseWriter) {
|
||||
var res []byte
|
||||
var err error
|
||||
if res, err = json.Marshal(m.Users); err != nil {
|
||||
http.Error(w, "I did a bad", 500)
|
||||
serverError(w)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, string(res))
|
||||
@ -80,13 +82,30 @@ func handleApiShowUser(uid string, w http.ResponseWriter) {
|
||||
var err error
|
||||
var u *User
|
||||
if u, err = m.GetUser(uid); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
|
||||
var res []byte
|
||||
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
|
||||
}
|
||||
fmt.Fprint(w, string(res))
|
||||
@ -98,23 +117,23 @@ func handleApiSubUser(uid string, slug string, w http.ResponseWriter) {
|
||||
var u *User
|
||||
var err error
|
||||
if u, err = m.GetUser(uid); err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
pts := strings.Split(slug, ";")
|
||||
if len(pts) != 2 {
|
||||
http.Error(w, err.Error(), 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
_, err = m.GetComic(pts[0], pts[1])
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
u.SubSlugs = append(u.SubSlugs, slug)
|
||||
err = m.SaveUser(u)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 400)
|
||||
userError(w)
|
||||
return
|
||||
}
|
||||
m.LoadUsers()
|
||||
|
@ -64,5 +64,5 @@ func getDilbertFeedDesc(date time.Time) (string, error) {
|
||||
if !exists {
|
||||
return "", errors.New("Couldn't find image source")
|
||||
}
|
||||
return "<img src=\"" + src + "\" />", nil
|
||||
return "<img src=\"https:" + src + "\" />", nil
|
||||
}
|
||||
|
@ -15,19 +15,22 @@ func downloadGoComicsList() []Comic {
|
||||
lstUrl := "http://www.gocomics.com/comics/a-to-z"
|
||||
res, err := http.Get(lstUrl)
|
||||
if err != nil {
|
||||
fmt.Println("Error getting gocomics list:", err.Error())
|
||||
return ret
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != 200 {
|
||||
fmt.Println("Error getting gocomics list:", res.StatusCode)
|
||||
return ret
|
||||
}
|
||||
|
||||
// Load the HTML document
|
||||
doc, err := goquery.NewDocumentFromReader(res.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error loading gocomics list:", err.Error())
|
||||
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
|
||||
slug, exists := s.Attr("href")
|
||||
if exists {
|
||||
@ -35,9 +38,15 @@ func downloadGoComicsList() []Comic {
|
||||
if len(pts) > 2 {
|
||||
slug = pts[1]
|
||||
}
|
||||
name := s.Find("h4.media-heading").Text()
|
||||
author := s.Find("h6.media-subheading").Text()
|
||||
inner := strings.TrimSpace(s.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.Replace(author, "\u0026", "&", -1)
|
||||
ret = append(ret, *NewComic(slug, name, author, "gocomics"))
|
||||
}
|
||||
})
|
||||
|
11
helpers.go
11
helpers.go
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@ -67,7 +68,7 @@ func buildRssFeed(uid string) (string, error) {
|
||||
"<rss version=\"2.0\">",
|
||||
" <channel>",
|
||||
" <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>",
|
||||
" <language>en-us</language>",
|
||||
" <lastBuildDate>" + time.Now().Format(time.RFC1123) + "</lastBuildDate>",
|
||||
@ -100,3 +101,11 @@ func addStringIfUnique(st string, sl []string) []string {
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
5
main.go
5
main.go
@ -64,8 +64,7 @@ func main() {
|
||||
} else if len(os.Args) > 1 {
|
||||
switch os.Args[1] {
|
||||
case "--test":
|
||||
//d, _ := getXKCDFeedDesc(time.Now())
|
||||
fmt.Println(buildRssFeed("09af5fda-43dc-416e-93ad-cc050e0c098a"))
|
||||
fmt.Println(buildRssFeed("30f14e57-6500-443c-8c77-f352788eacb0"))
|
||||
done()
|
||||
case "--list-comics":
|
||||
comics := m.GetAllComics()
|
||||
@ -115,7 +114,7 @@ func main() {
|
||||
http.Handle("/", r)
|
||||
chain := alice.New(loggingHandler).Then(r)
|
||||
|
||||
// Save changes to the DB every 5 minutes
|
||||
// Refresh the DB at 2 AM
|
||||
go func() {
|
||||
for {
|
||||
if m.Site.LastSave.IsZero() || (time.Now().Day() != m.Site.LastSave.Day() && time.Now().Hour() == 2) {
|
||||
|
7
model.go
7
model.go
@ -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)
|
||||
}
|
||||
|
@ -91,6 +91,14 @@ func (m *model) SaveComic(c *Comic) error {
|
||||
if err = m.bolt.SetTimestamp(bkt, "lastupdate", c.LastUpdate); err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pborman/uuid"
|
||||
@ -69,6 +70,7 @@ func (m *model) GetUser(uid string) (*User, error) {
|
||||
bkt := []string{"users", uid}
|
||||
ret.Uuid = uid
|
||||
if ret.Username, err = m.bolt.GetValue(bkt, "username"); err != nil {
|
||||
fmt.Println("Error getting username value:", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
var subs string
|
||||
|
Loading…
Reference in New Issue
Block a user