Skip to content

Commit

Permalink
Improve Trakt Linking from TvDB
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Mar 20, 2017
1 parent 168d5fe commit d2a23a9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 65 deletions.
115 changes: 54 additions & 61 deletions Shoko.Server/Commands/Trakt/CommandRequest_TraktSearchAnime.cs
Expand Up @@ -67,6 +67,7 @@ public override void ProcessCommand()
using (var session = DatabaseFactory.SessionFactory.OpenSession())
{
ISessionWrapper sessionWrapper = session.Wrap();
bool doReturn = false;

// first check if the user wants to use the web cache
if (ServerSettings.WebCache_Trakt_Get)
Expand All @@ -83,17 +84,17 @@ public override void ProcessCommand()
foreach (Azure_CrossRef_AniDB_Trakt xref in resultsCache)
{
TraktV2ShowExtended showInfo = TraktTVHelper.GetShowInfoV2(xref.TraktID);
if (showInfo != null)
{
logger.Trace("Found trakt match on web cache for {0} - id = {1}", AnimeID,
showInfo.title);
TraktTVHelper.LinkAniDBTrakt(AnimeID,
(enEpisodeType) xref.AniDBStartEpisodeType,
xref.AniDBStartEpisodeNumber,
xref.TraktID, xref.TraktSeasonNumber, xref.TraktStartEpisodeNumber, true);
return;
}
if (showInfo == null) continue;

logger.Trace("Found trakt match on web cache for {0} - id = {1}", AnimeID,
showInfo.title);
TraktTVHelper.LinkAniDBTrakt(AnimeID,
(enEpisodeType) xref.AniDBStartEpisodeType,
xref.AniDBStartEpisodeNumber,
xref.TraktID, xref.TraktSeasonNumber, xref.TraktStartEpisodeNumber, true);
doReturn = true;
}
if (doReturn) return;
}
}
catch (Exception ex)
Expand All @@ -115,49 +116,42 @@ public override void ProcessCommand()
List<TraktV2SearchTvDBIDShowResult> searchResults =
TraktTVHelper.SearchShowByIDV2(TraktSearchIDType.tvdb,
tvXRef.TvDBID.ToString());
if (searchResults != null && searchResults.Count > 0)
if (searchResults == null || searchResults.Count <= 0) continue;
// since we are searching by ID, there will only be one 'show' result
TraktV2Show resShow = null;
foreach (TraktV2SearchTvDBIDShowResult res in searchResults)
{
// since we are searching by ID, there will only be one 'show' result
TraktV2Show resShow = null;
foreach (TraktV2SearchTvDBIDShowResult res in searchResults)
{
if (res.ResultType == SearchIDType.Show)
{
resShow = res.show;
break;
}
}

if (resShow != null)
{
TraktV2ShowExtended showInfo = TraktTVHelper.GetShowInfoV2(resShow.ids.slug);
if (showInfo != null && showInfo.ids != null)
{
// make sure the season specified by TvDB also exists on Trakt
Trakt_Show traktShow =
RepoFactory.Trakt_Show.GetByTraktSlug(session, showInfo.ids.slug);
if (traktShow != null)
{
Trakt_Season traktSeason = RepoFactory.Trakt_Season.GetByShowIDAndSeason(
session,
traktShow.Trakt_ShowID,
xrefTvDBs[0].TvDBSeasonNumber);
if (traktSeason != null)
{
logger.Trace("Found trakt match using TvDBID locally {0} - id = {1}",
AnimeID, showInfo.title);
TraktTVHelper.LinkAniDBTrakt(AnimeID,
(enEpisodeType) tvXRef.AniDBStartEpisodeType,
tvXRef.AniDBStartEpisodeNumber, showInfo.ids.slug,
tvXRef.TvDBSeasonNumber, tvXRef.TvDBStartEpisodeNumber,
true);
return;
}
}
}
}
if (res.ResultType != SearchIDType.Show) continue;
resShow = res.show;
break;
}

if (resShow == null) continue;

TraktV2ShowExtended showInfo = TraktTVHelper.GetShowInfoV2(resShow.ids.slug);
if (showInfo?.ids == null) continue;

// make sure the season specified by TvDB also exists on Trakt
Trakt_Show traktShow =
RepoFactory.Trakt_Show.GetByTraktSlug(session, showInfo.ids.slug);
if (traktShow == null) continue;

Trakt_Season traktSeason = RepoFactory.Trakt_Season.GetByShowIDAndSeason(
session,
traktShow.Trakt_ShowID,
tvXRef.TvDBSeasonNumber);
if (traktSeason == null) continue;

logger.Trace("Found trakt match using TvDBID locally {0} - id = {1}",
AnimeID, showInfo.title);
TraktTVHelper.LinkAniDBTrakt(AnimeID,
(enEpisodeType) tvXRef.AniDBStartEpisodeType,
tvXRef.AniDBStartEpisodeNumber, showInfo.ids.slug,
tvXRef.TvDBSeasonNumber, tvXRef.TvDBStartEpisodeNumber,
true);
doReturn = true;
}
if (doReturn) return;
}

// finally lets try searching Trakt directly
Expand All @@ -173,19 +167,18 @@ public override void ProcessCommand()
if (ProcessSearchResults(session, results, searchCriteria)) return;


if (results.Count == 0)
if (results.Count != 0) return;

foreach (AniDB_Anime_Title title in anime.GetTitles())
{
foreach (AniDB_Anime_Title title in anime.GetTitles())
{
if (title.TitleType.ToUpper() != Shoko.Models.Constants.AnimeTitleType.Official.ToUpper())
continue;
if (!string.Equals(title.TitleType, Shoko.Models.Constants.AnimeTitleType.Official, StringComparison.InvariantCultureIgnoreCase))
continue;

if (searchCriteria.ToUpper() == title.Title.ToUpper()) continue;
if (string.Equals(searchCriteria, title.Title, StringComparison.InvariantCultureIgnoreCase)) continue;

results = TraktTVHelper.SearchShowV2(searchCriteria);
logger.Trace("Found {0} trakt results for search on {1}", results.Count, title.Title);
if (ProcessSearchResults(session, results, title.Title)) return;
}
results = TraktTVHelper.SearchShowV2(searchCriteria);
logger.Trace("Found {0} trakt results for search on {1}", results.Count, title.Title);
if (ProcessSearchResults(session, results, title.Title)) return;
}
}
}
Expand Down Expand Up @@ -222,7 +215,7 @@ public override void ProcessCommand()

public override void GenerateCommandID()
{
this.CommandID = string.Format("CommandRequest_TraktSearchAnime{0}", this.AnimeID);
this.CommandID = $"CommandRequest_TraktSearchAnime{AnimeID}";
}

public override bool LoadFromDBCommand(CommandRequest cq)
Expand Down
22 changes: 18 additions & 4 deletions Shoko.Server/Providers/TvDB/TvDBHelper.cs
Expand Up @@ -20,6 +20,7 @@
using Shoko.Server.Repositories;
using Shoko.Server.Repositories.NHibernate;
using Shoko.Server.Extensions;
using Shoko.Server.Providers.TraktTV;
using Utils = Shoko.Server.Utils;

namespace Shoko.Models.TvDB
Expand Down Expand Up @@ -901,8 +902,7 @@ public void UpdateAllInfoAndImages(int seriesID, bool forceRefresh, bool downloa

if (!excludeFromWebCache)
{
CommandRequest_WebCacheSendXRefAniDBTvDB req =
new CommandRequest_WebCacheSendXRefAniDBTvDB(xref.CrossRef_AniDB_TvDBV2ID);
var req = new CommandRequest_WebCacheSendXRefAniDBTvDB(xref.CrossRef_AniDB_TvDBV2ID);
req.Save();
}

Expand All @@ -913,10 +913,13 @@ public void UpdateAllInfoAndImages(int seriesID, bool forceRefresh, bool downloa
if (trakt.Count != 0)
{
// remove them and rescan
trakt.ForEach(a => RepoFactory.CrossRef_AniDB_TraktV2.Delete(a));
foreach (CrossRef_AniDB_TraktV2 a in trakt)
{
RepoFactory.CrossRef_AniDB_TraktV2.Delete(a);
}
}

CommandRequest_TraktSearchAnime cmd2 = new CommandRequest_TraktSearchAnime(animeID, false);
var cmd2 = new CommandRequest_TraktSearchAnime(animeID, false);
cmd2.Save(session);
}
}
Expand Down Expand Up @@ -965,6 +968,17 @@ public static void LinkAniDBTvDBEpisode(int aniDBID, int tvDBID, int animeID)

public static void RemoveAllAniDBTvDBLinks(ISessionWrapper session, int animeID, int aniEpType = -1)
{
// check for Trakt associations
List<CrossRef_AniDB_TraktV2> trakt = RepoFactory.CrossRef_AniDB_TraktV2.GetByAnimeID(animeID);
if (trakt.Count != 0)
{
// remove them and rescan
foreach (CrossRef_AniDB_TraktV2 a in trakt)
{
RepoFactory.CrossRef_AniDB_TraktV2.Delete(a);
}
}

List<CrossRef_AniDB_TvDBV2> xrefs = RepoFactory.CrossRef_AniDB_TvDBV2.GetByAnimeID(session, animeID);
if (xrefs == null || xrefs.Count == 0) return;

Expand Down

0 comments on commit d2a23a9

Please sign in to comment.