diff --git a/active.go b/active.go index eab5e69..1497383 100644 --- a/active.go +++ b/active.go @@ -6,7 +6,7 @@ import ( "time" humanize "github.com/pyed/go-humanize" - "gopkg.in/telegram-bot-api.v4" + tgbotapi "gopkg.in/telegram-bot-api.v4" ) // active will send torrents that are actively downloading or uploading @@ -24,9 +24,9 @@ func active() { torrents[i].UpRate > 0 { torrentName := mdReplacer.Replace(torrents[i].Name) // escape markdown buf.WriteString(fmt.Sprintf("`<%d>` *%s*\n%s *%s* (%s) ↓ *%s* ↑ *%s* R: *%.2f*\n\n", - i, torrentName, torrents[i].State, humanize.Bytes(torrents[i].Completed), - torrents[i].Percent, humanize.Bytes(torrents[i].DownRate), - humanize.Bytes(torrents[i].UpRate), torrents[i].Ratio)) + i, torrentName, torrents[i].State, humanize.IBytes(torrents[i].Completed), + torrents[i].Percent, humanize.IBytes(torrents[i].DownRate), + humanize.IBytes(torrents[i].UpRate), torrents[i].Ratio)) } } if buf.Len() == 0 { @@ -58,9 +58,9 @@ func active() { torrents[i].DownRate > 0 { torrentName := mdReplacer.Replace(torrents[i].Name) // replace markdown chars buf.WriteString(fmt.Sprintf("`<%d>` *%s*\n%s *%s* (%s) ↓ *%s* ↑ *%s* R: *%.2f*\n\n", - i, torrentName, torrents[i].State, humanize.Bytes(torrents[i].Completed), - torrents[i].Percent, humanize.Bytes(torrents[i].DownRate), - humanize.Bytes(torrents[i].UpRate), torrents[i].Ratio)) + i, torrentName, torrents[i].State, humanize.IBytes(torrents[i].Completed), + torrents[i].Percent, humanize.IBytes(torrents[i].DownRate), + humanize.IBytes(torrents[i].UpRate), torrents[i].Ratio)) } } @@ -80,7 +80,7 @@ func active() { // escape markdown torrentName := mdReplacer.Replace(torrents[i].Name) buf.WriteString(fmt.Sprintf("`<%d>` *%s*\n%s *%s* (%s) ↓ *-* ↑ *-* R: *%.2f*\n\n", - i, torrentName, torrents[i].State, humanize.Bytes(torrents[i].Completed), + i, torrentName, torrents[i].State, humanize.IBytes(torrents[i].Completed), torrents[i].Percent, torrents[i].Ratio)) } } diff --git a/head.go b/head.go index cb9a250..04a30c0 100644 --- a/head.go +++ b/head.go @@ -7,7 +7,7 @@ import ( "time" humanize "github.com/pyed/go-humanize" - "gopkg.in/telegram-bot-api.v4" + tgbotapi "gopkg.in/telegram-bot-api.v4" ) // head will list the first 5 or n torrents @@ -41,9 +41,9 @@ func head(tokens []string) { for i, torrent := range torrents[:n] { torrentName := mdReplacer.Replace(torrent.Name) // escape markdown buf.WriteString(fmt.Sprintf("`<%d>` *%s*\n%s *%s* (%s) ↓ *%s* ↑ *%s* R: *%.2f*\n\n", - i, torrentName, torrent.State, humanize.Bytes(torrent.Completed), - torrent.Percent, humanize.Bytes(torrent.DownRate), - humanize.Bytes(torrent.UpRate), torrent.Ratio)) + i, torrentName, torrent.State, humanize.IBytes(torrent.Completed), + torrent.Percent, humanize.IBytes(torrent.DownRate), + humanize.IBytes(torrent.UpRate), torrent.Ratio)) } if buf.Len() == 0 { @@ -80,9 +80,9 @@ func head(tokens []string) { for i, torrent := range torrents[:n] { torrentName := mdReplacer.Replace(torrent.Name) // escape markdown buf.WriteString(fmt.Sprintf("`<%d>` *%s*\n%s *%s* (%s) ↓ *%s* ↑ *%s* R: *%.2f*\n\n", - i, torrentName, torrent.State, humanize.Bytes(torrent.Completed), - torrent.Percent, humanize.Bytes(torrent.DownRate), - humanize.Bytes(torrent.UpRate), torrent.Ratio)) + i, torrentName, torrent.State, humanize.IBytes(torrent.Completed), + torrent.Percent, humanize.IBytes(torrent.DownRate), + humanize.IBytes(torrent.UpRate), torrent.Ratio)) } // no need to check if it is empty, as if the buffer is empty telegram won't change the message diff --git a/info.go b/info.go index 8143b98..ffe92d7 100644 --- a/info.go +++ b/info.go @@ -7,7 +7,7 @@ import ( humanize "github.com/pyed/go-humanize" "github.com/pyed/rtapi" - "gopkg.in/telegram-bot-api.v4" + tgbotapi "gopkg.in/telegram-bot-api.v4" ) // info takes an id of a torrent and returns some info about it @@ -38,9 +38,9 @@ func info(tokens []string) { // format the info torrentName := mdReplacer.Replace(torrents[id].Name) // escape markdown info := fmt.Sprintf("*%s*\n%s *%s* (*%s*) ↓ *%s* ↑ *%s* R: *%.2f* UP: *%s*\nAdded: *%s*, ETA: *%d*\nTracker: `%s`", - torrentName, torrents[id].State, humanize.Bytes(torrents[id].Completed), torrents[id].Percent, - humanize.Bytes(torrents[id].DownRate), humanize.Bytes(torrents[id].UpRate), torrents[id].Ratio, - humanize.Bytes(torrents[id].UpTotal), time.Unix(int64(torrents[id].Age), 0).Format(time.Stamp), + torrentName, torrents[id].State, humanize.IBytes(torrents[id].Completed), torrents[id].Percent, + humanize.IBytes(torrents[id].DownRate), humanize.IBytes(torrents[id].UpRate), torrents[id].Ratio, + humanize.IBytes(torrents[id].UpTotal), time.Unix(int64(torrents[id].Age), 0).Format(time.Stamp), torrents[id].ETA, torrents[id].Tracker.Hostname()) // send it @@ -63,9 +63,9 @@ func info(tokens []string) { torrentName := mdReplacer.Replace(torrent.Name) // escape markdown info := fmt.Sprintf("*%s*\n%s *%s* (*%s*) ↓ *%s* ↑ *%s* R: *%.2f* UP: *%s*\nAdded: *%s*, ETA: *%d*\nTracker: `%s`", - torrentName, torrent.State, humanize.Bytes(torrent.Completed), torrent.Percent, - humanize.Bytes(torrent.DownRate), humanize.Bytes(torrent.UpRate), torrent.Ratio, - humanize.Bytes(torrent.UpTotal), time.Unix(int64(torrent.Age), 0).Format(time.Stamp), + torrentName, torrent.State, humanize.IBytes(torrent.Completed), torrent.Percent, + humanize.IBytes(torrent.DownRate), humanize.IBytes(torrent.UpRate), torrent.Ratio, + humanize.IBytes(torrent.UpTotal), time.Unix(int64(torrent.Age), 0).Format(time.Stamp), torrent.ETA, torrent.Tracker.Hostname()) // update the message diff --git a/main.go b/main.go index 7c9c2cf..a5b510a 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( "github.com/pyed/rtapi" "github.com/pyed/tailer" - "gopkg.in/telegram-bot-api.v4" + tgbotapi "gopkg.in/telegram-bot-api.v4" ) const ( diff --git a/paused.go b/paused.go index d3c1c67..df80be4 100644 --- a/paused.go +++ b/paused.go @@ -22,8 +22,8 @@ func paused() { if torrents[i].State == rtapi.Stopped { buf.WriteString(fmt.Sprintf("<%d> %s\n%s (%s) DL: %s UL: %s R: %s\n\n", i, torrents[i].Name, torrents[i].State, - torrents[i].Percent, humanize.Bytes(torrents[i].Completed), - humanize.Bytes(torrents[i].UpTotal), torrents[i].Ratio)) + torrents[i].Percent, humanize.IBytes(torrents[i].Completed), + humanize.IBytes(torrents[i].UpTotal), torrents[i].Ratio)) } } diff --git a/recieveTorrent.go b/recieveTorrent.go index 58b8b1d..f8f2a5a 100644 --- a/recieveTorrent.go +++ b/recieveTorrent.go @@ -1,6 +1,13 @@ package main -import "gopkg.in/telegram-bot-api.v4" +import ( + "fmt" + "os" + "strings" + + "github.com/pyed/rtapi" + tgbotapi "gopkg.in/telegram-bot-api.v4" +) // receiveTorrent gets an update that potentially has a .torrent file to add func receiveTorrent(ud tgbotapi.Update) { @@ -18,6 +25,72 @@ func receiveTorrent(ud tgbotapi.Update) { return } - // add by file URL - add([]string{file.Link(BotToken)}, ud.Message.Document.FileName) + // if there's no options, just add the torrent + if ud.Message.Caption == "" { + add([]string{file.Link(BotToken)}, ud.Message.Document.FileName) + return + } + + var tFile rtapi.DotTorrentWithOptions + tFile.Link = file.Link(BotToken) + tFile.Name = ud.Message.Document.FileName + tFile.Dir, tFile.Label = processOptions(ud.Message.Caption) + + // check if dir is there, or try to make it. + if tFile.Dir != "" { + // if there's '~' expand it + if strings.HasPrefix(tFile.Dir, "~") { + homedir, err := os.UserHomeDir() + if err != nil { + send(fmt.Sprintf("receiver: Couldn't expand '~' in: %s", tFile.Dir), false) + return + } + tFile.Dir = strings.Replace(tFile.Dir, "~", homedir, 1) + } + + // if the directory isn't there, create it + if _, err := os.Stat(tFile.Dir); os.IsNotExist(err) { + if err = os.MkdirAll(tFile.Dir, os.ModePerm); err != nil { + send(fmt.Sprintf("receiver: Couldn't make directory %s, error: %s", tFile.Dir, err.Error()), false) + return + } else { + send("New directory created: "+tFile.Dir, false) + } + } + } + + // add the .torrent with options + if err := rtorrent.DownloadWithOptions(&tFile); err != nil { + logger.Print("add with options:", err) + send("add with options: %s"+err.Error(), false) + } + + send(fmt.Sprintf("Added: %s", tFile.Name), false) + +} + +// processOptions looks inside 'ud.Message.Caption' and processes the passed options if any; +// e.g. d=/dir/to/downlaods l=Software, will save the added torrent ; +// torrent to the specified direcotry, and will assigne the label "Software" ; +// to it, labels are saved to "d.custom1", which is used by ruTorrent. ; +func processOptions(options string) (dir, lable string) { + if options == "" { + return + } + + // more options can be added later + sliceOfOptions := strings.Split(options, " ") + for _, o := range sliceOfOptions { + switch { + case strings.HasPrefix(o, "d="): // directory + dir = o[2:] + case strings.HasPrefix(o, "l="): // label + lable = o[2:] + case strings.ContainsAny(o, "/\\"): // maybe a directory without 'd=' + dir = o + default: // if none of the above matches, then just make it a label + lable = o + } + } + return } diff --git a/speed.go b/speed.go index 7d8027d..3c8bba0 100644 --- a/speed.go +++ b/speed.go @@ -5,14 +5,14 @@ import ( "time" humanize "github.com/pyed/go-humanize" - "gopkg.in/telegram-bot-api.v4" + tgbotapi "gopkg.in/telegram-bot-api.v4" ) // speed will echo back the current download and upload speeds func speed() { down, up := rtorrent.Speeds() - msg := fmt.Sprintf("↓ %s ↑ %s", humanize.Bytes(down), humanize.Bytes(up)) + msg := fmt.Sprintf("↓ %s ↑ %s", humanize.IBytes(down), humanize.IBytes(up)) msgID := send(msg, false) @@ -24,7 +24,7 @@ func speed() { time.Sleep(time.Second * interval) down, up = rtorrent.Speeds() - msg = fmt.Sprintf("↓ %s ↑ %s", humanize.Bytes(down), humanize.Bytes(up)) + msg = fmt.Sprintf("↓ %s ↑ %s", humanize.IBytes(down), humanize.IBytes(up)) editConf := tgbotapi.NewEditMessageText(chatID, msgID, msg) Bot.Send(editConf) diff --git a/stats.go b/stats.go index 3ae1dd4..609a0c3 100644 --- a/stats.go +++ b/stats.go @@ -20,25 +20,25 @@ func stats() { if stats.ThrottleUp == 0 { throttleUp = "off" } else { - throttleUp = humanize.Bytes(stats.ThrottleUp) + throttleUp = humanize.IBytes(stats.ThrottleUp) } if stats.ThrottleDown == 0 { throttleDown = "off" } else { - throttleDown = humanize.Bytes(stats.ThrottleDown) + throttleDown = humanize.IBytes(stats.ThrottleDown) } msg := fmt.Sprintf( ` \[Throttle *%s* / *%s*] \[Port *%s*] - +\[*%s*] Total Uploaded: *%s* Total Download: *%s* `, - throttleUp, throttleDown, stats.Port, - humanize.Bytes(stats.TotalUp), humanize.Bytes(stats.ThrottleDown), + throttleUp, throttleDown, stats.Port, stats.Directory, + humanize.IBytes(stats.TotalUp), humanize.IBytes(stats.TotalDown), ) send(msg, true) diff --git a/tail.go b/tail.go index 9480e0b..9717d67 100644 --- a/tail.go +++ b/tail.go @@ -7,7 +7,7 @@ import ( "time" humanize "github.com/pyed/go-humanize" - "gopkg.in/telegram-bot-api.v4" + tgbotapi "gopkg.in/telegram-bot-api.v4" ) // tail lists the last 5 or n torrents @@ -41,9 +41,9 @@ func tail(tokens []string) { for i, torrent := range torrents[len(torrents)-n:] { torrentName := mdReplacer.Replace(torrent.Name) // escape markdown buf.WriteString(fmt.Sprintf("`<%d>` *%s*\n%s *%s* (%s) ↓ *%s* ↑ *%s* R: *%.2f*\n\n", - i+len(torrents)-n, torrentName, torrent.State, humanize.Bytes(torrent.Completed), - torrent.Percent, humanize.Bytes(torrent.DownRate), - humanize.Bytes(torrent.UpRate), torrent.Ratio)) + i+len(torrents)-n, torrentName, torrent.State, humanize.IBytes(torrent.Completed), + torrent.Percent, humanize.IBytes(torrent.DownRate), + humanize.IBytes(torrent.UpRate), torrent.Ratio)) } if buf.Len() == 0 { @@ -80,9 +80,9 @@ func tail(tokens []string) { for i, torrent := range torrents[len(torrents)-n:] { torrentName := mdReplacer.Replace(torrent.Name) // escape markdown buf.WriteString(fmt.Sprintf("`<%d>` *%s*\n%s *%s* (%s) ↓ *%s* ↑ *%s* R: *%.2f*\n\n", - i+len(torrents)-n, torrentName, torrent.State, humanize.Bytes(torrent.Completed), - torrent.Percent, humanize.Bytes(torrent.DownRate), - humanize.Bytes(torrent.UpRate), torrent.Ratio)) + i+len(torrents)-n, torrentName, torrent.State, humanize.IBytes(torrent.Completed), + torrent.Percent, humanize.IBytes(torrent.DownRate), + humanize.IBytes(torrent.UpRate), torrent.Ratio)) } // no need to check if it is empty, as if the buffer is empty telegram won't change the message