Skip to content

Commit

Permalink
Added tv show alias support and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danesparza committed Oct 14, 2013
1 parent 1dda5ce commit 79046c1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
18 changes: 17 additions & 1 deletion ShowInfo.Tests/ShowInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void ForFilename_WithValidSEFileName_ReturnsTVEpisodeInfo()
// Assert
Assert.AreEqual<int>(3, episode.SeasonNumber);
Assert.AreEqual<int>(1, episode.EpisodeNumber);
Assert.AreEqual<string>("Once Upon a Time", episode.ShowName);
Assert.AreEqual<string>("Once Upon a Time (2011)", episode.ShowName);
Assert.AreEqual<string>("The Heart of the Truest Believer", episode.EpisodeTitle);
}

Expand Down Expand Up @@ -72,5 +72,21 @@ public void GetEpisode_WithValidAirdate_ReturnsTVEpisode()
Assert.AreEqual<string>("The Colbert Report", episode.ShowName);
Assert.AreEqual<string>("Vince Gilligan", episode.EpisodeTitle);
}

[TestMethod]
public void GetEpisode_WithAliasValidSeasonEpisode_ReturnsTVEpisode()
{
// Arrange
ShowInformationManager showMgr = new ShowInformationManager();

// Act
TVEpisodeInfo episode = showMgr.GetEpisodeInfo("Once Upon a Time", 3, 2);

// Assert
Assert.AreEqual<int>(3, episode.SeasonNumber);
Assert.AreEqual<int>(2, episode.EpisodeNumber);
Assert.AreEqual<string>("Once Upon a Time (2011)", episode.ShowName); /* Notice this changes */
Assert.AreEqual<string>("Lost Girl", episode.EpisodeTitle);
}
}
}
4 changes: 2 additions & 2 deletions ShowInfo/ShowAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace ShowInfo
[DataContract]
public class ShowAlias
{
[DataMember]
[DataMember(Name="Show")]
public string Show { get; set; }

[DataMember]
[DataMember(Name="Alias")]
public string Alias { get; set; }
}
}
3 changes: 3 additions & 0 deletions ShowInfo/ShowInfo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<Content Include="showalias.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
Expand Down
39 changes: 37 additions & 2 deletions ShowInfo/ShowInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ShowInfoProvider;
using ServiceStack.Text;

namespace ShowInfo
{
Expand Down Expand Up @@ -43,6 +44,8 @@ private void Compose()

private string currentPath = string.Empty;

#region Aliases

/// <summary>
/// Our list of customizable show 'aliases'
/// </summary>
Expand All @@ -56,6 +59,7 @@ private void LoadAliases()
{
// If the show alias file exists, load it up:
currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string data = string.Empty;

try
{
Expand All @@ -66,8 +70,9 @@ private void LoadAliases()
// If the alias file exists, open it:
if(File.Exists(Path.Combine(currentPath, aliasFile)))
{
// TODO: Open the file and laod the aliases

// Open the file and load the aliases
data = File.ReadAllText(Path.Combine(currentPath, aliasFile));
showAliases = data.FromJson<List<ShowAlias>>();
}
}
catch(Exception ex)
Expand All @@ -76,6 +81,27 @@ private void LoadAliases()
}
}

/// <summary>
/// If an alias exists, the show's alias is returned. If the show has no alias,
/// the original show name is returned
/// </summary>
/// <param name="showName">The show name to check</param>
/// <returns></returns>
private string ResolveShowToAlias(string showName)
{
string retval = showName;

// If the given show has an alias, return it
if(this.showAliases.Where(s => s.Show == showName).Any())
{
retval = showAliases.Where(s => s.Show == showName).Select(s => s.Alias).FirstOrDefault();
}

return retval;
}

#endregion

/// <summary>
/// Default constructor
/// </summary>
Expand Down Expand Up @@ -147,6 +173,9 @@ public TVEpisodeInfo GetEpisodeInfoForFilename(string filename)
parsedEpisodeNumber = Convert.ToInt32(seMatch2.Groups["ep_num"].Value);
}

// Resolve the show alias (if it exists)
parsedShowName = ResolveShowToAlias(parsedShowName);

// Based on the type of information parsed, use either
// season/episode data providers or airdate data providers
switch(currentParseType)
Expand Down Expand Up @@ -192,6 +221,9 @@ public TVEpisodeInfo GetEpisodeInfo(string showName, int season, int episode)
// Create our new episode information:
TVEpisodeInfo retval = null;

// Resolve the show alias (if it exists)
showName = ResolveShowToAlias(showName);

foreach(var provider in SEShowInfoProviders)
{
retval = provider.GetShowInfo(showName, season, episode);
Expand All @@ -217,6 +249,9 @@ public TVEpisodeInfo GetEpisodeInfo(string showName, int year, int month, int da
// Create our new episode information:
TVEpisodeInfo retval = null;

// Resolve the show alias (if it exists)
showName = ResolveShowToAlias(showName);

foreach(var provider in ADShowInfoProviders)
{
retval = provider.GetShowInfo(showName, year, month, day);
Expand Down
10 changes: 10 additions & 0 deletions ShowInfo/showalias.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"Show" : "Once Upon a Time",
"Alias" : "Once Upon a Time (2011)"
},
{
"Show" : "Castle 2009",
"Alias" : "Castle"
}
]

0 comments on commit 79046c1

Please sign in to comment.