From 2ca59a846bb75f95a93495bbec5ba2cd7c30fa42 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Mon, 24 May 2021 20:58:15 +0100 Subject: [PATCH] added parsing of rss --- .gitignore | 2 +- datafile.toml | 3 -- podcatch/podcatch.go | 65 ++++++++++++++++++++++++++++++++++++++++++-- structs/structs.go | 38 ++++++++++++++++++++++++-- 4 files changed, 98 insertions(+), 10 deletions(-) delete mode 100644 datafile.toml diff --git a/.gitignore b/.gitignore index 258f27a..a0979b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -datafile +podcasts.toml diff --git a/datafile.toml b/datafile.toml deleted file mode 100644 index 5a676d9..0000000 --- a/datafile.toml +++ /dev/null @@ -1,3 +0,0 @@ -Name = "Hello Internet" -URL = "http://www.hellointernet.fm/podcast?format=rss" -Dir = "/home/psw/podcasts/HelloInternet" diff --git a/podcatch/podcatch.go b/podcatch/podcatch.go index 6119e8c..c9dee07 100644 --- a/podcatch/podcatch.go +++ b/podcatch/podcatch.go @@ -1,10 +1,69 @@ -package podcatch + package podcatch import ( "fmt" - // "podcatch/structs"s - // "github.com/pelletier/go-toml" + . "podcatch/structs" + "github.com/pelletier/go-toml" + "encoding/xml" + "io/ioutil" + "net/http" + "regexp" + "log" ) var Version string = "0.1" +var Podcasts map[string]Podcast = make(map[string]Podcast) func Start(){ fmt.Printf("Starting PodCatch Version : %s...\r\n", Version ) + getPodcasts() +} +func getPodcasts(){ + if len(Podcasts) == 0 { + getPodcastFiles() + } + for shortname,podcast := range Podcasts { + fmt.Println(shortname) + fmt.Printf("Checking RSS for %s...\r\n", podcast.Name) + podcast.RSS = getRSS(podcast) + downloadCasts(podcast) + } +} +func getPodcastFiles() { + content, err := ioutil.ReadFile("podcasts.toml") + if err != nil { + log.Fatal(err) + } + e := toml.Unmarshal(content,&Podcasts) + if e != nil { + log.Fatal(err) + } + fmt.Printf("Found %d podcasts.\r\n",len(Podcasts)) +} +func getRSS(podcast Podcast) Rss { + resp, err := http.Get(podcast.URL) + if err != nil { + log.Fatal(err) + } + defer resp.Body.Close() + html, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal(err) + } + return parseRSS(podcast,html) +} +func parseRSS(podcast Podcast, rssxml []byte) Rss { + var rss Rss + e := xml.Unmarshal(rssxml,&rss) + if e != nil { + log.Fatal(e) + } + return rss +} +func downloadCasts(podcast Podcast) { + fmt.Println(podcast.RSS.Version) + for _,item := range podcast.RSS.Channel.Items { + fmt.Printf("Downloading '%s' from : %s.\r\n", item.Title, item.Media.URL) + re := regexp.MustCompile(`[^0-9a-zA-Z-_]+`) + filename := re.ReplaceAllString(item.Title,"_") + ".mp3" + fmt.Println(filename) + // fmt.Printf("%s - %s - %s\r\n", item.Title, item.Media.URL, item.Media.Type) + } } diff --git a/structs/structs.go b/structs/structs.go index 54e72cc..47da4a3 100644 --- a/structs/structs.go +++ b/structs/structs.go @@ -1,7 +1,39 @@ package structs - +import ( + "encoding/xml" +) type Podcast struct { - Name string URL string - Dir string + Name string + Directory string + RSS Rss +} +type Rss struct { + XMLName xml.Name `xml:"rss"` + Version string `xml:"version,attr"` + Channel Channel `xml:"channel"` + Description string `xml:"description"` + Title string `xml:"title"` + Link string `xml:"link"` +} +type Channel struct { + XMLName xml.Name `xml:"channel"` + Title string `xml:"title"` + Link string `xml:"link"` + Description string `xml:"description"` + Items []Item `xml:"item"` +} +type Item struct { + XMLName xml.Name `xml:"item"` + Title string `xml:"title"` + Link string `xml:"link"` + Description string `xml:"description"` + PubDate string `xml:"pubdate"` + Guid string `xml:"guid"` + Media Media `xml:"enclosure"` +} +type Media struct { + XMLName xml.Name `xml:"enclosure"` + URL string `xml:"url,attr"` + Type string `xml:"type,attr"` }