added parsing of rss

This commit is contained in:
Paul Wilde 2021-05-24 20:58:15 +01:00
parent 9749f9eb49
commit 2ca59a846b
4 changed files with 98 additions and 10 deletions

2
.gitignore vendored
View file

@ -1 +1 @@
datafile
podcasts.toml

View file

@ -1,3 +0,0 @@
Name = "Hello Internet"
URL = "http://www.hellointernet.fm/podcast?format=rss"
Dir = "/home/psw/podcasts/HelloInternet"

View file

@ -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)
}
}

View file

@ -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"`
}