Skip to content

Commit

Permalink
Fix a few bugs with TvDB linking and updating
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Jun 12, 2017
1 parent a3087c1 commit eaf3a41
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
12 changes: 9 additions & 3 deletions Shoko.Server/Commands/TvDB/CommandRequest_TvDBSearchAnime.cs
Expand Up @@ -131,12 +131,18 @@ public override void ProcessCommand()
bool foundResult = false;
foreach (AniDB_Anime_Title title in anime.GetTitles())
{
if (title.TitleType.ToUpper() != Shoko.Models.Constants.AnimeTitleType.Official.ToUpper())
if (!title.TitleType.Equals(Shoko.Models.Constants.AnimeTitleType.Official, StringComparison.InvariantCultureIgnoreCase))
continue;
if (!title.Language.Equals(Shoko.Models.Constants.AniDBLanguageType.English,
StringComparison.InvariantCultureIgnoreCase) &&
!title.Language.Equals(Shoko.Models.Constants.AniDBLanguageType.Romaji,
StringComparison.InvariantCultureIgnoreCase))
continue;

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

results = TvDBApiHelper.SearchSeries(title.Title);
searchCriteria = title.Title;
results = TvDBApiHelper.SearchSeries(searchCriteria);
if (results.Count > 0) foundResult = true;
logger.Trace("Found {0} tvdb results for search on {1}", results.Count, title.Title);
if (ProcessSearchResults(results, title.Title)) return;
Expand Down
18 changes: 11 additions & 7 deletions Shoko.Server/Providers/TvDB/TvDBApiHelper.cs
Expand Up @@ -21,6 +21,7 @@
using Shoko.Commons.Extensions;
using TvDbSharper.Clients.Updates.Json;
using TvDbSharper.Clients.Episodes.Json;
using HttpUtility = Nancy.Helpers.HttpUtility;

namespace Shoko.Server.Providers.TvDB
{
Expand Down Expand Up @@ -107,6 +108,7 @@ public static async Task<List<TVDB_Series_Search_Response>> SearchSeriesAsync(st
{
List<TVDB_Series_Search_Response> results = new List<TVDB_Series_Search_Response>();

criteria = HttpUtility.UrlEncode(criteria);
try
{
await _checkAuthorizationAsync();
Expand Down Expand Up @@ -134,8 +136,8 @@ public static async Task<List<TVDB_Series_Search_Response>> SearchSeriesAsync(st
if (client.Authentication.Token != null)
return await SearchSeriesAsync(criteria);
// suppress 404 and move on
} else if (exception.StatusCode == HttpStatusCode.NotFound) return null;
logger.Error(exception, "TvDB returned an error code: " + exception.StatusCode + "\n " + exception.Message);
} else if (exception.StatusCode == HttpStatusCode.NotFound) return results;
logger.Error(exception, "TvDB returned an error code: " + exception.StatusCode + "\n " + exception.Message + "\n when searching for " + criteria);
}
catch (Exception ex)
{
Expand All @@ -152,7 +154,7 @@ public static async Task<List<TVDB_Series_Search_Response>> SearchSeriesAsync(st
{
if (!additiveLink)
// remove all current links
RemoveAllAniDBTvDBLinks(session.Wrap(), animeID);
RemoveAllAniDBTvDBLinks(session.Wrap(), animeID, -1, false);

// check if we have this information locally
// if not download it now
Expand Down Expand Up @@ -200,8 +202,6 @@ public static async Task<List<TVDB_Series_Search_Response>> SearchSeriesAsync(st

RepoFactory.CrossRef_AniDB_TvDBV2.Save(xref);

SVR_AniDB_Anime.UpdateStatsByAnimeID(animeID);

logger.Trace("Changed tvdb association: {0}", animeID);

if (!excludeFromWebCache)
Expand Down Expand Up @@ -231,7 +231,7 @@ public static async Task<List<TVDB_Series_Search_Response>> SearchSeriesAsync(st
return "";
}

public static void RemoveAllAniDBTvDBLinks(ISessionWrapper session, int animeID, int aniEpType = -1)
public static void RemoveAllAniDBTvDBLinks(ISessionWrapper session, int animeID, int aniEpType = -1, bool updateStats = true)
{
// check for Trakt associations
List<CrossRef_AniDB_TraktV2> trakt = RepoFactory.CrossRef_AniDB_TraktV2.GetByAnimeID(animeID);
Expand Down Expand Up @@ -274,7 +274,7 @@ public static void RemoveAllAniDBTvDBLinks(ISessionWrapper session, int animeID,
}
}

SVR_AniDB_Anime.UpdateStatsByAnimeID(animeID);
if (updateStats) SVR_AniDB_Anime.UpdateStatsByAnimeID(animeID);
}

public static List<TvDB_Language> GetLanguages()
Expand Down Expand Up @@ -908,6 +908,10 @@ public static async void UpdateAllInfoAndImages(int seriesID, bool forceRefresh,
if (!existingEpIds.Contains(oldEp.Id))
RepoFactory.TvDB_Episode.Delete(oldEp.TvDB_EpisodeID);
}

var xref = RepoFactory.CrossRef_AniDB_TvDBV2.GetByTvDBID(tvSeries.SeriesID).FirstOrDefault();
if (xref == null) return;
SVR_AniDB_Anime.UpdateStatsByAnimeID(xref.AnimeID);
}
catch (Exception ex)
{
Expand Down
Expand Up @@ -42,6 +42,26 @@ public List<CrossRef_AniDB_TvDBV2> GetByAnimeID(ISessionWrapper session, int id)
return new List<CrossRef_AniDB_TvDBV2>(xrefs);
}

public List<CrossRef_AniDB_TvDBV2> GetByTvDBID(int id)
{
using (var session = DatabaseFactory.SessionFactory.OpenSession())
{
return GetByTvDBID(session.Wrap(), id);
}
}

public List<CrossRef_AniDB_TvDBV2> GetByTvDBID(ISessionWrapper session, int id)
{
var xrefs = session
.CreateCriteria(typeof(CrossRef_AniDB_TvDBV2))
.Add(Restrictions.Eq("TvDBID", id))
.AddOrder(Order.Asc("AniDBStartEpisodeType"))
.AddOrder(Order.Asc("AniDBStartEpisodeNumber"))
.List<CrossRef_AniDB_TvDBV2>();

return new List<CrossRef_AniDB_TvDBV2>(xrefs);
}

public ILookup<int, CrossRef_AniDB_TvDBV2> GetByAnimeIDs(ISessionWrapper session,
IReadOnlyCollection<int> animeIds)
{
Expand Down

0 comments on commit eaf3a41

Please sign in to comment.