69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
)
|
|
|
|
func downloadDilbertList() []Comic {
|
|
var ret []Comic
|
|
ret = append(ret, *NewComic("dilbert", "Dilbert", "Scott Adams", "dilbert"))
|
|
return ret
|
|
}
|
|
|
|
func getDilbertRssItem(slug string) (string, error) {
|
|
desc, err := getDilbertFeedDesc(time.Now())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
comic, err := m.GetComic(SRC_DILBERT, slug)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
desc = "<![CDATA[" + desc + "]]>"
|
|
ret := " <item>\n"
|
|
ret += " <title>" + comic.Name + "</title>\n"
|
|
ret += " <pubDate>" + comic.LastUpdate.Format(time.RFC1123Z) + "</pubDate>\n"
|
|
ret += " <guid>dilbert;" + slug + ";" + comic.LastUpdate.Format(time.RFC1123Z) + "</guid>\n"
|
|
ret += " <link>" + getDilbertComicUrl(time.Now()) + "</link>\n"
|
|
ret += " <description>" + desc + "</description>\n"
|
|
ret += " </item>\n"
|
|
return ret, nil
|
|
}
|
|
|
|
func getDilbertComicUrl(date time.Time) string {
|
|
return fmt.Sprintf(
|
|
"http://dilbert.com/strip/%4d-%02d-%02d",
|
|
date.Year(),
|
|
date.Month(),
|
|
date.Day(),
|
|
)
|
|
}
|
|
|
|
func getDilbertFeedDesc(date time.Time) (string, error) {
|
|
res, err := http.Get(getDilbertComicUrl(date))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer res.Body.Close()
|
|
if res.StatusCode != 200 {
|
|
return "", errors.New(fmt.Sprintf("Status code error: %d %s", res.StatusCode, res.Status))
|
|
}
|
|
|
|
// Load the HTML document
|
|
doc, err := goquery.NewDocumentFromReader(res.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
// Find the Picture
|
|
src, exists := doc.Find("img.img-comic").Attr("src")
|
|
if !exists {
|
|
return "", errors.New("Couldn't find image source")
|
|
}
|
|
return "<img src=\"https:" + src + "\" />", nil
|
|
}
|