Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from pyed:master #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions active.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
}
}

Expand All @@ -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))
}
}
Expand Down
14 changes: 7 additions & 7 deletions head.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions paused.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
79 changes: 76 additions & 3 deletions recieveTorrent.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions speed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down