main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "log"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "strings"
  11. "github.com/JamZYM/golagrange/api"
  12. "github.com/joho/godotenv"
  13. )
  14. func msgProccess(msg map[string]interface{}, connBot api.WsBot) {
  15. // fmt.Println(msg)
  16. if msg["message_type"].(string) == "private" {
  17. user_id := int(msg["user_id"].(float64))
  18. // println(user_id)
  19. // fmt.Println(msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string))
  20. if msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string) == "text" {
  21. if strings.HasPrefix(msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string), "/bvdl ") {
  22. bv := strings.Replace(msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string), "/bvdl ", "", 1)
  23. // println(bv)
  24. // println(os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/info")
  25. // info, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/info", "application/json",
  26. // bytes.NewBuffer([]byte(fmt.Sprintf(`
  27. // {
  28. // "bv":%s
  29. // }
  30. // `, bv))))
  31. info, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/info?bv="+bv, "application/json", nil)
  32. if err != nil {
  33. connBot.Send_private_msg(user_id, "请求失败")
  34. fmt.Println(err)
  35. } else {
  36. defer info.Body.Close()
  37. var jsondata map[string]interface{}
  38. data, _ := io.ReadAll(info.Body)
  39. json.Unmarshal(data, &jsondata)
  40. // fmt.Println(string(data))
  41. title := jsondata["videos"].(map[string]interface{})["1"].(map[string]interface{})["title"].(string)
  42. // result, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/download", "application/json",
  43. // bytes.NewBuffer([]byte(fmt.Sprintf(`
  44. // {
  45. // "bv":%s;
  46. // "title":"%s"
  47. // }
  48. // `, bv, title))))
  49. fmt.Println("http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + bv + "&title=" + title)
  50. // result, err := http.PostForm("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/download?bv="+bv+"&title="+title, nil)
  51. qbv := url.QueryEscape(bv)
  52. qtitle := url.QueryEscape(title)
  53. geturl := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + qbv + "&title=" + qtitle
  54. result, err := http.Get(geturl)
  55. // url := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download"
  56. // body := fmt.Sprintf(`{"bv":"%s","title":"%s"}`, bv, title)
  57. // result, err := http.Post(url, "application/json", strings.NewReader(body))
  58. if err != nil {
  59. connBot.Send_private_msg(user_id, "下载失败")
  60. fmt.Println(err)
  61. } else {
  62. fmt.Print(result.Body)
  63. connBot.Send_private_msg(user_id, title)
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. func main() {
  71. err := godotenv.Load()
  72. if err != nil {
  73. log.Fatal("Error loading .env file")
  74. }
  75. connBot := api.WebsocketBot(os.Getenv("HOST"), os.Getenv("PORT"), os.Getenv("PATH"))
  76. defer connBot.Wsconn.Close()
  77. for {
  78. _, messageBytes, err := connBot.Wsconn.ReadMessage()
  79. if err != nil {
  80. log.Println("read:", err)
  81. return
  82. }
  83. var message map[string]interface{}
  84. json.Unmarshal(messageBytes, &message)
  85. if message["post_type"] == "message" {
  86. go msgProccess(message, connBot)
  87. } else if message["post_type"] == "meta_event" {
  88. }
  89. }
  90. // connBot.Send_private_msg(2945340446, "Hello, world!")
  91. // err = connBot.Upload_private_file(2945340446, "/home/jam/Documents/College/大学学习/大创/基于gm_Id方法的跨阻放大器设计_林佳辉.pdf", "基于gm_Id方法的跨阻放大器设计_林佳辉.pdf")
  92. // fmt.Println(err)
  93. }