improve queries from scheduled tasks

This commit is contained in:
Luke Pulverenti 2017-09-09 23:18:23 -04:00
parent a46f81d572
commit fe10df6935
9 changed files with 131 additions and 67 deletions

View File

@ -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)");

View File

@ -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;
} }

View File

@ -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}
}; };
} }

View File

@ -58,8 +58,8 @@ namespace Emby.Server.Implementations.ScheduledTasks
/// </summary> /// </summary>
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{ {
return new[] { return new[] {
new TaskTriggerInfo new TaskTriggerInfo
{ {
Type = TaskTriggerInfo.TriggerDaily, Type = TaskTriggerInfo.TriggerDaily,
@ -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>()

View File

@ -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[] { },

View File

@ -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()
{ {

View File

@ -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);
} }
} }

View File

@ -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,

View File

@ -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)
{ {
MediaTypes = new string[] { MediaType.Video }, var query = new InternalItemsQuery
IsVirtualItem = false, {
IncludeItemTypes = types.ToArray(types.Count), MediaTypes = new string[] {MediaType.Video},
DtoOptions = new DtoOptions(true) IsVirtualItem = false,
IncludeItemTypes = types.ToArray(types.Count),
DtoOptions = new DtoOptions(true),
SourceTypes = new[] {SourceType.Library}
};
}).OfType<Video>() if (options.SkipIfAudioTrackMatches)
.Where(i => i.LocationType != LocationType.Remote) {
.ToList(); 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);
foreach (var video in videosByLanguage)
{
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)
{ {