Use pyed/tailer instead. (#24)
* Use pyed/tailer instead of hpcloud/tail * Get rid of 'vendor' as we are no longer using hpcloud/tail * Fix usage message * Add the ability to pass the bot token via env variable * Add new line to the error messages
This commit is contained in:
parent
274f19ebe7
commit
7e4d1b7989
12
.gitmodules
vendored
12
.gitmodules
vendored
@ -1,12 +0,0 @@
|
|||||||
[submodule "vendor/github.com/dustin/go-humanize"]
|
|
||||||
path = vendor/github.com/dustin/go-humanize
|
|
||||||
url = https://github.com/dustin/go-humanize
|
|
||||||
[submodule "vendor/github.com/hpcloud/tail"]
|
|
||||||
path = vendor/github.com/hpcloud/tail
|
|
||||||
url = https://github.com/hpcloud/tail
|
|
||||||
[submodule "vendor/github.com/pyed/transmission"]
|
|
||||||
path = vendor/github.com/pyed/transmission
|
|
||||||
url = https://github.com/pyed/transmission
|
|
||||||
[submodule "vendor/gopkg.in/telegram-bot-api.v4"]
|
|
||||||
path = vendor/gopkg.in/telegram-bot-api.v4
|
|
||||||
url = https://gopkg.in/telegram-bot-api.v4
|
|
49
main.go
49
main.go
@ -13,7 +13,7 @@ import (
|
|||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/hpcloud/tail"
|
"github.com/pyed/tailer"
|
||||||
"github.com/pyed/transmission"
|
"github.com/pyed/transmission"
|
||||||
"gopkg.in/telegram-bot-api.v4"
|
"gopkg.in/telegram-bot-api.v4"
|
||||||
)
|
)
|
||||||
@ -169,7 +169,7 @@ func (masters masterSlice) Contains(master string) bool {
|
|||||||
// init flags
|
// init flags
|
||||||
func init() {
|
func init() {
|
||||||
// define arguments and parse them.
|
// define arguments and parse them.
|
||||||
flag.StringVar(&BotToken, "token", "", "Telegram bot token")
|
flag.StringVar(&BotToken, "token", "", "Telegram bot token, Can be passed via environment variable 'TT_BOTT'")
|
||||||
flag.Var(&Masters, "master", "Your telegram handler, So the bot will only respond to you. Can specify more than one")
|
flag.Var(&Masters, "master", "Your telegram handler, So the bot will only respond to you. Can specify more than one")
|
||||||
flag.StringVar(&RPCURL, "url", "http://localhost:9091/transmission/rpc", "Transmission RPC URL")
|
flag.StringVar(&RPCURL, "url", "http://localhost:9091/transmission/rpc", "Transmission RPC URL")
|
||||||
flag.StringVar(&Username, "username", "", "Transmission username")
|
flag.StringVar(&Username, "username", "", "Transmission username")
|
||||||
@ -180,12 +180,19 @@ func init() {
|
|||||||
|
|
||||||
// set the usage message
|
// set the usage message
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Fprint(os.Stderr, "Usage: transmission-bot <-token=TOKEN> <-master=@tuser> [-master=@yuser2] [-url=http://] [-username=user] [-password=pass]\n\n")
|
fmt.Fprint(os.Stderr, "Usage: transmission-telegram <-token=TOKEN> <-master=@tuser> [-master=@yuser2] [-url=http://] [-username=user] [-password=pass]\n\n")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
// if we don't have BotToken passed, check the environment variable "TT_BOTT"
|
||||||
|
if BotToken == "" {
|
||||||
|
if token := os.Getenv("TT_BOTT"); len(token) > 1 {
|
||||||
|
BotToken = token
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// make sure that we have the two madatory arguments: telegram token & master's handler.
|
// make sure that we have the two madatory arguments: telegram token & master's handler.
|
||||||
if BotToken == "" ||
|
if BotToken == "" ||
|
||||||
len(Masters) < 1 {
|
len(Masters) < 1 {
|
||||||
@ -211,16 +218,7 @@ func init() {
|
|||||||
// if we got a transmission log file, monitor it for torrents completion to notify upon them.
|
// if we got a transmission log file, monitor it for torrents completion to notify upon them.
|
||||||
if TransLogFile != "" {
|
if TransLogFile != "" {
|
||||||
go func() {
|
go func() {
|
||||||
ft, err := tail.TailFile(TransLogFile, tail.Config{
|
ft := tailer.RunFileTailer(TransLogFile, false, nil)
|
||||||
Location: &tail.SeekInfo{0, 2}, // ignore previous log lines
|
|
||||||
Follow: true, // as the -f in tail -f
|
|
||||||
MustExist: true, // if you can't find the file, don't wait for it to be created
|
|
||||||
Logger: logger, // log to our logger
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
logger.Printf("[ERROR] tailing transmission log: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// [2017-02-22 21:00:00.898] File-Name State changed from "Incomplete" to "Complete" (torrent.c:2218)
|
// [2017-02-22 21:00:00.898] File-Name State changed from "Incomplete" to "Complete" (torrent.c:2218)
|
||||||
const (
|
const (
|
||||||
@ -229,16 +227,23 @@ func init() {
|
|||||||
end = len(` State changed from "Incomplete" to "Complete" (torrent.c:2218)`)
|
end = len(` State changed from "Incomplete" to "Complete" (torrent.c:2218)`)
|
||||||
)
|
)
|
||||||
|
|
||||||
for line := range ft.Lines {
|
for {
|
||||||
if strings.Contains(line.Text, substring) {
|
select {
|
||||||
|
case line := <-ft.Lines():
|
||||||
|
if strings.Contains(line, substring) {
|
||||||
// if we don't have a chatID continue
|
// if we don't have a chatID continue
|
||||||
if chatID == 0 {
|
if chatID == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := fmt.Sprintf("Completed: %s", line.Text[start:len(line.Text)-end])
|
msg := fmt.Sprintf("Completed: %s", line[start:len(line)-end])
|
||||||
send(msg, chatID, false)
|
send(msg, chatID, false)
|
||||||
}
|
}
|
||||||
|
case err := <-ft.Errors():
|
||||||
|
logger.Printf("[ERROR] tailing transmission log: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@ -260,7 +265,7 @@ func init() {
|
|||||||
var err error
|
var err error
|
||||||
Client, err = transmission.New(RPCURL, Username, Password)
|
Client, err = transmission.New(RPCURL, Username, Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "[ERROR] Transmission: Make sure you have the right URL, Username and Password")
|
fmt.Fprintf(os.Stderr, "[ERROR] Transmission: Make sure you have the right URL, Username and Password\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,7 +277,7 @@ func init() {
|
|||||||
var err error
|
var err error
|
||||||
Bot, err = tgbotapi.NewBotAPI(BotToken)
|
Bot, err = tgbotapi.NewBotAPI(BotToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "[ERROR] Telegram: %s", err)
|
fmt.Fprintf(os.Stderr, "[ERROR] Telegram: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
logger.Printf("[INFO] Authorized: %s", Bot.Self.UserName)
|
logger.Printf("[INFO] Authorized: %s", Bot.Self.UserName)
|
||||||
@ -282,7 +287,7 @@ func init() {
|
|||||||
|
|
||||||
Updates, err = Bot.GetUpdatesChan(u)
|
Updates, err = Bot.GetUpdatesChan(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "[ERROR] Telegram: %s", err)
|
fmt.Fprintf(os.Stderr, "[ERROR] Telegram: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -317,7 +322,7 @@ func main() {
|
|||||||
go head(update, tokens[1:])
|
go head(update, tokens[1:])
|
||||||
|
|
||||||
case "tail", "/tail", "ta", "/ta":
|
case "tail", "/tail", "ta", "/ta":
|
||||||
go tailf(update, tokens[1:])
|
go tail(update, tokens[1:])
|
||||||
|
|
||||||
case "downs", "/downs", "dl", "/dl":
|
case "downs", "/downs", "dl", "/dl":
|
||||||
go downs(update)
|
go downs(update)
|
||||||
@ -522,8 +527,8 @@ func head(ud tgbotapi.Update, tokens []string) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// tailf lists the last 5 or n torrents
|
// tail lists the last 5 or n torrents
|
||||||
func tailf(ud tgbotapi.Update, tokens []string) {
|
func tail(ud tgbotapi.Update, tokens []string) {
|
||||||
var (
|
var (
|
||||||
n = 5 // default to 5
|
n = 5 // default to 5
|
||||||
err error
|
err error
|
||||||
|
1
vendor/github.com/dustin/go-humanize
generated
vendored
1
vendor/github.com/dustin/go-humanize
generated
vendored
@ -1 +0,0 @@
|
|||||||
Subproject commit 7a41df006ff9af79a29f0ffa9c5f21fbe6314a2d
|
|
1
vendor/github.com/hpcloud/tail
generated
vendored
1
vendor/github.com/hpcloud/tail
generated
vendored
@ -1 +0,0 @@
|
|||||||
Subproject commit a30252cb686a21eb2d0b98132633053ec2f7f1e5
|
|
1
vendor/github.com/pyed/transmission
generated
vendored
1
vendor/github.com/pyed/transmission
generated
vendored
@ -1 +0,0 @@
|
|||||||
Subproject commit 065032b0e966f5cfb06d0126d5aea72a92b30b8a
|
|
1
vendor/gopkg.in/telegram-bot-api.v4
generated
vendored
1
vendor/gopkg.in/telegram-bot-api.v4
generated
vendored
@ -1 +0,0 @@
|
|||||||
Subproject commit 0a57807db79efce7f6719fbb2c0e0f83fda79aec
|
|
Loading…
Reference in New Issue
Block a user