Skip to content

Commit

Permalink
Added lang flag / Some tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
cyruzin committed Oct 22, 2019
1 parent 52dc90c commit 495f74e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 39 deletions.
59 changes: 44 additions & 15 deletions cmd/trailer/movie.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package cmd

import (
"log"
"fmt"
"strings"

"github.com/spf13/cobra"
)

var (
movieOverview = "N/A"
usReleaseDate = "N/A"
)

func (t *Trailer) movieCmd() *cobra.Command {
return &cobra.Command{
Use: "movie [name of the movie]",
Expand All @@ -16,45 +21,69 @@ func (t *Trailer) movieCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
argsJoin := strings.Join(args, " ")

search, err := t.client.GetSearchMovies(argsJoin, nil)
language, err := cmd.Flags().GetString("lang")
if err != nil {
fmt.Println(err)
}

options := make(map[string]string)
if language != "" {
options["language"] = language
}

search, err := t.client.GetSearchMovies(argsJoin, options)
if err != nil {
log.Println(errorFetch)
fmt.Println(errorFetch)
return
}

if len(search.Results) <= 0 {
log.Println("No results for:", argsJoin)
fmt.Println("No results for:", argsJoin)
return
}

if search.Results[0].Overview != "" {
movieOverview = search.Results[0].Overview
}

releaseDates, err := t.client.GetMovieReleaseDates(int(search.Results[0].ID))
if err != nil {
log.Println(errorFetch)
fmt.Println(errorFetch)
return
}

usReleaseDate := "NA"

for _, res := range releaseDates.Results {
if res.Iso3166_1 == "US" {
usReleaseDate = parseDate(res.ReleaseDates[0].ReleaseDate)
}
}


trailers, err := t.client.GetMovieVideos(int(search.Results[0].ID), nil)
trailers, err := t.client.GetMovieVideos(int(search.Results[0].ID), options)
if err != nil {
log.Println(errorFetch)
fmt.Println(errorFetch)
return
}

if len(trailers.Results) <= 0 {
if language != "" {
fmt.Printf("No trailers are available for %s in the %s language.\n", argsJoin, language)
return
}
fmt.Printf("No trailers available for: %s.\n", argsJoin)
return
}

log.Printf("Results for: %s (US Release: %s)", argsJoin, usReleaseDate)
log.Println("")
fmt.Println("Results for: ", argsJoin)
fmt.Println("")
fmt.Println("Overview: ", movieOverview)
fmt.Println("")
fmt.Printf("US Release: %s\n", usReleaseDate)
fmt.Println("")

for _, trailer := range trailers.Results {
log.Println(trailer.Name)
log.Println(youtubeURL + trailer.Key)
log.Println("")
fmt.Println(trailer.Name)
fmt.Println(youtubeURL + trailer.Key)
fmt.Println("")
}
},
}
Expand Down
22 changes: 16 additions & 6 deletions cmd/trailer/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"log"
"fmt"

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

Expand All @@ -12,6 +12,9 @@ const youtubeURL = "https://www.youtube.com/watch?v="

const errorFetch = "Oh no! Looks like Thanos snapped his fingers! The Avengers are working to fix this."

// Lang string for language flag.
var Lang string

// Trailer client structure.
type Trailer struct {
client *tmdb.Client
Expand All @@ -29,14 +32,21 @@ func (t *Trailer) RootCmd() *cobra.Command {
Short: "Trailer is a tool that get trailers.",
Long: "Trailer is a tool that will quickly bring the link of any movie with a few commands.",
Run: func(cmd *cobra.Command, args []string) {
log.Println("Let's search some trailer?")
log.Println("")
log.Println(`Type "trailer --help" to see the commands available.`)
fmt.Println("Let's search some trailer?")
fmt.Println("")
fmt.Println(`Type "trailer --help" to see the commands available.`)
},
}

rootCmd.AddCommand(t.movieCmd())
rootCmd.AddCommand(t.tvCmd())
movie := t.movieCmd()
tv := t.tvCmd()

rootCmd.AddCommand(movie)
movie.Flags().StringVarP(&Lang, "lang", "l", "en-US", "language ouput")

rootCmd.AddCommand(tv)
tv.Flags().StringVarP(&Lang, "lang", "l", "en-US", "language ouput")

rootCmd.AddCommand(versionCmd())

return rootCmd
Expand Down
65 changes: 49 additions & 16 deletions cmd/trailer/tv.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package cmd

import (
"log"
"fmt"
"strings"

"github.com/spf13/cobra"
)

var (
tvOverview = "N/A"
firstAiredDate = "N/A"
lastAiredDate = "N/A"
)

func (t *Trailer) tvCmd() *cobra.Command {
return &cobra.Command{
Use: "tv [name of the tv show]",
Expand All @@ -16,45 +22,72 @@ func (t *Trailer) tvCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
argsJoin := strings.Join(args, " ")

search, err := t.client.GetSearchTVShow(argsJoin, nil)
language, err := cmd.Flags().GetString("lang")
if err != nil {
fmt.Println(err)
}

options := make(map[string]string)
if language != "" {
options["language"] = language
}

search, err := t.client.GetSearchTVShow(argsJoin, options)
if err != nil {
log.Println(errorFetch)
fmt.Println(errorFetch)
return
}

if len(search.Results) <= 0 {
log.Println("No results for:", argsJoin)
fmt.Println("No results for:", argsJoin)
return
}

tvDetails, err := t.client.GetTVDetails(int(search.Results[0].ID), nil)
tvDetails, err := t.client.GetTVDetails(int(search.Results[0].ID), options)
if err != nil {
log.Println(errorFetch)
fmt.Println(errorFetch)
return
}

firstAiredDate := "NA"
if tvDetails.Overview != "" {
tvOverview = tvDetails.Overview
}

if tvDetails.FirstAirDate != "" {
firstAiredDate = parseDate(tvDetails.FirstAirDate)
}

trailers, err := t.client.GetTVVideos(int(search.Results[0].ID), nil)
if tvDetails.LastAirDate != "" {
lastAiredDate = parseDate(tvDetails.LastAirDate)
}

trailers, err := t.client.GetTVVideos(int(search.Results[0].ID), options)
if err != nil {
log.Println(errorFetch)
fmt.Println(errorFetch)
return
}

if len(trailers.Results) <= 0 {
if language != "" {
fmt.Printf("No trailers are available for %s in the %s language.\n", argsJoin, language)
return
}
fmt.Printf("No trailers available for: %s.\n", argsJoin)
return
}


log.Printf("Results for: %s (First Aired: %s)", argsJoin, firstAiredDate)
log.Println("Results for:", argsJoin)
log.Println("")
fmt.Println("Results for: ", argsJoin)
fmt.Println("")
fmt.Println("Overview:", tvOverview)
fmt.Println("")
fmt.Printf("First Aired: %s\n", firstAiredDate)
fmt.Printf("Last Aired: %s\n", lastAiredDate)
fmt.Println("")

for _, trailer := range trailers.Results {
log.Println(trailer.Name)
log.Println(youtubeURL + trailer.Key)
log.Println("")
fmt.Println(trailer.Name)
fmt.Println(youtubeURL + trailer.Key)
fmt.Println("")
}
},
}
Expand Down
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ func init() {
}

apiKey, _ = os.LookupEnv("TMDB_KEY")

}
}

func main() {

client, err := tmdb.Init(apiKey)
if err != nil {
log.Println(err)
Expand Down

0 comments on commit 495f74e

Please sign in to comment.