Permalink
Browse files

Some Work on TvDB Matching

This will regen all matches on startup
  • Loading branch information...
da3dsoul committed Jun 29, 2018
1 parent fb75a05 commit 74c571c7fd2a3d6300471b541ee7ee2616109139
@@ -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()))
@@ -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>()
@@ -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;");
@@ -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)
{
@@ -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>
@@ -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)
@@ -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)
@@ -268,7 +268,10 @@ public static List<CrossRef_AniDB_TvDB_Episode> GetMatchPreviewWithOverrides(int
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);
}
@@ -297,6 +300,7 @@ ref List<(AniDB_Episode, TvDB_Episode, MatchRating)> matches)
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();
@@ -351,8 +355,8 @@ public static bool HasNumberedTitles(List<TvDB_Episode> eps)
* 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
@@ -628,6 +632,36 @@ ref List<(AniDB_Episode, TvDB_Episode, MatchRating)> matches)
}
}

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)
@@ -721,7 +755,7 @@ ref List<(AniDB_Episode AniDB, TvDB_Episode TvDB, MatchRating Match)> matches)
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++;
}

0 comments on commit 74c571c

Please sign in to comment.