slash.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package function
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "strings"
  10. "time"
  11. "github.com/JamZYM/golagrange/api"
  12. "github.com/shirou/gopsutil/v4/cpu"
  13. "github.com/shirou/gopsutil/v4/disk"
  14. "github.com/shirou/gopsutil/v4/mem"
  15. )
  16. func Func_bvdl(msg_text string, connBot api.WsBot, user_id int) {
  17. bv := strings.Replace(msg_text, "/bvdl ", "", 1)
  18. info, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/info?bv="+bv, "application/json", nil)
  19. if err != nil {
  20. connBot.Send_private_msg(user_id, "请求失败")
  21. fmt.Println(err)
  22. } else {
  23. defer info.Body.Close()
  24. var jsondata map[string]interface{}
  25. data, _ := io.ReadAll(info.Body)
  26. json.Unmarshal(data, &jsondata)
  27. title := jsondata["videos"].(map[string]interface{})["1"].(map[string]interface{})["title"].(string)
  28. fmt.Println("http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + bv + "&title=" + title)
  29. qbv := url.QueryEscape(bv)
  30. qtitle := url.QueryEscape(title)
  31. geturl := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + qbv + "&title=" + qtitle
  32. result, err := http.Get(geturl)
  33. if err != nil {
  34. connBot.Send_private_msg(user_id, "下载失败")
  35. fmt.Println(err)
  36. } else {
  37. fmt.Print(result.Body)
  38. connBot.Send_private_msg(user_id, title)
  39. }
  40. }
  41. }
  42. func Func_serverStatus(connBot api.WsBot, user_id int) {
  43. _mem, err := mem.VirtualMemory()
  44. _cpu, _ := cpu.Percent(time.Second, false)
  45. _disk, _ := disk.Usage("/")
  46. // _net, _ := net.IOCounters(true)
  47. response_text := fmt.Sprintf("内存: %v%%,\nCPU:%v%%,\n磁盘:%v%%", fmt.Sprintf("%.1f", _mem.UsedPercent), fmt.Sprintf("%.1f", _cpu[0]), fmt.Sprintf("%.1f", _disk.UsedPercent))
  48. print(response_text)
  49. if err != nil {
  50. connBot.Send_private_msg(user_id, "服务器异常")
  51. } else {
  52. connBot.Send_private_msg(user_id, response_text)
  53. }
  54. }