aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Kuznetsov <evgeny@kuznetsov.md>2020-02-05 20:27:25 +0300
committerEvgeny Kuznetsov <evgeny@kuznetsov.md>2020-02-05 20:27:25 +0300
commit3cd999ac2ed914eb2d0c76e31261b4416a84d9ca (patch)
tree5723bec595e13678b15506f43cf33582aa9eea47
parent789eb9f24ac72505ef8bd89d6c94a99ba9d7efad (diff)
downloadradiorus-rss-3cd999ac2ed914eb2d0c76e31261b4416a84d9ca.tar.gz
radiorus-rss-3cd999ac2ed914eb2d0c76e31261b4416a84d9ca.zip
don't panic when feed image can't be parsed
-rw-r--r--main.go18
-rw-r--r--main_test.go28
-rw-r--r--testdata/TestNoImage.golden7
-rw-r--r--testdata/episodes.noimg1
4 files changed, 46 insertions, 8 deletions
diff --git a/main.go b/main.go
index 5f990bb..febb3e0 100644
--- a/main.go
+++ b/main.go
@@ -119,12 +119,7 @@ func populateFeed(feed *feeds.Feed, page []byte) (err error) {
}
feed.Title = stripLink(string(title))
- programImage := programImageRe.FindSubmatch(page)
- feed.Image = &feeds.Image{
- Link: feed.Link.Href,
- Url: string(programImage[2]),
- Title: string(programImage[4]),
- }
+ addFeedImage(page, feed)
episodes := findEpisodes(page)
urlPrefix := episodeURLPrefix(feed.Link.Href)
@@ -154,6 +149,17 @@ func populateFeed(feed *feeds.Feed, page []byte) (err error) {
return
}
+func addFeedImage(page []byte, feed *feeds.Feed) {
+ programImage, err := parse(page, programImageRe, 4)
+ if err == nil {
+ feed.Image = &feeds.Image{
+ Link: feed.Link.Href,
+ Url: string(programImage[1]),
+ Title: string(programImage[3]),
+ }
+ }
+}
+
func parse(src []byte, re *regexp.Regexp, n int) (out [][]byte, err error) {
match := re.FindSubmatch(src)
if len(match) != n+1 {
diff --git a/main_test.go b/main_test.go
index d00a6ae..a85b65d 100644
--- a/main_test.go
+++ b/main_test.go
@@ -57,9 +57,16 @@ func assertGolden(t *testing.T, actual []byte, golden string) {
t.Helper()
if *update {
- writeFile(actual, golden)
+ if _, err := os.Stat(golden); os.IsNotExist(err) {
+ writeFile(actual, golden)
+ } else {
+ t.Log("file", golden, "exists, remove it to record new golden result")
+ }
+ }
+ expected, err := ioutil.ReadFile(golden)
+ if err != nil {
+ t.Error("no file:", golden)
}
- expected, _ := ioutil.ReadFile(golden)
if !bytes.Equal(actual, expected) {
t.Fail()
@@ -107,6 +114,23 @@ func TestBadEpisode(t *testing.T) {
}
}
+func TestNoImage(t *testing.T) {
+ feed := &feeds.Feed{
+ Link: &feeds.Link{Href: "http://www.radiorus.ru/brand/57083/episodes"},
+ }
+
+ page := helperLoadBytes(t, "episodes.noimg")
+ page = cleanText(page)
+
+ if err := populateFeed(feed, page); err != nil {
+ t.Fatal(err)
+ }
+
+ actual := createFeed(feed)
+ golden := filepath.Join("testdata", t.Name()+".golden")
+ assertGolden(t, actual, golden)
+}
+
func TestFindEpisodes(t *testing.T) {
var tests = []string{
"episodes",
diff --git a/testdata/TestNoImage.golden b/testdata/TestNoImage.golden
new file mode 100644
index 0000000..fcb03d7
--- /dev/null
+++ b/testdata/TestNoImage.golden
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
+ <channel>
+ <title>&#34;Аэростат&#34;</title>
+ <link>http://www.radiorus.ru/brand/57083/episodes</link>
+ <description></description>
+ </channel>
+</rss> \ No newline at end of file
diff --git a/testdata/episodes.noimg b/testdata/episodes.noimg
new file mode 100644
index 0000000..d13a0fc
--- /dev/null
+++ b/testdata/episodes.noimg
@@ -0,0 +1 @@
+ <h2><a href="/brand/57083">&quot;Аэростат&quot;</a></h2>