@@ -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 (
2123var musicLimiter = make (chan bool , 4 )
2224
2325func 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
0 commit comments