improve queries from scheduled tasks

pull/1154/head
Luke Pulverenti 7 years ago
parent a46f81d572
commit fe10df6935

@ -4254,6 +4254,54 @@ namespace Emby.Server.Implementations.Data
} }
} }
if (!string.IsNullOrWhiteSpace(query.HasNoAudioTrackWithLanguage))
{
whereClauses.Add("((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Audio' and MediaStreams.Language=@HasNoAudioTrackWithLanguage limit 1) is null)");
if (statement != null)
{
statement.TryBind("@HasNoAudioTrackWithLanguage", query.HasNoAudioTrackWithLanguage);
}
}
if (!string.IsNullOrWhiteSpace(query.HasNoInternalSubtitleTrackWithLanguage))
{
whereClauses.Add("((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' and MediaStreams.IsExternal=0 and MediaStreams.Language=@HasNoInternalSubtitleTrackWithLanguage limit 1) is null)");
if (statement != null)
{
statement.TryBind("@HasNoInternalSubtitleTrackWithLanguage", query.HasNoInternalSubtitleTrackWithLanguage);
}
}
if (!string.IsNullOrWhiteSpace(query.HasNoExternalSubtitleTrackWithLanguage))
{
whereClauses.Add("((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' and MediaStreams.IsExternal=1 and MediaStreams.Language=@HasNoExternalSubtitleTrackWithLanguage limit 1) is null)");
if (statement != null)
{
statement.TryBind("@HasNoExternalSubtitleTrackWithLanguage", query.HasNoExternalSubtitleTrackWithLanguage);
}
}
if (!string.IsNullOrWhiteSpace(query.HasNoSubtitleTrackWithLanguage))
{
whereClauses.Add("((select language from MediaStreams where MediaStreams.ItemId=A.Guid and MediaStreams.StreamType='Subtitle' and MediaStreams.Language=@HasNoSubtitleTrackWithLanguage limit 1) is null)");
if (statement != null)
{
statement.TryBind("@HasNoSubtitleTrackWithLanguage", query.HasNoSubtitleTrackWithLanguage);
}
}
if (query.HasChapterImages.HasValue)
{
if (query.HasChapterImages.Value)
{
whereClauses.Add("((select imagepath from Chapters2 where Chapters2.ItemId=A.Guid and imagepath not null limit 1) not null)");
}
else
{
whereClauses.Add("((select imagepath from Chapters2 where Chapters2.ItemId=A.Guid and imagepath not null limit 1) is null)");
}
}
if (query.HasDeadParentId.HasValue && query.HasDeadParentId.Value) if (query.HasDeadParentId.HasValue && query.HasDeadParentId.Value)
{ {
whereClauses.Add("ParentId NOT NULL AND ParentId NOT IN (select guid from TypedBaseItems)"); whereClauses.Add("ParentId NOT NULL AND ParentId NOT IN (select guid from TypedBaseItems)");

@ -620,37 +620,12 @@ namespace Emby.Server.Implementations.Library
return ResolveItem(args, resolvers); return ResolveItem(args, resolvers);
} }
private readonly List<string> _ignoredPaths = new List<string>();
public void RegisterIgnoredPath(string path)
{
lock (_ignoredPaths)
{
_ignoredPaths.Add(path);
}
}
public void UnRegisterIgnoredPath(string path)
{
lock (_ignoredPaths)
{
_ignoredPaths.Remove(path);
}
}
public bool IgnoreFile(FileSystemMetadata file, BaseItem parent) public bool IgnoreFile(FileSystemMetadata file, BaseItem parent)
{ {
if (EntityResolutionIgnoreRules.Any(r => r.ShouldIgnore(file, parent))) if (EntityResolutionIgnoreRules.Any(r => r.ShouldIgnore(file, parent)))
{ {
return true; return true;
} }
//lock (_ignoredPaths)
{
if (_ignoredPaths.Contains(file.FullName, StringComparer.OrdinalIgnoreCase))
{
return true;
}
}
return false; return false;
} }

@ -51,7 +51,7 @@ namespace Emby.Server.Implementations.LiveTv
return new[] { return new[] {
// Every so often // Every so often
new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(12).Ticks} new TaskTriggerInfo { Type = TaskTriggerInfo.TriggerInterval, IntervalTicks = TimeSpan.FromHours(24).Ticks}
}; };
} }

@ -88,6 +88,12 @@ namespace Emby.Server.Implementations.ScheduledTasks
IsFolder = false, IsFolder = false,
Recursive = true, Recursive = true,
DtoOptions = new DtoOptions(false) DtoOptions = new DtoOptions(false)
{
EnableImages = false
},
SourceTypes = new SourceType[] { SourceType.Library },
HasChapterImages = false,
IsVirtualItem = false
}) })
.OfType<Video>() .OfType<Video>()

@ -193,13 +193,11 @@ namespace Emby.Server.Implementations.Services
summary = info.Summary, summary = info.Summary,
produces = new[] produces = new[]
{ {
"application/json", "application/json"
"application/xml"
}, },
consumes = new[] consumes = new[]
{ {
"application/json", "application/json"
"application/xml"
}, },
operationId = info.RequestType.Name, operationId = info.RequestType.Name,
tags = new string[] { }, tags = new string[] { },

@ -160,6 +160,7 @@ namespace MediaBrowser.Controller.Entities
public bool ForceDirect { get; set; } public bool ForceDirect { get; set; }
public Dictionary<string, string> ExcludeProviderIds { get; set; } public Dictionary<string, string> ExcludeProviderIds { get; set; }
public bool EnableGroupByMetadataKey { get; set; } public bool EnableGroupByMetadataKey { get; set; }
public bool? HasChapterImages { get; set; }
public Tuple<string, SortOrder>[] OrderBy { get; set; } public Tuple<string, SortOrder>[] OrderBy { get; set; }
@ -169,6 +170,10 @@ namespace MediaBrowser.Controller.Entities
public DtoOptions DtoOptions { get; set; } public DtoOptions DtoOptions { get; set; }
public int MinSimilarityScore { get; set; } public int MinSimilarityScore { get; set; }
public string HasNoAudioTrackWithLanguage { get; set; }
public string HasNoInternalSubtitleTrackWithLanguage { get; set; }
public string HasNoExternalSubtitleTrackWithLanguage { get; set; }
public string HasNoSubtitleTrackWithLanguage { get; set; }
public InternalItemsQuery() public InternalItemsQuery()
{ {

@ -563,8 +563,6 @@ namespace MediaBrowser.Controller.Library
QueryResult<Tuple<BaseItem, ItemCounts>> GetAlbumArtists(InternalItemsQuery query); QueryResult<Tuple<BaseItem, ItemCounts>> GetAlbumArtists(InternalItemsQuery query);
QueryResult<Tuple<BaseItem, ItemCounts>> GetAllArtists(InternalItemsQuery query); QueryResult<Tuple<BaseItem, ItemCounts>> GetAllArtists(InternalItemsQuery query);
void RegisterIgnoredPath(string path);
void UnRegisterIgnoredPath(string path);
int GetCount(InternalItemsQuery query); int GetCount(InternalItemsQuery query);
} }
} }

@ -32,16 +32,40 @@ namespace MediaBrowser.Providers.MediaInfo
bool requirePerfectMatch, bool requirePerfectMatch,
IEnumerable<string> languages, IEnumerable<string> languages,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{
var downloadedLanguages = new List<string>();
foreach (var lang in languages)
{
var downloaded = await DownloadSubtitles(video, mediaStreams, skipIfEmbeddedSubtitlesPresent,
skipIfAudioTrackMatches, requirePerfectMatch, lang, cancellationToken).ConfigureAwait(false);
if (downloaded)
{
downloadedLanguages.Add(lang);
}
}
return downloadedLanguages;
}
public Task<bool> DownloadSubtitles(Video video,
List<MediaStream> mediaStreams,
bool skipIfEmbeddedSubtitlesPresent,
bool skipIfAudioTrackMatches,
bool requirePerfectMatch,
string lang,
CancellationToken cancellationToken)
{ {
if (video.LocationType != LocationType.FileSystem || if (video.LocationType != LocationType.FileSystem ||
video.VideoType != VideoType.VideoFile) video.VideoType != VideoType.VideoFile)
{ {
return new List<string>(); return Task.FromResult(false);
} }
if (!video.IsCompleteMedia) if (!video.IsCompleteMedia)
{ {
return new List<string>(); return Task.FromResult(false);
} }
VideoContentType mediaType; VideoContentType mediaType;
@ -57,30 +81,11 @@ namespace MediaBrowser.Providers.MediaInfo
else else
{ {
// These are the only supported types // These are the only supported types
return new List<string>(); return Task.FromResult(false);
} }
var downloadedLanguages = new List<string>(); return DownloadSubtitles(video, mediaStreams, skipIfEmbeddedSubtitlesPresent, skipIfAudioTrackMatches,
requirePerfectMatch, lang, mediaType, cancellationToken);
foreach (var lang in languages)
{
try
{
var downloaded = await DownloadSubtitles(video, mediaStreams, skipIfEmbeddedSubtitlesPresent, skipIfAudioTrackMatches, requirePerfectMatch, lang, mediaType, cancellationToken)
.ConfigureAwait(false);
if (downloaded)
{
downloadedLanguages.Add(lang);
}
}
catch (Exception ex)
{
_logger.ErrorException("Error downloading subtitles", ex);
}
}
return downloadedLanguages;
} }
private async Task<bool> DownloadSubtitles(Video video, private async Task<bool> DownloadSubtitles(Video video,

@ -80,17 +80,44 @@ namespace MediaBrowser.Providers.MediaInfo
return; return;
} }
var videos = _libraryManager.GetItemList(new InternalItemsQuery var dict = new Dictionary<Guid, BaseItem>();
foreach (var lang in options.DownloadLanguages)
{
var query = new InternalItemsQuery
{ {
MediaTypes = new string[] {MediaType.Video}, MediaTypes = new string[] {MediaType.Video},
IsVirtualItem = false, IsVirtualItem = false,
IncludeItemTypes = types.ToArray(types.Count), IncludeItemTypes = types.ToArray(types.Count),
DtoOptions = new DtoOptions(true) DtoOptions = new DtoOptions(true),
SourceTypes = new[] {SourceType.Library}
};
if (options.SkipIfAudioTrackMatches)
{
query.HasNoAudioTrackWithLanguage = lang;
}
if (options.SkipIfEmbeddedSubtitlesPresent)
{
// Exclude if it already has any subtitles of the same language
query.HasNoSubtitleTrackWithLanguage = lang;
}
else
{
// Exclude if it already has external subtitles of the same language
query.HasNoExternalSubtitleTrackWithLanguage = lang;
}
var videosByLanguage = _libraryManager.GetItemList(query);
}).OfType<Video>() foreach (var video in videosByLanguage)
.Where(i => i.LocationType != LocationType.Remote) {
.ToList(); dict[video.Id] = video;
}
}
var videos = dict.Values.ToList();
if (videos.Count == 0) if (videos.Count == 0)
{ {
return; return;
@ -100,9 +127,11 @@ namespace MediaBrowser.Providers.MediaInfo
foreach (var video in videos) foreach (var video in videos)
{ {
cancellationToken.ThrowIfCancellationRequested();
try try
{ {
await DownloadSubtitles(video, options, cancellationToken).ConfigureAwait(false); await DownloadSubtitles(video as Video, options, cancellationToken).ConfigureAwait(false);
} }
catch (Exception ex) catch (Exception ex)
{ {

Loading…
Cancel
Save