2013-02-12 21:55:38 +00:00
using System ;
2013-11-08 00:48:52 +00:00
using System.Collections.Generic ;
2013-02-12 21:55:38 +00:00
using System.IO ;
2013-02-12 23:10:44 +00:00
using System.Linq ;
2013-02-12 21:55:38 +00:00
using System.Text ;
2018-01-21 12:38:01 +00:00
using System.Threading.Tasks ;
2016-07-18 20:27:24 +00:00
using Newtonsoft.Json ;
2013-02-12 21:55:38 +00:00
using TMDbLib.Client ;
using TMDbLib.Objects.General ;
2013-02-12 23:10:44 +00:00
using TMDbLib.Objects.Movies ;
2013-11-08 00:48:52 +00:00
using TMDbLib.Objects.Search ;
2013-02-12 21:55:38 +00:00
namespace TestApplication
{
2013-11-08 00:48:52 +00:00
public class Program
2013-02-12 21:55:38 +00:00
{
2018-01-21 12:38:01 +00:00
private static async Task Main ( string [ ] args )
2013-02-12 21:55:38 +00:00
{
2013-11-08 00:48:52 +00:00
// Instantiate a new client, all that's needed is an API key, but it's possible to
// also specify if SSL should be used, and if another server address should be used.
2013-11-13 23:21:04 +00:00
TMDbClient client = new TMDbClient ( "c6b31d1cdad6a56a23f0c913e2482a31" ) ;
2013-02-12 21:55:38 +00:00
2013-11-08 00:48:52 +00:00
// We need the config from TMDb in case we want to get stuff like images
// The config needs to be fetched for each new client we create, but we can cache it to a file (as in this example).
2018-01-21 12:38:01 +00:00
await FetchConfig ( client ) ;
2013-11-08 00:48:52 +00:00
// Try fetching a movie
2018-01-21 12:38:01 +00:00
await FetchMovieExample ( client ) ;
2013-11-08 00:48:52 +00:00
// Once we've got a movie, or person, or so on, we can display images.
// TMDb follow the pattern shown in the following example
// This example also shows an important feature of most of the Get-methods.
2018-01-21 12:38:01 +00:00
await FetchImagesExample ( client ) ;
2013-11-08 00:48:52 +00:00
Console . WriteLine ( "Done." ) ;
Console . ReadLine ( ) ;
}
2018-01-21 12:38:01 +00:00
private static async Task FetchConfig ( TMDbClient client )
2013-11-08 00:48:52 +00:00
{
2016-07-18 20:27:24 +00:00
FileInfo configJson = new FileInfo ( "config.json" ) ;
2013-02-12 21:55:38 +00:00
2016-07-18 20:27:24 +00:00
Console . WriteLine ( "Config file: " + configJson . FullName + ", Exists: " + configJson . Exists ) ;
2013-11-08 00:48:52 +00:00
2016-07-18 20:27:24 +00:00
if ( configJson . Exists & & configJson . LastWriteTimeUtc > = DateTime . UtcNow . AddHours ( - 1 ) )
2013-02-12 21:55:38 +00:00
{
Console . WriteLine ( "Using stored config" ) ;
2016-07-18 20:27:24 +00:00
string json = File . ReadAllText ( configJson . FullName , Encoding . UTF8 ) ;
2018-01-21 12:38:01 +00:00
2016-07-18 20:27:24 +00:00
client . SetConfig ( JsonConvert . DeserializeObject < TMDbConfig > ( json ) ) ;
2013-02-12 21:55:38 +00:00
}
else
{
Console . WriteLine ( "Getting new config" ) ;
2018-01-21 12:38:01 +00:00
var config = await client . GetConfigAsync ( ) ;
2013-02-12 21:55:38 +00:00
Console . WriteLine ( "Storing config" ) ;
2018-01-21 12:38:01 +00:00
string json = JsonConvert . SerializeObject ( config ) ;
2016-07-18 20:27:24 +00:00
File . WriteAllText ( configJson . FullName , json , Encoding . UTF8 ) ;
2013-02-12 21:55:38 +00:00
}
2013-11-08 00:48:52 +00:00
Spacer ( ) ;
}
2018-01-21 12:38:01 +00:00
private static async Task FetchImagesExample ( TMDbClient client )
2013-11-08 00:48:52 +00:00
{
const int movieId = 76338 ; // Thor: The Dark World (2013)
// In the call below, we're fetching the wanted movie from TMDb, but we're also doing something else.
// We're requesting additional data, in this case: Images. This means that the Movie property "Images" will be populated (else it will be null).
// We could combine these properties, requesting even more information in one go:
2016-02-22 20:55:27 +00:00
// client.GetMovieAsync(movieId, MovieMethods.Images);
// client.GetMovieAsync(movieId, MovieMethods.Images | MovieMethods.Releases);
// client.GetMovieAsync(movieId, MovieMethods.Images | MovieMethods.Trailers | MovieMethods.Translations);
2013-11-08 00:48:52 +00:00
//
// .. and so on..
2013-02-12 21:55:38 +00:00
//
2013-11-08 00:48:52 +00:00
// Note: Each method normally corresponds to a property on the resulting object. If you haven't requested the information, the property will most likely be null.
2013-02-13 15:53:58 +00:00
2016-02-22 20:55:27 +00:00
// Also note, that while we could have used 'client.GetMovieImagesAsync()' - it was better to do it like this because we also wanted the Title of the movie.
2018-01-21 12:38:01 +00:00
Movie movie = await client . GetMovieAsync ( movieId , MovieMethods . Images ) ;
2013-11-08 00:48:52 +00:00
Console . WriteLine ( "Fetching images for '" + movie . Title + "'" ) ;
// Images come in two forms, each dispayed below
Console . WriteLine ( "Displaying Backdrops" ) ;
ProcessImages ( client , movie . Images . Backdrops . Take ( 3 ) , client . Config . Images . BackdropSizes ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( "Displaying Posters" ) ;
ProcessImages ( client , movie . Images . Posters . Take ( 3 ) , client . Config . Images . PosterSizes ) ;
Console . WriteLine ( ) ;
Spacer ( ) ;
}
private static void ProcessImages ( TMDbClient client , IEnumerable < ImageData > images , IEnumerable < string > sizes )
{
// Displays basic information about each image, as well as all the possible adresses for it.
// All images should be available in all the sizes provided by the configuration.
foreach ( ImageData imageData in images )
{
Console . WriteLine ( imageData . FilePath ) ;
Console . WriteLine ( "\t " + imageData . Width + "x" + imageData . Height ) ;
// Calculate the images path
// There are multiple resizing available for each image, directly from TMDb.
// There's always the "original" size if you're in doubt which to choose.
foreach ( string size in sizes )
{
Uri imageUri = client . GetImageUrl ( size , imageData . FilePath ) ;
Console . WriteLine ( "\t -> " + imageUri ) ;
}
Console . WriteLine ( ) ;
}
}
2018-01-21 12:38:01 +00:00
private static async Task FetchMovieExample ( TMDbClient client )
2013-11-08 00:48:52 +00:00
{
string query = "Thor" ;
// This example shows the fetching of a movie.
// Say the user searches for "Thor" in order to find "Thor: The Dark World" or "Thor"
2018-01-21 12:38:01 +00:00
SearchContainer < SearchMovie > results = await client . SearchMovieAsync ( query ) ;
2013-11-08 00:48:52 +00:00
// The results is a list, currently on page 1 because we didn't specify any page.
Console . WriteLine ( "Searched for movies: '" + query + "', found " + results . TotalResults + " results in " +
results . TotalPages + " pages" ) ;
// Let's iterate the first few hits
foreach ( SearchMovie result in results . Results . Take ( 3 ) )
{
// Print out each hit
Console . WriteLine ( result . Id + ": " + result . Title ) ;
Console . WriteLine ( "\t Original Title: " + result . OriginalTitle ) ;
Console . WriteLine ( "\t Release date : " + result . ReleaseDate ) ;
Console . WriteLine ( "\t Popularity : " + result . Popularity ) ;
Console . WriteLine ( "\t Vote Average : " + result . VoteAverage ) ;
Console . WriteLine ( "\t Vote Count : " + result . VoteCount ) ;
Console . WriteLine ( ) ;
Console . WriteLine ( "\t Backdrop Path : " + result . BackdropPath ) ;
Console . WriteLine ( "\t Poster Path : " + result . PosterPath ) ;
Console . WriteLine ( ) ;
}
Spacer ( ) ;
}
private static void Spacer ( )
{
Console . WriteLine ( ) ;
Console . WriteLine ( " ----- " ) ;
Console . WriteLine ( ) ;
2013-02-12 21:55:38 +00:00
}
}
2016-07-18 20:27:24 +00:00
}