Skip to content

Commit

Permalink
TvDB Linking Rewrite!
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Apr 14, 2018
1 parent 286ee3f commit aa94979
Show file tree
Hide file tree
Showing 44 changed files with 1,223 additions and 783 deletions.
2 changes: 1 addition & 1 deletion Shoko.Commons
Submodule Shoko.Commons updated 1 files
+1 −1 Shoko.Models
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ public CL_AniDB_AnimeCrossRefs GetCrossRefDetails(int animeID)
SVR_AniDB_Anime anime = RepoFactory.AniDB_Anime.GetByAnimeID(animeID); SVR_AniDB_Anime anime = RepoFactory.AniDB_Anime.GetByAnimeID(animeID);
if (anime == null) return result; if (anime == null) return result;


var xrefs = anime.GetCrossRefTvDBV2().ToList(); var xrefs = RepoFactory.CrossRef_AniDB_TvDB.GetV2LinksFromAnime(animeID);


// TvDB // TvDB
foreach (CrossRef_AniDB_TvDBV2 xref in xrefs) result.CrossRef_AniDB_TvDB = xrefs;
result.CrossRef_AniDB_TvDB.Add(xref);


foreach (TvDB_Episode ep in anime.GetTvDBEpisodes()) foreach (TvDB_Episode ep in anime.GetTvDBEpisodes())
result.TvDBEpisodes.Add(ep); result.TvDBEpisodes.Add(ep);
Expand Down Expand Up @@ -229,7 +228,7 @@ public string UseMyTvDBLinksWebCache(int animeID)
try try
{ {
// Get all the links for this user and anime // Get all the links for this user and anime
List<CrossRef_AniDB_TvDBV2> xrefs = RepoFactory.CrossRef_AniDB_TvDBV2.GetByAnimeID(animeID); List<CrossRef_AniDB_TvDB> xrefs = RepoFactory.CrossRef_AniDB_TvDB.GetByAnimeID(animeID);
if (xrefs == null) return "No Links found to use"; if (xrefs == null) return "No Links found to use";


SVR_AniDB_Anime anime = RepoFactory.AniDB_Anime.GetByAnimeID(animeID); SVR_AniDB_Anime anime = RepoFactory.AniDB_Anime.GetByAnimeID(animeID);
Expand All @@ -254,9 +253,9 @@ public string UseMyTvDBLinksWebCache(int animeID)
if (foundLinks) return "Links already exist, please approve them instead"; if (foundLinks) return "Links already exist, please approve them instead";


// send the links to the web cache // send the links to the web cache
foreach (CrossRef_AniDB_TvDBV2 xref in xrefs) foreach (CrossRef_AniDB_TvDB xref in xrefs)
{ {
AzureWebAPI.Send_CrossRefAniDBTvDB(xref, anime.MainTitle); AzureWebAPI.Send_CrossRefAniDBTvDB(xref.ToV2Model(), anime.MainTitle);
} }


// now get the links back from the cache and approve them // now get the links back from the cache and approve them
Expand Down Expand Up @@ -445,7 +444,7 @@ public List<CrossRef_AniDB_TvDBV2> GetTVDBCrossRefV2(int animeID)
{ {
try try
{ {
return RepoFactory.CrossRef_AniDB_TvDBV2.GetByAnimeID(animeID).Cast<CrossRef_AniDB_TvDBV2>().ToList(); return RepoFactory.CrossRef_AniDB_TvDB.GetV2LinksFromAnime(animeID);
} }
catch (Exception ex) catch (Exception ex)
{ {
Expand All @@ -454,11 +453,11 @@ public List<CrossRef_AniDB_TvDBV2> GetTVDBCrossRefV2(int animeID)
} }
} }


public List<CrossRef_AniDB_TvDB_Episode> GetTVDBCrossRefEpisode(int animeID) public List<CrossRef_AniDB_TvDB_Episode_Override> GetTVDBCrossRefEpisode(int animeID)
{ {
try try
{ {
return RepoFactory.CrossRef_AniDB_TvDB_Episode.GetByAnimeID(animeID).ToList(); return RepoFactory.CrossRef_AniDB_TvDB_Episode_Override.GetByAnimeID(animeID).ToList();
} }
catch (Exception ex) catch (Exception ex)
{ {
Expand Down Expand Up @@ -505,24 +504,21 @@ public string LinkAniDBTvDB(CrossRef_AniDB_TvDBV2 link)
{ {
try try
{ {
CrossRef_AniDB_TvDBV2 xref = RepoFactory.CrossRef_AniDB_TvDBV2.GetByTvDBID(link.TvDBID, CrossRef_AniDB_TvDB xref = RepoFactory.CrossRef_AniDB_TvDB.GetByAniDBAndTvDBID(link.AnimeID, link.TvDBID);
link.TvDBSeasonNumber, link.TvDBStartEpisodeNumber, link.AnimeID, link.AniDBStartEpisodeType,
link.AniDBStartEpisodeNumber);


if (xref != null && link.IsAdditive) if (xref != null && link.IsAdditive)
{ {
string msg = $"You have already linked Anime ID {xref.AnimeID} to this TvDB show/season/ep"; string msg = $"You have already linked Anime ID {xref.AniDBID} to this TvDB show/season/ep";
SVR_AniDB_Anime anime = RepoFactory.AniDB_Anime.GetByAnimeID(xref.AnimeID); SVR_AniDB_Anime anime = RepoFactory.AniDB_Anime.GetByAnimeID(xref.AniDBID);
if (anime != null) if (anime != null)
msg = msg =
$"You have already linked Anime {anime.MainTitle} ({xref.AnimeID}) to this TvDB show/season/ep"; $"You have already linked Anime {anime.MainTitle} ({xref.AniDBID}) to this TvDB show/season/ep";
return msg; return msg;
} }


// we don't need to proactively remove the link here anymore, as all links are removed when it is not marked as additive // we don't need to proactively remove the link here anymore, as all links are removed when it is not marked as additive
CommandRequest_LinkAniDBTvDB cmdRequest = new CommandRequest_LinkAniDBTvDB(link.AnimeID, CommandRequest_LinkAniDBTvDB cmdRequest =
(EpisodeType) link.AniDBStartEpisodeType, link.AniDBStartEpisodeNumber, link.TvDBID, new CommandRequest_LinkAniDBTvDB(link.AnimeID, link.TvDBID, link.IsAdditive);
link.TvDBSeasonNumber, link.TvDBStartEpisodeNumber, false, link.IsAdditive);
cmdRequest.Save(); cmdRequest.Save();


return string.Empty; return string.Empty;
Expand All @@ -534,11 +530,11 @@ public string LinkAniDBTvDB(CrossRef_AniDB_TvDBV2 link)
} }
} }


public string LinkAniDBTvDBEpisode(int aniDBID, int tvDBID, int animeID) public string LinkAniDBTvDBEpisode(int aniDBID, int tvDBID)
{ {
try try
{ {
TvDBApiHelper.LinkAniDBTvDBEpisode(aniDBID, tvDBID, animeID); TvDBApiHelper.LinkAniDBTvDBEpisode(aniDBID, tvDBID);


return string.Empty; return string.Empty;
} }
Expand All @@ -562,10 +558,10 @@ public string RemoveLinkAniDBTvDBForAnime(int animeID)


if (ser == null) return "Could not find Series for Anime!"; if (ser == null) return "Could not find Series for Anime!";


List<CrossRef_AniDB_TvDBV2> xrefs = RepoFactory.CrossRef_AniDB_TvDBV2.GetByAnimeID(animeID); List<CrossRef_AniDB_TvDB> xrefs = RepoFactory.CrossRef_AniDB_TvDB.GetByAnimeID(animeID);
if (xrefs == null) return string.Empty; if (xrefs == null) return string.Empty;


foreach (CrossRef_AniDB_TvDBV2 xref in xrefs) foreach (CrossRef_AniDB_TvDB xref in xrefs)
{ {
// check if there are default images used associated // check if there are default images used associated
List<AniDB_Anime_DefaultImage> images = RepoFactory.AniDB_Anime_DefaultImage.GetByAnimeID(animeID); List<AniDB_Anime_DefaultImage> images = RepoFactory.AniDB_Anime_DefaultImage.GetByAnimeID(animeID);
Expand All @@ -580,9 +576,7 @@ public string RemoveLinkAniDBTvDBForAnime(int animeID)
} }
} }


TvDBApiHelper.RemoveLinkAniDBTvDB(xref.AnimeID, (EpisodeType) xref.AniDBStartEpisodeType, TvDBApiHelper.RemoveLinkAniDBTvDB(xref.AniDBID, xref.TvDBID);
xref.AniDBStartEpisodeNumber,
xref.TvDBID, xref.TvDBSeasonNumber, xref.TvDBStartEpisodeNumber);
} }


return string.Empty; return string.Empty;
Expand Down Expand Up @@ -615,8 +609,7 @@ public string RemoveLinkAniDBTvDB(CrossRef_AniDB_TvDBV2 link)
} }
} }


TvDBApiHelper.RemoveLinkAniDBTvDB(link.AnimeID, (EpisodeType) link.AniDBStartEpisodeType, TvDBApiHelper.RemoveLinkAniDBTvDB(link.AnimeID, link.TvDBID);
link.AniDBStartEpisodeNumber, link.TvDBID, link.TvDBSeasonNumber, link.TvDBStartEpisodeNumber);


return string.Empty; return string.Empty;
} }
Expand All @@ -627,20 +620,21 @@ public string RemoveLinkAniDBTvDB(CrossRef_AniDB_TvDBV2 link)
} }
} }


public string RemoveLinkAniDBTvDBEpisode(int aniDBEpisodeID) public string RemoveLinkAniDBTvDBEpisode(int aniDBEpisodeID, int tvDBEpisodeID)
{ {
try try
{ {
AniDB_Episode ep = RepoFactory.AniDB_Episode.GetByEpisodeID(aniDBEpisodeID); AniDB_Episode ep = RepoFactory.AniDB_Episode.GetByEpisodeID(aniDBEpisodeID);


if (ep == null) return "Could not find Episode"; if (ep == null) return "Could not find Episode";


CrossRef_AniDB_TvDB_Episode xref = CrossRef_AniDB_TvDB_Episode_Override xref =
RepoFactory.CrossRef_AniDB_TvDB_Episode.GetByAniDBEpisodeID(aniDBEpisodeID); RepoFactory.CrossRef_AniDB_TvDB_Episode_Override.GetByAniDBAndTvDBEpisodeIDs(aniDBEpisodeID,
tvDBEpisodeID);
if (xref == null) return "Could not find Link!"; if (xref == null) return "Could not find Link!";




RepoFactory.CrossRef_AniDB_TvDB_Episode.Delete(xref.CrossRef_AniDB_TvDB_EpisodeID); RepoFactory.CrossRef_AniDB_TvDB_Episode_Override.Delete(xref.CrossRef_AniDB_TvDB_Episode_OverrideID);


return string.Empty; return string.Empty;
} }
Expand Down Expand Up @@ -1464,4 +1458,4 @@ public string UpdateMovieDBData(int movieD)


#endregion #endregion
} }
} }
201 changes: 16 additions & 185 deletions Shoko.Server/API/v1/Implementations/ShokoServiceImplementationMetro.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Metro_CommunityLinks GetCommunityLinks(int animeID)
} }


// TvDB // TvDB
List<CrossRef_AniDB_TvDBV2> tvdbRef = anime.GetCrossRefTvDBV2(); List<CrossRef_AniDB_TvDB> tvdbRef = anime.GetCrossRefTvDB();
if (tvdbRef != null && tvdbRef.Count > 0) if (tvdbRef != null && tvdbRef.Count > 0)
{ {
contract.TvDB_ID = tvdbRef[0].TvDBID.ToString(); contract.TvDB_ID = tvdbRef[0].TvDBID.ToString();
Expand Down Expand Up @@ -860,195 +860,26 @@ public static void SetTvDBInfo(int anidbid, AniDB_Episode ep, ref Metro_Anime_Ep


public static void SetTvDBInfo(TvDBSummary tvSummary, AniDB_Episode ep, ref Metro_Anime_Episode contract) public static void SetTvDBInfo(TvDBSummary tvSummary, AniDB_Episode ep, ref Metro_Anime_Episode contract)
{ {
#region episode override var override_link = RepoFactory.CrossRef_AniDB_TvDB_Episode_Override.GetByAniDBEpisodeID(ep.EpisodeID);

if (override_link.Any(a => a != null))
// check if this episode has a direct tvdb over-ride
if (tvSummary.DictTvDBCrossRefEpisodes.ContainsKey(ep.EpisodeID))
{
foreach (TvDB_Episode tvep in tvSummary.DictTvDBEpisodes.Values)
{
if (tvSummary.DictTvDBCrossRefEpisodes[ep.EpisodeID] == tvep.Id)
{
if (string.IsNullOrEmpty(tvep.Overview))
contract.EpisodeOverview = "Episode Overview Not Available";
else
contract.EpisodeOverview = tvep.Overview;

if (string.IsNullOrEmpty(tvep.GetFullImagePath()) || !File.Exists(tvep.GetFullImagePath()))
{
contract.ImageType = 0;
contract.ImageID = 0;
}
else
{
contract.ImageType = (int) ImageEntityType.TvDB_Episode;
contract.ImageID = tvep.TvDB_EpisodeID;
}

if (ServerSettings.EpisodeTitleSource == DataSourceType.TheTvDB &&
!string.IsNullOrEmpty(tvep.EpisodeName))
contract.EpisodeName = tvep.EpisodeName;

return;
}
}
}

#endregion

#region normal episodes

// now do stuff to improve performance
if (ep.GetEpisodeTypeEnum() == EpisodeType.Episode)
{ {
if (tvSummary.CrossRefTvDBV2 != null && tvSummary.CrossRefTvDBV2.Count > 0) var tvep = RepoFactory.TvDB_Episode.GetByTvDBID(override_link.FirstOrDefault().TvDBEpisodeID);
{ contract.EpisodeName = tvep.EpisodeName;
// find the xref that is right contract.EpisodeOverview = tvep.Overview;
// relies on the xref's being sorted by season number and then episode number (desc) contract.ImageID = tvep.Id;
List<CrossRef_AniDB_TvDBV2> tvDBCrossRef = tvSummary.CrossRefTvDBV2 contract.ImageType = (int) ImageEntityType.TvDB_Episode;
.OrderByDescending(a => a.AniDBStartEpisodeNumber) return;
.ToList();


bool foundStartingPoint = false;
CrossRef_AniDB_TvDBV2 xrefBase = null;
foreach (CrossRef_AniDB_TvDBV2 xrefTV in tvDBCrossRef)
{
if (xrefTV.AniDBStartEpisodeType != (int) EpisodeType.Episode) continue;
if (ep.EpisodeNumber >= xrefTV.AniDBStartEpisodeNumber)
{
foundStartingPoint = true;
xrefBase = xrefTV;
break;
}
}

// we have found the starting epiosde numbder from AniDB
// now let's check that the TvDB Season and Episode Number exist
if (foundStartingPoint)
{
Dictionary<int, int> dictTvDBSeasons = null;
Dictionary<int, TvDB_Episode> dictTvDBEpisodes = null;
foreach (TvDBDetails det in tvSummary.TvDetails.Values)
{
if (det.TvDBID == xrefBase.TvDBID)
{
dictTvDBSeasons = det.DictTvDBSeasons;
dictTvDBEpisodes = det.DictTvDBEpisodes;
break;
}
}

if (dictTvDBSeasons.ContainsKey(xrefBase.TvDBSeasonNumber))
{
int episodeNumber = dictTvDBSeasons[xrefBase.TvDBSeasonNumber] +
(ep.EpisodeNumber + xrefBase.TvDBStartEpisodeNumber - 2) -
(xrefBase.AniDBStartEpisodeNumber - 1);
if (dictTvDBEpisodes.ContainsKey(episodeNumber))
{
TvDB_Episode tvep = dictTvDBEpisodes[episodeNumber];
if (string.IsNullOrEmpty(tvep.Overview))
contract.EpisodeOverview = "Episode Overview Not Available";
else
contract.EpisodeOverview = tvep.Overview;

if (string.IsNullOrEmpty(tvep.GetFullImagePath()) ||
!File.Exists(tvep.GetFullImagePath()))
{
contract.ImageType = 0;
contract.ImageID = 0;
}
else
{
contract.ImageType = (int) ImageEntityType.TvDB_Episode;
contract.ImageID = tvep.TvDB_EpisodeID;
}

if (ServerSettings.EpisodeTitleSource == DataSourceType.TheTvDB &&
!string.IsNullOrEmpty(tvep.EpisodeName))
contract.EpisodeName = tvep.EpisodeName;
}
}
}
}
} }


#endregion var link = RepoFactory.CrossRef_AniDB_TvDB_Episode.GetByAniDBEpisodeID(ep.EpisodeID);

if (link.Any(a => a != null))
#region special episodes

if (ep.GetEpisodeTypeEnum() == EpisodeType.Special)
{ {
// find the xref that is right var tvep = RepoFactory.TvDB_Episode.GetByTvDBID(link.FirstOrDefault().TvDBEpisodeID);
// relies on the xref's being sorted by season number and then episode number (desc) contract.EpisodeName = tvep.EpisodeName;

contract.EpisodeOverview = tvep.Overview;
List<CrossRef_AniDB_TvDBV2> tvDBCrossRef = tvSummary.CrossRefTvDBV2 contract.ImageID = tvep.Id;
.OrderByDescending(a => a.AniDBStartEpisodeNumber) contract.ImageType = (int) ImageEntityType.TvDB_Episode;
.ToList();

bool foundStartingPoint = false;
CrossRef_AniDB_TvDBV2 xrefBase = null;
foreach (CrossRef_AniDB_TvDBV2 xrefTV in tvDBCrossRef)
{
if (xrefTV.AniDBStartEpisodeType != (int) EpisodeType.Special) continue;
if (ep.EpisodeNumber >= xrefTV.AniDBStartEpisodeNumber)
{
foundStartingPoint = true;
xrefBase = xrefTV;
break;
}
}

if (tvSummary != null && tvSummary.CrossRefTvDBV2 != null && tvSummary.CrossRefTvDBV2.Count > 0)
{
// we have found the starting epiosde numbder from AniDB
// now let's check that the TvDB Season and Episode Number exist
if (foundStartingPoint)
{
Dictionary<int, int> dictTvDBSeasons = null;
Dictionary<int, TvDB_Episode> dictTvDBEpisodes = null;
foreach (TvDBDetails det in tvSummary.TvDetails.Values)
{
if (det.TvDBID == xrefBase.TvDBID)
{
dictTvDBSeasons = det.DictTvDBSeasons;
dictTvDBEpisodes = det.DictTvDBEpisodes;
break;
}
}

if (dictTvDBSeasons.ContainsKey(xrefBase.TvDBSeasonNumber))
{
int episodeNumber = dictTvDBSeasons[xrefBase.TvDBSeasonNumber] +
(ep.EpisodeNumber + xrefBase.TvDBStartEpisodeNumber - 2) -
(xrefBase.AniDBStartEpisodeNumber - 1);
if (dictTvDBEpisodes.ContainsKey(episodeNumber))
{
TvDB_Episode tvep = dictTvDBEpisodes[episodeNumber];
contract.EpisodeOverview = tvep.Overview;

if (string.IsNullOrEmpty(tvep.GetFullImagePath()) ||
!File.Exists(tvep.GetFullImagePath()))
{
contract.ImageType = 0;
contract.ImageID = 0;
}
else
{
contract.ImageType = (int) ImageEntityType.TvDB_Episode;
contract.ImageID = tvep.TvDB_EpisodeID;
}

if (ServerSettings.EpisodeTitleSource == DataSourceType.TheTvDB &&
!string.IsNullOrEmpty(tvep.EpisodeName))
contract.EpisodeName = tvep.EpisodeName;
}
}
}
}
} }

#endregion
} }


public List<Metro_AniDB_Character> GetCharactersForAnime(int animeID, int maxRecords) public List<Metro_AniDB_Character> GetCharactersForAnime(int animeID, int maxRecords)
Expand Down
Loading

0 comments on commit aa94979

Please sign in to comment.