-
Notifications
You must be signed in to change notification settings - Fork 8
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
ColinRoitt
wants to merge
12
commits into
development
Choose a base branch
from
colin-music-blog
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1ab5bb7
add a basic music page
ColinRoitt 2fe8f53
re-add line break
ColinRoitt 37b9a94
Move rss link to config file
ColinRoitt 205812b
Move html parse to util
ColinRoitt 5963119
escape html to display
ColinRoitt e210843
format blog post date
ColinRoitt 9707d1a
update formatting of list of articles
ColinRoitt 3c051d7
remove deprecated lib
ColinRoitt d4521c8
fix error handling
ColinRoitt 7c8d1e9
resolve lint error
ColinRoitt 6d13683
Update controllers/music.go
ColinRoitt 345d506
stupid aligning stupid headers
ColinRoitt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
// 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 eithergoto
the error return or do an early-return above (where you setisError = true
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oop... good spot
There was a problem hiding this comment.
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.