ribbit/plugin_src/plugin_dilbert.go

63 lines
1.3 KiB
Go
Raw Normal View History

2018-04-26 11:53:03 +00:00
package main
2019-01-10 16:39:42 +00:00
import (
"C"
2018-04-26 11:53:03 +00:00
"errors"
"fmt"
"net/http"
"time"
"github.com/PuerkitoBio/goquery"
)
2019-01-13 14:22:29 +00:00
func GetSourceName() string {
return "dilbert"
2018-04-26 11:53:03 +00:00
}
2019-01-13 14:22:29 +00:00
func GetFeedList() []map[string]string {
var ret []map[string]string
feedMap := make(map[string]string)
feedMap["slug"] = "dilbert"
feedMap["name"] = "Dilbert"
feedMap["author"] = "Scott Adams"
feedMap["source"] = GetSourceName()
ret = append(ret, feedMap)
return ret
2018-04-26 11:53:03 +00:00
}
2019-01-13 14:22:29 +00:00
func GetFeedUrl(slug string, date time.Time) (string, error) {
2018-04-26 11:53:03 +00:00
return fmt.Sprintf(
"http://dilbert.com/strip/%4d-%02d-%02d",
date.Year(),
date.Month(),
date.Day(),
2019-01-13 14:22:29 +00:00
), nil
2018-04-26 11:53:03 +00:00
}
2019-01-13 14:22:29 +00:00
func GetFeedDesc(slug string, date time.Time) (string, error) {
url, err := GetFeedUrl(slug, date)
if err != nil {
return "", err
}
res, err := http.Get(url)
2018-04-26 11:53:03 +00:00
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
2018-04-26 11:53:03 +00:00
}