fixed subtitle downloading

This commit is contained in:
Luke Pulverenti 2014-05-08 01:04:39 -04:00
parent 0b7e398772
commit 374dd8d441
11 changed files with 143 additions and 29 deletions

View File

@ -11,6 +11,7 @@ namespace MediaBrowser.Controller.Providers
{
List<FileSystemInfo> GetFileSystemEntries(string path);
IEnumerable<FileSystemInfo> GetFiles(string path);
IEnumerable<FileSystemInfo> GetFiles(string path, bool clearCache);
FileSystemInfo GetFile(string path);
}
@ -26,9 +27,21 @@ namespace MediaBrowser.Controller.Providers
}
public List<FileSystemInfo> GetFileSystemEntries(string path)
{
return GetFileSystemEntries(path, false);
}
private List<FileSystemInfo> GetFileSystemEntries(string path, bool clearCache)
{
List<FileSystemInfo> entries;
if (clearCache)
{
List<FileSystemInfo> removed;
_cache.TryRemove(path, out removed);
}
if (!_cache.TryGetValue(path, out entries))
{
//_logger.Debug("Getting files for " + path);
@ -50,7 +63,12 @@ namespace MediaBrowser.Controller.Providers
public IEnumerable<FileSystemInfo> GetFiles(string path)
{
return GetFileSystemEntries(path).Where(i => (i.Attributes & FileAttributes.Directory) != FileAttributes.Directory);
return GetFiles(path, false);
}
public IEnumerable<FileSystemInfo> GetFiles(string path, bool clearCache)
{
return GetFileSystemEntries(path, clearCache).Where(i => (i.Attributes & FileAttributes.Directory) != FileAttributes.Directory);
}
public FileSystemInfo GetFile(string path)

View File

@ -1,4 +1,5 @@
using System;
using MediaBrowser.Model.Dto;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
@ -34,7 +35,7 @@ namespace MediaBrowser.Model.Entities
/// </summary>
/// <value>The type of the media.</value>
public string MediaType { get; set; }
/// <summary>
/// Gets or sets the run time ticks.
/// </summary>
@ -64,7 +65,7 @@ namespace MediaBrowser.Model.Entities
/// </summary>
/// <value>The logo item identifier.</value>
public string LogoItemId { get; set; }
/// <summary>
/// Gets or sets the thumb image tag.
/// </summary>
@ -136,7 +137,25 @@ namespace MediaBrowser.Model.Entities
/// </summary>
/// <value>The artists.</value>
public List<string> Artists { get; set; }
/// <summary>
/// Gets or sets the media streams.
/// </summary>
/// <value>The media streams.</value>
public List<MediaStream> MediaStreams { get; set; }
/// <summary>
/// Gets or sets the chapter images item identifier.
/// </summary>
/// <value>The chapter images item identifier.</value>
public string ChapterImagesItemId { get; set; }
/// <summary>
/// Gets or sets the chapters.
/// </summary>
/// <value>The chapters.</value>
public List<ChapterInfoDto> Chapters { get; set; }
/// <summary>
/// Gets a value indicating whether this instance has primary image.
/// </summary>
@ -150,6 +169,8 @@ namespace MediaBrowser.Model.Entities
public BaseItemInfo()
{
Artists = new List<string>();
MediaStreams = new List<MediaStream>();
Chapters = new List<ChapterInfoDto>();
}
}
}

View File

@ -173,7 +173,7 @@ namespace MediaBrowser.Providers.MediaInfo
{
var prober = new FFProbeVideoInfo(_logger, _isoManager, _mediaEncoder, _itemRepo, _blurayExaminer, _localization, _appPaths, _json, _encodingManager, _fileSystem, _config, _subtitleManager);
return !video.SubtitleFiles.SequenceEqual(prober.GetSubtitleFiles(video, directoryService).Select(i => i.FullName).OrderBy(i => i), StringComparer.OrdinalIgnoreCase);
return !video.SubtitleFiles.SequenceEqual(prober.GetSubtitleFiles(video, directoryService, false).Select(i => i.FullName).OrderBy(i => i), StringComparer.OrdinalIgnoreCase);
}
}

View File

@ -417,7 +417,7 @@ namespace MediaBrowser.Providers.MediaInfo
}
}
public IEnumerable<FileSystemInfo> GetSubtitleFiles(Video video, IDirectoryService directoryService)
public IEnumerable<FileSystemInfo> GetSubtitleFiles(Video video, IDirectoryService directoryService, bool clearCache)
{
var containingPath = video.ContainingFolderPath;
@ -426,7 +426,7 @@ namespace MediaBrowser.Providers.MediaInfo
throw new ArgumentException(string.Format("Cannot search for items that don't have a path: {0} {1}", video.Name, video.Id));
}
var files = directoryService.GetFiles(containingPath);
var files = directoryService.GetFiles(containingPath, clearCache);
var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path);
@ -460,7 +460,7 @@ namespace MediaBrowser.Providers.MediaInfo
/// <param name="currentStreams">The current streams.</param>
private async Task AddExternalSubtitles(Video video, List<MediaStream> currentStreams, IDirectoryService directoryService, CancellationToken cancellationToken)
{
var externalSubtitleStreams = GetExternalSubtitleStreams(video, currentStreams.Count, directoryService).ToList();
var externalSubtitleStreams = GetExternalSubtitleStreams(video, currentStreams.Count, directoryService, false).ToList();
if ((_config.Configuration.SubtitleOptions.DownloadEpisodeSubtitles &&
video is Episode) ||
@ -480,7 +480,7 @@ namespace MediaBrowser.Providers.MediaInfo
// Rescan
if (downloadedLanguages.Count > 0)
{
externalSubtitleStreams = GetExternalSubtitleStreams(video, currentStreams.Count, directoryService).ToList();
externalSubtitleStreams = GetExternalSubtitleStreams(video, currentStreams.Count, directoryService, true).ToList();
}
}
@ -491,9 +491,10 @@ namespace MediaBrowser.Providers.MediaInfo
private IEnumerable<MediaStream> GetExternalSubtitleStreams(Video video,
int startIndex,
IDirectoryService directoryService)
IDirectoryService directoryService,
bool clearCache)
{
var files = GetSubtitleFiles(video, directoryService);
var files = GetSubtitleFiles(video, directoryService, clearCache);
var streams = new List<MediaStream>();

View File

@ -91,9 +91,9 @@ namespace MediaBrowser.Providers.MediaInfo
return false;
}
// There's already an audio stream for this language
// There's already a default audio stream for this language
if (skipIfAudioTrackMatches &&
internalMediaStreams.Any(i => i.Type == MediaStreamType.Audio && string.Equals(i.Language, language, StringComparison.OrdinalIgnoreCase)))
internalMediaStreams.Any(i => i.Type == MediaStreamType.Audio && i.IsDefault && string.Equals(i.Language, language, StringComparison.OrdinalIgnoreCase)))
{
return false;
}

View File

@ -120,7 +120,13 @@ namespace MediaBrowser.Providers.Subtitles
if (results.Count == 0)
{
throw new ResourceNotFoundException("Subtitle with Id " + ossId + " was not found.");
var msg = string.Format("Subtitle with Id {0} was not found. Name: {1}. Status: {2}. Message: {3}",
ossId,
resultDownLoad.Name ?? string.Empty,
resultDownLoad.Message ?? string.Empty,
resultDownLoad.Status ?? string.Empty);
throw new ResourceNotFoundException(msg);
}
var data = Convert.FromBase64String(results.First().Data);
@ -245,7 +251,7 @@ namespace MediaBrowser.Providers.Subtitles
ProviderName = Name,
Language = i.SubLanguageID,
Id = i.SubFormat + "-" + i.SubLanguageID + "-" + i.IDSubtitle,
Id = i.SubFormat + "-" + i.SubLanguageID + "-" + i.IDSubtitleFile,
Name = i.SubFileName,
DateCreated = DateTime.Parse(i.SubAddDate, _usCulture),

View File

@ -14,12 +14,12 @@ using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Notifications;
using MediaBrowser.Model.Tasks;
using MediaBrowser.Model.Updates;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Updates;
namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
{
@ -40,6 +40,9 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
private readonly ISessionManager _sessionManager;
private readonly IServerApplicationHost _appHost;
private Timer LibraryUpdateTimer { get; set; }
private readonly object _libraryChangedSyncLock = new object();
public Notifications(IInstallationManager installationManager, IUserManager userManager, ILogger logger, ITaskManager taskManager, INotificationManager notificationManager, IServerConfigurationManager config, ILibraryManager libraryManager, ISessionManager sessionManager, IServerApplicationHost appHost)
{
_installationManager = installationManager;
@ -210,21 +213,55 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
return null;
}
async void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
private readonly List<BaseItem> _itemsAdded = new List<BaseItem>();
void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
{
if (e.Item.LocationType == LocationType.FileSystem)
if (e.Item.LocationType == LocationType.FileSystem && !e.Item.IsFolder)
{
var type = NotificationType.NewLibraryContent.ToString();
lock (_libraryChangedSyncLock)
{
if (LibraryUpdateTimer == null)
{
LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, 5000,
Timeout.Infinite);
}
else
{
LibraryUpdateTimer.Change(5000, Timeout.Infinite);
}
var item = e.Item;
_itemsAdded.Add(e.Item);
}
}
}
private async void LibraryUpdateTimerCallback(object state)
{
List<BaseItem> items;
lock (_libraryChangedSyncLock)
{
items = _itemsAdded.ToList();
_itemsAdded.Clear();
DisposeLibraryUpdateTimer();
}
var item = items.FirstOrDefault();
if (item != null)
{
var notification = new NotificationRequest
{
NotificationType = type
NotificationType = NotificationType.NewLibraryContent.ToString()
};
notification.Variables["Name"] = item.Name;
if (items.Count > 1)
{
notification.Name = items.Count + " new library items.";
}
await SendNotification(notification).ConfigureAwait(false);
}
}
@ -313,6 +350,8 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
public void Dispose()
{
DisposeLibraryUpdateTimer();
_installationManager.PluginInstalled -= _installationManager_PluginInstalled;
_installationManager.PluginUpdated -= _installationManager_PluginUpdated;
_installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
@ -328,5 +367,14 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
_appHost.HasUpdateAvailableChanged -= _appHost_HasUpdateAvailableChanged;
_appHost.ApplicationUpdated -= _appHost_ApplicationUpdated;
}
private void DisposeLibraryUpdateTimer()
{
if (LibraryUpdateTimer != null)
{
LibraryUpdateTimer.Dispose();
LibraryUpdateTimer = null;
}
}
}
}

View File

@ -715,6 +715,6 @@
"LabelAudioLanguagePreferenceHelp": "If empty, the default audio track will be selected, regardless of language.",
"LabelDownloadLanguages": "Download languages:",
"ButtonRegister": "Register",
"LabelSkipIfAudioTrackPresent": "Skip if the video has an audio track with the download language",
"LabelSkipIfAudioTrackPresent": "Skip if the default audio track matches the download language",
"LabelSkipIfAudioTrackPresentHelp": "Uncheck this to ensure all videos have subtitles, regardless of audio language."
}

File diff suppressed because one or more lines are too long

View File

@ -48,6 +48,7 @@ namespace MediaBrowser.Server.Implementations.Session
private readonly IMusicManager _musicManager;
private readonly IDtoService _dtoService;
private readonly IImageProcessor _imageProcessor;
private readonly IItemRepository _itemRepo;
/// <summary>
/// Gets or sets the configuration manager.
@ -90,7 +91,7 @@ namespace MediaBrowser.Server.Implementations.Session
/// <param name="logger">The logger.</param>
/// <param name="userRepository">The user repository.</param>
/// <param name="libraryManager">The library manager.</param>
public SessionManager(IUserDataManager userDataRepository, IServerConfigurationManager configurationManager, ILogger logger, IUserRepository userRepository, ILibraryManager libraryManager, IUserManager userManager, IMusicManager musicManager, IDtoService dtoService, IImageProcessor imageProcessor)
public SessionManager(IUserDataManager userDataRepository, IServerConfigurationManager configurationManager, ILogger logger, IUserRepository userRepository, ILibraryManager libraryManager, IUserManager userManager, IMusicManager musicManager, IDtoService dtoService, IImageProcessor imageProcessor, IItemRepository itemRepo)
{
_userDataRepository = userDataRepository;
_configurationManager = configurationManager;
@ -101,6 +102,7 @@ namespace MediaBrowser.Server.Implementations.Session
_musicManager = musicManager;
_dtoService = dtoService;
_imageProcessor = imageProcessor;
_itemRepo = itemRepo;
}
/// <summary>
@ -279,7 +281,7 @@ namespace MediaBrowser.Server.Implementations.Session
if (!string.IsNullOrWhiteSpace(info.ItemId) && libraryItem != null)
{
info.Item = GetItemInfo(libraryItem, runtimeTicks);
info.Item = GetItemInfo(libraryItem, runtimeTicks, libraryItem, info.MediaSourceId);
}
session.NowPlayingItem = info.Item;
@ -1172,9 +1174,11 @@ namespace MediaBrowser.Server.Implementations.Session
/// </summary>
/// <param name="item">The item.</param>
/// <param name="runtimeTicks">The now playing runtime ticks.</param>
/// <param name="chapterOwner">The chapter owner.</param>
/// <param name="mediaSourceId">The media source identifier.</param>
/// <returns>BaseItemInfo.</returns>
/// <exception cref="System.ArgumentNullException">item</exception>
private BaseItemInfo GetItemInfo(BaseItem item, long? runtimeTicks)
private BaseItemInfo GetItemInfo(BaseItem item, long? runtimeTicks, BaseItem chapterOwner, string mediaSourceId)
{
if (item == null)
{
@ -1322,6 +1326,22 @@ namespace MediaBrowser.Server.Implementations.Session
info.LogoItemId = GetDtoId(logoItem);
}
if (chapterOwner != null)
{
info.ChapterImagesItemId = chapterOwner.Id.ToString("N");
info.Chapters = _itemRepo.GetChapters(chapterOwner.Id).Select(i => _dtoService.GetChapterInfoDto(i, chapterOwner)).ToList();
}
if (!string.IsNullOrWhiteSpace(mediaSourceId))
{
info.MediaStreams = _itemRepo.GetMediaStreams(new MediaStreamQuery
{
ItemId = new Guid(mediaSourceId)
}).ToList();
}
return info;
}
@ -1347,7 +1367,7 @@ namespace MediaBrowser.Server.Implementations.Session
{
var item = _libraryManager.GetItemById(new Guid(itemId));
var info = GetItemInfo(item, item.RunTimeTicks);
var info = GetItemInfo(item, item.RunTimeTicks, null, null);
ReportNowViewingItem(sessionId, info);
}

View File

@ -496,7 +496,7 @@ namespace MediaBrowser.ServerApplication
DtoService = new DtoService(Logger, LibraryManager, UserManager, UserDataManager, ItemRepository, ImageProcessor, ServerConfigurationManager, FileSystemManager, ProviderManager);
RegisterSingleInstance(DtoService);
SessionManager = new SessionManager(UserDataManager, ServerConfigurationManager, Logger, UserRepository, LibraryManager, UserManager, musicManager, DtoService, ImageProcessor);
SessionManager = new SessionManager(UserDataManager, ServerConfigurationManager, Logger, UserRepository, LibraryManager, UserManager, musicManager, DtoService, ImageProcessor, ItemRepository);
RegisterSingleInstance(SessionManager);
var newsService = new Server.Implementations.News.NewsService(ApplicationPaths, JsonSerializer);