migration and fixes
This commit is contained in:
parent
13c24a614f
commit
718c642ab7
|
@ -0,0 +1,61 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using Dapper;
|
||||
using FluentMigrator;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
using NzbDrone.Core.MediaFiles;
|
||||
using NzbDrone.Core.Parser;
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.Datastore.Migration
|
||||
{
|
||||
[Migration(197)]
|
||||
public class parse_title_from_existing_subtitle_files : NzbDroneMigrationBase
|
||||
{
|
||||
protected override void MainDbUpgrade()
|
||||
{
|
||||
Alter.Table("SubtitleFiles").AddColumn("Title").AsString().Nullable();
|
||||
Alter.Table("SubtitleFiles").AddColumn("Copy").AsInt32().WithDefaultValue(0);
|
||||
Execute.WithConnection(UpdateTitles);
|
||||
}
|
||||
|
||||
private void UpdateTitles(IDbConnection conn, IDbTransaction tran)
|
||||
{
|
||||
var updatedTitles = new List<object>();
|
||||
|
||||
using (var cmd = conn.CreateCommand())
|
||||
{
|
||||
cmd.Transaction = tran;
|
||||
cmd.CommandText = "SELECT \"Id\", \"RelativePath\", \"EpisodeFileId\" 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 episodeFileId = reader.GetInt32(2);
|
||||
|
||||
var episodeFile = conn.QuerySingle<EpisodeFile>("SELECT * FROM \"EpisodeFiles\" WHERE \"Id\" = @Id", new { Id = episodeFileId });
|
||||
var episode = new Episode
|
||||
{
|
||||
EpisodeFile = new LazyLoaded<EpisodeFile>(episodeFile)
|
||||
};
|
||||
|
||||
var title = LanguageParser.ParseLanguageTagsAndTitle(relativePath, episode).title;
|
||||
|
||||
var copy = LanguageParser.CopyFromTitle(title);
|
||||
|
||||
updatedTitles.Add(new
|
||||
{
|
||||
Id = id,
|
||||
Title = title,
|
||||
Copy = copy
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var updateSubtitleFilesSql = "UPDATE \"SubtitleFiles\" SET \"Title\" = @Title, \"Copy\" = @Copy, \"LastUpdated\" = CURRENT_TIMESTAMP WHERE \"Id\" = @Id";
|
||||
conn.Execute(updateSubtitleFilesSql, updatedTitles, transaction: tran);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -81,9 +81,9 @@ namespace NzbDrone.Core.Extras.Subtitles
|
|||
{
|
||||
(languageTags, title) = LanguageParser.ParseLanguageTagsAndTitle(possibleSubtitleFile, firstEpisode);
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (ArgumentException)
|
||||
{
|
||||
_logger.Debug(ex, "Failed parsing language tags with title from subtitle file: {0}", possibleSubtitleFile);
|
||||
_logger.Debug("Failed parsing language tags with title from subtitle file: {0}", possibleSubtitleFile);
|
||||
}
|
||||
|
||||
var subtitleFile = new SubtitleFile
|
||||
|
@ -95,7 +95,8 @@ namespace NzbDrone.Core.Extras.Subtitles
|
|||
Language = LanguageParser.ParseSubtitleLanguage(possibleSubtitleFile),
|
||||
LanguageTags = languageTags ?? LanguageParser.ParseLanguageTags(possibleSubtitleFile),
|
||||
Title = title,
|
||||
Extension = extension
|
||||
Extension = extension,
|
||||
Copy = LanguageParser.CopyFromTitle(title)
|
||||
};
|
||||
|
||||
subtitleFiles.Add(subtitleFile);
|
||||
|
|
|
@ -33,6 +33,8 @@ namespace NzbDrone.Core.Parser
|
|||
|
||||
private static readonly Regex SubtitleLanguageTitleRegex = new Regex(".+?(\\.((?<tags1>full|forced|foreign|default|cc|psdh|sdh)|(?<iso_code>[a-z]{2,3})))*\\.(?<title>[^.]*)(\\.((?<tags2>full|forced|foreign|default|cc|psdh|sdh)|(?<iso_code>[a-z]{2,3})))*$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||
|
||||
private static readonly Regex SubtitleTitleRegex = new Regex("(.+ - )?(?<copy>\\d+)", RegexOptions.Compiled);
|
||||
|
||||
public static List<Language> ParseLanguages(string title)
|
||||
{
|
||||
foreach (var regex in CleanSeriesTitleRegex)
|
||||
|
@ -273,7 +275,7 @@ namespace NzbDrone.Core.Parser
|
|||
{
|
||||
var episodeFile = episode.EpisodeFile.Value;
|
||||
var episodeFileTitle = Path.GetFileNameWithoutExtension(episodeFile.RelativePath);
|
||||
var originalEpisodeFileTitle = Path.GetFileNameWithoutExtension(episodeFile.OriginalFilePath);
|
||||
var originalEpisodeFileTitle = Path.GetFileNameWithoutExtension(episodeFile.OriginalFilePath) ?? string.Empty;
|
||||
|
||||
if (episodeFileTitle.Contains(title, StringComparison.OrdinalIgnoreCase) || originalEpisodeFileTitle.Contains(title, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
|
@ -284,6 +286,24 @@ namespace NzbDrone.Core.Parser
|
|||
return (languageTags.ToList(), title);
|
||||
}
|
||||
|
||||
public static int CopyFromTitle(string title)
|
||||
{
|
||||
if (title is null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var match = SubtitleTitleRegex.Match(title);
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
var copy = match.Groups["copy"].ToString();
|
||||
return int.Parse(copy);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static List<string> ParseLanguageTags(string fileName)
|
||||
{
|
||||
try
|
||||
|
|
Loading…
Reference in New Issue