1

So I am using the movieDB (https://developers.themoviedb.org/3/find/find-by-id) and using it to search for movies in various ways i.e popularity, by name or by ID. I want to now implement a random movie generator onto my site, there is no explicit way to do this on the documentation.

Here is a method I used to search by ID, I have taken the key out, also value in the case below represents the ID of a movie.

  private _TheMovieDb: string = 'https://api.themoviedb.org/3/movie/';

  getOneMovie(value): Observable<any> {
  return this._http.get<any>(this._TheMovieDb + value + '?api_key=MyKey')
  .do(data => console.log('All: ' + JSON.stringify(data)))
  .catch(this.handleError);
 }

My question: I want to search for a random movie ID, how to I randomise the ID and search for it?

VicentVega
  • 191
  • 1
  • 4
  • 11
  • Cache data or store it in a database then randomly select that. – ggdx Dec 15 '17 at 14:04
  • Thats is a lot of data to store in a database, I was thinking more along the lines of randomizing the ID and searching it that way – VicentVega Dec 15 '17 at 14:05

1 Answers1

5

As far as I know, there are two ways to do this without having to download the entire database:

  1. Ping /movie/latest to get the latest inserted movie and its id. Then roll a random number between 0 and that id and fetch it using /movie/movie_id. Remember that if you get a 404 (a deleted id, for instance) you'll have to re-roll.
  2. Download a list of (working) ids from this format: http://files.tmdb.org/p/exports/movie_ids_MM_DD_YYYY.json.gz, and replace the values of M, D and Y. Then roll a random one from this list and do a /movie/movie_id fetch for that.
lennyklb
  • 1,307
  • 2
  • 15
  • 32