Added functions to get credits and format the data for viewing
This commit is contained in:
54
tmdb/credits.go
Normal file
54
tmdb/credits.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package tmdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Credits struct {
|
||||||
|
ID int32 `json:"id"`
|
||||||
|
Cast []Cast `json:"cast"`
|
||||||
|
Crew []Crew `json:"crew"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Cast struct {
|
||||||
|
Adult bool `json:"adult"`
|
||||||
|
Gender int `json:"gender"`
|
||||||
|
ID int32 `json:"id"`
|
||||||
|
KnownFor string `json:"known_for_department"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
OriginalName string `json:"original_name"`
|
||||||
|
Popularity int `json:"popularity"`
|
||||||
|
Profile string `json:"profile_path"`
|
||||||
|
CastID int32 `json:"cast_id"`
|
||||||
|
Character string `json:"character"`
|
||||||
|
CreditID string `json:"credit_id"`
|
||||||
|
Order int `json:"order"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Crew struct {
|
||||||
|
Adult bool `json:"adult"`
|
||||||
|
Gender int `json:"gender"`
|
||||||
|
ID int32 `json:"id"`
|
||||||
|
KnownFor string `json:"known_for_department"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
OriginalName string `json:"original_name"`
|
||||||
|
Popularity int `json:"popularity"`
|
||||||
|
Profile string `json:"profile_path"`
|
||||||
|
CreditID string `json:"credit_id"`
|
||||||
|
Department string `json:"department"`
|
||||||
|
Job string `json:"job"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCredits(movieid int32, token string) (*Credits, error) {
|
||||||
|
url := fmt.Sprintf("https://api.themoviedb.org/3/movie/%v/credits?language=en-US", movieid)
|
||||||
|
data, err := tmdbGet(url, token)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "tmdbGet")
|
||||||
|
}
|
||||||
|
credits := Credits{}
|
||||||
|
json.Unmarshal(data, &credits)
|
||||||
|
return &credits, nil
|
||||||
|
}
|
||||||
41
tmdb/crew_functions.go
Normal file
41
tmdb/crew_functions.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package tmdb
|
||||||
|
|
||||||
|
import "sort"
|
||||||
|
|
||||||
|
type BilledCrew struct {
|
||||||
|
Name string
|
||||||
|
Roles []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (credits *Credits) BilledCrew() []BilledCrew {
|
||||||
|
crewmap := make(map[string][]string)
|
||||||
|
billedcrew := []BilledCrew{}
|
||||||
|
for _, crew := range credits.Crew {
|
||||||
|
if crew.Job == "Director" ||
|
||||||
|
crew.Job == "Screenplay" ||
|
||||||
|
crew.Job == "Writer" ||
|
||||||
|
crew.Job == "Novel" ||
|
||||||
|
crew.Job == "Story" {
|
||||||
|
crewmap[crew.Name] = append(crewmap[crew.Name], crew.Job)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, jobs := range crewmap {
|
||||||
|
billedcrew = append(billedcrew, BilledCrew{Name: name, Roles: jobs})
|
||||||
|
}
|
||||||
|
for i := range billedcrew {
|
||||||
|
sort.Strings(billedcrew[i].Roles)
|
||||||
|
}
|
||||||
|
sort.Slice(billedcrew, func(i, j int) bool {
|
||||||
|
return billedcrew[i].Roles[0] < billedcrew[j].Roles[0]
|
||||||
|
})
|
||||||
|
return billedcrew
|
||||||
|
}
|
||||||
|
|
||||||
|
func (billedcrew *BilledCrew) FRoles() string {
|
||||||
|
jobs := ""
|
||||||
|
for _, job := range billedcrew.Roles {
|
||||||
|
jobs += job + ", "
|
||||||
|
}
|
||||||
|
return jobs[:len(jobs)-2]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user