feed: refactor into internal function for core conversion

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2024-06-09 15:19:55 +03:00
parent 034e3adabe
commit 156ecd35e9
Signed by: Hesham
GPG Key ID: 74876157D199B09E
3 changed files with 14 additions and 80 deletions

View File

@ -27,8 +27,10 @@ func main() {
} }
fmt.Fprintf(os.Stderr, "id: %s\nbouncer: %s\n", *chan_id, *bounc_url) fmt.Fprintf(os.Stderr, "id: %s\nbouncer: %s\n", *chan_id, *bounc_url)
err := feed.ConvertYtToRss(os.Stdout, *chan_id, *bounc_url, err := feed.ConvertYtToRss(os.Stdout, *chan_id,
feed.RSSMetadata{Languge: "en", Copyright: "N/A", Summary: "YouTube Channel as podcast"}) feed.RSSMetadata{
Languge: "en", Copyright: "N/A", Summary: "YouTube Channel as podcast", BounceURL: *bounc_url})
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err.Error()) fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)

View File

@ -29,97 +29,29 @@ type RSSMetadata struct {
} }
/* bounce_url in the format of http://domain/?id=%s */ /* bounce_url in the format of http://domain/?id=%s */
func ConvertYtToRss(w io.Writer, channel_id string, bounce_url string, meta RSSMetadata) error { func ConvertYtToRss(w io.Writer, channel_id string, meta RSSMetadata) error {
var podFeed templates.FeedData
channelUrl := fmt.Sprintf(YT_FEED_URL, channel_id) channelUrl := fmt.Sprintf(YT_FEED_URL, channel_id)
feed, err := getFeed(channelUrl) feed, err := getFeed(channelUrl)
if err != nil { if err != nil {
return feedErr(err) return feedErr(err)
} }
return convertFeedToRSS(w, *feed, meta)
t_now := time.Now().UTC()
podFeed.Title = feed.Title
podFeed.Summary = meta.Summary
podFeed.BuildDateRfcEmail = t_now.Format(time.RFC1123Z)
podFeed.CopyRight = meta.Copyright
podFeed.PublishDateRfcEmail = t_now.Format(time.RFC1123Z)
podFeed.PodcastPage = feed.Link
podFeed.Lang = meta.Languge
podFeed.GeneratorName = __GENERATOR_NAME
for i, item := range feed.Items {
subStrings := strings.Split(item.GUID, ":")
id := subStrings[2]
bounceURL, err := url.Parse(
fmt.Sprintf(bounce_url, id),
)
if err != nil {
return err
}
// Check this out
g := item.Extensions["media"]["group"]
gg := g[len(g)-1]
thumb := gg.Children["thumbnail"][0]
coverArtUrl, err := url.Parse(thumb.Attrs["url"])
if err != nil {
return errors.Join(err, errors.New(
fmt.Sprintf(
"could not parse item cover art for %s GUID: %s\n",
item.Title,
item.GUID,
)))
}
if i == 0 {
podFeed.PodcastImageURL = coverArtUrl.String()
}
podFeed.Items = append(podFeed.Items,
templates.FeedItem{
Title: item.Title,
CoverImageURL: coverArtUrl.String(),
Id: id,
Duration: "0",
PublishDateRfcEmail: item.PublishedParsed.Format(time.RFC1123Z),
Description: "TBI",
Length: 0,
EnclosureURL: bounceURL.String(),
})
}
rssTemplate, err := template.New("rss").Parse(templates.RSSTemplate)
if err != nil {
return err
}
err = rssTemplate.Execute(w, podFeed)
if err != nil {
return err
}
rssResult := bytes.Buffer{}
rssTemplate.Execute(&rssResult, podFeed)
_, err = gofeed.NewParser().ParseString(rssResult.String())
if err != nil {
return feedErr(err)
}
return nil
} }
// Convert to Yt Atom to RSS given a Reader that provides xml // Convert to Yt Atom to RSS given a Reader that provides xml
func convertAtomToRSS(w io.Writer, r io.Reader, meta RSSMetadata) error { func ConvertAtomToRSS(w io.Writer, r io.Reader, meta RSSMetadata) error {
var podFeed templates.FeedData
feed, err := gofeed.NewParser().Parse(r) feed, err := gofeed.NewParser().Parse(r)
if err != nil { if err != nil {
return feedErr(err) return feedErr(err)
} }
return convertFeedToRSS(w, *feed, meta)
}
func convertFeedToRSS(w io.Writer, feed gofeed.Feed, meta RSSMetadata) error {
var podFeed templates.FeedData
t_now := time.Now().UTC() t_now := time.Now().UTC()
podFeed.Title = feed.Title podFeed.Title = feed.Title

View File

@ -84,7 +84,7 @@ func TestAtomToRSS(t *testing.T) {
buf := bytes.Buffer{} buf := bytes.Buffer{}
err = convertAtomToRSS(&buf, f, RSSMetadata{BounceURL: "http://localhost:8081/q=%s"}) err = ConvertAtomToRSS(&buf, f, RSSMetadata{BounceURL: "http://localhost:8081/q=%s"})
if err != nil { if err != nil {
t.Error(err.Error()) t.Error(err.Error())
t.FailNow() t.FailNow()