mirror of
https://github.com/Radarr/Radarr
synced 2024-12-21 23:42:23 +00:00
New: Parse language tags from existing subtitles files
(cherry picked from commit b20e247feb24084348261ef2fad61a05f1fd69e8)
This commit is contained in:
parent
1935abbde2
commit
a0f13b19cd
2 changed files with 173 additions and 0 deletions
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue