# Working on a TV shows trivia game – retrieving the data

- Link: https://saguiitay.com/working-on-a-tv-shows-trivia-game-retrieving-the-data/
- Published: 2016-11-29T22:56:00+02:00
- Author: Itay

In our [previous post](https://saguiitay.com/working-on-a-tv-shows-trivia-game/)
we discussed representing TV shows data as a graph using objects and their relations.
In this post, we’ll see how to gather the information needed for our trivia game.
There are a few websites that allow you to follow up on the show you watch, such
as [Rotten Tomatoes](https://www.rottentomatoes.com/browse/tv_series_browse/) and
[TheTvDB](https://thetvdb.com/). I’ve decided to start my work with [TvMaze](https://www.tvmaze.com/),
mainly because they have a [simple API](https://www.tvmaze.com/api) that is easy
to use, and which provides me with a good starting point for my data.

Let’s start by defining a new class:

    ```notranslate
    public class ShowListing
    {
        public int id { get; set; }
        public string name { get; set; }
    }
    ```

With that, we can start retrieving the list of shows from TvMaze:

    ```notranslate
    var hasMore = true;
    var pageNum = 6;
    var webClient = new WebClient();
    do
    {
        string url;
        if (pageNum > 0)
        {
            url = $"http://api.tvmaze.com/shows? Page={pageNum}";
        }
        else
        {
            url = "http://api.tvmaze.com/shows";
        }
        var data = webClient.DownloadString(new Uri(url));
        var pagedSearch = JsonConvert.DeserializeObject<ShowListing[]>(data);

        hasMore = pagedSearch.Length > 0;

        foreach (var serie in pagedSearch)
        {
            Console.WriteLine($"Show: {serie.name} (Id: {serie.id}");
        }
        pageNum++;

        // We'll add a delay, so as not to overload the TvMaze servers and get throttled
        Thread.Sleep(100);

    } while (hasMore);
    ```

We now have a list of TV shows, and their Id. You can find the object model I’ve
created using Visual Studio’s Paste Special feature in the following code:

    ```notranslate
    public class SingleSearch
    {
        public int id { get; set; }
        public string url { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public string language { get; set; }
        public string[] genres { get; set; }
        public string status { get; set; }
        public int? runtime { get; set; }
        public string premiered { get; set; }
        //public Schedule schedule { get; set; }
        //public Rating rating { get; set; }
        public int? weight { get; set; }
        public Network network { get; set; }
        public Externals externals { get; set; }
        public Image image { get; set; }
        public string summary { get; set; }
        public int? updated { get; set; }
        public Links _links { get; set; }
        public Embedded _embedded { get; set; }
    }

    public class ShowListing
    {
        public int id { get; set; }
        public string name { get; set; }
    }

    public class Schedule
    {
        public string time { get; set; }
        public string[] days { get; set; }
    }

    public class Rating
    {
        public float? average { get; set; }
    }

    public class Network
    {
        public int id { get; set; }
        public string name { get; set; }
        public Country country { get; set; }
    }

    public class Country
    {
        public string name { get; set; }
        public string code { get; set; }
        public string timezone { get; set; }
    }

    public class Externals
    {
        public int? tvrage { get; set; }
        public int? thetvdb { get; set; }
        public string imdb { get; set; }
    }

    public class Image
    {
        public string medium { get; set; }
        public string original { get; set; }
    }

    public class Links
    {
        public Self self { get; set; }
        public PreviousEpisode previousepisode { get; set; }
    }

    public class Self
    {
        public string href { get; set; }
    }

    public class PreviousEpisode
    {
        public string href { get; set; }
    }

    public class Embedded
    {
        public Cast[] cast { get; set; }
        public Season[] seasons { get; set; }
    }

    public class Cast
    {
        public Person person { get; set; }
        public Character character { get; set; }
    }

    public class Person
    {
        public int id { get; set; }
        public string url { get; set; }
        public string name { get; set; }
        public Image image { get; set; }
        public Links _links { get; set; }
    }

    public class Character
    {
        public int id { get; set; }
        public string url { get; set; }
        public string name { get; set; }
        public Image image { get; set; }
        public Links _links { get; set; }
    }

    public class Season
    {
        public int id { get; set; }
        public string url { get; set; }
        public int? number { get; set; }
        public string name { get; set; }
        public int? episodeOrder { get; set; }
        public string premiereDate { get; set; }
        public string endDate { get; set; }
        public Network network { get; set; }
        public Image image { get; set; }
        public string summary { get; set; }
        public Links _links { get; set; }
    }

    public class People
    {
        public int id { get; set; }
        public string url { get; set; }
        public string name { get; set; }
        public Image image { get; set; }
        public Links _links { get; set; }
    }

    public class CastCredits
    {
        public CastCredit[] Property1 { get; set; }
    }

    public class CastCredit
    {
        public CastCreditLinks _links { get; set; }
    }

    public class CastCreditLinks
    {
        public CreditsShow show { get; set; }
        public CreditsCharacter character { get; set; }
    }

    public class CreditsShow
    {
        public string href { get; set; }
    }

    public class CreditsCharacter
    {
        public string href { get; set; }
    }
    ```

We can now retrieve the information about a series based on its name or id:

    ```notranslate
    // Use one of the following
    string name = "";
    string id = "";
    string url;
    if (!string.IsNullOrEmpty(name))
    {
        url = $"http://api.tvmaze.com/singlesearch/shows?q={name}embed[]=castembed[]=seasons";
    }
    else
    {
        url = $"http://api.tvmaze.com/shows/{id}?embed[]=castembed[]=seasons";
    }
    var data = webClient.DownloadString(new Uri(url));
    var singleSearch = JsonConvert.DeserializeObject<SingleSearch>(data);
    ```

From here, it’s just about retrieving the information we need:

 * `singleSearch.premiered` Year when show premiered
 * `singleSearch._embedded`
    - `singleSearch._embedded.cast` List of cast
       * `singleSearch._embedded.cast[].person` Actor playing character
          - `singleSearch._embedded.cast[].person.image` Image of the actor
       * `singleSearch._embedded.cast[].character` Character
          - `singleSearch._embedded.cast[].character.image` Image of the character
    - `singleSearch._embedded.seasons` List of seasons
       * `singleSearch._embedded.season[].image` Image of season

In the next post, we’ll see where we can get taglines and quotes for the shows.

### _Related_
