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":
|
||||
// takes url to a torrent to add it
|
||||
go add(&update, tokens[1:])
|
||||
|
||||
case "search", "/search":
|
||||
// search for a torrent
|
||||
go search(&update, tokens[1:])
|
||||
@ -202,6 +204,10 @@ func main() {
|
||||
// prints a help message
|
||||
case "version", "/version":
|
||||
// print transmission and transmission-telegram versions
|
||||
case "":
|
||||
// might be a file received
|
||||
go receiveTorrent(&update)
|
||||
|
||||
default:
|
||||
// no such command, try help
|
||||
|
||||
@ -337,7 +343,49 @@ func trackers(ud *tgbotapi.Update) {
|
||||
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
|
||||
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)
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
@ -614,6 +664,7 @@ func del(ud *tgbotapi.Update, tokens []string) {
|
||||
}
|
||||
|
||||
send("Deleted: "+name, ud.Message.Chat.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// deldata takes an id or more, and delete the corresponding torrent/s with their data
|
||||
@ -623,10 +674,11 @@ func deldata(ud *tgbotapi.Update, tokens []string) {
|
||||
send("deldata: needs an ID", ud.Message.Chat.ID)
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
@ -637,6 +689,7 @@ func deldata(ud *tgbotapi.Update, tokens []string) {
|
||||
}
|
||||
|
||||
send("Deleted with data: "+name, ud.Message.Chat.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// send takes a chat id and a message to send.
|
||||
|
Loading…
Reference in New Issue
Block a user