package main import ( "C" "errors" "fmt" "net/http" "time" "github.com/PuerkitoBio/goquery" ) func GetSourceName() string { return "xkcd" } func GetFeedList() []map[string]string { var ret []map[string]string feedMap := make(map[string]string) feedMap["slug"] = "xkcd" feedMap["name"] = "XKCD" feedMap["author"] = "Randall Munroe" feedMap["source"] = GetSourceName() ret = append(ret, feedMap) 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 = "" ret := " \n" ret += " " + feed.Name + "\n" ret += " " + feed.LastUpdate.Format(time.RFC1123Z) + "\n" ret += " xkcd;" + slug + ";" + feed.LastUpdate.Format(time.RFC1123Z) + "\n" ret += " " + getUrl(time.Now()) + "\n" ret += " " + desc + "\n" ret += " \n" return ret, nil } */ func GetFeedUrl(slug string, dt 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(slug string, dt time.Time) (string, error) { var url string var err error if url, err = GetFeedUrl(slug, dt); err != nil { return "", err } res, err := http.Get(url) 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 "

" + title + "

", nil }