From 708a8115d5bd53fb079d5f0a9109b4df12f417f9 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 1 Jul 2013 14:17:55 -0400 Subject: [PATCH] added game xml saving --- MediaBrowser.Api/BaseApiService.cs | 22 ++++- MediaBrowser.Api/ItemUpdateService.cs | 14 +-- .../UserLibrary/GameGenresService.cs | 2 +- .../MediaBrowser.Providers.csproj | 1 + MediaBrowser.Providers/Savers/GameXmlSaver.cs | 85 +++++++++++++++++++ .../Library/LuceneSearchEngine.cs | 28 +++++- 6 files changed, 142 insertions(+), 10 deletions(-) create mode 100644 MediaBrowser.Providers/Savers/GameXmlSaver.cs diff --git a/MediaBrowser.Api/BaseApiService.cs b/MediaBrowser.Api/BaseApiService.cs index 5147e93db..193a23b3e 100644 --- a/MediaBrowser.Api/BaseApiService.cs +++ b/MediaBrowser.Api/BaseApiService.cs @@ -117,7 +117,7 @@ namespace MediaBrowser.Api protected Task GetGameGenre(string name, ILibraryManager libraryManager) { - return libraryManager.GetGameGenre(DeSlugGenreName(name, libraryManager)); + return libraryManager.GetGameGenre(DeSlugGameGenreName(name, libraryManager)); } protected Task GetPerson(string name, ILibraryManager libraryManager) @@ -174,6 +174,26 @@ namespace MediaBrowser.Api }) ?? name; } + protected string DeSlugGameGenreName(string name, ILibraryManager libraryManager) + { + if (name.IndexOf(SlugChar) == -1) + { + return name; + } + + return libraryManager.RootFolder.RecursiveChildren + .OfType() + .SelectMany(i => i.Genres) + .Distinct(StringComparer.OrdinalIgnoreCase) + .FirstOrDefault(i => + { + i = _dashReplaceChars.Aggregate(i, (current, c) => current.Replace(c, SlugChar)); + + return string.Equals(i, name, StringComparison.OrdinalIgnoreCase); + + }) ?? name; + } + /// /// Deslugs a studio name by finding the correct entry in the library /// diff --git a/MediaBrowser.Api/ItemUpdateService.cs b/MediaBrowser.Api/ItemUpdateService.cs index 71f1e0ddc..a86db896b 100644 --- a/MediaBrowser.Api/ItemUpdateService.cs +++ b/MediaBrowser.Api/ItemUpdateService.cs @@ -68,7 +68,7 @@ namespace MediaBrowser.Api [ApiMember(Name = "GenreName", Description = "The name of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] public string GenreName { get; set; } } - + public class ItemUpdateService : BaseApiService { private readonly ILibraryManager _libraryManager; @@ -105,7 +105,7 @@ namespace MediaBrowser.Api private async Task UpdateItem(UpdatePerson request) { - var item = await _libraryManager.GetPerson(request.PersonName).ConfigureAwait(false); + var item = await GetPerson(request.PersonName, _libraryManager).ConfigureAwait(false); UpdateItem(request, item); @@ -121,7 +121,7 @@ namespace MediaBrowser.Api private async Task UpdateItem(UpdateArtist request) { - var item = await _libraryManager.GetArtist(request.ArtistName).ConfigureAwait(false); + var item = await GetArtist(request.ArtistName, _libraryManager).ConfigureAwait(false); UpdateItem(request, item); @@ -137,7 +137,7 @@ namespace MediaBrowser.Api private async Task UpdateItem(UpdateStudio request) { - var item = await _libraryManager.GetStudio(request.StudioName).ConfigureAwait(false); + var item = await GetStudio(request.StudioName, _libraryManager).ConfigureAwait(false); UpdateItem(request, item); @@ -153,7 +153,7 @@ namespace MediaBrowser.Api private async Task UpdateItem(UpdateMusicGenre request) { - var item = await _libraryManager.GetMusicGenre(request.GenreName).ConfigureAwait(false); + var item = await GetMusicGenre(request.GenreName, _libraryManager).ConfigureAwait(false); UpdateItem(request, item); @@ -169,7 +169,7 @@ namespace MediaBrowser.Api private async Task UpdateItem(UpdateGameGenre request) { - var item = await _libraryManager.GetGameGenre(request.GenreName).ConfigureAwait(false); + var item = await GetGameGenre(request.GenreName, _libraryManager).ConfigureAwait(false); UpdateItem(request, item); @@ -185,7 +185,7 @@ namespace MediaBrowser.Api private async Task UpdateItem(UpdateGenre request) { - var item = await _libraryManager.GetGenre(request.GenreName).ConfigureAwait(false); + var item = await GetGenre(request.GenreName, _libraryManager).ConfigureAwait(false); UpdateItem(request, item); diff --git a/MediaBrowser.Api/UserLibrary/GameGenresService.cs b/MediaBrowser.Api/UserLibrary/GameGenresService.cs index fc99ce6b8..813407e55 100644 --- a/MediaBrowser.Api/UserLibrary/GameGenresService.cs +++ b/MediaBrowser.Api/UserLibrary/GameGenresService.cs @@ -149,7 +149,7 @@ namespace MediaBrowser.Api.UserLibrary /// System.Object. public object Get(GetGameGenreItemCounts request) { - var name = DeSlugGenreName(request.Name, LibraryManager); + var name = DeSlugGameGenreName(request.Name, LibraryManager); var items = GetItems(request.UserId).Where(i => i.Genres != null && i.Genres.Contains(name, StringComparer.OrdinalIgnoreCase)).ToList(); diff --git a/MediaBrowser.Providers/MediaBrowser.Providers.csproj b/MediaBrowser.Providers/MediaBrowser.Providers.csproj index f3c8cd45b..a3f7c2704 100644 --- a/MediaBrowser.Providers/MediaBrowser.Providers.csproj +++ b/MediaBrowser.Providers/MediaBrowser.Providers.csproj @@ -82,6 +82,7 @@ + diff --git a/MediaBrowser.Providers/Savers/GameXmlSaver.cs b/MediaBrowser.Providers/Savers/GameXmlSaver.cs new file mode 100644 index 000000000..e0640df4f --- /dev/null +++ b/MediaBrowser.Providers/Savers/GameXmlSaver.cs @@ -0,0 +1,85 @@ +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Providers.Movies; +using System; +using System.Globalization; +using System.IO; +using System.Text; +using System.Threading; + +namespace MediaBrowser.Providers.Savers +{ + /// + /// Saves game.xml for games + /// + public class GameXmlSaver : IMetadataSaver + { + private readonly IServerConfigurationManager _config; + + public GameXmlSaver(IServerConfigurationManager config) + { + _config = config; + } + + /// + /// Determines whether [is enabled for] [the specified item]. + /// + /// The item. + /// Type of the update. + /// true if [is enabled for] [the specified item]; otherwise, false. + public bool IsEnabledFor(BaseItem item, ItemUpdateType updateType) + { + var wasMetadataEdited = (updateType & ItemUpdateType.MetadataEdit) == ItemUpdateType.MetadataEdit; + var wasMetadataDownloaded = (updateType & ItemUpdateType.MetadataDownload) == ItemUpdateType.MetadataDownload; + + // If new metadata has been downloaded and save local is on, OR metadata was manually edited, proceed + if ((_config.Configuration.SaveLocalMeta && (wasMetadataEdited || wasMetadataDownloaded)) || wasMetadataEdited) + { + return item is Game; + } + + return false; + } + + private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); + + /// + /// Saves the specified item. + /// + /// The item. + /// The cancellation token. + /// Task. + public void Save(BaseItem item, CancellationToken cancellationToken) + { + var builder = new StringBuilder(); + + builder.Append(""); + + XmlSaverHelpers.AddCommonNodes(item, builder); + + builder.Append(""); + + var xmlFilePath = GetSavePath(item); + + XmlSaverHelpers.Save(builder, xmlFilePath, new string[] + { + }); + + // Set last refreshed so that the provider doesn't trigger after the file save + MovieProviderFromXml.Current.SetLastRefreshed(item, DateTime.UtcNow); + } + + public string GetSavePath(BaseItem item) + { + if (item.ResolveArgs.IsDirectory) + { + var path = Directory.Exists(item.Path) ? item.Path : Path.GetDirectoryName(item.Path); + + return Path.Combine(path, "game.xml"); + } + + return Path.ChangeExtension(item.Path, ".xml"); + } + } +} diff --git a/MediaBrowser.Server.Implementations/Library/LuceneSearchEngine.cs b/MediaBrowser.Server.Implementations/Library/LuceneSearchEngine.cs index e7565ff65..f47d6c448 100644 --- a/MediaBrowser.Server.Implementations/Library/LuceneSearchEngine.cs +++ b/MediaBrowser.Server.Implementations/Library/LuceneSearchEngine.cs @@ -145,7 +145,7 @@ namespace MediaBrowser.Server.Implementations.Library } // Find genres, from non-audio items - var genres = items.Where(i => !(i is Audio) && !(i is MusicAlbum) && !(i is MusicAlbumDisc) && !(i is MusicArtist) && !(i is MusicVideo)) + var genres = items.Where(i => !(i is Audio) && !(i is MusicAlbum) && !(i is MusicAlbumDisc) && !(i is MusicArtist) && !(i is MusicVideo) && !(i is Game)) .SelectMany(i => i.Genres) .Where(i => !string.IsNullOrEmpty(i)) .Distinct(StringComparer.OrdinalIgnoreCase) @@ -196,6 +196,32 @@ namespace MediaBrowser.Server.Implementations.Library } } + // Find music genres + var gameGenres = items.OfType() + .SelectMany(i => i.Genres) + .Where(i => !string.IsNullOrEmpty(i)) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + + foreach (var item in gameGenres) + { + var index = GetIndex(item, searchTerm, terms); + + if (index.Item2 != -1) + { + try + { + var genre = await _libraryManager.GetGameGenre(item).ConfigureAwait(false); + + hints.Add(new Tuple(genre, index.Item1, index.Item2)); + } + catch (Exception ex) + { + _logger.ErrorException("Error getting {0}", ex, item); + } + } + } + // Find studios var studios = items.SelectMany(i => i.Studios) .Where(i => !string.IsNullOrEmpty(i))