2020-05-25 03:29:08 +00:00
|
|
|
package couchpotato
|
|
|
|
|
|
|
|
import (
|
2020-05-29 20:43:12 +00:00
|
|
|
"encoding/json"
|
2021-12-04 06:04:04 +00:00
|
|
|
"fmt"
|
2020-05-29 20:43:12 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2021-12-04 06:04:04 +00:00
|
|
|
"strings"
|
2020-05-29 20:43:12 +00:00
|
|
|
|
2020-05-25 03:29:08 +00:00
|
|
|
"github.com/mattburchett/go_telegram/pkg/core/config"
|
|
|
|
"github.com/yanzay/tbot/v2"
|
|
|
|
)
|
|
|
|
|
2021-12-04 06:04:04 +00:00
|
|
|
type couchpotatoSearch struct {
|
2020-05-29 20:43:12 +00:00
|
|
|
Movies []struct {
|
2021-12-04 06:04:04 +00:00
|
|
|
Title string `json:"original_title"`
|
|
|
|
Imdb string `json:"imdb"`
|
|
|
|
Year int `json:"year"`
|
|
|
|
InLibrary bool `json:"in_library"`
|
|
|
|
InWanted bool `json:"in_wanted"`
|
2020-05-29 20:43:12 +00:00
|
|
|
} `json:"movies"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type request struct {
|
|
|
|
ImdbID string `json:"imdbid"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Year int `json:"year"`
|
|
|
|
Requested bool `json:"requested"`
|
2021-12-04 06:04:04 +00:00
|
|
|
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"`
|
2020-05-29 20:43:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 03:29:08 +00:00
|
|
|
// Search performs the lookup actions within CouchPotato
|
|
|
|
func Search(m *tbot.Message, config config.Config) ([]response, error) {
|
2021-12-04 06:04:04 +00:00
|
|
|
searchLookup, err := http.Get(config.CouchPotato.URL + config.CouchPotato.APIKey + "/movie.search?q=" + url.QueryEscape(strings.TrimPrefix(strings.TrimPrefix(m.Text, "/m"), " ")))
|
2020-05-29 20:43:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-04 06:04:04 +00:00
|
|
|
search := couchpotatoSearch{}
|
2020-05-29 20:43:12 +00:00
|
|
|
|
2021-12-04 06:04:04 +00:00
|
|
|
searchData, err := ioutil.ReadAll(searchLookup.Body)
|
2020-05-29 20:43:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-04 06:04:04 +00:00
|
|
|
requestJSON := json.Unmarshal(searchData, &search)
|
2020-05-29 20:43:12 +00:00
|
|
|
if requestJSON != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-04 06:04:04 +00:00
|
|
|
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),
|
|
|
|
})
|
2020-05-25 03:29:08 +00:00
|
|
|
|
2020-05-29 20:43:12 +00:00
|
|
|
}
|
2021-12-04 06:04:04 +00:00
|
|
|
|
|
|
|
return responseData, err
|
2020-05-25 03:29:08 +00:00
|
|
|
}
|