api.go 825 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/JamZYM/biliMusic/download"
  6. )
  7. func ApiInfo(w http.ResponseWriter, r *http.Request) {
  8. r.ParseForm()
  9. bv := r.Form.Get("bv")
  10. info := download.MusicInfo(bv)
  11. jsonData, err := json.Marshal(info)
  12. if err != nil {
  13. panic(err)
  14. }
  15. w.Header().Set("Content-Type", "application/json")
  16. w.Write(jsonData)
  17. }
  18. func ApiDownload(w http.ResponseWriter, r *http.Request) {
  19. r.ParseForm()
  20. bv := r.Form.Get("bv")
  21. title := r.Form.Get("title")
  22. url_list := download.MusicUrl(bv)
  23. result := download.MusicDownload(url_list, title)
  24. if result {
  25. w.Write([]byte("success"))
  26. return
  27. } else {
  28. w.Write([]byte("fail"))
  29. return
  30. }
  31. }
  32. func StartServer() {
  33. http.HandleFunc("/api/download", ApiDownload)
  34. http.HandleFunc("/api/info", ApiInfo)
  35. http.ListenAndServe(":5000", nil)
  36. }