Make author field reflect who published the episode

Hopefully this doesn't break too badly, but the author field in the feed will reflect the "realname" for the person who published the episode.
This commit is contained in:
gmemstr 2017-12-27 11:21:28 -08:00
parent 30159082f5
commit 04cabdb84d
2 changed files with 76 additions and 10 deletions

View file

@ -32,6 +32,14 @@ type User struct {
type UserList struct {
Users []User
}
type Config struct {
Name string
Host string
Email string
Description string
Image string
PodcastUrl string
}
/*
* The following is a set of admin commands
@ -362,7 +370,16 @@ func EditEpisode() common.Handler {
func CreateEpisode() common.Handler {
return func(rc *common.RouterContext, w http.ResponseWriter, r *http.Request) *common.HTTPError {
err := r.ParseMultipartForm(32 << 20)
d, err := ioutil.ReadFile("assets/config/config.json")
if err != nil {
panic(err)
}
var config Config
err = json.Unmarshal(d, &config)
if err != nil {
panic(err)
}
err = r.ParseMultipartForm(32 << 20)
if err != nil {
return &common.HTTPError{
Message: err.Error(),
@ -373,11 +390,36 @@ func CreateEpisode() common.Handler {
// Build filename for episode
date := strings.Join(r.Form["date"], "")
title := strings.Join(r.Form["title"], "")
db, err := sql.Open("sqlite3", "assets/config/users.db")
defer db.Close()
author := config.Host
if err != nil {
fmt.Println("Error getting user from database", err)
}
statement, err := db.Prepare("SELECT realname FROM users WHERE username=?")
if err != nil {
fmt.Println("Error getting user from database", err)
}
rows, err := statement.Query(rc.User.Username)
if err != nil {
fmt.Println("Error getting user from database", err)
}
var realname string
for rows.Next() {
err = rows.Scan(&realname)
if err != nil {
fmt.Println("Error getting user from database", err)
}
author = realname
}
name := fmt.Sprintf("%v_%v", date, title)
filename := name + ".mp3"
shownotes := name + "_SHOWNOTES.md"
description := strings.Join(r.Form["description"], "")
description := author + "\n" + strings.Join(r.Form["description"], "")
// Finish building filenames
err = ioutil.WriteFile("./podcasts/"+shownotes, []byte(description), 0644)

View file

@ -8,13 +8,15 @@
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"strings"
"os"
"strconv"
"strings"
"time"
"encoding/json"
"github.com/fsnotify/fsnotify"
"github.com/gorilla/feeds"
@ -99,7 +101,9 @@ func GenerateRss() {
s := strings.Split(file.Name(), "_")
t := strings.Split(s[1], ".")
title := t[0]
description, err := ioutil.ReadFile("podcasts/" + strings.Replace(file.Name(), ".mp3", "_SHOWNOTES.md", 2))
descfilelines := File2lines("podcasts/" + strings.Replace(file.Name(), ".mp3", "_SHOWNOTES.md", 2))
author := descfilelines[0]
description := descfilelines[1]
if err != nil {
log.Fatal(err)
}
@ -115,12 +119,12 @@ func GenerateRss() {
Title: title,
Link: &feeds.Link{Href: link, Length: size, Type: "audio/mpeg"},
Enclosure: &feeds.Enclosure{Url: link, Length: size, Type: "audio/mpeg"},
Description: string(description),
Author: &feeds.Author{Name: config.Host, Email: config.Email},
Description: description,
Author: &feeds.Author{Name: author, Email: config.Email}, // Replace with author in shownotes
Created: date,
},
)
i = i + 1;
i = i + 1
}
}
@ -143,3 +147,23 @@ func GenerateRss() {
json_byte := []byte(json)
ioutil.WriteFile("assets/web/feed.json", json_byte, 0644)
}
// From https://siongui.github.io/2016/04/06/go-readlines-from-file-or-string/
func File2lines(filePath string) []string {
f, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer f.Close()
var lines []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
return lines
}