makes 'info' variadic + live update

This commit is contained in:
pyed 2016-07-04 05:49:36 +03:00
parent 950963dc31
commit d8ed7da5a4

View File

@ -758,29 +758,71 @@ func info(ud tgbotapi.Update, tokens []string) {
return return
} }
// try to read the id for _, id := range tokens {
num, err := strconv.Atoi(tokens[0]) torrentID, err := strconv.Atoi(id)
if err != nil { if err != nil {
send(fmt.Sprintf("info: %s is not a number", tokens[0]), ud.Message.Chat.ID) send(fmt.Sprintf("info: %s is not a number", id), ud.Message.Chat.ID)
return continue
} }
// get the torrent // get the torrent
torrent, err := Client.GetTorrent(num) torrent, err := Client.GetTorrent(torrentID)
if err != nil { if err != nil {
send(fmt.Sprintf("info: Can't find a torrent with an ID of %d", num), ud.Message.Chat.ID) send(fmt.Sprintf("info: Can't find a torrent with an ID of %d", torrentID), ud.Message.Chat.ID)
return continue
}
// get the trackers using 'trackerRegex'
var trackers string
for _, tracker := range torrent.Trackers {
sm := trackerRegex.FindSubmatch([]byte(tracker.Announce))
if len(sm) > 1 {
trackers += string(sm[1]) + " "
}
} }
// format the info // format the info
info := fmt.Sprintf("<%d> %s\n%s\t%s of %s (%.1f%%)\t↓ %s ↑ %s R:%s\nUP: %s DL: %s Added: %s ETA: %s\nTrackers: %s", info := fmt.Sprintf("<%d> %s\n%s\t%s of %s (%.1f%%) ↓ %s ↑ %s, R: %s\nDL: %s, UP: %s\nAdded: %s, ETA: %s\nTrackers: %s",
torrent.ID, torrent.Name, torrent.TorrentStatus(), humanize.Bytes(torrent.DownloadedEver), humanize.Bytes(torrent.SizeWhenDone), torrent.ID, torrent.Name, torrent.TorrentStatus(), humanize.Bytes(torrent.Have()), humanize.Bytes(torrent.SizeWhenDone),
torrent.PercentDone*100, humanize.Bytes(torrent.RateDownload), humanize.Bytes(torrent.RateUpload), torrent.Ratio(), torrent.PercentDone*100, humanize.Bytes(torrent.RateDownload), humanize.Bytes(torrent.RateUpload), torrent.Ratio(),
humanize.Bytes(torrent.UploadedEver), humanize.Bytes(torrent.DownloadedEver), time.Unix(torrent.AddedDate, 0).Format(time.Stamp), humanize.Bytes(torrent.DownloadedEver), humanize.Bytes(torrent.UploadedEver), time.Unix(torrent.AddedDate, 0).Format(time.Stamp),
torrent.ETA(), torrent.GetTrackers()) torrent.ETA(), trackers)
// send it // send it
send(info, ud.Message.Chat.ID) msgID := send(info, ud.Message.Chat.ID)
// this go-routine will make the info live for 20 seconds
// takes trackers so we don't have to regex them over and over.
go func(trackers string, torrentID, msgID int) {
for i := 0; i < 10; i++ {
torrent, err := Client.GetTorrent(torrentID)
if err != nil {
continue // skip this iteration if there's an error retrieving the torrent's info
}
info := fmt.Sprintf("<%d> %s\n%s\t%s of %s (%.1f%%) ↓ %s ↑ %s, R: %s\nDL: %s, UP: %s\nAdded: %s, ETA: %s\nTrackers: %s",
torrent.ID, torrent.Name, torrent.TorrentStatus(), humanize.Bytes(torrent.Have()), humanize.Bytes(torrent.SizeWhenDone),
torrent.PercentDone*100, humanize.Bytes(torrent.RateDownload), humanize.Bytes(torrent.RateUpload), torrent.Ratio(),
humanize.Bytes(torrent.DownloadedEver), humanize.Bytes(torrent.UploadedEver), time.Unix(torrent.AddedDate, 0).Format(time.Stamp),
torrent.ETA(), trackers)
// update the message
editConf := tgbotapi.NewEditMessageText(ud.Message.Chat.ID, msgID, info)
Bot.Send(editConf)
time.Sleep(time.Second * 2)
}
// at the end write dashes to indicate that we are done being live.
info := fmt.Sprintf("<%d> %s\n%s\t%s of %s (%.1f%%) ↓ - B ↑ - B, R: %s\nDL: %s, UP: %s\nAdded: %s, ETA: -\nTrackers: %s",
torrent.ID, torrent.Name, torrent.TorrentStatus(), humanize.Bytes(torrent.Have()), humanize.Bytes(torrent.SizeWhenDone),
torrent.PercentDone*100, torrent.Ratio(), humanize.Bytes(torrent.DownloadedEver), humanize.Bytes(torrent.UploadedEver),
time.Unix(torrent.AddedDate, 0).Format(time.Stamp), trackers)
editConf := tgbotapi.NewEditMessageText(ud.Message.Chat.ID, msgID, info)
Bot.Send(editConf)
}(trackers, torrentID, msgID)
}
} }
// stop takes id[s] of torrent[s] or 'all' to stop them // stop takes id[s] of torrent[s] or 'all' to stop them