Explorar o código

Implemented some IBN functionality - GetPerson, GetYear, GetStudio, GetGenre

LukePulverenti Luke Pulverenti luke pulverenti %!s(int64=13) %!d(string=hai) anos
pai
achega
ee1fa6e816

+ 1 - 1
MediaBrowser.Api/HttpHandlers/GenreHandler.cs → MediaBrowser.Api/HttpHandlers/ItemsWithGenreHandler.cs

@@ -7,7 +7,7 @@ namespace MediaBrowser.Api.HttpHandlers
     /// <summary>
     /// Gets all items within a Genre
     /// </summary>
-    public class GenreHandler : ItemListHandler
+    public class ItemsWithGenreHandler : ItemListHandler
     {
         protected override IEnumerable<BaseItem> ItemsToSerialize
         {

+ 1 - 1
MediaBrowser.Api/HttpHandlers/StudioHandler.cs → MediaBrowser.Api/HttpHandlers/ItemsWithStudioHandler.cs

@@ -7,7 +7,7 @@ namespace MediaBrowser.Api.HttpHandlers
     /// <summary>
     /// Gets all items within containing a studio
     /// </summary>
-    public class StudioHandler : ItemListHandler
+    public class ItemsWithStudioHandler : ItemListHandler
     {
         protected override IEnumerable<BaseItem> ItemsToSerialize
         {

+ 22 - 0
MediaBrowser.Api/HttpHandlers/ItemsWithYearHandler.cs

@@ -0,0 +1,22 @@
+using System.Collections.Generic;
+using MediaBrowser.Controller;
+using MediaBrowser.Model.Entities;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+    /// <summary>
+    /// Gets all items within containing a studio
+    /// </summary>
+    public class ItemsWithYearHandler : ItemListHandler
+    {
+        protected override IEnumerable<BaseItem> ItemsToSerialize
+        {
+            get
+            {
+                Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder;
+
+                return Kernel.Instance.GetItemsWithYear(parent, int.Parse(QueryString["name"]), UserId);
+            }
+        }
+    }
+}

+ 20 - 0
MediaBrowser.Api/HttpHandlers/YearsHandler.cs

@@ -0,0 +1,20 @@
+using System;
+using MediaBrowser.Controller;
+using MediaBrowser.Model.Entities;
+
+namespace MediaBrowser.Api.HttpHandlers
+{
+    public class YearsHandler : JsonHandler
+    {
+        protected override object ObjectToSerialize
+        {
+            get
+            {
+                Folder parent = ApiService.GetItemById(QueryString["id"]) as Folder;
+                Guid userId = Guid.Parse(QueryString["userid"]);
+
+                return Kernel.Instance.GetAllYears(parent, userId);
+            }
+        }
+    }
+}

+ 4 - 2
MediaBrowser.Api/MediaBrowser.Api.csproj

@@ -49,7 +49,7 @@
     <Compile Include="ApiService.cs" />
     <Compile Include="HttpHandlers\AudioHandler.cs" />
     <Compile Include="HttpHandlers\BaseMediaHandler.cs" />
-    <Compile Include="HttpHandlers\GenreHandler.cs" />
+    <Compile Include="HttpHandlers\ItemsWithGenreHandler.cs" />
     <Compile Include="HttpHandlers\GenresHandler.cs" />
     <Compile Include="HttpHandlers\ImageHandler.cs" />
     <Compile Include="HttpHandlers\InProgressItemsHandler.cs" />
@@ -60,11 +60,13 @@
     <Compile Include="HttpHandlers\PluginConfigurationHandler.cs" />
     <Compile Include="HttpHandlers\PluginsHandler.cs" />
     <Compile Include="HttpHandlers\RecentlyAddedItemsHandler.cs" />
-    <Compile Include="HttpHandlers\StudioHandler.cs" />
+    <Compile Include="HttpHandlers\ItemsWithStudioHandler.cs" />
     <Compile Include="HttpHandlers\StudiosHandler.cs" />
     <Compile Include="HttpHandlers\UserConfigurationHandler.cs" />
     <Compile Include="HttpHandlers\UsersHandler.cs" />
     <Compile Include="HttpHandlers\VideoHandler.cs" />
+    <Compile Include="HttpHandlers\ItemsWithYearHandler.cs" />
+    <Compile Include="HttpHandlers\YearsHandler.cs" />
     <Compile Include="ImageProcessor.cs" />
     <Compile Include="Plugin.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />

+ 12 - 4
MediaBrowser.Api/Plugin.cs

@@ -49,17 +49,25 @@ namespace MediaBrowser.Api
             {
                 return new UsersHandler();
             }
-            else if (localPath.EndsWith("/api/genre", StringComparison.OrdinalIgnoreCase))
+            else if (localPath.EndsWith("/api/itemswithgenre", StringComparison.OrdinalIgnoreCase))
             {
-                return new GenreHandler();
+                return new ItemsWithGenreHandler();
             }
             else if (localPath.EndsWith("/api/genres", StringComparison.OrdinalIgnoreCase))
             {
                 return new GenresHandler();
             }
-            else if (localPath.EndsWith("/api/studio", StringComparison.OrdinalIgnoreCase))
+            else if (localPath.EndsWith("/api/itemswithyear", StringComparison.OrdinalIgnoreCase))
             {
-                return new StudioHandler();
+                return new ItemsWithYearHandler();
+            }
+            else if (localPath.EndsWith("/api/years", StringComparison.OrdinalIgnoreCase))
+            {
+                return new YearsHandler();
+            }
+            else if (localPath.EndsWith("/api/itemswithstudio", StringComparison.OrdinalIgnoreCase))
+            {
+                return new ItemsWithStudioHandler();
             }
             else if (localPath.EndsWith("/api/studios", StringComparison.OrdinalIgnoreCase))
             {

+ 32 - 6
MediaBrowser.ApiInteraction/ApiClient.cs

@@ -183,16 +183,42 @@ namespace MediaBrowser.ApiInteraction
             }
         }
 
+        /// <summary>
+        /// Gets all Years
+        /// </summary>
+        public async Task<IEnumerable<CategoryInfo<Year>>> GetAllYearsAsync(Guid userId)
+        {
+            string url = ApiUrl + "/years?userId=" + userId.ToString();
+
+            using (Stream stream = await HttpClient.GetStreamAsync(url))
+            {
+                return JsonSerializer.DeserializeFromStream<IEnumerable<CategoryInfo<Year>>>(stream);
+            }
+        }
+
+        /// <summary>
+        /// Gets a Year
+        /// </summary>
+        public async Task<IEnumerable<ApiBaseItemWrapper<ApiBaseItem>>> GetItemsWithYearAsync(string name, Guid userId)
+        {
+            string url = ApiUrl + "/itemswithyear?userId=" + userId.ToString() + "&name=" + name;
+
+            using (Stream stream = await HttpClient.GetStreamAsync(url))
+            {
+                return JsonSerializer.DeserializeFromStream<IEnumerable<ApiBaseItemWrapper<ApiBaseItem>>>(stream);
+            }
+        }
+
         /// <summary>
         /// Gets a Genre
         /// </summary>
-        public async Task<CategoryInfo<Genre>> GetGenreAsync(string name, Guid userId)
+        public async Task<IEnumerable<ApiBaseItemWrapper<ApiBaseItem>>> GetItemsWithGenreAsync(string name, Guid userId)
         {
-            string url = ApiUrl + "/genre?userId=" + userId.ToString() + "&name=" + name;
+            string url = ApiUrl + "/itemswithgenre?userId=" + userId.ToString() + "&name=" + name;
 
             using (Stream stream = await HttpClient.GetStreamAsync(url))
             {
-                return JsonSerializer.DeserializeFromStream<CategoryInfo<Genre>>(stream);
+                return JsonSerializer.DeserializeFromStream<IEnumerable<ApiBaseItemWrapper<ApiBaseItem>>>(stream);
             }
         }
 
@@ -225,13 +251,13 @@ namespace MediaBrowser.ApiInteraction
         /// <summary>
         /// Gets a Studio
         /// </summary>
-        public async Task<CategoryInfo<Studio>> GetStudioAsync(string name, Guid userId)
+        public async Task<IEnumerable<ApiBaseItemWrapper<ApiBaseItem>>> GetItemsWithStudioAsync(string name, Guid userId)
         {
-            string url = ApiUrl + "/studio?userId=" + userId.ToString() + "&name=" + name;
+            string url = ApiUrl + "/itemswithstudio?userId=" + userId.ToString() + "&name=" + name;
 
             using (Stream stream = await HttpClient.GetStreamAsync(url))
             {
-                return JsonSerializer.DeserializeFromStream<CategoryInfo<Studio>>(stream);
+                return JsonSerializer.DeserializeFromStream<IEnumerable<ApiBaseItemWrapper<ApiBaseItem>>>(stream);
             }
         }
     }

+ 85 - 1
MediaBrowser.Common/Configuration/ApplicationPaths.cs

@@ -175,7 +175,7 @@ namespace MediaBrowser.Common.Configuration
         /// <summary>
         /// Gets the path to the Images By Name directory
         /// </summary>
-        public static string IBNPath
+        public static string ImagesByNamePath
         {
             get
             {
@@ -192,6 +192,90 @@ namespace MediaBrowser.Common.Configuration
             }
         }
 
+        private static string _PeoplePath;
+        /// <summary>
+        /// Gets the path to the People directory
+        /// </summary>
+        public static string PeoplePath
+        {
+            get
+            {
+                if (_PeoplePath == null)
+                {
+                    _PeoplePath = Path.Combine(ImagesByNamePath, "People");
+                    if (!Directory.Exists(_PeoplePath))
+                    {
+                        Directory.CreateDirectory(_PeoplePath);
+                    }
+                }
+
+                return _PeoplePath;
+            }
+        }
+
+        private static string _GenrePath;
+        /// <summary>
+        /// Gets the path to the Genre directory
+        /// </summary>
+        public static string GenrePath
+        {
+            get
+            {
+                if (_GenrePath == null)
+                {
+                    _GenrePath = Path.Combine(ImagesByNamePath, "Genre");
+                    if (!Directory.Exists(_GenrePath))
+                    {
+                        Directory.CreateDirectory(_GenrePath);
+                    }
+                }
+
+                return _GenrePath;
+            }
+        }
+
+        private static string _StudioPath;
+        /// <summary>
+        /// Gets the path to the Studio directory
+        /// </summary>
+        public static string StudioPath
+        {
+            get
+            {
+                if (_StudioPath == null)
+                {
+                    _StudioPath = Path.Combine(ImagesByNamePath, "Studio");
+                    if (!Directory.Exists(_StudioPath))
+                    {
+                        Directory.CreateDirectory(_StudioPath);
+                    }
+                }
+
+                return _StudioPath;
+            }
+        }
+
+        private static string _yearPath;
+        /// <summary>
+        /// Gets the path to the Year directory
+        /// </summary>
+        public static string YearPath
+        {
+            get
+            {
+                if (_yearPath == null)
+                {
+                    _yearPath = Path.Combine(ImagesByNamePath, "Year");
+                    if (!Directory.Exists(_yearPath))
+                    {
+                        Directory.CreateDirectory(_yearPath);
+                    }
+                }
+
+                return _yearPath;
+            }
+        }
+        
         /// <summary>
         /// Gets the path to the application's ProgramDataFolder
         /// </summary>

+ 60 - 1
MediaBrowser.Controller/Kernel.cs

@@ -314,6 +314,14 @@ namespace MediaBrowser.Controller
             return GetParentalAllowedRecursiveChildren(parent, userId).Where(f => f.Genres != null && f.Genres.Any(s => s.Equals(genre, StringComparison.OrdinalIgnoreCase)));
         }
 
+        /// <summary>
+        /// Finds all recursive items within a top-level parent that contain the given year and are allowed for the current user
+        /// </summary>
+        public IEnumerable<BaseItem> GetItemsWithYear(Folder parent, int year, Guid userId)
+        {
+            return GetParentalAllowedRecursiveChildren(parent, userId).Where(f => f.ProductionYear.HasValue && f.ProductionYear == year);
+        }
+        
         /// <summary>
         /// Finds all recursive items within a top-level parent that contain the given person and are allowed for the current user
         /// </summary>
@@ -322,6 +330,57 @@ namespace MediaBrowser.Controller
             return GetParentalAllowedRecursiveChildren(parent, userId).Where(f => f.People != null && f.People.Any(s => s.Name.Equals(personName, StringComparison.OrdinalIgnoreCase)));
         }
 
+        /// <summary>
+        /// Gets all years from all recursive children of a folder
+        /// The CategoryInfo class is used to keep track of the number of times each year appears
+        /// </summary>
+        public IEnumerable<CategoryInfo<Year>> GetAllYears(Folder parent, Guid userId)
+        {
+            Dictionary<int, int> data = new Dictionary<int, int>();
+
+            // Get all the allowed recursive children
+            IEnumerable<BaseItem> allItems = Kernel.Instance.GetParentalAllowedRecursiveChildren(parent, userId);
+
+            foreach (var item in allItems)
+            {
+                // Add the year from the item to the data dictionary
+                // If the year already exists, increment the count
+                if (item.ProductionYear == null)
+                {
+                    continue;
+                }
+
+                if (!data.ContainsKey(item.ProductionYear.Value))
+                {
+                    data.Add(item.ProductionYear.Value, 1);
+                }
+                else
+                {
+                    data[item.ProductionYear.Value]++;
+                }
+            }
+
+            // Now go through the dictionary and create a Category for each studio
+            List<CategoryInfo<Year>> list = new List<CategoryInfo<Year>>();
+
+            foreach (int key in data.Keys)
+            {
+                // Get the original entity so that we can also supply the PrimaryImagePath
+                Year entity = Kernel.Instance.ItemController.GetYear(key);
+
+                if (entity != null)
+                {
+                    list.Add(new CategoryInfo<Year>()
+                    {
+                        Item = entity,
+                        ItemCount = data[key]
+                    });
+                }
+            }
+
+            return list;
+        }
+
         /// <summary>
         /// Gets all studios from all recursive children of a folder
         /// The CategoryInfo class is used to keep track of the number of times each studio appears
@@ -441,7 +500,7 @@ namespace MediaBrowser.Controller
             User user = new User();
 
             user.Name = "Default User";
-            user.Id = Guid.NewGuid();
+            user.Id = Guid.Parse("5d1cf7fce25943b790d140095457a42b");
 
             list.Add(user);
 

+ 90 - 19
MediaBrowser.Controller/Library/ItemController.cs

@@ -8,6 +8,7 @@ using MediaBrowser.Controller.Events;
 using MediaBrowser.Controller.IO;
 using MediaBrowser.Controller.Resolvers;
 using MediaBrowser.Model.Entities;
+using MediaBrowser.Common.Configuration;
 
 namespace MediaBrowser.Controller.Library
 {
@@ -57,41 +58,41 @@ namespace MediaBrowser.Controller.Library
         }
         #endregion
 
-        #region Item Events
+        #region BaseItem Events
         /// <summary>
         /// Called when an item is being created.
         /// This should be used to fill item values, such as metadata
         /// </summary>
-        public event EventHandler<GenericItemEventArgs<BaseItem>> ItemCreating;
+        public event EventHandler<GenericItemEventArgs<BaseItem>> BaseItemCreating;
 
         /// <summary>
         /// Called when an item has been created.
         /// This should be used to process or modify item values.
         /// </summary>
-        public event EventHandler<GenericItemEventArgs<BaseItem>> ItemCreated;
+        public event EventHandler<GenericItemEventArgs<BaseItem>> BaseItemCreated;
         #endregion
 
         /// <summary>
         /// Called when an item has been created
         /// </summary>
-        private void OnItemCreated(BaseItem item, Folder parent)
+        private void OnBaseItemCreated(BaseItem item, Folder parent)
         {
             GenericItemEventArgs<BaseItem> args = new GenericItemEventArgs<BaseItem> { Item = item };
 
-            if (ItemCreating != null)
+            if (BaseItemCreating != null)
             {
-                ItemCreating(this, args);
+                BaseItemCreating(this, args);
             }
 
-            if (ItemCreated != null)
+            if (BaseItemCreated != null)
             {
-                ItemCreated(this, args);
+                BaseItemCreated(this, args);
             }
         }
 
         private void FireCreateEventsRecursive(Folder folder, Folder parent)
         {
-            OnItemCreated(folder, parent);
+            OnBaseItemCreated(folder, parent);
 
             int count = folder.Children.Length;
 
@@ -107,7 +108,7 @@ namespace MediaBrowser.Controller.Library
                 }
                 else
                 {
-                    OnItemCreated(item, folder);
+                    OnBaseItemCreated(item, folder);
                 }
             });
         }
@@ -153,7 +154,7 @@ namespace MediaBrowser.Controller.Library
                 }
                 else
                 {
-                    OnItemCreated(item, parent);
+                    OnBaseItemCreated(item, parent);
                 }
             }
 
@@ -299,28 +300,98 @@ namespace MediaBrowser.Controller.Library
             return returnFiles;
         }
 
+        /// <summary>
+        /// Gets a Person
+        /// </summary>
         public Person GetPerson(string name)
         {
-            // not yet implemented
-            return null;
+            string path = Path.Combine(ApplicationPaths.PeoplePath, name);
+
+            return GetImagesByNameItem<Person>(path, name);
         }
 
+        /// <summary>
+        /// Gets a Studio
+        /// </summary>
         public Studio GetStudio(string name)
         {
-            // not yet implemented
-            return null;
+            string path = Path.Combine(ApplicationPaths.StudioPath, name);
+
+            return GetImagesByNameItem<Studio>(path, name);
         }
 
+        /// <summary>
+        /// Gets a Genre
+        /// </summary>
         public Genre GetGenre(string name)
         {
-            // not yet implemented
-            return null;
+            string path = Path.Combine(ApplicationPaths.GenrePath, name);
+
+            return GetImagesByNameItem<Genre>(path, name);
         }
 
+        /// <summary>
+        /// Gets a Year
+        /// </summary>
         public Year GetYear(int value)
         {
-            // not yet implemented
-            return null;
+            string path = Path.Combine(ApplicationPaths.YearPath, value.ToString());
+
+            return GetImagesByNameItem<Year>(path, value.ToString());
+        }
+
+        private Dictionary<string, object> ImagesByNameItemCache = new Dictionary<string, object>();
+
+        /// <summary>
+        /// Generically retrieves an IBN item
+        /// </summary>
+        private T GetImagesByNameItem<T>(string path, string name)
+            where T : BaseEntity, new()
+        {
+            string key = path.ToLower();
+
+            // Look for it in the cache, if it's not there, create it
+            if (!ImagesByNameItemCache.ContainsKey(key))
+            {
+                ImagesByNameItemCache[key] = CreateImagesByNameItem<T>(path, name);
+            }
+
+            return ImagesByNameItemCache[key] as T;
+        }
+
+        /// <summary>
+        /// Creates an IBN item based on a given path
+        /// </summary>
+        private T CreateImagesByNameItem<T>(string path, string name)
+            where T : BaseEntity, new ()
+        {
+            T item = new T();
+
+            item.Name = name;
+            item.Id = Kernel.GetMD5(path);
+
+            if (Directory.Exists(path))
+            {
+                item.DateCreated = Directory.GetCreationTime(path);
+                item.DateModified = Directory.GetLastAccessTime(path);
+                if (File.Exists(Path.Combine(path, "folder.jpg")))
+                {
+                    item.PrimaryImagePath = Path.Combine(path, "folder.jpg");
+                }
+                else if (File.Exists(Path.Combine(path, "folder.png")))
+                {
+                    item.PrimaryImagePath = Path.Combine(path, "folder.png");
+                }
+            }
+            else
+            {
+                DateTime now = DateTime.Now;
+
+                item.DateCreated = now;
+                item.DateModified = now;
+            }
+
+            return item;
         }
     }
 }