diff options
author | siddharth ravikumar <s@ricketyspace.net> | 2022-11-26 07:49:26 -0500 |
---|---|---|
committer | siddharth ravikumar <s@ricketyspace.net> | 2022-11-26 07:49:26 -0500 |
commit | f06da1815748cbad110e817097c7bc14e32b1055 (patch) | |
tree | 36ca13efb99d63fe1291ed40e209c947b94e8fa3 | |
parent | fac1158a67ce41809a69feb76c88bfae0a0b4bfa (diff) |
feed: add `podcastUnmarshal`
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | feed/feed.go | 35 | ||||
-rw-r--r-- | feed/feed_test.go | 51 | ||||
l--------- | feed/testdata/pc-atp.xml | 1 | ||||
l--------- | feed/testdata/pc-daringfireball.xml | 1 | ||||
l--------- | feed/testdata/pc-kara.xml | 1 | ||||
l--------- | feed/testdata/pc-scwpod.xml | 1 |
7 files changed, 91 insertions, 1 deletions
@@ -16,5 +16,5 @@ vet: go vet ./... test: - go test -v ${MOD}/db ${MOD}/file ${MOD}/schema + go test ${MOD}/db ${MOD}/feed ${MOD}/file ${MOD}/schema .PHONY: test diff --git a/feed/feed.go b/feed/feed.go index 1223195..3a3fa30 100644 --- a/feed/feed.go +++ b/feed/feed.go @@ -297,3 +297,38 @@ func youtubeUnmarshal(bs []byte) ([]schema.Entry, error) { } return entries, nil } + +// Unmarshal a Podcast feed. +func podcastUnmarshal(bs []byte) ([]schema.Entry, error) { + pcFeed := new(schema.PodcastFeed) + err := xml.Unmarshal(bs, pcFeed) + if err != nil { + return nil, err + } + + // Get all entries. + entries := make([]schema.Entry, 0) + for _, e := range pcFeed.Entries { + layout := time.RFC1123Z + if e.Pub[len(e.Pub)-1:] == "T" { + // Textual time zone. like 'EDT'. + if e.Pub[6:7] == " " { + layout = "Mon, 2 Jan 2006 15:04:05 MST" + } else { + layout = time.RFC1123 + } + } + t, err := time.Parse(layout, e.Pub) + if err != nil { + return nil, err + } + entry := schema.Entry{ + Id: e.Id, + Title: e.Title, + PubTime: t, + Link: e.Link.Url, + } + entries = append(entries, entry) + } + return entries, nil +} diff --git a/feed/feed_test.go b/feed/feed_test.go new file mode 100644 index 0000000..8f566e6 --- /dev/null +++ b/feed/feed_test.go @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: ISC +// Copyright © 2022 siddharth <s@ricketyspace.net> + +package feed + +import ( + "net/url" + "testing" + + "ricketyspace.net/fern/file" +) + +func TestPodcastUnmarshal(t *testing.T) { + testFeeds := []string{ + "testdata/pc-atp.xml", + "testdata/pc-daringfireball.xml", + "testdata/pc-kara.xml", + "testdata/pc-scwpod.xml", + } + for _, feed := range testFeeds { + bs, err := file.ReadFile(feed) + if err != nil { + t.Errorf("read feed: %v", err) + return + } + entries, err := podcastUnmarshal(bs) + if err != nil { + t.Errorf("feed unmarshal: %v", err) + return + } + for _, entry := range entries { + if len(entry.Id) < 1 { + t.Errorf("entry id: %v", entry.Id) + return + } + if len(entry.Title) < 1 { + t.Errorf("entry title: %v", entry.Title) + return + } + if entry.PubTime.Unix() < 994702392 { + t.Errorf("entry time: %v", entry.PubTime) + return + } + _, err = url.Parse(entry.Link) + if err != nil { + t.Errorf("entry link: %s: %v", entry.Link, err) + return + } + } + } +} diff --git a/feed/testdata/pc-atp.xml b/feed/testdata/pc-atp.xml new file mode 120000 index 0000000..e63b34f --- /dev/null +++ b/feed/testdata/pc-atp.xml @@ -0,0 +1 @@ +../../schema/testdata/pc-atp.xml
\ No newline at end of file diff --git a/feed/testdata/pc-daringfireball.xml b/feed/testdata/pc-daringfireball.xml new file mode 120000 index 0000000..d5cba1e --- /dev/null +++ b/feed/testdata/pc-daringfireball.xml @@ -0,0 +1 @@ +../../schema/testdata/pc-daringfireball.xml
\ No newline at end of file diff --git a/feed/testdata/pc-kara.xml b/feed/testdata/pc-kara.xml new file mode 120000 index 0000000..433fc3c --- /dev/null +++ b/feed/testdata/pc-kara.xml @@ -0,0 +1 @@ +../../schema/testdata/pc-kara.xml
\ No newline at end of file diff --git a/feed/testdata/pc-scwpod.xml b/feed/testdata/pc-scwpod.xml new file mode 120000 index 0000000..d0c236e --- /dev/null +++ b/feed/testdata/pc-scwpod.xml @@ -0,0 +1 @@ +../../schema/testdata/pc-scwpod.xml
\ No newline at end of file |