112 lines
2.4 KiB
Go
112 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
SRC_GOCOMICS = "gocomics"
|
|
SRC_DILBERT = "dilbert"
|
|
SRC_XKCD = "xkcd"
|
|
)
|
|
|
|
func downloadComicsList() []Comic {
|
|
var ret []Comic
|
|
ret = append(ret, downloadGoComicsList()...)
|
|
ret = append(ret, downloadDilbertList()...)
|
|
ret = append(ret, downloadXKCDList()...)
|
|
return ret
|
|
}
|
|
|
|
func getRssItem(source, slug string) (string, error) {
|
|
switch source {
|
|
case SRC_GOCOMICS:
|
|
return getGoComicsRssItem(slug)
|
|
case SRC_DILBERT:
|
|
return getDilbertRssItem(slug)
|
|
case SRC_XKCD:
|
|
return getXKCDRssItem(slug)
|
|
}
|
|
return "", errors.New("Invalid source")
|
|
}
|
|
|
|
func getComicUrl(source, slug string, dt time.Time) (string, error) {
|
|
switch source {
|
|
case SRC_GOCOMICS:
|
|
return getGoComicsComicUrl(slug, dt), nil
|
|
case SRC_DILBERT:
|
|
return getDilbertComicUrl(dt), nil
|
|
case SRC_XKCD:
|
|
return getXKCDComicUrl(dt), nil
|
|
}
|
|
return "", errors.New("Invalid source")
|
|
}
|
|
|
|
func getComicDesc(source, slug string, dt time.Time) (string, error) {
|
|
switch source {
|
|
case SRC_GOCOMICS:
|
|
return getGoComicsFeedDesc(slug, dt)
|
|
case SRC_DILBERT:
|
|
return getDilbertFeedDesc(dt)
|
|
case SRC_XKCD:
|
|
return getXKCDFeedDesc(dt)
|
|
}
|
|
return "", errors.New("Unknown Comic Source")
|
|
}
|
|
|
|
func buildRssFeed(uid string) (string, error) {
|
|
var usr *User
|
|
var err error
|
|
if usr, err = m.GetUser(uid); err != nil {
|
|
return "", err
|
|
}
|
|
output := []string{
|
|
"<?xml version=\"1.0\"?>",
|
|
"<rss version=\"2.0\">",
|
|
" <channel>",
|
|
" <title>BCW Comic Feed</title>",
|
|
" <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>",
|
|
" <ttl>40</ttl>",
|
|
}
|
|
|
|
//date := time.Now()
|
|
for _, slug := range usr.SubSlugs {
|
|
pts := strings.Split(slug, ";")
|
|
if len(pts) != 2 {
|
|
continue
|
|
}
|
|
if comic, err := m.GetComic(pts[0], pts[1]); err == nil {
|
|
output = append(output, comic.GetRssItem())
|
|
}
|
|
}
|
|
|
|
output = append(output, []string{
|
|
" </channel>",
|
|
"</rss>",
|
|
}...)
|
|
return strings.Join(output, "\n"), nil
|
|
}
|
|
|
|
func addStringIfUnique(st string, sl []string) []string {
|
|
for i := range sl {
|
|
if sl[i] == st {
|
|
return sl
|
|
}
|
|
}
|
|
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)
|
|
}
|