Couchpotato Search

This commit is contained in:
2021-12-04 06:04:04 +00:00
parent 9af1dda45d
commit 229572be10
5 changed files with 66 additions and 89 deletions

View File

@ -11,7 +11,7 @@ import (
type Config struct {
Telegram struct {
Token string `json:"token"`
ChatID string `json:"chatID"`
ChatID string `json:"chatId"`
Admins []int `json:"admins"`
AuthorizedChats []string `json:"authorizedChats"`
} `json:"telegram"`

View File

@ -2,23 +2,23 @@ package couchpotato
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/mattburchett/go_telegram/pkg/core/config"
"github.com/yanzay/tbot/v2"
)
type response struct {
type couchpotatoSearch struct {
Movies []struct {
Titles []string `json:"titles"`
Imdb string `json:"imdb"`
Year int `json:"year"`
InLibrary struct {
Status string `json:"status"`
} `json:"in_library"`
InWanted bool `json:"in_wanted"`
Title string `json:"original_title"`
Imdb string `json:"imdb"`
Year int `json:"year"`
InLibrary bool `json:"in_library"`
InWanted bool `json:"in_wanted"`
} `json:"movies"`
Success bool `json:"success"`
}
@ -28,31 +28,60 @@ type request struct {
Title string `json:"title"`
Year int `json:"year"`
Requested bool `json:"requested"`
Downloaded struct {
Status string `json:"status"`
} `json:"downloaded"`
Downloaded bool `json:"downloaded"`
}
func (search couchpotatoSearch) Convert() []request {
requests := []request{}
for _, result := range search.Movies {
requests = append(requests, request{
ImdbID: result.Imdb,
Title: result.Title,
Year: result.Year,
Requested: result.InWanted,
Downloaded: result.InLibrary,
})
}
return requests
}
type response struct {
Button string `json:"button"`
Callback string `json:"callback"`
}
// Search performs the lookup actions within CouchPotato
func Search(m *tbot.Message, config config.Config) ([]response, error) {
requestLookup, err := http.Get(config.CouchPotato.URL + "/api/" + config.CouchPotato.APIKey + "/movie.search?q=" + url.QueryEscape(strings.TrimPreFix(strings.TrimPrefix(m.Text, "/s"), " ")))
searchLookup, err := http.Get(config.CouchPotato.URL + config.CouchPotato.APIKey + "/movie.search?q=" + url.QueryEscape(strings.TrimPrefix(strings.TrimPrefix(m.Text, "/m"), " ")))
if err != nil {
return nil, err
}
request := []response{}
search := couchpotatoSearch{}
requestData, err := ioutil.ReadAll(requestLookup.Body)
searchData, err := ioutil.ReadAll(searchLookup.Body)
if err != nil {
return nil, err
}
requestJSON := json.Unmarshal(requestData, &request)
requestJSON := json.Unmarshal(searchData, &search)
if requestJSON != nil {
return nil, err
}
for r := range request {
requests := search.Convert()
responseData := []response{}
for _, r := range requests {
responseData = append(responseData,
response{
fmt.Sprintf("%v (%v)", r.Title, r.Year),
fmt.Sprintf("%v", r.ImdbID),
})
}
return responseData, err
}

View File

@ -14,6 +14,9 @@ func (tb *Bot) Handler() {
tb.Client.SendMessage(m.Chat.ID, "pong")
})
// telegram/couchpotato.go
tb.Bot.HandleMessage("/m", tb.couchpotatoSearch)
// telegram/sonar.go
tb.Bot.HandleMessage("/s", tb.sonarrSearch)
tb.Bot.HandleMessage("/admin sonarrStatus", tb.sonarrStatus)