108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
)
|
|
|
|
func downloadGoComicsList() []Comic {
|
|
var ret []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(".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 {
|
|
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])
|
|
}
|
|
author = strings.TrimPrefix(author, "By ")
|
|
author = strings.Replace(author, "\u0026", "&", -1)
|
|
ret = append(ret, *NewComic(slug, name, author, "gocomics"))
|
|
}
|
|
})
|
|
return ret
|
|
}
|
|
|
|
func getGoComicsRssItem(slug string) (string, error) {
|
|
desc, err := getGoComicsFeedDesc(slug, time.Now())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
comic, err := m.GetComic(SRC_GOCOMICS, 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>gocomics;" + slug + ";" + comic.LastUpdate.Format(time.RFC1123Z) + "</guid>\n"
|
|
ret += " <link>" + getGoComicsComicUrl(slug, time.Now()) + "</link>\n"
|
|
ret += " <description>" + desc + "</description>\n"
|
|
ret += " </item>\n"
|
|
return ret, nil
|
|
}
|
|
|
|
func getGoComicsComicUrl(slug string, date time.Time) string {
|
|
return fmt.Sprintf(
|
|
"http://www.gocomics.com/%s/%04d/%02d/%02d",
|
|
slug,
|
|
date.Year(),
|
|
date.Month(),
|
|
date.Day(),
|
|
)
|
|
}
|
|
|
|
func getGoComicsFeedDesc(slug string, date time.Time) (string, error) {
|
|
res, err := http.Get(getGoComicsComicUrl(slug, 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("picture.item-comic-image>img").Attr("src")
|
|
if !exists {
|
|
return "", errors.New("Couldn't find image source")
|
|
}
|
|
return "<img src=\"" + src + "\" />", nil
|
|
}
|