-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss_feed.go
More file actions
139 lines (116 loc) · 2.66 KB
/
rss_feed.go
File metadata and controls
139 lines (116 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"regexp"
"strings"
)
type RSS struct {
Channel Channel `xml:"channel"`
}
type Channel struct {
Items []Item `xml:"item"`
}
type Item struct {
Title string `xml:"title"`
Description string `xml:"description"`
PubDate string `xml:"pubDate"`
}
type Song struct {
Title string
Source string
Artist string
}
type TimeSlot struct {
Time string
Songs []Song
}
type ConcertDay struct {
Title string
TimeSlots []TimeSlot
}
// matches HTML tag
var tagPattern = regexp.MustCompile(`<[^>]+>`)
// removes all HTML tags from a string
func stripTags(s string) string {
return strings.TrimSpace(tagPattern.ReplaceAllString(s, ""))
}
var originPattern = regexp.MustCompile(`\(from "([^"]+)"\)`)
func parseSong(line string) Song {
song := Song{}
title, artist, found := strings.Cut(line, " / ")
if found {
song.Artist = strings.TrimSpace(artist)
}
match := originPattern.FindStringSubmatch(title)
if match != nil {
song.Source = match[1]
title = strings.TrimSpace(originPattern.ReplaceAllString(title, ""))
}
song.Title = title
return song
}
// converts HTML into TimeSlot structure
func parseDescription(desc string) []TimeSlot {
var slots []TimeSlot
var current *TimeSlot
var lines []string
for _, current := range strings.Split(desc, "<br>") {
for _, line := range strings.Split(current, "\n") {
lines = append(lines, line)
}
}
for _, line := range lines {
text := stripTags(line)
if text == "" {
continue
}
isHeader := false
switch text {
case "Morning", "Afternoon", "Evening":
isHeader = true
}
if isHeader {
slots = append(slots, TimeSlot{Time: text})
current = &slots[len(slots)-1]
} else if current != nil {
current.Songs = append(current.Songs, parseSong(text))
}
}
return slots
}
func main() {
resp, err := http.Get("https://apps.chimes.cornell.edu/music/rss.xml")
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var rss RSS
if err := xml.Unmarshal(data, &rss); err != nil {
panic(err)
}
var allConcerts []ConcertDay
for _, item := range rss.Channel.Items {
cleanHTML := strings.NewReplacer("<", "<", ">", ">", "&", "&").Replace(item.Description)
day := ConcertDay{
Title: item.Title,
TimeSlots: parseDescription(cleanHTML),
}
allConcerts = append(allConcerts, day)
}
for _, day := range allConcerts {
fmt.Println("=====", day.Title, "=====")
for _, slot := range day.TimeSlots {
fmt.Println(slot.Time)
for _, song := range slot.Songs {
fmt.Printf("%q by %q from %q\n", song.Title, song.Artist, song.Source)
}
}
}
}