podcatch/podCatch/podcatch.go

295 lines
6.9 KiB
Go
Raw Normal View History

2021-05-25 20:24:15 +02:00
package podcatch
2021-05-24 20:30:56 +02:00
import (
"fmt"
2021-05-25 23:22:48 +02:00
. "podcatch/structs"
2021-05-24 21:58:15 +02:00
"github.com/pelletier/go-toml"
"encoding/xml"
2021-05-25 20:24:15 +02:00
"io"
2021-05-24 21:58:15 +02:00
"io/ioutil"
"net/http"
"regexp"
2021-05-24 23:05:12 +02:00
"os"
2021-05-24 21:58:15 +02:00
"log"
2021-05-24 23:05:12 +02:00
"strings"
2021-05-26 12:57:55 +02:00
id3 "github.com/mikkyang/id3-go"
v2 "github.com/mikkyang/id3-go/v2"
2021-05-24 20:30:56 +02:00
)
var Version string = "0.1"
2021-05-24 23:05:12 +02:00
var Settings Settings
2021-05-24 21:58:15 +02:00
var Podcasts map[string]Podcast = make(map[string]Podcast)
2021-05-24 23:05:12 +02:00
var donefile string
2021-05-25 20:24:15 +02:00
var podcatchdir string
var homedir string
var dbdir string
2021-05-24 20:30:56 +02:00
func Start(){
fmt.Printf("Starting PodCatch Version : %s...\r\n", Version )
2021-05-25 20:24:15 +02:00
getHomeDirs()
2021-05-24 23:05:12 +02:00
getSettings()
2021-05-24 21:58:15 +02:00
getPodcasts()
}
2021-05-25 20:24:15 +02:00
func getHomeDirs(){
h, err := os.UserHomeDir()
if err != nil {
log.Fatal( err )
}
homedir = h
podcatchdir = h + "/.podcatch/"
}
2021-05-24 23:05:12 +02:00
func getSettings(){
2021-05-25 20:24:15 +02:00
settings := podcatchdir + "settings.toml"
if !checkFileExists(settings){
pwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
fmt.Println("Copying default settings.toml to user dir.")
ok, err := copyFile(pwd + "/defaults/settings.toml",settings)
if err != nil {
log.Fatal(err)
}
if ok > 0 {
fmt.Println("Copied.")
}
}
content, err := ioutil.ReadFile(settings)
2021-05-24 23:05:12 +02:00
if err != nil {
log.Fatal(err)
}
e := toml.Unmarshal(content,&Settings)
if e != nil {
log.Fatal(err)
}
2021-05-25 20:24:15 +02:00
Settings.Directory = strings.Replace(Settings.Directory,"~",homedir,1)
dbdir = Settings.Directory + ".db/"
os.Mkdir(dbdir,0755)
2021-05-24 23:05:12 +02:00
}
2021-05-24 21:58:15 +02:00
func getPodcasts(){
if len(Podcasts) == 0 {
getPodcastFiles()
}
for shortname,podcast := range Podcasts {
2021-05-24 23:05:12 +02:00
podcast.Directory = shortname
2021-05-24 21:58:15 +02:00
fmt.Printf("Checking RSS for %s...\r\n", podcast.Name)
podcast.RSS = getRSS(podcast)
downloadCasts(podcast)
}
}
func getPodcastFiles() {
2021-05-25 20:24:15 +02:00
pcs := podcatchdir + "podcasts.toml"
if !checkFileExists(pcs){
pwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
fmt.Println("Copying default podcasts.toml to user dir.")
ok, err := copyFile(pwd + "/defaults/podcasts.toml",pcs)
if err != nil {
log.Fatal(err)
}
if ok > 0 {
fmt.Println("Copied.")
}
}
content, err := ioutil.ReadFile(podcatchdir + "podcasts.toml")
2021-05-24 21:58:15 +02:00
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) {
2021-05-24 23:05:12 +02:00
count := 0
2021-05-24 21:58:15 +02:00
for _,item := range podcast.RSS.Channel.Items {
2021-05-24 23:05:12 +02:00
if count >= Settings.Limit {
break
}
if !podcastDownloaded(item){
fmt.Printf("Downloading '%s %s' from : %s.\r\n", item.Episode, item.Title, item.Media.URL)
re := regexp.MustCompile(`[^0-9a-zA-Z-_]+`)
filename := item.Episode + re.ReplaceAllString(item.Title,"_") + ".mp3"
dir := Settings.Directory + podcast.Directory
err := os.Mkdir(dir, 0777)
if err != nil && err.Error() != fmt.Sprintf("mkdir %s: file exists",dir){
log.Fatal(err)
}
ok := downloadMp3(item.Media.URL, dir + "/" + filename)
if ok {
// createNFO(item, strings.Replace(dir + "/" + filename,".mp3",".nfo",1))
2021-05-26 12:57:55 +02:00
addId3(podcast.Name, item,dir + "/" + filename)
2021-05-24 23:05:12 +02:00
markAsReceived(item)
} else {
markAsErrored(item)
}
} else {
fmt.Printf("Skipping '%s' - already downloaded\r\n", item.Title)
}
count = count + 1
}
}
func podcastDownloaded(item Item) bool {
2021-05-25 20:24:15 +02:00
db := dbdir + "complete"
if checkCreate(db) {
if len(donefile) < 1 {
content, err := ioutil.ReadFile(db)
if err != nil {
log.Fatal(err)
}
donefile = string(content)
}
if strings.Contains(donefile,item.Title){
return true
}
if strings.Contains(donefile,item.Media.URL){
return true
2021-05-24 23:05:12 +02:00
}
}
return false
}
func downloadMp3(url string, file string) bool {
ok := false
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(file, data, 0775)
if err != nil {
log.Fatal(err)
}
ok = true
return ok
}
2021-05-26 12:57:55 +02:00
func addId3(artist string, item Item, file string) {
fmt.Printf("Saving ID3 to %s\r\n",file)
mp3File, err := id3.Open(file)
2021-05-24 23:05:12 +02:00
if err != nil {
log.Fatal(err)
}
2021-05-26 12:57:55 +02:00
defer mp3File.Close()
if mp3File.Artist() == "" {
mp3File.SetArtist(artist)
}
if mp3File.Album() == "" {
mp3File.SetAlbum(artist)
}
if mp3File.Title() == "" {
mp3File.SetTitle(item.Title)
}
if len(mp3File.Comments()) == 0 {
ft := v2.V23FrameTypeMap["COMM"]
textFrame := v2.NewTextFrame(ft, item.Description)
mp3File.AddFrames(textFrame)
2021-05-24 23:05:12 +02:00
}
}
func markAsReceived(item Item) {
2021-05-25 20:24:15 +02:00
db := dbdir + "complete"
checkCreate(db)
file, err := os.OpenFile(db, os.O_APPEND|os.O_WRONLY, 0755)
2021-05-24 23:05:12 +02:00
if err != nil {
log.Println(err)
}
defer file.Close()
content := fmt.Sprintf("%s - %s\r\n",item.Title, item.Media.URL)
if _, err := file.WriteString(content); err != nil {
log.Fatal(err)
}
}
func markAsErrored(item Item) {
2021-05-25 20:24:15 +02:00
db := dbdir + "error"
checkCreate(db)
file, err := os.OpenFile(db, os.O_APPEND|os.O_WRONLY, 0755)
2021-05-24 23:05:12 +02:00
if err != nil {
log.Println(err)
}
defer file.Close()
2021-05-26 13:52:27 +02:00
content := fmt.Sprintf("%s\r\n%s\r\n",item.Title, item.Media.URL)
2021-05-24 23:05:12 +02:00
if _, err := file.WriteString(content); err != nil {
log.Fatal(err)
2021-05-24 21:58:15 +02:00
}
2021-05-24 20:30:56 +02:00
}
2021-05-25 20:24:15 +02:00
func checkFileExists(file string) bool {
if _, err := os.Stat(file); err == nil {
// fmt.Println("Exists")
// exists
return true
} else if os.IsNotExist(err) {
// fmt.Println("Not Exists")
// not exists
return false
} else {
// fmt.Println("Maybe Exists, Maybe Not")
return false
// Schrodinger: file may or may not exist. See err for details.
// Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence
}
return false
}
func checkCreate(file string) bool {
if checkFileExists(file) {
return true
} else {
if createFile(file) {
return true
}
}
return false
}
func createFile(file string) bool {
f, err := os.Create(file)
if err != nil {
log.Fatal(err)
return false
}
defer f.Close()
return true
}
func copyFile(src, dst string) (int64, error) {
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}