ribbit/plugin_src/plugin_gocomics.go

102 lines
2.3 KiB
Go
Raw Normal View History

2018-04-26 11:53:03 +00:00
package main
import (
2019-01-10 16:39:42 +00:00
"C"
2018-04-26 11:53:03 +00:00
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
2019-01-13 14:22:29 +00:00
func GetSourceName() string {
return "gocomics"
}
func GetFeedList() []map[string]string {
var ret []map[string]string
2018-04-26 11:53:03 +00:00
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())
2018-04-26 11:53:03 +00:00
return ret
}
defer res.Body.Close()
if res.StatusCode != 200 {
fmt.Println("Error getting gocomics list:", res.StatusCode)
2018-04-26 11:53:03 +00:00
return ret
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
fmt.Println("Error loading gocomics list:", err.Error())
2018-04-26 11:53:03 +00:00
return ret
}
doc.Find(".gc-blended-link").Each(func(i int, s *goquery.Selection) {
2018-04-26 11:53:03 +00:00
// For each item found, get the band and title
slug, exists := s.Attr("href")
if exists {
pts := strings.Split(slug, "/")
if len(pts) > 2 {
slug = pts[1]
}
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])
}
2018-04-26 11:53:03 +00:00
author = strings.TrimPrefix(author, "By ")
author = strings.Replace(author, "\u0026", "&", -1)
2019-01-13 14:22:29 +00:00
feedMap := make(map[string]string)
feedMap["slug"] = slug
feedMap["name"] = name
feedMap["author"] = author
feedMap["source"] = GetSourceName()
ret = append(ret, feedMap)
2018-04-26 11:53:03 +00:00
}
})
return ret
}
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://www.gocomics.com/%s/%04d/%02d/%02d",
slug,
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("picture.item-comic-image>img").Attr("src")
if !exists {
return "", errors.New("Couldn't find image source")
}
return "<img src=\"" + src + "\" />", nil
}