Esempio n. 1
0
        static void _RunMovieDemo(int movieId = -1)
        {
            // fetch a movie - this just grabs the top result. By default most
            // results from the API are sorted by popularity, most popular first.
            var movie = (0 > movieId) ? Tmdb.SearchMovies("Star Wars", minPage: 0, maxPage: 0)[0] : new TmdbMovie(movieId);

            //Console.WriteLine(movie.Json); // write the json (may not be complete)

            // A note about paged functions, like the SearchMovies() function above:
            // Be careful not specifying pages. Some searches can return a lot of results.
            // if you want to limit the pages, specify BOTH parameters. Each one is 0 based.
            // The first page is 0. If they are unspecified, all pages are returned. This
            // will take several HTTP requests in most cases. Some of the paged functions
            // *require* the minPage and maxPage parameters. They'll still accept zero and
            // behave the same way, but typically, some of these functions may return
            // hundreds of pages which is why the parameters are required in those cases.

            // The search itself is not cached, but the individual results will be.

            Console.WriteLine();
            Console.WriteLine(movie.Name);
            Console.WriteLine();
            Console.Write("Overview: ");
            Console.WriteLine(movie.Overview);
            Console.WriteLine();

            // write out the genres
            Console.Write("Genres:");
            foreach (var g in movie.GenresById)
            {
                Console.Write(string.Concat(" ", g.Value));
            }

            Console.WriteLine();

            Console.WriteLine();

            // write out the cast
            Console.Write("Cast:");
            foreach (var c in movie.Cast)
            {
                Console.WriteLine("\t{0} as {1}", c.Name, c.Character);
            }

            Console.WriteLine();

            // write out the crew
            Console.Write("Crew:");
            foreach (var c in from cs in movie.Crew select cs)
            {
                Console.WriteLine("\t{1} - {0}", c.Name, c.Job);
            }

            Console.WriteLine();

            // write out the reviews we found, if their are any.
            // this is not cached, which is why it's a paged method
            // the individual reviews are still cached
            var reviews = movie.GetReviews();

            if (0 < reviews.Length)
            {
                Console.WriteLine();
                Console.WriteLine("Reviews:");
                foreach (var r in reviews)
                {
                    Console.WriteLine("\t\"{1}\" - {0}", r.Author, r.Content);
                }
            }

            var images = movie.Images;

            if (0 < images.Length)
            {
                Console.WriteLine();
                Console.WriteLine("Images:");
                foreach (var i in images)
                {
                    Console.WriteLine("{0}: {1}", i.ImageType, Tmdb.GetImageUrl(i.Path));
                }
            }

            // Use the DiscoverXXXX() methods to find movies and TV shows based on many
            // possible parameters.

            try
            {
                // create a request token
                var requestToken = Tmdb.CreateRequestToken();

                // now authenticate the token using the supplied credentials
                Tmdb.AuthenticateRequestToken(requestToken.Key, Username, Password);

                // The other option is to send a user to a website so that *they* can log
                // in and therefore validate this token. Use
                // Tmdb.GetAuthenticateRequestTokenUserUrl() to retrieve the url to send
                // the user to. That url is not an rpc sink - it requires a browser.

                // now our token is "special" in that until it expires it can be used to
                // create sessions. You don't *have* to dispose the session - the remote
                // end will eventually close it, but implementing dispose makes it easer
                // to do what i'm about to do, and also it's good practice (causes a
                // little network traffic)
                using (var session = Tmdb.CreateSession(requestToken.Key))
                {
                    // You can also perform tasks under a guest session simply by
                    // ommitting the sessionId from the calls that accept one. The system
                    // keeps a guest session open but only if it is used. If you really
                    // want to, you can use a guest session without authenticating simply by
                    // not passing around a sessionId to methods that take one. Be careful
                    // using guest actions. The service tracks IP and tracks usage. Don't
                    // use them for automated tasks.

                    // you can use a session to rate a movie under a particular account.
                    // if you don't pass a session, the current guest session is used.
                    movie.Rate(5, session);

                    // you can also delete your rating
                    movie.ClearRating(session);

                    // ending the using block or calling Close() deletes the session,
                    // basically logging the session off
                    // and expiring the session id. This doesn't apply to guest sessions.
                }
            }
            catch
            {
                Console.WriteLine();
                Console.WriteLine("Couldn't log in.");
            }
            // note how the Json data associated with the object grew in response
            // to use using the object. This is to minimize network traffic.
            // Anything fetched before will be retrieved from the cache.
            //Console.WriteLine(movie.Json); // write the json again
        }