Browse Source

规范化function,进行模块化,方便进一步开发

JamZYM 7 months ago
parent
commit
2360bbfd8a
2 changed files with 48 additions and 56 deletions
  1. 42 0
      function/slash.go
  2. 6 56
      main.go

+ 42 - 0
function/slash.go

@@ -0,0 +1,42 @@
+package function
+
+import (
+	"encoding/json"
+	"fmt"
+	"io"
+	"net/http"
+	"net/url"
+	"os"
+	"strings"
+
+	"github.com/JamZYM/golagrange/api"
+)
+
+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)
+		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)
+
+		if err != nil {
+			connBot.Send_private_msg(user_id, "下载失败")
+			fmt.Println(err)
+		} else {
+			fmt.Print(result.Body)
+			connBot.Send_private_msg(user_id, title)
+		}
+	}
+}

+ 6 - 56
main.go

@@ -2,15 +2,12 @@ package main
 
 import (
 	"encoding/json"
-	"fmt"
-	"io"
 	"log"
-	"net/http"
-	"net/url"
 	"os"
 	"strings"
 
 	"github.com/JamZYM/golagrange/api"
+	"github.com/JamZYM/golagrange/function"
 	"github.com/joho/godotenv"
 )
 
@@ -18,58 +15,11 @@ func msgProccess(msg map[string]interface{}, connBot api.WsBot) {
 	// fmt.Println(msg)
 	if msg["message_type"].(string) == "private" {
 		user_id := int(msg["user_id"].(float64))
-		// println(user_id)
-		// fmt.Println(msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string))
-		if msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string) == "text" {
-			if strings.HasPrefix(msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string), "/bvdl ") {
-				bv := strings.Replace(msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string), "/bvdl ", "", 1)
-				// println(bv)
-				// println(os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/info")
-				// info, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/info", "application/json",
-				// 	bytes.NewBuffer([]byte(fmt.Sprintf(`
-				// 	{
-				// 		"bv":%s
-				// 	}
-				// 	`, bv))))
-				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)
-					// fmt.Println(string(data))
-					title := jsondata["videos"].(map[string]interface{})["1"].(map[string]interface{})["title"].(string)
-					// result, err := http.Post("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/download", "application/json",
-					// 	bytes.NewBuffer([]byte(fmt.Sprintf(`
-					// {
-					// 	"bv":%s;
-					// 	"title":"%s"
-					// }
-					// `, bv, title))))
-					fmt.Println("http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download?bv=" + bv + "&title=" + title)
-					// result, err := http.PostForm("http://"+os.Getenv("HOST")+":"+os.Getenv("BVDLPORT")+"/api/download?bv="+bv+"&title="+title, nil)
-
-					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)
-
-					// url := "http://" + os.Getenv("HOST") + ":" + os.Getenv("BVDLPORT") + "/api/download"
-					// body := fmt.Sprintf(`{"bv":"%s","title":"%s"}`, bv, title)
-					// result, err := http.Post(url, "application/json", strings.NewReader(body))
-
-					if err != nil {
-						connBot.Send_private_msg(user_id, "下载失败")
-						fmt.Println(err)
-					} else {
-						fmt.Print(result.Body)
-						connBot.Send_private_msg(user_id, title)
-					}
-
-				}
+		msg_type := msg["message"].([]interface{})[0].(map[string]interface{})["type"].(string)
+		if msg_type == "text" {
+			msg_text := msg["message"].([]interface{})[0].(map[string]interface{})["data"].(map[string]interface{})["text"].(string)
+			if strings.HasPrefix(msg_text, "/bvdl ") {
+				function.Func_bvdl(msg_text, connBot, user_id)
 			}
 		}
 	}