From f06da1815748cbad110e817097c7bc14e32b1055 Mon Sep 17 00:00:00 2001 From: siddharth ravikumar Date: Sat, 26 Nov 2022 07:49:26 -0500 Subject: feed: add `podcastUnmarshal` --- feed/feed.go | 35 +++++++++++++++++++++++++ feed/feed_test.go | 51 +++++++++++++++++++++++++++++++++++++ feed/testdata/pc-atp.xml | 1 + feed/testdata/pc-daringfireball.xml | 1 + feed/testdata/pc-kara.xml | 1 + feed/testdata/pc-scwpod.xml | 1 + 6 files changed, 90 insertions(+) create mode 100644 feed/feed_test.go create mode 120000 feed/testdata/pc-atp.xml create mode 120000 feed/testdata/pc-daringfireball.xml create mode 120000 feed/testdata/pc-kara.xml create mode 120000 feed/testdata/pc-scwpod.xml (limited to 'feed') 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 + +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 -- cgit v1.2.3