Skip to content

Commit

Permalink
Refactored SearchMulti object (Issue #145)
Browse files Browse the repository at this point in the history
  • Loading branch information
LordMike committed Aug 16, 2015
1 parent 4ea3333 commit 02c8e9b
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 41 deletions.
15 changes: 10 additions & 5 deletions TMDbLib/Client/TMDbClientSearch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using System;
using RestSharp;
using RestSharp.Deserializers;
using TMDbLib.Objects.General;
using TMDbLib.Objects.Search;
using TMDbLib.Utilities;
Expand All @@ -26,7 +27,7 @@ private RestRequest SearchBuildRequest(string method, string query, int page)
public async Task<SearchContainer<SearchCompany>> SearchCompany(string query, int page = 0)
{
RestRequest req = SearchBuildRequest("company", query, page);

IRestResponse<SearchContainer<SearchCompany>> resp = await _client.ExecuteGetTaskAsync<SearchContainer<SearchCompany>>(req).ConfigureAwait(false);
return resp.Data;
}
Expand Down Expand Up @@ -108,17 +109,21 @@ public async Task<SearchContainer<SearchMulti>> SearchMulti(string query, string
req.AddParameter("language", language);

req.AddParameter("include_adult", includeAdult ? "true" : "false");

IRestResponse<SearchContainer<SearchMulti>> resp = await _client.ExecuteGetTaskAsync<SearchContainer<SearchMulti>>(req).ConfigureAwait(false);

// Do custom parsing
CustomDeserialization.DeserializeMultiSearchContent(resp.Data, resp.Content);

return resp.Data;
}

public async Task<SearchContainer<SearchPerson>> SearchPerson(string query, int page = 0, bool includeAdult = false, SearchQueryType searchType = SearchQueryType.Undefined)
{
RestRequest req = SearchBuildRequest("person", query, page);

req.AddParameter("include_adult", includeAdult ? "true" : "false");

if (searchType != SearchQueryType.Undefined)
req.AddParameter("search_type", searchType.GetDescription());

Expand All @@ -138,7 +143,7 @@ public async Task<SearchContainer<SearchTv>> SearchTvShow(string query, string l
language = language ?? DefaultLanguage;
if (!String.IsNullOrWhiteSpace(language))
req.AddParameter("language", language);

if (firstAirDateYear > 0)
req.AddParameter("first_air_date_year", firstAirDateYear);

Expand Down
18 changes: 18 additions & 0 deletions TMDbLib/Objects/Search/SearchMediaType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.ComponentModel;

namespace TMDbLib.Objects.Search
{
public enum SearchMediaType
{
Unknown,

[Description("movie")]
Movie,

[Description("tv")]
TVShow,

[Description("person")]
Person
}
}
54 changes: 29 additions & 25 deletions TMDbLib/Objects/Search/SearchMulti.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
using System;
using System.Collections.Generic;
using TMDbLib.Objects.General;
using TMDbLib.Utilities;

namespace TMDbLib.Objects.Search
{
public class SearchMulti
{
public int Id { get; set; }
public string Title { set { Name = value; } }

public string Title { set { Name = value; } get { return Name; } }
public string Name { get; set; }
public string OriginalName { get; set; }
public string OriginalTitle { set { OriginalName = value; } }
public DateTime? FirstAirDate { get; set; }
public string BackdropPath { get; set; }

public string BackdropPath { set { PosterPath = value; } get { return PosterPath; } }
public string PosterPath { get; set; }

public SearchMediaType Type { get; set; }
public double Popularity { get; set; }
public double VoteAverage { get; set; }
public int VoteCount { get; set; }
public bool Adult { get; set; }
public DateTime? ReleaseDate { get; set; }
public MediaType Type { get; set; }
public List<string> OriginCountry { get; set; }

public SearchTv AsTvShow { get; set; }
public SearchMovie AsMovie { get; set; }
public SearchPerson AsPerson { get; set; }

// Individual fields not found in all three Media Types
//public string OriginalName { get; set; }
//public string OriginalTitle { set { OriginalName = value; } }
//public DateTime? FirstAirDate { get; set; }
//public double VoteAverage { get; set; }
//public int VoteCount { get; set; }
//public bool Adult { get; set; }
//public DateTime? ReleaseDate { get; set; }
//public List<string> OriginCountry { get; set; }

public string MediaType
{
set
{
switch (value)
{
case "tv":
Type = General.MediaType.TVShow;
break;
case "movie":
Type = General.MediaType.Movie;
break;
default:
Type = General.MediaType.Unknown;
break;
}
if (SearchMediaType.Movie.GetDescription().Equals(value, StringComparison.InvariantCultureIgnoreCase))
Type = SearchMediaType.Movie;
else if (SearchMediaType.TVShow.GetDescription().Equals(value, StringComparison.InvariantCultureIgnoreCase))
Type = SearchMediaType.TVShow;
else if (SearchMediaType.Person.GetDescription().Equals(value, StringComparison.InvariantCultureIgnoreCase))
Type = SearchMediaType.Person;
else
Type = SearchMediaType.Unknown;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions TMDbLib/TMDbLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<Reference Include="System.Runtime.Serialization" />
</ItemGroup>
<ItemGroup>
<Compile Include="Objects\Search\SearchMediaType.cs" />
<Compile Include="Objects\Search\SearchQueryType.cs" />
<Compile Include="Objects\Discover\DiscoverBase.cs" />
<Compile Include="Objects\Discover\DiscoverMovie.cs" />
Expand Down
36 changes: 36 additions & 0 deletions TMDbLib/Utilities/CustomDeserialization.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using RestSharp;
using RestSharp.Deserializers;
using TMDbLib.Objects.General;
using TMDbLib.Objects.Movies;
using TMDbLib.Objects.Search;

namespace TMDbLib.Utilities
{
Expand Down Expand Up @@ -54,5 +57,38 @@ internal static void DeserializeAccountStatesRating(TvEpisodeAccountState accoun
CultureInfo.InvariantCulture.NumberFormat);
}
}

public static void DeserializeMultiSearchContent(SearchContainer<SearchMulti> data, string content)
{
JsonDeserializer deserializer = new JsonDeserializer();

JsonObject objContainer = SimpleJson.DeserializeObject(content) as JsonObject;
JsonArray resultsList = objContainer["results"] as JsonArray;

for (int i = 0; i < data.Results.Count; i++)
{
string jsonItem = resultsList[i].ToString();
SearchMulti multiItem = data.Results[i];

if (multiItem.Type == SearchMediaType.TVShow)
{
SearchTv asTv = deserializer.Deserialize<SearchTv>(new RestResponse { Content = jsonItem });

multiItem.AsTvShow = asTv;
}
else if (multiItem.Type == SearchMediaType.Movie)
{
SearchMovie asMovie = deserializer.Deserialize<SearchMovie>(new RestResponse { Content = jsonItem });

multiItem.AsMovie = asMovie;
}
else if (multiItem.Type == SearchMediaType.Person)
{
SearchPerson asPerson = deserializer.Deserialize<SearchPerson>(new RestResponse { Content = jsonItem });

multiItem.AsPerson = asPerson;
}
}
}
}
}
27 changes: 16 additions & 11 deletions TMDbLibTests/ClientSearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,25 @@ public void TestSearchMulti()

Assert.IsNotNull(item);
Assert.AreEqual(1412, item.Id);
Assert.AreEqual("/dXTyVDTIgeByvUOUEiHjbi8xX9A.jpg", item.BackdropPath);
Assert.AreEqual(new DateTime(2012, 10, 10), item.FirstAirDate);
Assert.AreEqual(MediaType.TVShow, item.Type);
Assert.AreEqual(SearchMediaType.TVShow, item.Type);
Assert.AreEqual("Arrow", item.Name);
Assert.AreEqual("Arrow", item.OriginalName);
Assert.AreEqual("/mo0FP1GxOFZT4UDde7RFDz5APXF.jpg", item.PosterPath);
Assert.AreEqual("Arrow", item.Title);
Assert.IsTrue(item.Popularity > 0);
Assert.IsTrue(item.VoteAverage > 0);
Assert.IsTrue(item.VoteCount > 0);

Assert.IsNotNull(item.OriginCountry);
Assert.AreEqual(2, item.OriginCountry.Count);
Assert.IsTrue(item.OriginCountry.Contains("US"));
Assert.IsTrue(item.OriginCountry.Contains("CA"));
Assert.IsNotNull(item.PosterPath);
Assert.IsNotNull(item.BackdropPath);

foreach (SearchMulti multi in result.Results)
{
if (multi.Type == SearchMediaType.Movie)
Assert.IsNotNull(multi.AsMovie);

if (multi.Type == SearchMediaType.TVShow)
Assert.IsNotNull(multi.AsTvShow);

if (multi.Type == SearchMediaType.Person)
Assert.IsNotNull(multi.AsPerson);
}
}

/// <summary>
Expand Down

0 comments on commit 02c8e9b

Please sign in to comment.