Files
podcast-api/main.go
2026-03-05 13:22:00 +03:00

213 lines
5.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"log"
"net/http"
"subsonic-api/subsonic"
"time"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("GET /rest/ping", func(w http.ResponseWriter, r *http.Request) {
err := subsonic.VerifyUser(r, "daniil", "Daniil2000")
if err != nil {
subsonic.SendHTTPResponse(w, r, subsonic.ErrorResponse{
Response: subsonic.Response{
Status: "failed",
Version: "1.16.1",
},
Error: subsonic.ErrorWrongAuth,
}, 401)
return
}
subsonic.SendHTTPResponse(w, r, subsonic.PingResponse{
Response: subsonic.Response{
Status: "ok",
Version: "1.16.1",
},
}, 200)
})
mux.HandleFunc("GET /rest/getLicense", func(w http.ResponseWriter, r *http.Request) {
subsonic.SendHTTPResponse(w, r, subsonic.LicenseResponse{
Response: subsonic.Response{
Status: "ok",
Version: "1.16.1",
},
License: subsonic.License{
Valid: true,
Email: "daniil@tsivinsky.com",
Expires: time.Now().Add(time.Hour * 24 * 365),
},
}, 200)
})
mux.HandleFunc("GET /rest/getMusicFolders", func(w http.ResponseWriter, r *http.Request) {
subsonic.SendHTTPResponse(w, r, subsonic.MusicFoldersResponse{
Response: subsonic.Response{
Status: "ok",
Version: "1.16.1",
},
MusicFolders: []subsonic.MusicFolder{
{
ID: 1,
Name: "Music",
},
{
ID: 2,
Name: "Movies",
},
{
ID: 3,
Name: "Incoming",
},
},
}, 200)
})
mux.HandleFunc("GET /rest/getPodcasts", func(w http.ResponseWriter, r *http.Request) {
subsonic.SendHTTPResponse(w, r, subsonic.PodcastsResponse{
Response: subsonic.Response{
Status: "ok",
Version: "1.16.1",
},
Podcasts: []subsonic.Podcast{
{
ID: 1,
URL: "http://downloads.bbc.co.uk/podcasts/fivelive/drkarl/rss.xml",
Title: "Dr Karl and the Naked Scientist",
Description: "Dr Chris Smith aka The Naked Scientist with the latest news from the world of science and Dr Karl answers listeners' science questions.",
CoverArt: "pod-1",
OriginalImageURL: "http://downloads.bbc.co.uk/podcasts/fivelive/drkarl/drkarl.jpg",
Status: "completed",
Episodes: []subsonic.Episode{
{
ID: 34,
StreamID: 523,
ChannelID: 1,
Title: "Scorpions have re-evolved eyes",
Description: "This week Dr Chris fills us in on the UK's largest free science festival, plus all this week's big scientific discoveries.",
PublishDate: time.Now(),
Status: "completed",
Parent: 11,
IsDir: false,
Year: 2011,
Genre: "Podcast",
CoverArt: 24,
Size: 78421341,
ContentType: "audio/mpeg",
Suffix: "mp3",
Duration: 3146,
Bitrate: 128,
Path: "Podcast/drkarl/20110203.mp3",
},
},
},
{
ID: 3,
URL: "http://foo.bar.com/xyz.rss",
Status: "error",
ErrorMessage: "Not found.",
},
},
}, 200)
})
mux.HandleFunc("GET /rest/getNewestPodcasts", func(w http.ResponseWriter, r *http.Request) {
subsonic.SendHTTPResponse(w, r, subsonic.NewestPodcastsResponse{
Response: subsonic.Response{
Status: "ok",
Version: "1.16.1",
},
NewestPodcasts: []subsonic.NewestEpisode{
{
ID: 7389,
Parent: 7389,
IsDir: false,
Title: "Jonas Gahr Støre",
Album: "NRK Hallo P3",
Artist: "Podcast",
Year: 2015,
CoverArt: 7389,
Size: 41808585,
ContentType: "audio/mpeg",
Suffix: "mp3",
Duration: 2619,
Bitrate: 128,
IsVideo: false,
Created: time.Now(),
ArtistID: 453,
Type: "podcast",
StreamID: 7410,
ChannelID: 17,
Description: "Jonas Gahr Støre fra Arbeiderpartiet er med i dagens partilederutspørring i Hallo P3! Han svarer på det som lytterne lurer på, alt fra bomring i Bodø til flyktningkrisen. I tillegg til dette rapporterer Reporter Silje Ese fra den greske øya Kos der det nå er flere tusen flyktninger. Og så skal vi ta farvel med et svært utrydningstruet dyr.",
Status: "completed",
PublishDate: time.Now(),
},
},
}, 200)
})
mux.HandleFunc("GET /rest/refreshPodcasts", func(w http.ResponseWriter, r *http.Request) {
subsonic.SendHTTPResponse(w, r, subsonic.RefreshPodcastsResponse{
Response: subsonic.Response{
Status: "ok",
Version: "1.16.1",
},
}, 200)
})
mux.HandleFunc("GET /rest/createPodcastChannel", func(w http.ResponseWriter, r *http.Request) {
subsonic.SendHTTPResponse(w, r, subsonic.CreatePodcastResponse{
Response: subsonic.Response{
Status: "ok",
Version: "1.16.1",
},
}, 200)
})
mux.HandleFunc("GET /rest/deletePodcastChannel", func(w http.ResponseWriter, r *http.Request) {
subsonic.SendHTTPResponse(w, r, subsonic.DeletePodcastResponse{
Response: subsonic.Response{
Status: "ok",
Version: "1.16.1",
},
}, 200)
})
mux.HandleFunc("GET /rest/deletePodcastEpisode", func(w http.ResponseWriter, r *http.Request) {
subsonic.SendHTTPResponse(w, r, subsonic.DeleteEpisodeResponse{
Response: subsonic.Response{
Status: "ok",
Version: "1.16.1",
},
}, 200)
})
mux.HandleFunc("GET /rest/downloadPodcastEpisode", func(w http.ResponseWriter, r *http.Request) {
subsonic.SendHTTPResponse(w, r, subsonic.DownloadEpisodeResponse{
Response: subsonic.Response{
Status: "ok",
Version: "1.16.1",
},
}, 200)
})
mux.HandleFunc("GET /rest/error", func(w http.ResponseWriter, r *http.Request) {
subsonic.SendHTTPResponse(w, r, subsonic.ErrorResponse{
Response: subsonic.Response{
Status: "failed",
Version: "1.16.1",
},
Error: subsonic.ErrorWrongAuth,
}, 500)
})
if err := http.ListenAndServe(":5000", mux); err != nil {
log.Fatalf("failed to start http server: %v", err)
}
}