123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package function
- import (
- "encoding/json"
- "fmt"
- "io"
- "log"
- "net/http"
- "os"
- "strconv"
- "strings"
- "time"
- "github.com/JamZYM/golagrange/api"
- "github.com/shirou/gopsutil/v4/cpu"
- "github.com/shirou/gopsutil/v4/disk"
- "github.com/shirou/gopsutil/v4/mem"
- )
- func Func_bvdl(msg_text string, connBot api.WsBot, user_id int) {
- bv := strings.Replace(msg_text, "/bvdl ", "", 1)
- info, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/info?bv="+bv, "application/json", nil)
- if err != nil {
- connBot.Send_private_msg(user_id, "请求失败")
- fmt.Println(err)
- } else {
- defer info.Body.Close()
- var jsondata map[string]interface{}
- data, _ := io.ReadAll(info.Body)
- json.Unmarshal(data, &jsondata)
- if jsondata["isSingle"].(bool) {
- title := jsondata["videos"].(map[string]interface{})["1"].(map[string]interface{})["title"].(string)
- fmt.Println("http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + bv + "&title=" + title)
- // qbv := url.QueryEscape(bv)
- qbv := bv
- // qtitle := url.QueryEscape(title)
- qtitle := title
- geturl := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + qbv + "&title=" + qtitle
- result, err := http.Get(geturl)
- if err != nil {
- connBot.Send_private_msg(user_id, "下载失败")
- fmt.Println(err)
- } else {
- fmt.Print(result.Body)
- connBot.Send_private_msg(user_id, title)
- }
- } else {
- connBot.Send_private_msg(user_id, "该视频包含多个合集,请选择需要下载的集数(0为全选)")
- videoList := make(map[int]interface{})
- for page, video := range jsondata["videos"].(map[string]interface{}) {
- title := video.(map[string]interface{})["title"].(string)
- // qbv := url.QueryEscape(bv + "?p=" + page)
- qbv := bv + "?p=" + page
- // qtitle := url.QueryEscape(title)
- qtitle := title
- geturl := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + qbv + "&title=" + qtitle
- // fmt.Println(geturl)
- pageInt, err := strconv.Atoi(page)
- if err != nil {
- connBot.Send_private_msg(user_id, "下载失败")
- fmt.Println(err)
- break
- }
- if videoList[pageInt] == nil {
- videoList[pageInt] = make(map[string]interface{})
- }
- videoList[pageInt].(map[string]interface{})["title"] = title
- videoList[pageInt].(map[string]interface{})["geturl"] = geturl
- }
- for page := 1; page <= len(videoList); page++ {
- connBot.Send_private_msg(user_id, fmt.Sprintf("%v. %v", page, videoList[page].(map[string]interface{})["title"].(string)))
- }
- timeout := false
- go func() {
- time.Sleep(90 * time.Second)
- timeout = true
- }()
- temp_connBot := api.WebsocketBot(os.Getenv("HOST"), os.Getenv("PORT"), os.Getenv("PATH"))
- defer temp_connBot.Wsconn.Close()
- for {
- if timeout {
- fmt.Println("timeout")
- temp_connBot.Send_private_msg(user_id, "回复超时,取消下载")
- break
- }
- time.Sleep(1 * time.Second)
- _, messageBytes, err := temp_connBot.Wsconn.ReadMessage()
- if err != nil {
- log.Println("read:", err)
- return
- }
- var message map[string]interface{}
- json.Unmarshal(messageBytes, &message)
- if message["post_type"] == "message" {
- if (message["message_type"].(string) == "private") && (user_id == int(message["user_id"].(float64))) && (message["message"].([]interface{})[0].(map[string]interface{})["type"].(string) == "text") {
- msg_text := message["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string)
- if msg_text == "0" {
- temp_connBot.Send_private_msg(user_id, "即将下载全部分集")
- for page := 1; page <= len(videoList); page++ {
- _, err = http.Get(videoList[page].(map[string]interface{})["geturl"].(string))
- if err != nil {
- connBot.Send_private_msg(user_id, "下载失败")
- fmt.Println(err)
- }
- // fmt.Println(videoList[page].(map[string]interface{})["geturl"].(string))
- }
- temp_connBot.Send_private_msg(user_id, "下载完成")
- break
- }
- }
- } else {
- }
- }
- }
- }
- }
- func Func_serverStatus(connBot api.WsBot, user_id int) {
- _mem, err := mem.VirtualMemory()
- _cpu, _ := cpu.Percent(time.Second, false)
- _disk, _ := disk.Usage("/")
- // _net, _ := net.IOCounters(true)
- // response_text := fmt.Sprintf("内存: %v%%,\nCPU:%v%%,\n磁盘:%v%%", fmt.Sprintf("%.1f", _mem.UsedPercent), fmt.Sprintf("%.1f", _cpu[0]), fmt.Sprintf("%.1f", _disk.UsedPercent))
- response_text := fmt.Sprintf("内存: %v%%,CPU:%v%%,磁盘:%v%%", fmt.Sprintf("%.1f", _mem.UsedPercent), fmt.Sprintf("%.1f", _cpu[0]), fmt.Sprintf("%.1f", _disk.UsedPercent))
- println(response_text)
- if err != nil {
- connBot.Send_private_msg(user_id, "服务器异常")
- } else {
- connBot.Send_private_msg(user_id, "服务器状态:")
- connBot.Send_private_msg(user_id, response_text)
- }
- }
|