Merge branch 'main' of ssh://100.75.27.65:2222/lixiangwuxian/qq_bot

This commit is contained in:
lixiangwuxian
2025-03-10 17:44:49 +08:00
56 changed files with 1988 additions and 584 deletions

22
util/automatic.go Normal file
View File

@@ -0,0 +1,22 @@
package util
import (
"errors"
"os"
"os/exec"
)
func GitPull() error {
workDir, err := os.Getwd()
if err != nil {
return err
}
cmd := exec.Command("git", "pull")
cmd.Dir = workDir
output, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(output) + err.Error())
}
return nil
}

View File

@@ -0,0 +1,7 @@
package util
import "fmt"
func GenTempFilePath(fileName string) string {
return fmt.Sprintf("/tmp/qqbot/%s", fileName)
}

166
util/picture.go Normal file
View File

@@ -0,0 +1,166 @@
package util
import (
"errors"
"image"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"strings"
"github.com/nfnt/resize"
)
func ResizeImage(imagePath string, width int, height int) (outputPath string, err error) {
// 打开源图片文件
file, err := os.Open(imagePath)
if err != nil {
return "", err
}
defer file.Close()
// 解码图片
var img image.Image
var decodeErr error
ext := strings.ToLower(filepath.Ext(imagePath))
switch ext {
case ".jpg", ".jpeg":
img, decodeErr = jpeg.Decode(file)
case ".png":
img, decodeErr = png.Decode(file)
default:
return "", errors.New("unsupported image format")
}
if decodeErr != nil {
return "", decodeErr
}
// 调整图片大小
resized := resize.Resize(uint(width), uint(height), img, resize.Lanczos3)
// 创建输出文件
outputPath = strings.TrimSuffix(imagePath, ext) + "_resized" + ext
out, err := os.Create(outputPath)
if err != nil {
return "", err
}
defer out.Close()
// 根据文件扩展名选择编码方式
switch ext {
case ".jpg", ".jpeg":
return outputPath, jpeg.Encode(out, resized, nil)
case ".png":
return outputPath, png.Encode(out, resized)
default:
return "", errors.New("unsupported image format")
}
}
// ResizeImageByMaxWidth 按最大宽度缩放图片,保持宽高比
func ResizeImageByMaxWidth(imagePath string, maxWidth uint) (outputPath string, err error) {
// 打开源图片文件
file, err := os.Open(imagePath)
if err != nil {
return "", err
}
defer file.Close()
// 解码图片
var img image.Image
var decodeErr error
ext := strings.ToLower(filepath.Ext(imagePath))
switch ext {
case ".jpg", ".jpeg":
img, decodeErr = jpeg.Decode(file)
case ".png":
img, decodeErr = png.Decode(file)
default:
return "", errors.New("unsupported image format")
}
if decodeErr != nil {
return "", decodeErr
}
// 计算缩放后的尺寸,保持宽高比
// 传入0作为高度resize包会自动计算等比例的高度
resized := resize.Resize(maxWidth, 0, img, resize.Lanczos3)
// 创建输出文件
outputPath = strings.TrimSuffix(imagePath, ext) + "_resized" + ext
out, err := os.Create(outputPath)
if err != nil {
return "", err
}
defer out.Close()
// 根据文件扩展名选择编码方式
switch ext {
case ".jpg", ".jpeg":
return outputPath, jpeg.Encode(out, resized, nil)
case ".png":
return outputPath, png.Encode(out, resized)
default:
return "", errors.New("unsupported image format")
}
}
// ResizeImageByMaxHeight 按最大高度缩放图片,保持宽高比
func ResizeImageByMaxHeight(imagePath string, maxHeight uint) (outputPath string, err error) {
// 打开源图片文件
file, err := os.Open(imagePath)
if err != nil {
return "", err
}
defer file.Close()
// 解码图片
var img image.Image
var decodeErr error
ext := strings.ToLower(filepath.Ext(imagePath))
switch ext {
case ".jpg", ".jpeg":
img, decodeErr = jpeg.Decode(file)
case ".png":
img, decodeErr = png.Decode(file)
default:
return "", errors.New("unsupported image format")
}
if decodeErr != nil {
return "", decodeErr
}
// 计算缩放后的尺寸,保持宽高比
// 传入0作为宽度resize包会自动计算等比例的宽度
resized := resize.Resize(0, maxHeight, img, resize.Lanczos3)
// 创建输出文件
outputPath = strings.TrimSuffix(imagePath, ext) + "_resized" + ext
out, err := os.Create(outputPath)
if err != nil {
return "", err
}
defer out.Close()
// 根据文件扩展名选择编码方式
switch ext {
case ".jpg", ".jpeg":
return outputPath, jpeg.Encode(out, resized, nil)
case ".png":
return outputPath, png.Encode(out, resized)
default:
return "", errors.New("unsupported image format")
}
}
func GetResizedIamgePathByOrgPath(orgPath string) string {
ext := strings.ToLower(filepath.Ext(orgPath))
return strings.TrimSuffix(orgPath, ext) + "_resized" + ext
}

57
util/song_id.go Normal file
View File

@@ -0,0 +1,57 @@
package util
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
func GetSongIdsByHash(hashs []string) (hashToSongId map[string]string, err error) {
if len(hashs) == 0 {
return nil, nil
}
//每批最多49个
hashToSongId = make(map[string]string)
batchSize := 49
for i := 0; i < len(hashs); i += batchSize {
end := i + batchSize
if end > len(hashs) {
end = len(hashs)
}
batchHashs := hashs[i:end]
queryUrl := "https://api.beatsaver.com/maps/hash/" + strings.Join(batchHashs, ",")
resp, err := http.Get(queryUrl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("获取歌曲ID失败状态码%d,url:%s", resp.StatusCode, queryUrl)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var response = make(map[string]struct {
ID string `json:"id"`
})
if len(batchHashs) == 1 {
var singleResponse struct {
ID string `json:"id"`
}
err = json.Unmarshal(body, &singleResponse)
response[batchHashs[0]] = singleResponse
} else {
err = json.Unmarshal(body, &response)
}
if err != nil {
return nil, err
}
for hash, data := range response {
hashToSongId[hash] = data.ID
}
}
return hashToSongId, nil
}

View File

@@ -36,11 +36,10 @@ func normalizeURL(rawURL string) string {
return u.String()
}
func DownloadFile(url string, filepath string) error {
func DownloadFile(url string, filepath string) (err error) {
// 发送 HTTP GET 请求
// resp, err := http.Get(url)
var resp *http.Response
var err error
var maxRetry = 100
var retry = 0
for resp, err = http.Get(url); err != nil && retry < maxRetry; resp, err = http.Get(url) {