func main() { args := os.Args if len(args) != 3 { fmt.Println("Give me a URL, and a name and off we go.") return } url := args[1] fmt.Println(url) name := args[2] var result map[string]interface{} var w writeAndPrint response, err := napping.Get(url, nil, &result, nil) if err != nil { fmt.Println(err) return } if response.Status() != 200 { fmt.Printf("Could not download JSON. Sorry. \n The Server responded with: \n %v \n And HTTP status code: %v", response.RawText(), response.Status()) } err = generator.WriteGo(result, name, w) if err != nil { fmt.Print(err) } }
func (api *DockerApi) GetImageDetails(fqImageName string) (*ApiDockerImage, error) { url := strings.Join( []string{ api.configFile.DockerEndpoint, "images", fqImageName, "json", }, "/", ) // For testing, also need to `import "encoding/json"` // result := map[string]json.RawMessage{} result := ApiDockerImage{} Logger.Trace("GetImageDetails Api call to:", url) if resp, err := napping.Get(url, nil, &result, nil); err != nil { Logger.Error("Error while getting Image information from docker,", err) return nil, err } else { switch resp.Status() { case 200: Logger.Trace(result) return &result, nil case 404: Logger.Debug("Image not found") return nil, nil } return nil, nil } }
func Find(externalId string, externalSource string) *FindResult { var result *FindResult cacheStore := cache.NewFileStore(path.Join(config.Get().ProfilePath, "cache")) key := fmt.Sprintf("com.tmdb.find.%s.%s", externalSource, externalId) if err := cacheStore.Get(key, &result); err != nil { rateLimiter.Call(func() { urlValues := napping.Params{ "api_key": apiKey, "external_source": externalSource, }.AsUrlValues() resp, err := napping.Get( tmdbEndpoint+"find/"+externalId, &urlValues, &result, nil, ) if err != nil { log.Error(err.Error()) xbmc.Notify("Quasar", "Failed Find call, check your logs.", config.AddonIcon()) } else if resp.Status() != 200 { message := fmt.Sprintf("Find call bad status: %d", resp.Status()) log.Error(message) xbmc.Notify("Quasar", message, config.AddonIcon()) } cacheStore.Set(key, result, 365*24*time.Hour) }) } return result }
func ListEntities(endpoint string, params napping.Params) []*Entity { var wg sync.WaitGroup entities := make([]*Entity, popularMoviesMaxPages*moviesPerPage) params["api_key"] = apiKey params["language"] = "en" wg.Add(popularMoviesMaxPages) for i := 0; i < popularMoviesMaxPages; i++ { go func(page int) { defer wg.Done() var tmp *EntityList tmpParams := napping.Params{ "page": strconv.Itoa(popularMoviesStartPage + page), } for k, v := range params { tmpParams[k] = v } p := tmpParams.AsUrlValues() rateLimiter.Call(func() { napping.Get( tmdbEndpoint+endpoint, &p, &tmp, nil, ) }) for i, entity := range tmp.Results { entities[page*moviesPerPage+i] = entity } }(i) } wg.Wait() return entities }
func ListMoviesComplete(endpoint string, params napping.Params) Movies { movies := make(Movies, popularMoviesMaxPages*moviesPerPage) params["api_key"] = apiKey wg := sync.WaitGroup{} for i := 0; i < popularMoviesMaxPages; i++ { wg.Add(1) go func(page int) { defer wg.Done() var tmp *EntityList tmpParams := napping.Params{ "page": strconv.Itoa(popularMoviesStartPage + page), } for k, v := range params { tmpParams[k] = v } rateLimiter.Call(func() { napping.Get( tmdbEndpoint+endpoint, &tmpParams, &tmp, nil, ) }) for i, movie := range tmp.Results { movies[page*moviesPerPage+i] = GetMovie(movie.Id, params["language"]) } }(i) } wg.Wait() return movies }
func ListShows(endpoint string, params napping.Params, page int) (shows Shows) { var results *EntityList params["page"] = strconv.Itoa(startPage + page) params["api_key"] = apiKey p := params.AsUrlValues() rateLimiter.Call(func() { resp, err := napping.Get( tmdbEndpoint + endpoint, &p, &results, nil, ) if err != nil { log.Error(err.Error()) xbmc.Notify("Quasar", "ListShows failed, check your logs.", config.AddonIcon()) } else if resp.Status() != 200 { message := fmt.Sprintf("ListShows bad status: %d", resp.Status()) log.Error(message) xbmc.Notify("Quasar", message, config.AddonIcon()) } }) if results != nil { for _, show := range results.Results { shows = append(shows, GetShow(show.Id, params["language"])) } } return shows }
func getMovieById(movieId string, language string) *Movie { var movie *Movie cacheStore := cache.NewFileStore(path.Join(config.Get().ProfilePath, "cache")) key := fmt.Sprintf("com.tmdb.movie.%s.%s", movieId, language) if err := cacheStore.Get(key, &movie); err != nil { rateLimiter.Call(func() { urlValues := napping.Params{ "api_key": apiKey, "append_to_response": "credits,images,alternative_titles,translations,external_ids,trailers", "language": language, }.AsUrlValues() napping.Get( tmdbEndpoint+"movie/"+movieId, &urlValues, &movie, nil, ) if movie != nil { cacheStore.Set(key, movie, cacheTime) } }) } if movie == nil { return nil } switch t := movie.RawPopularity.(type) { case string: popularity, _ := strconv.ParseFloat(t, 64) movie.Popularity = popularity case float64: movie.Popularity = t } return movie }
func GetSeason(showId int, seasonNumber int, language string) *Season { var season *Season cacheStore := cache.NewFileStore(path.Join(config.Get().ProfilePath, "cache")) key := fmt.Sprintf("com.tmdb.season.%d.%d.%s", showId, seasonNumber, language) if err := cacheStore.Get(key, &season); err != nil { rateLimiter.Call(func() { urlValues := napping.Params{ "api_key": apiKey, "append_to_response": "credits,images,videos,external_ids", "language": language, }.AsUrlValues() resp, err := napping.Get( fmt.Sprintf("%stv/%d/season/%d", tmdbEndpoint, showId, seasonNumber), &urlValues, &season, nil, ) if err != nil { panic(err) } if resp.Status() != 200 { panic(errors.New(fmt.Sprintf("Bad status: %d", resp.Status()))) } }) season.EpisodeCount = len(season.Episodes) if season != nil { cacheStore.Set(key, season, cacheTime) } } if season == nil { return nil } return season }
func GetList(listId string, language string, page int) Movies { var results *List listResultsPerPage := config.Get().ResultsPerPage rateLimiter.Call(func() { urlValues := napping.Params{ "api_key": apiKey, }.AsUrlValues() resp, err := napping.Get( tmdbEndpoint+"list/"+listId, &urlValues, &results, nil, ) if err != nil { log.Error(err.Error()) xbmc.Notify("Quasar", "GetList failed, check your logs.", config.AddonIcon()) } else if resp.Status() != 200 { message := fmt.Sprintf("GetList bad status: %d", resp.Status()) log.Error(message) xbmc.Notify("Quasar", message, config.AddonIcon()) } }) tmdbIds := make([]int, 0, listResultsPerPage) for i, movie := range results.Items { if i < page*listResultsPerPage { continue } tmdbIds = append(tmdbIds, movie.Id) if i >= (startPage+page)*listResultsPerPage-1 { break } } return GetMovies(tmdbIds, language) }
func Find(externalId string, externalSource string) *FindResult { var result *FindResult cacheStore := cache.NewFileStore(path.Join(config.Get().ProfilePath, "cache")) key := fmt.Sprintf("com.tmdb.find.%s.%s", externalSource, externalId) if err := cacheStore.Get(key, &result); err != nil { rateLimiter.Call(func() { urlValues := napping.Params{ "api_key": apiKey, "external_source": externalSource, }.AsUrlValues() resp, err := napping.Get( tmdbEndpoint+"find/"+externalId, &urlValues, &result, nil, ) if err != nil { panic(err) } if resp.Status() != 200 { panic(errors.New(fmt.Sprintf("Bad status: %d", resp.Status()))) } cacheStore.Set(key, result, 365*24*time.Hour) }) } return result }
func GetList(listId string, language string) Movies { var results *List rateLimiter.Call(func() { urlValues := napping.Params{ "api_key": apiKey, }.AsUrlValues() resp, err := napping.Get( tmdbEndpoint+"list/"+listId, &urlValues, &results, nil, ) if err != nil { panic(err) } if resp.Status() != 200 { panic(errors.New(fmt.Sprintf("Bad status: %d", resp.Status()))) } }) tmdbIds := make([]int, 0, len(results.Items)) for _, movie := range results.Items { tmdbIds = append(tmdbIds, movie.Id) } return GetMovies(tmdbIds, language) }
func SearchShows(query string, language string) Shows { var results EntityList rateLimiter.Call(func() { urlValues := napping.Params{ "api_key": apiKey, "query": query, }.AsUrlValues() resp, err := napping.Get( tmdbEndpoint+"search/tv", &urlValues, &results, nil, ) if err != nil { panic(err) } if resp.Status() != 200 { panic(errors.New(fmt.Sprintf("Bad status: %d", resp.Status()))) } }) tmdbIds := make([]int, 0, len(results.Results)) for _, entity := range results.Results { tmdbIds = append(tmdbIds, entity.Id) } return GetShows(tmdbIds, language) }
func SearchShows(query string, language string, page int) Shows { var results EntityList rateLimiter.Call(func() { urlValues := napping.Params{ "api_key": apiKey, "query": query, "page": strconv.Itoa(startPage + page), }.AsUrlValues() resp, err := napping.Get( tmdbEndpoint+"search/tv", &urlValues, &results, nil, ) if err != nil { log.Error(err.Error()) xbmc.Notify("Quasar", "SearchShows failed, check your logs.", config.AddonIcon()) } else if resp.Status() != 200 { message := fmt.Sprintf("SearchShows bad status: %d", resp.Status()) log.Error(message) xbmc.Notify("Quasar", message, config.AddonIcon()) } }) tmdbIds := make([]int, 0, len(results.Results)) for _, entity := range results.Results { tmdbIds = append(tmdbIds, entity.Id) } return GetShows(tmdbIds, language) }
func GetShow(showId int, language string) *Show { var show *Show cacheStore := cache.NewFileStore(path.Join(config.Get().ProfilePath, "cache")) key := fmt.Sprintf("com.tmdb.show.%d.%s", showId, language) if err := cacheStore.Get(key, &show); err != nil { rateLimiter.Call(func() { napping.Get( tmdbEndpoint+"tv/"+strconv.Itoa(showId), &napping.Params{"api_key": apiKey, "append_to_response": "credits,images,alternative_titles,translations,external_ids", "language": language}, &show, nil, ) }) if show != nil { cacheStore.Set(key, show, cacheTime) } } if show == nil { return nil } switch t := show.RawPopularity.(type) { case string: if popularity, err := strconv.ParseFloat(t, 64); err == nil { show.Popularity = popularity } case float64: show.Popularity = t } return show }
func TrendingShows() ShowList { var shows ShowList napping.Get(fmt.Sprintf("%s/shows/trending.json/%s", ENDPOINT, APIKEY), nil, &shows, nil) for _, show := range shows { sanitizeIds(show) } return shows }
func (show *Show) Seasons() []*ShowSeason { var seasons []*ShowSeason napping.Get(fmt.Sprintf("%s/show/seasons.json/%s/%s", ENDPOINT, APIKEY, show.TVDBId), nil, &seasons, nil) for _, season := range seasons { season.Show = show } return seasons }
func SearchMovies(query string) MovieList { var movies MovieList napping.Get(fmt.Sprintf("%s/search/movies.json/%s", ENDPOINT, APIKEY), &napping.Params{"query": query, "limit": SearchLimit}, &movies, nil) return movies }
func SearchShows(query string) ShowList { var shows ShowList napping.Get(fmt.Sprintf("%s/search/shows.json/%s", ENDPOINT, APIKEY), &napping.Params{"query": query, "limit": SearchLimit}, &shows, nil) for _, show := range shows { sanitizeIds(show) } return shows }
func ListShowsComplete(endpoint string, params napping.Params, page int) Shows { maxPages := MaxPages if page >= 0 { maxPages = 1 } shows := make(Shows, maxPages*resultsPerPage) params["api_key"] = apiKey wg := sync.WaitGroup{} for i := 0; i < maxPages; i++ { wg.Add(1) currentpage := i startIndex := i * resultsPerPage if page >= 0 { currentpage = page } go func(page int) { defer wg.Done() var tmp *EntityList tmpParams := napping.Params{ "page": strconv.Itoa(startPage + page), } for k, v := range params { tmpParams[k] = v } urlValues := tmpParams.AsUrlValues() rateLimiter.Call(func() { resp, err := napping.Get( tmdbEndpoint+endpoint, &urlValues, &tmp, nil, ) if err != nil { log.Error(err.Error()) xbmc.Notify("Quasar", "ListShows failed, check your logs.", config.AddonIcon()) } else if resp.Status() != 200 { message := fmt.Sprintf("ListShows bad status: %d", resp.Status()) xbmc.Notify("Quasar", message, config.AddonIcon()) } }) if tmp != nil { for i, entity := range tmp.Results { shows[startIndex+i] = GetShow(entity.Id, params["language"]) } } }(currentpage) } wg.Wait() return shows }
func GetMovieGenres(language string) []*Genre { genres := GenreList{} rateLimiter.Call(func() { napping.Get( tmdbEndpoint+"genre/movie/list", &napping.Params{"api_key": apiKey, "language": language}, &genres, nil, ) }) return genres.Genres }
func GetTVGenres(language string) []*Genre { genres := GenreList{} rateLimiter.Call(func() { p := napping.Params{"api_key": apiKey, "language": language}.AsUrlValues() napping.Get( tmdbEndpoint+"genre/tv/list", &p, &genres, nil, ) }) return genres.Genres }
func GetShow(showId int, language string) *Show { var show *Show cacheStore := cache.NewFileStore(path.Join(config.Get().ProfilePath, "cache")) key := fmt.Sprintf("com.tmdb.show.%d.%s", showId, language) if err := cacheStore.Get(key, &show); err != nil { rateLimiter.Call(func() { urlValues := napping.Params{ "api_key": apiKey, "append_to_response": "credits,images,alternative_titles,translations,external_ids", "language": language, }.AsUrlValues() resp, err := napping.Get( tmdbEndpoint+"tv/"+strconv.Itoa(showId), &urlValues, &show, nil, ) if err != nil { switch e := err.(type) { case *json.UnmarshalTypeError: log.Errorf("UnmarshalTypeError: Value[%s] Type[%v] Offset[%d] for %d", e.Value, e.Type, e.Offset, showId) case *json.InvalidUnmarshalError: log.Errorf("InvalidUnmarshalError: Type[%v]", e.Type) default: log.Error(err.Error()) } LogError(err) xbmc.Notify("Quasar", "GetShow failed, check your logs.", config.AddonIcon()) } else if resp.Status() != 200 { message := fmt.Sprintf("GetShow bad status: %d", resp.Status()) log.Error(message) xbmc.Notify("Quasar", message, config.AddonIcon()) } }) if show != nil { cacheStore.Set(key, show, cacheTime) } } if show == nil { return nil } switch t := show.RawPopularity.(type) { case string: if popularity, err := strconv.ParseFloat(t, 64); err == nil { show.Popularity = popularity } case float64: show.Popularity = t } return show }
func GetSeason(showId int, seasonNumber int, language string) *Season { var season *Season cacheStore := cache.NewFileStore(path.Join(config.Get().ProfilePath, "cache")) key := fmt.Sprintf("com.tmdb.season.%d.%d.%s", showId, seasonNumber, language) if err := cacheStore.Get(key, &season); err != nil { rateLimiter.Call(func() { urlValues := napping.Params{ "api_key": apiKey, "append_to_response": "credits,images,videos,external_ids", "language": language, }.AsUrlValues() resp, err := napping.Get( fmt.Sprintf("%stv/%d/season/%d", tmdbEndpoint, showId, seasonNumber), &urlValues, &season, nil, ) if err != nil { log.Error(err.Error()) xbmc.Notify("Quasar", err.Error(), config.AddonIcon()) } else if resp.Status() != 200 { message := fmt.Sprintf("GetSeason bad status: %d", resp.Status()) log.Error(message) xbmc.Notify("Quasar", message, config.AddonIcon()) } }) season.EpisodeCount = len(season.Episodes) // Fix for shows that have translations but return empty strings // for episode names and overviews. // We detect if episodes have their name filled, and if not re-query // with no language set. // See https://github.com/scakemyer/plugin.video.quasar/issues/249 if season.EpisodeCount > 0 { for index := 0; index < season.EpisodeCount; index++ { if season.Episodes[index].Name == "" { season.Episodes[index] = GetEpisode(showId, seasonNumber, index+1, "") } } } if season != nil { cacheStore.Set(key, season, cacheTime) } } if season == nil { return nil } return season }
func ListShowsComplete(endpoint string, params napping.Params, page int) Shows { MaxPages := popularMoviesMaxPages if page >= 0 { MaxPages = 1 } shows := make(Shows, MaxPages*moviesPerPage) params["api_key"] = apiKey wg := sync.WaitGroup{} for i := 0; i < MaxPages; i++ { wg.Add(1) currentpage := i startMoviesIndex := i * moviesPerPage if page >= 0 { currentpage = page } go func(page int) { defer wg.Done() var tmp *EntityList tmpParams := napping.Params{ "page": strconv.Itoa(popularMoviesStartPage + page), } for k, v := range params { tmpParams[k] = v } urlValues := tmpParams.AsUrlValues() rateLimiter.Call(func() { resp, err := napping.Get( tmdbEndpoint+endpoint, &urlValues, &tmp, nil, ) if err != nil { panic(err) } if resp.Status() != 200 { panic(errors.New(fmt.Sprintf("Bad status: %d", resp.Status()))) } }) for i, entity := range tmp.Results { shows[startMoviesIndex+i] = GetShow(entity.Id, params["language"]) } }(currentpage) } wg.Wait() return shows }
func GetItem(id int, lifespan int) HNItem { if items_cache == nil { items_cache = make(map[int]HNItem) } if item, ok := items_cache[id]; ok { if int(time.Now().Unix())-item.RequestTime <= lifespan { return item } } var item HNItem napping.Get(fmt.Sprintf("%s%d.json", item_url, id), nil, &item, nil) item.RequestTime = int(time.Now().Unix()) items_cache[id] = item return item }
func (season *ShowSeason) Episodes() []*ShowEpisode { var episodes []*ShowEpisode napping.Get(fmt.Sprintf("%s/show/season.json/%s/%s/%d", ENDPOINT, APIKEY, season.Show.TVDBId, season.Season), nil, &episodes, nil) var airedEpisodes []*ShowEpisode now := time.Now().UTC().Unix() for _, episode := range episodes { if episode.FirstAiredUTC <= now { episode.Show = season.Show episode.Season = season airedEpisodes = append(airedEpisodes, episode) } } return airedEpisodes }
func GetList(listId string, language string) Movies { var results *List rateLimiter.Call(func() { p := napping.Params{"api_key": apiKey}.AsUrlValues() napping.Get( tmdbEndpoint+"list/"+listId, &p, &results, nil, ) }) tmdbIds := make([]int, 0, len(results.Items)) for _, movie := range results.Items { tmdbIds = append(tmdbIds, movie.Id) } return GetMovies(tmdbIds, language) }
func SearchShows(query string, language string) Shows { var results EntityList rateLimiter.Call(func() { p := napping.Params{"api_key": apiKey, "query": query}.AsUrlValues() napping.Get( tmdbEndpoint+"search/tv", &p, &results, nil, ) }) tmdbIds := make([]int, 0, len(results.Results)) for _, entity := range results.Results { tmdbIds = append(tmdbIds, entity.Id) } return GetShows(tmdbIds, language) }
func SearchMovies(query string, language string) Movies { var results EntityList rateLimiter.Call(func() { napping.Get( tmdbEndpoint+"search/movie", &napping.Params{ "api_key": apiKey, "query": query, }, &results, nil, ) }) tmdbIds := make([]int, 0, len(results.Results)) for _, movie := range results.Results { tmdbIds = append(tmdbIds, movie.Id) } return GetMovies(tmdbIds, language) }
func Find(externalId string, externalSource string) *FindResult { var result *FindResult cacheStore := cache.NewFileStore(path.Join(config.Get().ProfilePath, "cache")) key := fmt.Sprintf("com.tmdb.find.%s.%s", externalSource, externalId) if err := cacheStore.Get(key, &result); err != nil { rateLimiter.Call(func() { napping.Get( tmdbEndpoint+"find/"+externalId, &napping.Params{"api_key": apiKey, "external_source": externalSource}, &result, nil, ) cacheStore.Set(key, result, 365*24*time.Hour) }) } return result }