Skip to content

Commit

Permalink
Some Work on TvDB Matching
Browse files Browse the repository at this point in the history
This will regen all matches on startup
  • Loading branch information
da3dsoul committed Jun 29, 2018
1 parent fb75a05 commit 74c571c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Shoko.Commons
21 changes: 21 additions & 0 deletions Shoko.Server/Databases/DatabaseFixes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,27 @@ public static void MigrateTvDBLinks_v2_to_V3()
}
}

public static void RegenTvDBMatches()
{
RepoFactory.CrossRef_AniDB_TvDB_Episode.DeleteAllUnverifiedLinks();

var list = RepoFactory.AnimeSeries.GetAll().ToList();
int count = 0;

list.AsParallel().ForAll(animeseries =>
{
Interlocked.Increment(ref count);
if (count % 50 == 0)
{
ServerState.Instance.CurrentSetupStatus = string.Format(
Commons.Properties.Resources.Database_Validating, "Generating TvDB Episode Matchings",
$" {count}/{list.Count}");
}
TvDBLinkingHelper.GenerateTvDBEpisodeMatches(animeseries.AniDB_ID, true);
});
}

public static void FixAniDB_EpisodesWithMissingTitles()
{
var specials = RepoFactory.AniDB_Episode.GetAll().Where(a => string.IsNullOrEmpty(a.GetEnglishTitle()))
Expand Down
3 changes: 2 additions & 1 deletion Shoko.Server/Databases/MySQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Shoko.Server.Databases
public class MySQL : BaseDatabase<MySqlConnection>, IDatabase
{
public string Name { get; } = "MySQL";
public int RequiredVersion { get; } = 85;
public int RequiredVersion { get; } = 86;


private List<DatabaseCommand> createVersionTable = new List<DatabaseCommand>()
Expand Down Expand Up @@ -621,6 +621,7 @@ public class MySQL : BaseDatabase<MySqlConnection>, IDatabase
new DatabaseCommand(84, 11, DatabaseFixes.MigrateTvDBLinks_v2_to_V3),
// DatabaseFixes.MigrateTvDBLinks_v2_to_V3() drops the CrossRef_AniDB_TvDBV2 table. We do it after init to migrate
new DatabaseCommand(85, 1, DatabaseFixes.FixAniDB_EpisodesWithMissingTitles),
new DatabaseCommand(86, 1, DatabaseFixes.RegenTvDBMatches),
};

private DatabaseCommand linuxTableVersionsFix = new DatabaseCommand("RENAME TABLE versions TO Versions;");
Expand Down
3 changes: 2 additions & 1 deletion Shoko.Server/Databases/SQLServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Shoko.Server.Databases
public class SQLServer : BaseDatabase<SqlConnection>, IDatabase
{
public string Name { get; } = "SQLServer";
public int RequiredVersion { get; } = 79;
public int RequiredVersion { get; } = 80;

public void BackupDatabase(string fullfilename)
{
Expand Down Expand Up @@ -585,6 +585,7 @@ public void CreateDatabase()
new DatabaseCommand(78, 11, DatabaseFixes.MigrateTvDBLinks_v2_to_V3),
// DatabaseFixes.MigrateTvDBLinks_v2_to_V3() drops the CrossRef_AniDB_TvDBV2 table. We do it after init to migrate
new DatabaseCommand(79, 1, DatabaseFixes.FixAniDB_EpisodesWithMissingTitles),
new DatabaseCommand(80, 1, DatabaseFixes.RegenTvDBMatches),
};

private List<DatabaseCommand> updateVersionTable = new List<DatabaseCommand>
Expand Down
3 changes: 2 additions & 1 deletion Shoko.Server/Databases/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SQLite : BaseDatabase<SQLiteConnection>, IDatabase

public string Name { get; } = "SQLite";

public int RequiredVersion { get; } = 74;
public int RequiredVersion { get; } = 75;


public void BackupDatabase(string fullfilename)
Expand Down Expand Up @@ -524,6 +524,7 @@ public void CreateDatabase()
new DatabaseCommand(73, 9, DatabaseFixes.MigrateTvDBLinks_v2_to_V3),
// DatabaseFixes.MigrateTvDBLinks_v2_to_V3() drops the CrossRef_AniDB_TvDBV2 table. We do it after init to migrate
new DatabaseCommand(74, 1, DatabaseFixes.FixAniDB_EpisodesWithMissingTitles),
new DatabaseCommand(75, 1, DatabaseFixes.RegenTvDBMatches),
};

private static Tuple<bool, string> DropAniDB_EpisodeTitles(object connection)
Expand Down
40 changes: 37 additions & 3 deletions Shoko.Server/Providers/TvDB/TvDBLinkingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ private static void TryToMatchNormalEpisodesToTvDB(List<AniDB_Episode> aniepsNor
TryToMatchEpisodes1To1ByAirDate(ref aniepsNormal, ref tvepsNormal, ref matches);

if (!hasNumberedTitles)
{
TryToMatchEpisodes1To1ByTitle(ref aniepsNormal, ref tvepsNormal, ref matches);
CorrectMatchRatings(ref matches);
}

FillUnmatchedEpisodes1To1(ref aniepsNormal, ref tvepsNormal, ref matches);
}
Expand Down Expand Up @@ -297,6 +300,7 @@ private static void TryToMatchSpeicalsToTvDB(List<AniDB_Episode> aniepsSpecial,
TryToMatchEpisodes1To1ByTitle(ref aniepsSpecial, ref tvepsSpecial, ref matches);
TryToMatchEpisodes1To1ByAirDate(ref aniepsSpecial, ref tvepsSpecial, ref matches);
FillUnmatchedEpisodes1To1(ref aniepsSpecial, ref tvepsSpecial, ref matches);
CorrectMatchRatings(ref matches);
}

private static readonly char[] separators = " /.,<>?;':\"\\!@#$%^&*()-=_+|`~".ToCharArray();
Expand Down Expand Up @@ -351,8 +355,8 @@ private static void TryToMatchSeasonsByAirDates(List<AniDB_Episode> aniepsNormal
* d.gray-man s1---------------------------------------------w1 s2-----------------------w2
*
*
* calc s1'--------------------------e1' s3'---------------------------e3'
* s2'------------------------e2'
* ^ calc'd s1'-------------------e1' s3'---------------------e3'
* s2'--------------------e2'
*
* Aldoah.Zero
* tvdb long ss-----------------------------------------------------------------------se
Expand Down Expand Up @@ -628,6 +632,36 @@ private static void TryToMatchEpisodes1To1ByTitle(ref List<AniDB_Episode> anieps
}
}

private static void CorrectMatchRatings(ref List<(AniDB_Episode, TvDB_Episode, MatchRating)> matches)
{
for (int index = 0; index < matches.Count; index++)
{
var match = matches[index];
if (match.Item1 == null || match.Item2 == null)
{
match.Item3 = MatchRating.SarahJessicaParker;
continue;
}

DateTime? aniair = match.Item1.GetAirDateAsDate();
DateTime? tvair = match.Item2.AirDate;
bool datesMatch = aniair != null && tvair != null;

if (datesMatch) datesMatch = aniair.Value.IsWithinErrorMargin(tvair.Value, TimeSpan.FromDays(1.5));

if (!datesMatch) continue;

// if the dates match, then they would have filled with Good, so the fuzzy search is only being done once

var aniTitle = match.Item1.GetEnglishTitle();
var tvTitle = match.Item2.EpisodeName;
// this method returns false if either is null
bool titlesMatch = aniTitle.FuzzyMatches(tvTitle);

if (!titlesMatch) match.Item3 = MatchRating.Mkay;
}
}

private static void FillUnmatchedEpisodes1To1(ref List<AniDB_Episode> aniepsNormal,
ref List<TvDB_Episode> tvepsNormal,
ref List<(AniDB_Episode AniDB, TvDB_Episode TvDB, MatchRating Match)> matches)
Expand Down Expand Up @@ -721,7 +755,7 @@ private static bool TryToMatchRegularlyDistributedEpisodes(ref List<AniDB_Episod
if (ep == null) break;

// It goes against the initial rules for Good rating, but this is a very specific case
matches.Add((aniep, ep, MatchRating.Good));
matches.Add((aniep, ep, MatchRating.Mkay));
aniepsNormal.Remove(aniep);
count++;
}
Expand Down

0 comments on commit 74c571c

Please sign in to comment.