2017-07-23 09:20:22 +01:00
|
|
|
/* generate_rss.go
|
2017-09-21 20:10:16 +01:00
|
|
|
*
|
|
|
|
* This file contains functions for monitoring for file changes and
|
2017-07-23 09:20:22 +01:00
|
|
|
* regenerating the RSS feed accordingly, pulling in shownote files
|
|
|
|
* and configuration parameters
|
2017-09-21 20:10:16 +01:00
|
|
|
*/
|
2017-07-23 09:20:22 +01:00
|
|
|
|
2017-06-14 06:57:02 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-18 21:57:43 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"strings"
|
2017-10-09 01:57:58 +01:00
|
|
|
"strconv"
|
2017-06-18 21:57:43 +01:00
|
|
|
"time"
|
2017-10-05 20:27:55 +01:00
|
|
|
"encoding/json"
|
2017-06-18 21:57:43 +01:00
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2017-11-17 21:59:53 +00:00
|
|
|
"github.com/gorilla/feeds"
|
2017-06-14 06:57:02 +01:00
|
|
|
)
|
2017-06-18 21:57:43 +01:00
|
|
|
|
2017-10-05 20:27:55 +01:00
|
|
|
type Config struct {
|
|
|
|
Name string
|
|
|
|
Host string
|
|
|
|
Email string
|
|
|
|
Description string
|
|
|
|
Image string
|
|
|
|
PodcastUrl string
|
|
|
|
}
|
|
|
|
|
2017-07-23 09:20:22 +01:00
|
|
|
// Watch folder for changes, called from webserver.go
|
2017-06-18 21:57:43 +01:00
|
|
|
func watch() {
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
done := make(chan bool)
|
2017-09-19 20:28:59 +01:00
|
|
|
|
|
|
|
// Call func asynchronously
|
2017-06-18 21:57:43 +01:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-watcher.Events:
|
2017-09-11 18:10:35 +01:00
|
|
|
// log.Println("event:", event)
|
2017-06-18 21:57:43 +01:00
|
|
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
2017-10-05 20:27:55 +01:00
|
|
|
GenerateRss()
|
2017-06-18 21:57:43 +01:00
|
|
|
}
|
|
|
|
case err := <-watcher.Errors:
|
|
|
|
log.Println("error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = watcher.Add("podcasts/")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-09-25 19:22:30 +01:00
|
|
|
err = watcher.Add("assets/config/config.json")
|
2017-09-11 19:36:40 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-06-18 21:57:43 +01:00
|
|
|
<-done
|
2017-06-14 06:57:02 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 20:27:55 +01:00
|
|
|
// Iterate through podcasts directory and build feed
|
|
|
|
// object, then compile as json and rss and write to file
|
|
|
|
func GenerateRss() {
|
|
|
|
d, err := ioutil.ReadFile("assets/config/config.json")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var config Config
|
|
|
|
err = json.Unmarshal(d, &config)
|
2017-09-29 19:29:36 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-10-05 20:27:55 +01:00
|
|
|
|
2017-06-18 21:57:43 +01:00
|
|
|
now := time.Now()
|
|
|
|
files, err := ioutil.ReadDir("podcasts")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-06-14 06:57:02 +01:00
|
|
|
|
2017-09-25 16:58:05 +01:00
|
|
|
podcasturl := config.PodcastUrl
|
2017-06-18 21:57:43 +01:00
|
|
|
feed := &feeds.Feed{
|
2017-09-25 16:58:05 +01:00
|
|
|
Title: config.Name,
|
2017-09-11 19:36:40 +01:00
|
|
|
Link: &feeds.Link{Href: podcasturl},
|
2017-09-25 16:58:05 +01:00
|
|
|
Description: config.Description,
|
|
|
|
Author: &feeds.Author{Name: config.Host, Email: config.Email},
|
2017-06-18 21:57:43 +01:00
|
|
|
Created: now,
|
2017-09-25 16:58:05 +01:00
|
|
|
Image: &feeds.Image{Url: config.Image},
|
2017-06-18 21:57:43 +01:00
|
|
|
}
|
2017-10-08 18:54:48 +01:00
|
|
|
i := 0
|
2017-06-18 21:57:43 +01:00
|
|
|
for _, file := range files {
|
2017-07-01 08:26:32 +01:00
|
|
|
if strings.Contains(file.Name(), ".mp3") {
|
|
|
|
s := strings.Split(file.Name(), "_")
|
|
|
|
t := strings.Split(s[1], ".")
|
|
|
|
title := t[0]
|
2017-09-21 20:10:16 +01:00
|
|
|
description, err := ioutil.ReadFile("podcasts/" + strings.Replace(file.Name(), ".mp3", "_SHOWNOTES.md", 2))
|
2017-07-01 08:26:32 +01:00
|
|
|
if err != nil {
|
2017-09-21 20:10:16 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-07-01 08:26:32 +01:00
|
|
|
date, err := time.Parse("2006-01-02", s[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-07-16 21:49:18 +01:00
|
|
|
size := fmt.Sprintf("%d", file.Size())
|
2017-09-11 19:36:40 +01:00
|
|
|
link := podcasturl + "/download/" + file.Name()
|
2017-09-21 20:10:16 +01:00
|
|
|
feed.Add(
|
2017-07-01 08:26:32 +01:00
|
|
|
&feeds.Item{
|
2017-10-09 01:57:58 +01:00
|
|
|
Id: strconv.Itoa(i),
|
2017-07-01 08:26:32 +01:00
|
|
|
Title: title,
|
2017-09-11 19:36:40 +01:00
|
|
|
Link: &feeds.Link{Href: link, Length: size, Type: "audio/mpeg"},
|
|
|
|
Enclosure: &feeds.Enclosure{Url: link, Length: size, Type: "audio/mpeg"},
|
2017-07-01 08:26:32 +01:00
|
|
|
Description: string(description),
|
2017-09-25 16:58:05 +01:00
|
|
|
Author: &feeds.Author{Name: config.Host, Email: config.Email},
|
2017-07-01 08:26:32 +01:00
|
|
|
Created: date,
|
|
|
|
},
|
2017-07-16 21:37:05 +01:00
|
|
|
)
|
2017-06-18 21:57:43 +01:00
|
|
|
}
|
2017-10-08 18:54:48 +01:00
|
|
|
i = i + 1;
|
2017-06-18 21:57:43 +01:00
|
|
|
}
|
2017-07-23 09:20:22 +01:00
|
|
|
|
|
|
|
// Translate the feed to both RSS and JSON,
|
|
|
|
// RSS for readers and JSON for frontend (& API I guess)
|
2017-06-18 21:57:43 +01:00
|
|
|
rss, err := feed.ToRss()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
json, err := feed.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-07-16 18:43:47 +01:00
|
|
|
// fmt.Println(rss)
|
2017-07-23 09:20:22 +01:00
|
|
|
|
2017-09-21 20:10:16 +01:00
|
|
|
// Write to files as neccesary
|
2017-06-18 21:57:43 +01:00
|
|
|
rss_byte := []byte(rss)
|
2017-09-25 19:22:30 +01:00
|
|
|
ioutil.WriteFile("assets/web/feed.rss", rss_byte, 0644)
|
2017-09-21 20:10:16 +01:00
|
|
|
|
2017-06-18 21:57:43 +01:00
|
|
|
json_byte := []byte(json)
|
2017-09-25 19:22:30 +01:00
|
|
|
ioutil.WriteFile("assets/web/feed.json", json_byte, 0644)
|
2017-06-18 21:57:43 +01:00
|
|
|
}
|