Selaa lähdekoodia

feat: bvdl增加集合下载功能,但目前只能全部下载

JamZYM 5 kuukautta sitten
vanhempi
commit
29a5773330
1 muutettua tiedostoa jossa 92 lisäystä ja 12 poistoa
  1. 92 12
      function/slash.go

+ 92 - 12
function/slash.go

@@ -4,9 +4,10 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
+	"log"
 	"net/http"
-	"net/url"
 	"os"
+	"strconv"
 	"strings"
 	"time"
 
@@ -27,20 +28,99 @@ func Func_bvdl(msg_text string, connBot api.WsBot, user_id int) {
 		var jsondata map[string]interface{}
 		data, _ := io.ReadAll(info.Body)
 		json.Unmarshal(data, &jsondata)
-		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)
+		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)
-		qtitle := url.QueryEscape(title)
-		geturl := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + qbv + "&title=" + qtitle
-		result, err := http.Get(geturl)
+			// 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)
+			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 {
-			fmt.Print(result.Body)
-			connBot.Send_private_msg(user_id, title)
+			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 {
+
+				}
+			}
 		}
 	}
 }