slash.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package function
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "log"
  7. "net/http"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/JamZYM/golagrange/api"
  13. "github.com/shirou/gopsutil/v4/cpu"
  14. "github.com/shirou/gopsutil/v4/disk"
  15. "github.com/shirou/gopsutil/v4/mem"
  16. )
  17. func Func_bvdl(msg_text string, connBot api.WsBot, user_id int) {
  18. bv := strings.Replace(msg_text, "/bvdl ", "", 1)
  19. info, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/info?bv="+bv, "application/json", nil)
  20. if err != nil {
  21. connBot.Send_private_msg(user_id, "请求失败")
  22. fmt.Println(err)
  23. } else {
  24. defer info.Body.Close()
  25. var jsondata map[string]interface{}
  26. data, _ := io.ReadAll(info.Body)
  27. json.Unmarshal(data, &jsondata)
  28. if jsondata["isSingle"].(bool) {
  29. title := jsondata["videos"].(map[string]interface{})["1"].(map[string]interface{})["title"].(string)
  30. fmt.Println("http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + bv + "&title=" + title)
  31. // qbv := url.QueryEscape(bv)
  32. qbv := bv
  33. // qtitle := url.QueryEscape(title)
  34. qtitle := title
  35. geturl := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + qbv + "&title=" + qtitle
  36. result, err := http.Get(geturl)
  37. if err != nil {
  38. connBot.Send_private_msg(user_id, "下载失败")
  39. fmt.Println(err)
  40. } else {
  41. fmt.Print(result.Body)
  42. connBot.Send_private_msg(user_id, title)
  43. }
  44. } else {
  45. connBot.Send_private_msg(user_id, "该视频包含多个合集,请选择需要下载的集数(0为全选)")
  46. videoList := make(map[int]interface{})
  47. for page, video := range jsondata["videos"].(map[string]interface{}) {
  48. title := video.(map[string]interface{})["title"].(string)
  49. // qbv := url.QueryEscape(bv + "?p=" + page)
  50. qbv := bv + "?p=" + page
  51. // qtitle := url.QueryEscape(title)
  52. qtitle := title
  53. geturl := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + qbv + "&title=" + qtitle
  54. // fmt.Println(geturl)
  55. pageInt, err := strconv.Atoi(page)
  56. if err != nil {
  57. connBot.Send_private_msg(user_id, "下载失败")
  58. fmt.Println(err)
  59. break
  60. }
  61. if videoList[pageInt] == nil {
  62. videoList[pageInt] = make(map[string]interface{})
  63. }
  64. videoList[pageInt].(map[string]interface{})["title"] = title
  65. videoList[pageInt].(map[string]interface{})["geturl"] = geturl
  66. }
  67. for page := 1; page <= len(videoList); page++ {
  68. connBot.Send_private_msg(user_id, fmt.Sprintf("%v. %v", page, videoList[page].(map[string]interface{})["title"].(string)))
  69. }
  70. timeout := false
  71. go func() {
  72. time.Sleep(90 * time.Second)
  73. timeout = true
  74. }()
  75. temp_connBot := api.WebsocketBot(os.Getenv("HOST"), os.Getenv("PORT"), os.Getenv("PATH"))
  76. defer temp_connBot.Wsconn.Close()
  77. for {
  78. if timeout {
  79. fmt.Println("timeout")
  80. temp_connBot.Send_private_msg(user_id, "回复超时,取消下载")
  81. break
  82. }
  83. time.Sleep(1 * time.Second)
  84. _, messageBytes, err := temp_connBot.Wsconn.ReadMessage()
  85. if err != nil {
  86. log.Println("read:", err)
  87. return
  88. }
  89. var message map[string]interface{}
  90. json.Unmarshal(messageBytes, &message)
  91. if message["post_type"] == "message" {
  92. if (message["message_type"].(string) == "private") && (user_id == int(message["user_id"].(float64))) && (message["message"].([]interface{})[0].(map[string]interface{})["type"].(string) == "text") {
  93. msg_text := message["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string)
  94. if msg_text == "0" {
  95. temp_connBot.Send_private_msg(user_id, "即将下载全部分集")
  96. for page := 1; page <= len(videoList); page++ {
  97. _, err = http.Get(videoList[page].(map[string]interface{})["geturl"].(string))
  98. if err != nil {
  99. connBot.Send_private_msg(user_id, "下载失败")
  100. fmt.Println(err)
  101. }
  102. // fmt.Println(videoList[page].(map[string]interface{})["geturl"].(string))
  103. }
  104. temp_connBot.Send_private_msg(user_id, "下载完成")
  105. break
  106. }
  107. }
  108. } else {
  109. }
  110. }
  111. }
  112. }
  113. }
  114. func Func_serverStatus(connBot api.WsBot, user_id int) {
  115. _mem, err := mem.VirtualMemory()
  116. _cpu, _ := cpu.Percent(time.Second, false)
  117. _disk, _ := disk.Usage("/")
  118. // _net, _ := net.IOCounters(true)
  119. // response_text := fmt.Sprintf("内存: %v%%,\nCPU:%v%%,\n磁盘:%v%%", fmt.Sprintf("%.1f", _mem.UsedPercent), fmt.Sprintf("%.1f", _cpu[0]), fmt.Sprintf("%.1f", _disk.UsedPercent))
  120. response_text := fmt.Sprintf("内存: %v%%,CPU:%v%%,磁盘:%v%%", fmt.Sprintf("%.1f", _mem.UsedPercent), fmt.Sprintf("%.1f", _cpu[0]), fmt.Sprintf("%.1f", _disk.UsedPercent))
  121. println(response_text)
  122. if err != nil {
  123. connBot.Send_private_msg(user_id, "服务器异常")
  124. } else {
  125. connBot.Send_private_msg(user_id, "服务器状态:")
  126. connBot.Send_private_msg(user_id, response_text)
  127. }
  128. }