commit
325af97b63
4 changed files with 146 additions and 0 deletions
@ -0,0 +1,7 @@ |
|||||||
|
# Usage |
||||||
|
|
||||||
|
``` |
||||||
|
curl http://127.0.0.1:3478?token=plext0ken |
||||||
|
``` |
||||||
|
|
||||||
|
Add the request in Radarr under 'lists' > 'steven lu custom' |
||||||
@ -0,0 +1,135 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"log" |
||||||
|
"fmt" |
||||||
|
"sync" |
||||||
|
"regexp" |
||||||
|
"strings" |
||||||
|
"encoding/json" |
||||||
|
"encoding/xml" |
||||||
|
"io/ioutil" |
||||||
|
"net/http" |
||||||
|
) |
||||||
|
|
||||||
|
var ( |
||||||
|
urlBase string = "https://metadata.provider.plex.tv" |
||||||
|
urlType string = "movie" |
||||||
|
urlToken string = "pass_as_get_param:token" |
||||||
|
) |
||||||
|
|
||||||
|
type Watchlist struct { |
||||||
|
Videos []struct { |
||||||
|
Key string `xml:"key,attr"` |
||||||
|
Type string `xml:"type,attr"` |
||||||
|
Url string `xml:"publicPagesURL,attr"` |
||||||
|
Title string `xml:"title,attr"` |
||||||
|
Year int `xml:"year,attr"` |
||||||
|
ImdbId string |
||||||
|
} `xml:"Video"` |
||||||
|
} |
||||||
|
|
||||||
|
type Metadata struct { |
||||||
|
Video struct { |
||||||
|
Title string `xml:"title,attr"` |
||||||
|
Guid []struct { |
||||||
|
Text string `xml:",chardata"` |
||||||
|
ID string `xml:"id,attr"` |
||||||
|
} `xml:"Guid"` |
||||||
|
} `xml:"Video"` |
||||||
|
} |
||||||
|
|
||||||
|
type StevenLuJson struct { |
||||||
|
Title string `json:"title"` |
||||||
|
ImdbId string `json:"imdb_id"` |
||||||
|
} |
||||||
|
|
||||||
|
func urlTypeInt() string { |
||||||
|
switch urlType { |
||||||
|
case "movie": |
||||||
|
return "1" |
||||||
|
case "serie": |
||||||
|
return "2" |
||||||
|
default: |
||||||
|
return "1" |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func generateUrl(get string) string { |
||||||
|
return urlBase + get + "?type=" + urlTypeInt() + "&X-Plex-Token=" + urlToken |
||||||
|
} |
||||||
|
|
||||||
|
func requestWatchlist() Watchlist { |
||||||
|
log.Println(generateUrl("/library/sections/watchlist/all")) |
||||||
|
resp, err := http.Get(generateUrl("/library/sections/watchlist/all")) |
||||||
|
|
||||||
|
if err != nil { |
||||||
|
log.Fatalln(err) |
||||||
|
} |
||||||
|
|
||||||
|
byteWatchlist, _ := ioutil.ReadAll(resp.Body) |
||||||
|
watchlist := Watchlist{} |
||||||
|
xml.Unmarshal(byteWatchlist, &watchlist) |
||||||
|
|
||||||
|
return watchlist |
||||||
|
} |
||||||
|
|
||||||
|
func requestMetadata(watchlist Watchlist) []Metadata { |
||||||
|
sliceMetadata := []Metadata{} |
||||||
|
var wg sync.WaitGroup |
||||||
|
wg.Add(len(watchlist.Videos)) |
||||||
|
for i := 0; i < len(watchlist.Videos); i++ { |
||||||
|
go (func(url string) { |
||||||
|
log.Println(url) |
||||||
|
resp, err := http.Get(url) |
||||||
|
if err != nil { |
||||||
|
log.Fatalln(err) |
||||||
|
} |
||||||
|
byteImdb, _ := ioutil.ReadAll(resp.Body) |
||||||
|
metadata := Metadata{} |
||||||
|
xml.Unmarshal(byteImdb, &metadata) |
||||||
|
sliceMetadata = append(sliceMetadata, metadata) |
||||||
|
wg.Done() |
||||||
|
})(generateUrl(watchlist.Videos[i].Key)) |
||||||
|
} |
||||||
|
wg.Wait() |
||||||
|
return sliceMetadata |
||||||
|
} |
||||||
|
|
||||||
|
func responseStevenLu(metadata []Metadata) string { |
||||||
|
sliceStevenLu := []StevenLuJson{} |
||||||
|
for _, data := range metadata { |
||||||
|
munged := StevenLuJson{} |
||||||
|
munged.Title = data.Video.Title |
||||||
|
for _, v := range data.Video.Guid { |
||||||
|
matched, _ := regexp.MatchString("imdb://", v.ID) |
||||||
|
if matched { |
||||||
|
munged.ImdbId = strings.Replace(v.ID, "imdb://", "", 1) |
||||||
|
} |
||||||
|
} |
||||||
|
sliceStevenLu = append(sliceStevenLu, munged) |
||||||
|
} |
||||||
|
b, err := json.Marshal(sliceStevenLu) |
||||||
|
if err != nil { |
||||||
|
log.Println(err) |
||||||
|
} |
||||||
|
return string(b) |
||||||
|
} |
||||||
|
|
||||||
|
func RequestHandler(w http.ResponseWriter, r *http.Request) { |
||||||
|
query := r.URL.Query() |
||||||
|
urlToken = query["token"][0] |
||||||
|
log.Println("token:", urlToken) |
||||||
|
log.Println("type:", urlType) |
||||||
|
watchlist := requestWatchlist() |
||||||
|
metadata := requestMetadata(watchlist) |
||||||
|
fmt.Fprintf(w, responseStevenLu(metadata)) |
||||||
|
} |
||||||
|
|
||||||
|
func main() { |
||||||
|
|
||||||
|
http.HandleFunc("/", RequestHandler) |
||||||
|
|
||||||
|
log.Println("Listening...") |
||||||
|
log.Fatal(http.ListenAndServe(":3478", nil)) |
||||||
|
} |
||||||
Loading…
Reference in new issue