-
Notifications
You must be signed in to change notification settings - Fork 1
/
playlist_lcdz.go
61 lines (50 loc) · 1.31 KB
/
playlist_lcdz.go
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
package main
import (
"fmt"
"net/http"
"regexp"
"golang.org/x/net/html"
)
func NewPlaylistLCDZ() (*Playlist, error) {
urlSource := "https://livecode.demozoo.org/type/Byte_Jam.html"
resp, err := http.Get(urlSource)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("LCDZ page responded with [%s] rather than 200", resp.Status)
}
doc, err := html.Parse(resp.Body)
if err != nil {
return nil, err
}
p := NewPlaylist()
links := findAllLuaLinks(doc)
if len(links) == 0 {
return nil, fmt.Errorf("LCDZ page responded, but no lua files found")
}
for _, link := range links {
p.items = append(p.items, PlaylistItem{location: link})
}
p.order = ORDER_RANDOM
return p, nil
}
func findAllLuaLinks(n *html.Node) []string {
var links []string
re := regexp.MustCompile(`.lua$`)
if n.Type == html.ElementNode && n.Data == "a" {
for _, element := range n.Attr {
if element.Key == "href" {
if re.MatchString(element.Val) {
links = append(links, fmt.Sprintf("https://livecode.demozoo.org%s", element.Val))
}
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
newLinks := findAllLuaLinks(c)
links = append(links, newLinks...)
}
return links
}