Started Plugin Rewrite
This commit is contained in:
71
plugin_src/plugin_dilbert.go
Normal file
71
plugin_src/plugin_dilbert.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"C"
|
||||
)
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
func getFeedList() []Feed {
|
||||
var ret []Feed
|
||||
ret = append(ret, *NewFeed("dilbert", "Dilbert", "Scott Adams", "dilbert"))
|
||||
return ret
|
||||
}
|
||||
|
||||
func getRssItem(slug string) (string, error) {
|
||||
desc, err := getFeedDesc(time.Now())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
feed, err := m.GetFeed(SRC_DILBERT, slug)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
desc = "<![CDATA[" + desc + "]]>"
|
||||
ret := " <item>\n"
|
||||
ret += " <title>" + feed.Name + "</title>\n"
|
||||
ret += " <pubDate>" + feed.LastUpdate.Format(time.RFC1123Z) + "</pubDate>\n"
|
||||
ret += " <guid>dilbert;" + slug + ";" + feed.LastUpdate.Format(time.RFC1123Z) + "</guid>\n"
|
||||
ret += " <link>" + getFeedUrl(time.Now()) + "</link>\n"
|
||||
ret += " <description>" + desc + "</description>\n"
|
||||
ret += " </item>\n"
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func getFeedUrl(date time.Time) string {
|
||||
return fmt.Sprintf(
|
||||
"http://dilbert.com/strip/%4d-%02d-%02d",
|
||||
date.Year(),
|
||||
date.Month(),
|
||||
date.Day(),
|
||||
)
|
||||
}
|
||||
|
||||
func getFeedDesc(date time.Time) (string, error) {
|
||||
res, err := http.Get(getFeedUrl(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
|
||||
}
|
108
plugin_src/plugin_gocomics.go
Normal file
108
plugin_src/plugin_gocomics.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"C"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
func downloadList() {
|
||||
var ret []Feed
|
||||
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, *NewFeed(slug, name, author, "gocomics"))
|
||||
}
|
||||
})
|
||||
return ret
|
||||
}
|
||||
|
||||
func getRssItem(slug string) (string, error) {
|
||||
desc, err := getFeedDesc(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 getFeedUrl(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 getFeedDesc(slug string, date time.Time) (string, error) {
|
||||
res, err := http.Get(getFeedUrl(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
|
||||
}
|
84
plugin_src/plugin_xkcd.go
Normal file
84
plugin_src/plugin_xkcd.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"C"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
)
|
||||
|
||||
func getFeedList() []Feed {
|
||||
var ret []Feed
|
||||
ret = append(ret, *NewFeed("xkcd", "XKCD", "Randall Munroe", "xkcd"))
|
||||
return ret
|
||||
}
|
||||
|
||||
func getRssItem(slug string) (string, error) {
|
||||
desc, err := getFeedDesc(time.Now())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
feed, err := m.GetFeed(SRC_XKCD, slug)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
desc = "<![CDATA[" + desc + "]]>"
|
||||
ret := " <item>\n"
|
||||
ret += " <title>" + feed.Name + "</title>\n"
|
||||
ret += " <pubDate>" + feed.LastUpdate.Format(time.RFC1123Z) + "</pubDate>\n"
|
||||
ret += " <guid>xkcd;" + slug + ";" + feed.LastUpdate.Format(time.RFC1123Z) + "</guid>\n"
|
||||
ret += " <link>" + getUrl(time.Now()) + "</link>\n"
|
||||
ret += " <description>" + desc + "</description>\n"
|
||||
ret += " </item>\n"
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func getFeedUrl(date time.Time) (string, error) {
|
||||
var isComicDay = func(dt time.Time) bool {
|
||||
return dt.Weekday() == time.Monday || dt.Weekday() == time.Wednesday || dt.Weekday() == time.Friday
|
||||
}
|
||||
if !isComicDay(dt) {
|
||||
return "", errors.New("No URL for the given day")
|
||||
}
|
||||
var num int
|
||||
wrkDate := time.Date(2005, time.August, 19, 0, 0, 0, 0, time.UTC)
|
||||
for wrkDate.Before(dt) {
|
||||
if isComicDay(wrkDate) {
|
||||
num++
|
||||
}
|
||||
wrkDate = wrkDate.Add(time.Hour * 24)
|
||||
}
|
||||
return fmt.Sprintf("https://xkcd.com/%d", num), nil
|
||||
}
|
||||
|
||||
func getFeedDesc(date time.Time) (string, error) {
|
||||
res, err := http.Get(getUrl(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
|
||||
sel := doc.Find("div#comic>img")
|
||||
src, exists := sel.Attr("src")
|
||||
if !exists {
|
||||
return "", errors.New("Couldn't find image source")
|
||||
}
|
||||
src = "https:" + src
|
||||
title, exists := sel.Attr("title")
|
||||
if !exists {
|
||||
title = ""
|
||||
}
|
||||
return "<img src=\"" + src + "\" /><p>" + title + "</p>", nil
|
||||
}
|
Reference in New Issue
Block a user