Introduce -no-live flag

if -no-live is set, messages won't get updated after sending them.
This commit is contained in:
pyed 2017-03-13 23:30:56 +03:00
parent ef2c50d073
commit c01767d342

40
main.go
View File

@ -112,6 +112,7 @@ var (
Password string Password string
LogFile string LogFile string
TransLogFile string // Transmission log file TransLogFile string // Transmission log file
NoLive bool
// transmission // transmission
Client *transmission.TransmissionClient Client *transmission.TransmissionClient
@ -175,6 +176,7 @@ func init() {
flag.StringVar(&Password, "password", "", "Transmission password") flag.StringVar(&Password, "password", "", "Transmission password")
flag.StringVar(&LogFile, "logfile", "", "Send logs to a file") flag.StringVar(&LogFile, "logfile", "", "Send logs to a file")
flag.StringVar(&TransLogFile, "transmission-logfile", "", "Open transmission logfile to monitor torrents completion") flag.StringVar(&TransLogFile, "transmission-logfile", "", "Open transmission logfile to monitor torrents completion")
flag.BoolVar(&NoLive, "no-live", false, "Don't edit and update info after sending")
// set the usage message // set the usage message
flag.Usage = func() { flag.Usage = func() {
@ -481,6 +483,10 @@ func head(ud tgbotapi.Update, tokens []string) {
msgID := send(buf.String(), ud.Message.Chat.ID, true) msgID := send(buf.String(), ud.Message.Chat.ID, true)
if NoLive {
return
}
// keep the info live // keep the info live
for i := 0; i < duration; i++ { for i := 0; i < duration; i++ {
time.Sleep(time.Second * interval) time.Sleep(time.Second * interval)
@ -558,6 +564,10 @@ func tailf(ud tgbotapi.Update, tokens []string) {
msgID := send(buf.String(), ud.Message.Chat.ID, true) msgID := send(buf.String(), ud.Message.Chat.ID, true)
if NoLive {
return
}
// keep the info live // keep the info live
for i := 0; i < duration; i++ { for i := 0; i < duration; i++ {
time.Sleep(time.Second * interval) time.Sleep(time.Second * interval)
@ -722,6 +732,10 @@ func active(ud tgbotapi.Update) {
msgID := send(buf.String(), ud.Message.Chat.ID, true) msgID := send(buf.String(), ud.Message.Chat.ID, true)
if NoLive {
return
}
// keep the active list live for 'duration * interval' // keep the active list live for 'duration * interval'
for i := 0; i < duration; i++ { for i := 0; i < duration; i++ {
time.Sleep(time.Second * interval) time.Sleep(time.Second * interval)
@ -1085,6 +1099,10 @@ func info(ud tgbotapi.Update, tokens []string) {
// send it // send it
msgID := send(info, ud.Message.Chat.ID, true) msgID := send(info, ud.Message.Chat.ID, true)
if NoLive {
return
}
// this go-routine will make the info live for 'duration * interval' // this go-routine will make the info live for 'duration * interval'
go func(torrentID, msgID int) { go func(torrentID, msgID int) {
for i := 0; i < duration; i++ { for i := 0; i < duration; i++ {
@ -1287,9 +1305,6 @@ func stats(ud tgbotapi.Update) {
// speed will echo back the current download and upload speeds // speed will echo back the current download and upload speeds
func speed(ud tgbotapi.Update) { func speed(ud tgbotapi.Update) {
// keep track of the returned message ID from 'send()' to edit the message.
var msgID int
for i := 0; i < duration; i++ {
stats, err := Client.GetStats() stats, err := Client.GetStats()
if err != nil { if err != nil {
send("speed: "+err.Error(), ud.Message.Chat.ID, false) send("speed: "+err.Error(), ud.Message.Chat.ID, false)
@ -1298,20 +1313,29 @@ func speed(ud tgbotapi.Update) {
msg := fmt.Sprintf("↓ %s ↑ %s", humanize.Bytes(stats.DownloadSpeed), humanize.Bytes(stats.UploadSpeed)) msg := fmt.Sprintf("↓ %s ↑ %s", humanize.Bytes(stats.DownloadSpeed), humanize.Bytes(stats.UploadSpeed))
// if we haven't send a message, send it and save the message ID to edit it the next iteration msgID := send(msg, ud.Message.Chat.ID, false)
if msgID == 0 {
msgID = send(msg, ud.Message.Chat.ID, false) if NoLive {
return
}
for i := 0; i < duration; i++ {
time.Sleep(time.Second * interval) time.Sleep(time.Second * interval)
stats, err = Client.GetStats()
if err != nil {
continue continue
} }
// we have sent the message, let's update. msg = fmt.Sprintf("↓ %s ↑ %s", humanize.Bytes(stats.DownloadSpeed), humanize.Bytes(stats.UploadSpeed))
editConf := tgbotapi.NewEditMessageText(ud.Message.Chat.ID, msgID, msg) editConf := tgbotapi.NewEditMessageText(ud.Message.Chat.ID, msgID, msg)
Bot.Send(editConf) Bot.Send(editConf)
time.Sleep(time.Second * interval) time.Sleep(time.Second * interval)
} }
// sleep one more time before switching to dashes
time.Sleep(time.Second * interval)
// after the 10th iteration, show dashes to indicate that we are done updating. // show dashes to indicate that we are done updating.
editConf := tgbotapi.NewEditMessageText(ud.Message.Chat.ID, msgID, "↓ - B ↑ - B") editConf := tgbotapi.NewEditMessageText(ud.Message.Chat.ID, msgID, "↓ - B ↑ - B")
Bot.Send(editConf) Bot.Send(editConf)
} }