diff --git a/main.go b/main.go index c7c64bc..6a8604d 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "encoding/json" "flag" "fmt" + "html/template" "io" "log" "net/http" @@ -176,6 +177,71 @@ func main() { } }) + mux.HandleFunc("GET /glance", func(w http.ResponseWriter, r *http.Request) { + type glanceTorrent struct { + Title string + } + + type glanceItem struct { + Query string + Link string + Torrents []glanceTorrent + } + + glanceItems := []glanceItem{} + + items, err := m.GetItems() + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + baseUrl := r.URL.Query().Get("base_url") + + for _, item := range items { + gItem := glanceItem{ + Query: item.Query, + Link: fmt.Sprintf("%s?item=%d", baseUrl, item.ID), + Torrents: []glanceTorrent{}, + } + torrents, err := m.GetItemTorrents(item.ID) + if err != nil { + continue + } + + for _, torrent := range torrents { + gItem.Torrents = append(gItem.Torrents, glanceTorrent{ + Title: torrent.Title, + }) + } + + glanceItems = append(glanceItems, gItem) + } + + tmpl := ` + + ` + + t, err := template.New("glance").Parse(tmpl) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + w.Header().Set("Widget-Title", "tvqueue") + w.Header().Set("Widget-Content-Type", "html") + + t.ExecuteTemplate(w, "glance", struct { + Items []glanceItem + }{ + Items: glanceItems, + }) + }) + mux.HandleFunc("POST /api/items", func(w http.ResponseWriter, r *http.Request) { var body struct { Query string