Skip to content

Commit b8c7cd7

Browse files
committed
add more shit code
1 parent ed7ba81 commit b8c7cd7

File tree

6 files changed

+135
-225
lines changed

6 files changed

+135
-225
lines changed

bot/downloader.go

Lines changed: 0 additions & 160 deletions
This file was deleted.

bot/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ via @%s`
9090
fetchInfoFailed = `获取歌曲信息失败`
9191
waitForDown = `等待下载中...`
9292
downloading = `下载中...`
93+
downloadStatus = " %s\n%.2fMB/%.2fMB %d%%"
94+
redownloading = `下载失败,尝试重新下载中...`
9395
uploading = `下载完成, 发送中...`
9496
md5VerFailed = "MD5校验失败"
9597
reTrying = "尝试重新下载中 (%d/%d)"

bot/processMusic.go

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"net/http"
77
"os"
88
"path"
9+
"strconv"
910
"strings"
1011
"time"
1112

1213
marker "github.com/XiaoMengXinX/163KeyMarker"
1314
"github.com/XiaoMengXinX/Music163Api-Go/api"
1415
"github.com/XiaoMengXinX/Music163Api-Go/types"
16+
downloader "github.com/XiaoMengXinX/SimpleDownloader"
1517
"github.com/go-telegram-bot-api/telegram-bot-api/v5"
1618
"github.com/sirupsen/logrus"
1719
"gorm.io/gorm"
@@ -21,6 +23,15 @@ import (
2123
var musicLimiter = make(chan bool, 4)
2224

2325
func processMusic(musicID int, message tgbotapi.Message, bot *tgbotapi.BotAPI) (err error) {
26+
d := downloader.NewDownloader().SetSavePath(cacheDir).SetBreakPoint(true)
27+
28+
timeout, _ := strconv.Atoi(config["DownloadTimeout"])
29+
if timeout != 0 {
30+
d.SetTimeOut(time.Duration(int64(timeout)) * time.Second)
31+
} else {
32+
d.SetTimeOut(60 * time.Second) // 默认超时时间为 60 秒
33+
}
34+
2435
defer func() {
2536
e := recover()
2637
if e != nil {
@@ -185,50 +196,66 @@ func processMusic(musicID int, message tgbotapi.Message, bot *tgbotapi.BotAPI) (
185196
return err
186197
}
187198

188-
timeStamp := time.Now().UnixMicro()
199+
hostReplacer := strings.NewReplacer("m8.", "m7.", "m801.", "m701.", "m804.", "m701.", "m704.", "m701.")
189200

190-
d, err := newDownloader(url, fmt.Sprintf("%d-%s", timeStamp, path.Base(url)), 8)
191-
if err != nil {
192-
sendFailed(err)
201+
timeStamp := time.Now().UnixMicro()
202+
musicFileName := fmt.Sprintf("%d-%s", timeStamp, path.Base(url))
203+
204+
task, _ := d.NewDownloadTask(url)
205+
host := task.GetHostName()
206+
task.ReplaceHostName(hostReplacer.Replace(host)).ForceHttps().ForceMultiThread()
207+
errCh := task.SetFileName(musicFileName).DownloadWithChannel()
208+
209+
updateStatus := func(task *downloader.DownloadTask, ch chan error, statusText string) (err error) {
210+
var lastUpdateTime int64
211+
loop:
212+
for {
213+
select {
214+
case err = <-ch:
215+
break loop
216+
default:
217+
writtenBytes := task.GetWrittenBytes()
218+
if task.GetFileSize() == 0 || writtenBytes == 0 || time.Now().Unix()-lastUpdateTime < 5 {
219+
continue
220+
}
221+
editMsg = tgbotapi.NewEditMessageText(message.Chat.ID, msgResult.MessageID, fmt.Sprintf(musicInfoMsg+statusText+downloadStatus, songInfo.SongName, songInfo.SongAlbum, songInfo.FileExt, float64(songInfo.MusicSize)/1024/1024, task.CalculateSpeed(time.Millisecond*500), float64(writtenBytes)/1024/1024, float64(task.GetFileSize())/1024/1024, (writtenBytes*100)/task.GetFileSize()))
222+
_, _ = bot.Send(editMsg)
223+
lastUpdateTime = time.Now().Unix()
224+
}
225+
}
193226
return err
194227
}
195228

196-
var isMD5Verified = false
197-
for i := 0; i < maxRetryTimes && songURL.Data[0].Md5 != ""; i++ {
198-
err = d.download()
199-
if err != nil && !isTimeout(err) {
200-
sendFailed(err)
201-
return err
202-
} else if err != nil && isTimeout(err) {
203-
sendFailed(fmt.Errorf(downloadTimeout + "\n" + retryLater))
204-
return err
205-
}
206-
207-
if isMD5Verified, err = verifyMD5(cacheDir+"/"+fmt.Sprintf("%d-%s", timeStamp, path.Base(url)), songURL.Data[0].Md5); !isMD5Verified && config["AutoRetry"] != "false" {
208-
sendFailed(fmt.Errorf("%s\n"+reTrying, err, i+1, maxRetryTimes))
209-
err := os.Remove(cacheDir + "/" + fmt.Sprintf("%d-%s", timeStamp, path.Base(url)))
229+
err = updateStatus(task, errCh, downloading)
230+
if err != nil {
231+
if config["ReverseProxy"] != "" {
232+
ch := task.WithResolvedIpOnHost(config["ReverseProxy"]).DownloadWithChannel()
233+
err = updateStatus(task, ch, redownloading)
210234
if err != nil {
211-
logrus.Errorln(err)
212-
}
213-
if songUrl, _ := api.GetSongURL(data, api.SongURLConfig{Ids: []int{musicID}}); len(songUrl.Data) != 0 {
214-
d, err = newDownloader(url, fmt.Sprintf("%d-%s", timeStamp, path.Base(songUrl.Data[0].Url)), 2)
215-
if err != nil {
216-
sendFailed(err)
217-
return err
218-
}
235+
sendFailed(err)
236+
task.CleanTempFiles()
237+
return err
219238
}
220239
} else {
221-
break
240+
sendFailed(err)
241+
task.CleanTempFiles()
242+
return err
222243
}
223244
}
245+
246+
isMD5Verified, _ := verifyMD5(cacheDir+"/"+musicFileName, songURL.Data[0].Md5)
224247
if !isMD5Verified && songURL.Data[0].Md5 != "" {
248+
err = os.Remove(cacheDir + "/" + fmt.Sprintf("%d-%s", timeStamp, path.Base(url)))
249+
if err != nil {
250+
logrus.Errorln(err)
251+
}
225252
sendFailed(fmt.Errorf("%s\n%s", md5VerFailed, retryLater))
226253
return nil
227254
}
228255

229256
var picPath, resizePicPath string
230-
p, _ := newDownloader(songDetail.Songs[0].Al.PicUrl, fmt.Sprintf("%d-%s", timeStamp, path.Base(songDetail.Songs[0].Al.PicUrl)), 8)
231-
err = p.download()
257+
p, _ := d.NewDownloadTask(songDetail.Songs[0].Al.PicUrl)
258+
err = p.SetFileName(fmt.Sprintf("%d-%s", timeStamp, path.Base(songDetail.Songs[0].Al.PicUrl))).Download()
232259
if err != nil {
233260
logrus.Errorln(err)
234261
} else {
@@ -255,6 +282,13 @@ func processMusic(musicID int, message tgbotapi.Message, bot *tgbotapi.BotAPI) (
255282
logrus.Errorln(err)
256283
}
257284

285+
var pic *os.File = nil
286+
287+
if picStat != nil && err == nil {
288+
pic, _ = os.Open(musicPic)
289+
defer pic.Close()
290+
}
291+
258292
var replacer = strings.NewReplacer("/", " ", "?", " ", "*", " ", ":", " ", "|", " ", "\\", " ", "<", " ", ">", " ", "\"", " ")
259293
fileName := replacer.Replace(fmt.Sprintf("%v - %v.%v", strings.Replace(songInfo.SongArtists, "/", ",", -1), songInfo.SongName, songInfo.FileExt))
260294
err = os.Rename(cacheDir+"/"+fmt.Sprintf("%d-%s", timeStamp, path.Base(url)), cacheDir+"/"+fileName)
@@ -267,10 +301,12 @@ func processMusic(musicID int, message tgbotapi.Message, bot *tgbotapi.BotAPI) (
267301
file, _ := os.Open(cacheDir + "/" + fileName)
268302
defer file.Close()
269303

270-
pic, _ := os.Open(musicPic)
271-
defer pic.Close()
272-
273304
err = marker.AddMusicID3V2(file, pic, mark)
305+
if err != nil {
306+
file, _ = os.Open(cacheDir + "/" + fileName)
307+
defer file.Close()
308+
err = marker.AddMusicID3V2(file, nil, mark)
309+
}
274310
if err != nil {
275311
sendFailed(err)
276312
return err

config_example.ini

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ MUSIC_U = YOUR_MUSIC_U
1111
BotAPI = https://api.telegram.org
1212

1313
# 设置 bot 管理员 ID, 用 “," 分隔
14-
BotAdmin = 1234,3456
14+
BotAdmin = 115414,1919810
1515

1616
# 是否开启 bot 的 debug 功能
1717
BotDebug = false
@@ -34,11 +34,5 @@ MaxRetryTimes = 3
3434
# 下载超时时长 (单位秒, 默认为 60)
3535
DownloadTimeout = 60
3636

37-
# 是否校验更新文件 md5 (默认开启), 若设置为 false 相当于 -no-md5-check 参数
38-
CheckMD5 = true
39-
40-
# 自定义源码路径
41-
SrcPath = ./src
42-
43-
# 自定义 bot 函数入口 (默认为 bot.Start)
44-
BotEntry = bot.Start
37+
# 自定义下载反向代理
38+
ReverseProxy = 114.5.1.4:8080

go.mod

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,29 @@ go 1.17
55
require (
66
github.com/XiaoMengXinX/163KeyMarker v0.0.0-20221030134715-67afb724a936
77
github.com/XiaoMengXinX/Music163Api-Go v0.1.29
8+
github.com/XiaoMengXinX/SimpleDownloader v0.0.0-20221110191044-7a2606dfcf7c
89
github.com/glebarez/sqlite v1.5.0
9-
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.0
10+
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
1011
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
11-
github.com/sirupsen/logrus v1.8.1
12-
gorm.io/gorm v1.24.0
12+
github.com/sirupsen/logrus v1.9.0
13+
gorm.io/gorm v1.24.1
1314
)
1415

1516
require (
1617
github.com/bogem/id3v2 v1.2.0 // indirect
17-
github.com/glebarez/go-sqlite v1.19.1 // indirect
18+
github.com/glebarez/go-sqlite v1.19.2 // indirect
1819
github.com/go-flac/flacpicture v0.2.0 // indirect
1920
github.com/go-flac/flacvorbis v0.1.0 // indirect
2021
github.com/go-flac/go-flac v0.3.1 // indirect
2122
github.com/google/uuid v1.3.0 // indirect
2223
github.com/jinzhu/inflection v1.0.0 // indirect
2324
github.com/jinzhu/now v1.1.5 // indirect
2425
github.com/mattn/go-isatty v0.0.16 // indirect
25-
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
26-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
27-
golang.org/x/text v0.3.7 // indirect
28-
modernc.org/libc v1.19.0 // indirect
26+
github.com/remyoudompheng/bigfft v0.0.0-20220927061507-ef77025ab5aa // indirect
27+
golang.org/x/sys v0.2.0 // indirect
28+
golang.org/x/text v0.4.0 // indirect
29+
modernc.org/libc v1.21.4 // indirect
2930
modernc.org/mathutil v1.5.0 // indirect
3031
modernc.org/memory v1.4.0 // indirect
31-
modernc.org/sqlite v1.19.1 // indirect
32+
modernc.org/sqlite v1.19.4 // indirect
3233
)

0 commit comments

Comments
 (0)