From 18daf268e2e334649c0e07f9c2117b91dab4bae5 Mon Sep 17 00:00:00 2001 From: Evgeny Kuznetsov Date: Tue, 28 Jan 2020 00:30:35 +0300 Subject: decouple feed description and output --- main.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index d81ea48..75750aa 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ import ( "net/http" "regexp" "strconv" + "strings" "time" "github.com/gorilla/feeds" @@ -64,16 +65,22 @@ func main() { } populateFeed(feed, page) + describeFeed(feed) describeEpisodes(feed) feed.Created = time.Now() + outputFile := outputPath + "radiorus-" + programNumber + ".rss" + + writeFeed(feed, outputFile) +} + +func writeFeed(feed *feeds.Feed, filename string) { rss, err := feed.ToRss() if err != nil { log.Fatal(err) } - outputFile := outputPath + "radiorus-" + programNumber + ".rss" output := []byte(rss) - if err := ioutil.WriteFile(outputFile, output, 0644); err != nil { + if err := ioutil.WriteFile(filename, output, 0644); err != nil { log.Fatal(err) } } @@ -90,12 +97,6 @@ func populateFeed(feed *feeds.Feed, page []byte) { episodes := episodeRe.FindAll(page, -1) - programAboutUrl := "http://www.radiorus.ru/brand/" + programNumber + "/about" - programAboutPage := getPage(programAboutUrl) - programAbout := programAboutRe.FindSubmatch(programAboutPage)[1] - re := regexp.MustCompile(`<(.+?)?>`) - feed.Description = string(re.ReplaceAll(programAbout, []byte(``))) - badFeed := false for _, episode := range episodes { @@ -140,6 +141,14 @@ func populateFeed(feed *feeds.Feed, page []byte) { } } +func describeFeed(feed *feeds.Feed) { + programAboutUrl := strings.TrimSuffix(feed.Link.Href, "episodes") + "about" + programAboutPage := getPage(programAboutUrl) + programAbout := programAboutRe.FindSubmatch(programAboutPage)[1] + re := regexp.MustCompile(`<(.+?)?>`) + feed.Description = string(re.ReplaceAll(programAbout, []byte(``))) +} + func describeEpisodes(feed *feeds.Feed) { for _, item := range feed.Items { page := getPage(item.Link.Href) -- cgit v1.2.3