Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a list of music blog articles #329

Open
wants to merge 12 commits into
base: development
Choose a base branch
from
104 changes: 104 additions & 0 deletions controllers/music.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package controllers

import (
"log"
"net/http"
"encoding/xml"
"fmt"
"io/ioutil"
"html"

"github.com/UniversityRadioYork/2016-site/structs"
"github.com/UniversityRadioYork/2016-site/utils"
"github.com/UniversityRadioYork/myradio-go"
)

// MusicController is the controller for the Music page.
type MusicController struct {
Controller
}

// Post represents an individual post in the RSS feed
type Post struct {
Title string `xml:"title"`
Link string `xml:"link"`
PubDate string `xml:"pubDate"`
Updated string `xml:"updated"`
Content string `xml:"encoded"`
}

// NewMusicController returns a new MusicController with the MyRadio session s
// and configuration context c.
func NewMusicController(s *myradio.Session, c *structs.Config) *MusicController {
return &MusicController{Controller{session: s, config: c}}
}

// Get handles the HTTP GET request r for the Music page, writing to w.
func (sc *MusicController) Get(w http.ResponseWriter, r *http.Request) {
// URL of the RSS feed to query
url := sc.config.Music.RSSFeed

isError := false

// Make HTTP GET request
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Failed to query URL: %v\n", err)
isError = true
}
defer resp.Body.Close()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resp.Body will be nil if there was an error, so this will panic - you'll need to either goto the error return or do an early-return above (where you set isError = true)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oop... good spot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have pushed a fix if you want to double-check it. Currently throwing to 404 page not ideal but does the job when not configured correctly.


// Read the response body
xmlData, err := ioutil.ReadAll(resp.Body)
ColinRoitt marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Printf("Failed to read response body: %v\n", err)
isError = true
}

// Parse the XML data into a struct
var rss struct {
Channel struct {
Items []Post `xml:"item"`
} `xml:"channel"`
}

err = xml.Unmarshal(xmlData, &rss)
if err != nil {
fmt.Printf("Failed to parse XML: %v\n", err)
isError = true
}

// set content of each post to to scrape the first <em> tag
for i := range rss.Channel.Items {
var text string
text, err = utils.ExtractFirstEmTagContent(string(rss.Channel.Items[i].Content))
if err == nil {
rss.Channel.Items[i].Content = html.EscapeString(text)
}
}

// format the date of each post to be more readable
for i := range rss.Channel.Items {
var text string
var layout string = "Mon, 02 Jan 2006 15:04:05 MST"
text, err = utils.FormatRSSDate(rss.Channel.Items[i].PubDate, layout)
if err == nil {
rss.Channel.Items[i].PubDate = text
}
}

// If there was an error, render the error page
if isError {
err = utils.RenderTemplate(w, sc.config.PageContext, nil, "404.tmpl")
if err != nil {
log.Println(err)
return
}
}

err = utils.RenderTemplate(w, sc.config.PageContext, rss.Channel.Items, "music.tmpl")
if err != nil {
log.Println(err)
return
}
}
8 changes: 8 additions & 0 deletions controllers/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func (staticC *StaticController) GetCompetitions(w http.ResponseWriter, r *http.
}
}

func (staticC *StaticController) GetMusic(w http.ResponseWriter, r *http.Request) {
err := utils.RenderTemplate(w, staticC.config.PageContext, nil, "music.tmpl")
if err != nil {
log.Println(err)
return
}
}

func (staticC *StaticController) GetCIN(w http.ResponseWriter, r *http.Request) {
err := utils.RenderTemplate(w, staticC.config.PageContext, nil, "cin.tmpl")
if err != nil {
Expand Down
14 changes: 13 additions & 1 deletion sass/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,16 @@ body {
.mainPageGrowingSpacer{
flex-grow: 1;
background-color: $off-white-color;
}
}

.stretched-link {
&::#{after} {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: after;
content: "";
}
}
3 changes: 3 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ func NewServer(c *structs.Config) (*Server, error) {
signupC := controllers.NewSignUpController(session, c)
postRouter.HandleFunc("/signup/", signupC.Post)

musicC := controllers.NewMusicController(session, c)
getRouter.HandleFunc("/music/", musicC.Get)

staticC := controllers.NewStaticController(c)
getRouter.HandleFunc("/about/", staticC.GetAbout)
getRouter.HandleFunc("/contact/", staticC.GetContact)
Expand Down
5 changes: 5 additions & 0 deletions structs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Config struct {
PageContext PageContext `toml:"pageContext"`
Schedule ScheduleConfig `toml:"schedule"`
ShortURLs ShortURLsConfig `toml:"shortUrls"`
Music MusicConfig `toml:"music"`
TrustedProxies []string `toml:"trustedProxies"`
}

Expand Down Expand Up @@ -110,6 +111,10 @@ type ShortURLsConfig struct {
UpdateInterval uint `toml:"updateInterval"`
}

type MusicConfig struct {
RSSFeed string `toml:"rssFeed"`
}

type tomlTime struct {
time.Time
}
Expand Down
10 changes: 10 additions & 0 deletions utils/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,13 @@ func coerceTime(raw interface{}) (time.Time, error) {
return time.Time{}, fmt.Errorf("invalid time type: %T", raw)
}
}

func FormatRSSDate(dateString string, layout string) (string, error) {
t, err := time.Parse(layout, dateString)
if err != nil {
fmt.Println("Error parsing date:", err)
return "", err
}
formattedDate := t.Format("02 Jan 2006")
return formattedDate, err
}
16 changes: 16 additions & 0 deletions utils/get_em.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package utils

import (
"regexp"
"fmt"
)

// Extract first em tag from html string
func ExtractFirstEmTagContent(htmlStr string) (string, error) {
re := regexp.MustCompile(`(?i)<em>(.*?)</em>`)
match := re.FindStringSubmatch(htmlStr)
if len(match) == 2 {
return match[1], nil
}
return "", fmt.Errorf("no <em> tag found")
}
32 changes: 32 additions & 0 deletions views/music.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{define "title"}}{{.PageContext.ShortName}} | Music{{end}}
{{define "content"}}
<div class="container-fluid banner-2">
<div class="container">
<div class="row align-items-center text-center">
<div class="col">
<h1 class="display-3">Music Blog</h1>
</div>
</div>
</div>
</div>
<div class="container-fluid container-padded bg-off-white">
<div class="container d-flex flex-column align-items-center">
{{range .PageData}}
<div class="card my-3 w-75 shadow rouded">
<div class="card-body">
<h2 class="card-title mb-1"><a target="_blank" class="stretched-link" href="{{.Link}}">{{.Title}}</a></h2>
<p class="card-text text-secondary">Published on {{.PubDate}}</p>
<p class="card-text">{{.Content}}</p>
</div>
</div>
{{end}}
<div class="card my-3 w-75 shadow-lg rouded">
<div class="card-body d-flex justify-content-center">
<a href="https://medium.com/@urymusic" target="_blank" class="stretched-link">
<h3>More Posts</h3>
</a>
</div>
</div>
</div>
</div>
{{end}}
Loading