-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilders.go
169 lines (158 loc) · 5.37 KB
/
builders.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"encoding/json"
"fmt"
"strings"
)
// GetObject returns an object, either from the cache, or live if possible
func GetObject(uri string) (obj *Object) {
//Try the cache first, it will update frequently during a live expansion
obj = GetObjectCached(uri)
if obj != nil {
return obj
}
//Fetch the object live
obj = GetObjectLive(uri)
if obj == nil {
return
}
obj.Sync()
return
}
// GetObjectLive returns a live object from a given URI
func GetObjectLive(mediaURI string) (obj *Object) {
if mediaURI == "" {
Error.Println("Cannot get object with empty mediaURI")
return nil
}
obj = &Object{URI: mediaURI, Object: &json.RawMessage{}}
Trace.Println("Fetching " + mediaURI + " live")
splitURI := strings.Split(mediaURI, ":")
switch splitURI[0] {
case "bestmatch": //Returns an object that best matches the given search query
if len(splitURI) < 2 {
return NewObjError("bestmatch: need query")
}
query := strings.ReplaceAll(splitURI[1], "+", " ")
query = strings.ReplaceAll(query, "%20", " ")
query = strings.ToLower(query)
searchResultsObj := GetObjectLive("search:" + splitURI[1])
if searchResultsObj.Type != "search" {
return searchResultsObj
}
searchResults := searchResultsObj.SearchResults()
if len(searchResults.Streams) > 0 {
return GetObjectLive(searchResults.Streams[0].Stream().URI)
}
if len(searchResults.Creators) > 0 {
return GetObjectLive(searchResults.Creators[0].Creator().URI)
}
if len(searchResults.Albums) > 0 {
return GetObjectLive(searchResults.Albums[0].Album().URI)
}
return NewObjError("bestmatch: try a better query")
case "search": //Main search handler
if len(splitURI) < 2 {
return NewObjError("search: need query")
}
query := strings.ReplaceAll(splitURI[1], "+", " ")
query = strings.ReplaceAll(query, "%20", " ")
query = strings.ToLower(query)
obj.URI = "search:" + query
results := &ObjectSearchResults{Query: query}
for i := 0; i < len(providers); i++ {
Trace.Println("Searching for '" + query + "' on " + providers[i])
handler := handlers[providers[i]]
res, err := handler.Search(query)
if err != nil {
Error.Printf("Error searching on %s: %v", providers[i], err)
continue
}
if len(res.Creators) > 0 {
results.Creators = append(results.Creators, res.Creators...)
}
if len(res.Albums) > 0 {
results.Albums = append(results.Albums, res.Albums...)
}
if len(res.Streams) > 0 {
results.Streams = append(results.Streams, res.Streams...)
}
}
obj.Type = "search"
obj.Provider = "libremedia"
resultsJSON, err := json.Marshal(results)
if err != nil {
Error.Printf("Unable to marshal search results: %v\n", err)
return NewObjError(fmt.Sprintf("invalid search %s: %v", query, err))
}
if err := obj.Object.UnmarshalJSON(resultsJSON); err != nil {
Error.Printf("Unable to unmarshal search results: %v\n", err)
return NewObjError(fmt.Sprintf("invalid search %s: %v", query, err))
}
return
}
if handler, exists := handlers[splitURI[0]]; exists {
obj.Provider = splitURI[0]
if len(splitURI) > 2 {
id := splitURI[2]
switch splitURI[1] {
case "artist", "creator", "user", "channel", "chan", "streamer":
creator, err := handler.Creator(id)
if err != nil {
Error.Printf("Invalid creator %s: %v\n", id, err)
return NewObjError(fmt.Sprintf("invalid creator %s: %v", id, err))
}
obj.Type = "creator"
creatorJSON, err := json.Marshal(creator)
if err != nil {
Error.Printf("Unable to marshal creator: %v\n", err)
return NewObjError(fmt.Sprintf("invalid creator %s: %v", id, err))
}
if err := obj.Object.UnmarshalJSON(creatorJSON); err != nil {
Error.Printf("Unable to unmarshal creator: %v\n", err)
return NewObjError(fmt.Sprintf("invalid creator %s: %v", id, err))
}
case "album":
Trace.Printf("Searching for album %s\n", mediaURI)
album, err := handler.Album(id)
if err != nil {
Error.Printf("Invalid album %s: %v\n", id, err)
return NewObjError(fmt.Sprintf("invalid album %s: %v", id, err))
}
Trace.Printf("Found album %s\n", mediaURI)
obj.Type = "album"
albumJSON, err := json.Marshal(album)
if err != nil {
Error.Printf("Unable to marshal album: %v\n", err)
return NewObjError(fmt.Sprintf("invalid album %s: %v", id, err))
}
if err := obj.Object.UnmarshalJSON(albumJSON); err != nil {
Error.Printf("Unable to unmarshal album: %v\n", err)
return NewObjError(fmt.Sprintf("invalid album %s: %v", id, err))
}
Trace.Printf("Successfully loaded album %s\n", mediaURI)
case "track", "song", "video", "audio", "stream":
stream, err := handler.Stream(id)
if err != nil {
Error.Printf("Invalid stream %s: %v\n", id, err)
return NewObjError(fmt.Sprintf("invalid stream %s: %v", id, err))
}
obj.Type = "stream"
stream.Transcribe()
streamJSON, err := json.Marshal(stream)
if err != nil {
Error.Printf("Unable to marshal stream: %v\n", err)
return NewObjError(fmt.Sprintf("invalid stream %s: %v", id, err))
}
if err := obj.Object.UnmarshalJSON(streamJSON); err != nil {
Error.Printf("Unable to unmarshal stream: %v\n", err)
return NewObjError(fmt.Sprintf("invalid stream %s: %v", id, err))
}
}
Info.Printf("Successfully found live object for %s\n", mediaURI)
return obj
}
}
Error.Printf("Failed to find live object for %s\n", mediaURI)
return nil
}