You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
podgrab/service/podcastService.go

152 lines
3.4 KiB

4 years ago
package service
import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
"github.com/akhilrex/podgrab/db"
4 years ago
"github.com/microcosm-cc/bluemonday"
4 years ago
"gorm.io/gorm"
)
//Fetch is
func FetchURL(url string) (PodcastData, error) {
4 years ago
body, err := makeQuery(url)
if err != nil {
return PodcastData{}, err
}
4 years ago
var response PodcastData
4 years ago
err = xml.Unmarshal(body, &response)
4 years ago
return response, err
}
func GetAllPodcasts() *[]db.Podcast {
var podcasts []db.Podcast
db.GetAllPodcasts(&podcasts)
return &podcasts
}
func AddPodcast(url string) (db.Podcast, error) {
data, err := FetchURL(url)
if err != nil {
fmt.Println("Error")
4 years ago
//log.Fatal(err)
4 years ago
return db.Podcast{}, err
}
var podcast db.Podcast
err = db.GetPodcastByTitleAndAuthor(data.Channel.Title, data.Channel.Author, &podcast)
if errors.Is(err, gorm.ErrRecordNotFound) {
4 years ago
p := bluemonday.StripTagsPolicy()
4 years ago
podcast := db.Podcast{
Title: data.Channel.Title,
4 years ago
Summary: p.Sanitize(data.Channel.Summary),
4 years ago
Author: data.Channel.Author,
Image: data.Channel.Image.URL,
URL: url,
}
err = db.CreatePodcast(&podcast)
return podcast, err
}
return podcast, err
}
func AddPodcastItems(podcast *db.Podcast) error {
fmt.Println("Creating: " + podcast.ID)
data, err := FetchURL(podcast.URL)
if err != nil {
log.Fatal(err)
return err
}
4 years ago
p := bluemonday.StripTagsPolicy()
4 years ago
for i := 0; i < 5; i++ {
obj := data.Channel.Item[i]
var podcastItem db.PodcastItem
err := db.GetPodcastItemByPodcastIdAndGUID(podcast.ID, obj.Guid.Text, &podcastItem)
if errors.Is(err, gorm.ErrRecordNotFound) {
duration, _ := strconv.Atoi(obj.Duration)
pubDate, _ := time.Parse(time.RFC1123Z, obj.PubDate)
podcastItem = db.PodcastItem{
PodcastID: podcast.ID,
Title: obj.Title,
4 years ago
Summary: p.Sanitize(obj.Summary),
4 years ago
EpisodeType: obj.EpisodeType,
Duration: duration,
PubDate: pubDate,
FileURL: obj.Enclosure.URL,
GUID: obj.Guid.Text,
}
db.CreatePodcastItem(&podcastItem)
}
}
return err
}
func SetPodcastItemAsDownloaded(id string, location string) {
var podcastItem db.PodcastItem
db.GetPodcastItemById(id, &podcastItem)
podcastItem.DownloadDate = time.Now()
podcastItem.DownloadPath = location
db.UpdatePodcastItem(&podcastItem)
}
func DownloadMissingEpisodes() error {
data, err := db.GetAllPodcastItemsToBeDownloaded()
4 years ago
fmt.Println("Processing episodes: ", strconv.Itoa(len(*data)))
4 years ago
if err != nil {
return err
}
for _, item := range *data {
4 years ago
4 years ago
url, _ := Download(item.FileURL, item.Title, item.Podcast.Title)
SetPodcastItemAsDownloaded(item.ID, url)
4 years ago
return nil
4 years ago
}
return nil
}
func RefreshEpisodes() error {
var data []db.Podcast
err := db.GetAllPodcasts(&data)
if err != nil {
return err
}
for _, item := range data {
AddPodcastItems(&item)
}
return nil
}
4 years ago
func makeQuery(url string) ([]byte, error) {
4 years ago
//link := "https://www.goodreads.com/search/index.xml?q=Good%27s+Omens&key=" + "jCmNlIXjz29GoB8wYsrd0w"
//link := "https://www.goodreads.com/search/index.xml?key=jCmNlIXjz29GoB8wYsrd0w&q=Ender%27s+Game"
//fmt.Println(url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
4 years ago
return nil, err
4 years ago
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
4 years ago
return nil, err
4 years ago
}
defer resp.Body.Close()
fmt.Println("Response status:", resp.Status)
body, err := ioutil.ReadAll(resp.Body)
4 years ago
return body, nil
4 years ago
}