Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for language #46

Merged
merged 4 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions examples/trending/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"os"

tmdb "github.com/cyruzin/golang-tmdb"
)

func main() {
tmdbClient, err := tmdb.Init(os.Getenv("APIKey"))

if err != nil {
fmt.Println(err)
}

trending, err := tmdbClient.GetTrending("movie", "week", nil)

if err != nil {
fmt.Println(err)
}

for _, result := range trending.Results {
fmt.Println(result.Title)
}

fmt.Println("------")

// With options
options := make(map[string]string)
options["page"] = "2"
options["language"] = "es-ES"

trending, err = tmdbClient.GetTrending("tv", "day", options)

if err != nil {
fmt.Println(err)
}

for _, result := range trending.Results {
fmt.Println(result.Name)
}
}
14 changes: 10 additions & 4 deletions trending.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
)

// Trending type is a struct for treding JSON response.
// Trending type is a struct for trending JSON response.
type Trending struct {
Page int `json:"page"`
*TrendingResults
Expand All @@ -19,18 +19,18 @@ type Trending struct {
// list tracks items over a 7 day period, with a 7 day
// half life.
//
// Valid Media Types
// # Valid Media Types
//
// all - Include all movies, TV shows and people in the
// results as a global trending list.
//
// movie - Show the trending movies in the results.
//
// tv - Show the trending tv shows in the results.
// tv - Show the trending TV shows in the results.
//
// person - Show the trending people in the results.
//
// Valid Time Windows
// # Valid Time Windows
//
// day - View the trending list for the day.
//
Expand All @@ -40,6 +40,7 @@ type Trending struct {
func (c *Client) GetTrending(
mediaType string,
timeWindow string,
options map[string]string,
) (*Trending, error) {
tmdbURL := fmt.Sprintf(
"%s/trending/%s/%s?api_key=%s",
Expand All @@ -48,6 +49,11 @@ func (c *Client) GetTrending(
timeWindow,
c.apiKey,
)
if options != nil {
for key, value := range options {
tmdbURL += fmt.Sprintf("&%s=%s", key, value)
}
}
trending := Trending{}
if err := c.get(tmdbURL, &trending); err != nil {
return nil, err
Expand Down
14 changes: 12 additions & 2 deletions trending_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package tmdb

func (suite *TMBDTestSuite) TestGetTrending() {
trending, err := suite.client.GetTrending("movie", "day")
trending, err := suite.client.GetTrending("movie", "day", nil)
suite.Nil(err)
suite.NotNil(trending)
}

func (suite *TMBDTestSuite) TestGetTrendingWithOptions() {
options := map[string]string{
"language": "en-US",
"region": "US",
}
trending, err := suite.client.GetTrending("tv", "week", options)
suite.Nil(err)
suite.NotNil(trending)
}

func (suite *TMBDTestSuite) TestGetTrendingFail() {
suite.client.apiKey = ""
_, err := suite.client.GetTrending("movie", "day")
_, err := suite.client.GetTrending("movie", "day", nil)
suite.NotNil(err)
}
Loading