impl: add files
- fixed del and deldata to deal with more than one ID
This commit is contained in:
parent
5f7c107864
commit
661146c4f6
@ -154,6 +154,8 @@ func main() {
|
|||||||
|
|
||||||
case "add", "/add":
|
case "add", "/add":
|
||||||
// takes url to a torrent to add it
|
// takes url to a torrent to add it
|
||||||
|
go add(&update, tokens[1:])
|
||||||
|
|
||||||
case "search", "/search":
|
case "search", "/search":
|
||||||
// search for a torrent
|
// search for a torrent
|
||||||
go search(&update, tokens[1:])
|
go search(&update, tokens[1:])
|
||||||
@ -202,6 +204,10 @@ func main() {
|
|||||||
// prints a help message
|
// prints a help message
|
||||||
case "version", "/version":
|
case "version", "/version":
|
||||||
// print transmission and transmission-telegram versions
|
// print transmission and transmission-telegram versions
|
||||||
|
case "":
|
||||||
|
// might be a file received
|
||||||
|
go receiveTorrent(&update)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// no such command, try help
|
// no such command, try help
|
||||||
|
|
||||||
@ -337,7 +343,49 @@ func trackers(ud *tgbotapi.Update) {
|
|||||||
send(buf.String(), ud.Message.Chat.ID)
|
send(buf.String(), ud.Message.Chat.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// add
|
// add takes an URL to a .torrent file to add it to transmission
|
||||||
|
func add(ud *tgbotapi.Update, tokens []string) {
|
||||||
|
if len(tokens) == 0 {
|
||||||
|
send("add: needs atleast one URL", ud.Message.Chat.ID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// loop over the URL/s and add them
|
||||||
|
for _, url := range tokens {
|
||||||
|
cmd := transmission.NewAddCmdByURL(url)
|
||||||
|
|
||||||
|
torrent, err := Client.ExecuteAddCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
send("add: "+err.Error(), ud.Message.Chat.ID)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if torrent.Name is empty, then an error happened
|
||||||
|
if torrent.Name == "" {
|
||||||
|
send("add: error adding "+url, ud.Message.Chat.ID)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
send("Added: "+torrent.Name, ud.Message.Chat.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// receiveTorrent gets an update that potentially has a .torrent file to add
|
||||||
|
func receiveTorrent(ud *tgbotapi.Update) {
|
||||||
|
if ud.Message.Document.FileID == "" {
|
||||||
|
return // has no document
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the file ID and make the config
|
||||||
|
fconfig := tgbotapi.FileConfig{ud.Message.Document.FileID}
|
||||||
|
file, err := Bot.GetFile(fconfig)
|
||||||
|
if err != nil {
|
||||||
|
send("receiver: "+err.Error(), ud.Message.Chat.ID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// add by file URL
|
||||||
|
add(ud, []string{file.Link(BotToken)})
|
||||||
|
}
|
||||||
|
|
||||||
// search takes a query and returns torrents with match
|
// search takes a query and returns torrents with match
|
||||||
func search(ud *tgbotapi.Update, tokens []string) {
|
func search(ud *tgbotapi.Update, tokens []string) {
|
||||||
@ -600,10 +648,12 @@ func del(ud *tgbotapi.Update, tokens []string) {
|
|||||||
send("del: needs an ID", ud.Message.Chat.ID)
|
send("del: needs an ID", ud.Message.Chat.ID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// try to read the id into an int
|
|
||||||
num, err := strconv.Atoi(tokens[0])
|
// loop over tokens to read each potential id
|
||||||
|
for _, id := range tokens {
|
||||||
|
num, err := strconv.Atoi(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
send(fmt.Sprintf("del: %s is not an ID", tokens[0]), ud.Message.Chat.ID)
|
send(fmt.Sprintf("del: %s is not an ID", id), ud.Message.Chat.ID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -615,6 +665,7 @@ func del(ud *tgbotapi.Update, tokens []string) {
|
|||||||
|
|
||||||
send("Deleted: "+name, ud.Message.Chat.ID)
|
send("Deleted: "+name, ud.Message.Chat.ID)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// deldata takes an id or more, and delete the corresponding torrent/s with their data
|
// deldata takes an id or more, and delete the corresponding torrent/s with their data
|
||||||
func deldata(ud *tgbotapi.Update, tokens []string) {
|
func deldata(ud *tgbotapi.Update, tokens []string) {
|
||||||
@ -623,10 +674,11 @@ func deldata(ud *tgbotapi.Update, tokens []string) {
|
|||||||
send("deldata: needs an ID", ud.Message.Chat.ID)
|
send("deldata: needs an ID", ud.Message.Chat.ID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// try to read the id into an int
|
// loop over tokens to read each potential id
|
||||||
num, err := strconv.Atoi(tokens[0])
|
for _, id := range tokens {
|
||||||
|
num, err := strconv.Atoi(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
send(fmt.Sprintf("deldata: %s is not an ID", tokens[0]), ud.Message.Chat.ID)
|
send(fmt.Sprintf("deldata: %s is not an ID", id), ud.Message.Chat.ID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -638,6 +690,7 @@ func deldata(ud *tgbotapi.Update, tokens []string) {
|
|||||||
|
|
||||||
send("Deleted with data: "+name, ud.Message.Chat.ID)
|
send("Deleted with data: "+name, ud.Message.Chat.ID)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// send takes a chat id and a message to send.
|
// send takes a chat id and a message to send.
|
||||||
func send(text string, chatID int64) {
|
func send(text string, chatID int64) {
|
||||||
|
Loading…
Reference in New Issue
Block a user