-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Parse language tags from existing subtitles files
Closes #5890
- Loading branch information
Showing
2 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
...e.Test/Datastore/Migration/195_parse_language_tags_from_existing_subtitle_filesFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Dapper; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using NzbDrone.Core.Datastore.Migration; | ||
using NzbDrone.Core.Test.Framework; | ||
|
||
namespace NzbDrone.Core.Test.Datastore.Migration | ||
{ | ||
[TestFixture] | ||
public class parse_language_tags_from_existing_subtitle_filesFixture : MigrationTest<parse_language_tags_from_existing_subtitle_files> | ||
{ | ||
[Test] | ||
public void should_process_file_with_missing_null_language_tags() | ||
{ | ||
var now = DateTime.UtcNow; | ||
|
||
var db = WithDapperMigrationTestDb(c => | ||
{ | ||
c.Insert.IntoTable("SubtitleFiles").Row(new | ||
{ | ||
SeriesId = 1, | ||
SeasonNumber = 1, | ||
EpisodeFileId = 1, | ||
RelativePath = "S01E05.eng.srt", | ||
Added = now, | ||
LastUpdated = now, | ||
Extension = ".srt", | ||
Language = 1 | ||
}); | ||
}); | ||
|
||
var files = db.Query<SubtitleFile195>("SELECT * FROM \"SubtitleFiles\"").ToList(); | ||
|
||
files.Should().HaveCount(1); | ||
files.First().LanguageTags.Should().HaveCount(0); | ||
} | ||
|
||
[Test] | ||
public void should_process_file_with_missing_language_tags() | ||
{ | ||
var now = DateTime.UtcNow; | ||
|
||
var db = WithDapperMigrationTestDb(c => | ||
{ | ||
c.Insert.IntoTable("SubtitleFiles").Row(new | ||
{ | ||
SeriesId = 1, | ||
SeasonNumber = 1, | ||
EpisodeFileId = 1, | ||
RelativePath = "S01E05.eng.forced.srt", | ||
Added = now, | ||
LastUpdated = now, | ||
Extension = ".srt", | ||
Language = 1 | ||
}); | ||
}); | ||
|
||
var files = db.Query<SubtitleFile195>("SELECT * FROM \"SubtitleFiles\"").ToList(); | ||
|
||
files.Should().HaveCount(1); | ||
|
||
var languageTags = files.First().LanguageTags; | ||
|
||
languageTags.Should().HaveCount(1); | ||
languageTags.Should().Contain("forced"); | ||
} | ||
|
||
[Test] | ||
public void should_not_process_file_with_empty_language_tags() | ||
{ | ||
var now = DateTime.UtcNow; | ||
|
||
var db = WithDapperMigrationTestDb(c => | ||
{ | ||
c.Insert.IntoTable("SubtitleFiles").Row(new | ||
{ | ||
SeriesId = 1, | ||
SeasonNumber = 1, | ||
EpisodeFileId = 1, | ||
RelativePath = "S01E05.eng.srt", | ||
Added = now, | ||
LastUpdated = now, | ||
Extension = ".srt", | ||
Language = 1, | ||
LanguageTags = "[]" | ||
}); | ||
}); | ||
|
||
var files = db.Query<SubtitleFile195>("SELECT * FROM \"SubtitleFiles\"").ToList(); | ||
|
||
files.Should().HaveCount(1); | ||
files.First().LastUpdated.Should().BeCloseTo(now, TimeSpan.FromMilliseconds(999)); | ||
files.First().LanguageTags.Should().HaveCount(0); | ||
} | ||
} | ||
|
||
public class SubtitleFile195 | ||
{ | ||
public int Id { get; set; } | ||
public int SeriesId { get; set; } | ||
public int? EpisodeFileId { get; set; } | ||
public int? SeasonNumber { get; set; } | ||
public string RelativePath { get; set; } | ||
public DateTime Added { get; set; } | ||
public DateTime LastUpdated { get; set; } | ||
public string Extension { get; set; } | ||
public int Language { get; set; } | ||
public List<string> LanguageTags { get; set; } | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...NzbDrone.Core/Datastore/Migration/195_parse_language_tags_from_existing_subtitle_files.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Dapper; | ||
using FluentMigrator; | ||
using NzbDrone.Core.Datastore.Migration.Framework; | ||
using NzbDrone.Core.Parser; | ||
|
||
namespace NzbDrone.Core.Datastore.Migration | ||
{ | ||
[Migration(195)] | ||
public class parse_language_tags_from_existing_subtitle_files : NzbDroneMigrationBase | ||
{ | ||
protected override void MainDbUpgrade() | ||
{ | ||
Execute.WithConnection(UpdateLanguageTags); | ||
} | ||
|
||
private void UpdateLanguageTags(IDbConnection conn, IDbTransaction tran) | ||
{ | ||
var updatedLanguageTags = new List<object>(); | ||
var now = DateTime.Now; | ||
|
||
using (var cmd = conn.CreateCommand()) | ||
{ | ||
cmd.Transaction = tran; | ||
cmd.CommandText = "SELECT \"Id\", \"RelativePath\" FROM \"SubtitleFiles\" WHERE \"LanguageTags\" IS NULL"; | ||
|
||
using var reader = cmd.ExecuteReader(); | ||
while (reader.Read()) | ||
{ | ||
var id = reader.GetInt32(0); | ||
var relativePath = reader.GetString(1); | ||
var languageTags = LanguageParser.ParseLanguageTags(relativePath); | ||
|
||
updatedLanguageTags.Add(new | ||
{ | ||
Id = id, | ||
LanguageTags = languageTags | ||
}); | ||
} | ||
} | ||
|
||
var serializerSettings = new JsonSerializerOptions | ||
{ | ||
AllowTrailingCommas = true, | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
PropertyNameCaseInsensitive = true, | ||
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
WriteIndented = true | ||
}; | ||
|
||
var updateSubtitleFilesSql = "UPDATE \"SubtitleFiles\" SET \"LanguageTags\" = @LanguageTags, \"LastUpdated\" = CURRENT_TIMESTAMP WHERE \"Id\" = @Id"; | ||
conn.Execute(updateSubtitleFilesSql, updatedLanguageTags, transaction: tran); | ||
} | ||
} | ||
} |