Jump to content

Get/Create Collections Plugin Service


bakes82

Recommended Posts

bakes82

Im making a schedule task to automate some collections till I move on to the UI.

What service is used to find if a "Collection" exists by name, and then how do we add items to a collection.

This is what I have so far which I think is correct.  I assume Containers = Librarys which are Guids/strings from what I can tell based off the parent ID in the query string  EX: &parentId=f137a2dd21bbc1b99aa5c0f6bf02a805

@chef

 var collectionsToProcess = new List<TraktCollectionsVideos>();
            var numComplete = 0;
            var totalItems = 0;
            
            if (Plugin.Instance.Configuration.Collections.Any())
            {
                foreach (var traktCollection in Plugin.Instance.Configuration.Collections)
                {
                    var items = _libraryManager.GetItemList(new InternalItemsQuery
                    {
                        MediaTypes = new[] { MediaType.Video },
                        IsVirtualItem = false,
                        Containers = traktCollection.LibraryList.ToArray()
                    }).OfType<Video>().ToList();
                    
                    collectionsToProcess.Add(new TraktCollectionsVideos()
                    {
                        TraktCollection =  traktCollection,
                        Videos = items
                    });
                    
                    totalItems += items.Count;
                }

                if (collectionsToProcess.Any())
                {
                    foreach (var collectionsVideos in collectionsToProcess)
                    {
                        //Find Collection By Name
                        //If not found make new
                        //Else append new items where Ids matched
                    }
                }
                
                
            }

 

  • Like 2
Link to comment
Share on other sites

chef

Hi @bakes82

That looks proper to me :)

 

To query Collections I do this:

            var result = LibraryManager.GetItemIds(new InternalItemsQuery
            {
                IncludeItemTypes = new[] { "collections", "Boxset" },
                User             = user ////try using the user with policy.IsAdministator for testing               
            });

Now get each of the collections items:

            var collectionQuery = LibraryManager.QueryItems(new InternalItemsQuery()
            {
                ListIds        = new[] { result.InternalId },
                EnableAutoSort = true,
                OrderBy        = new[] { ItemSortBy.PremiereDate }.Select(i => new ValueTuple<string, SortOrder>(i, SortOrder.Ascending)).ToArray(),
            });

 

//To search By Name use "Name = "

            var result = LibraryManager.GetItemIds(new InternalItemsQuery
            {
                Name = "My_COLLECTION_NAME",
                IncludeItemTypes = new[] { "collections", "Boxset" },
                User             = user ////try using the user with policy.IsAdministator for testing               
            });

There are different ways to handle collections.

What I did for collections was create a dictionary object, to access, quickly, without having to query over and over again...

I  create the dictionary with the Key as the collection BaseItem, and then the Value being a List of the collection items.

 

        public Dictionary<BaseItem, List<BaseItem>> GetCollectionItems()
        {
            var result = LibraryManager.GetItemIds(new InternalItemsQuery
            {
                IncludeItemTypes = new[] { "collections", "Boxset" },
                User             = user //try using the user with policy.IsAdministator for testing               
            });


            var collectionQuery = LibraryManager.QueryItems(new InternalItemsQuery()
            {
                ListIds        = new[] { result.InternalId },
                EnableAutoSort = true,
                OrderBy        = new[] { ItemSortBy.PremiereDate }.Select(i => new ValueTuple<string, SortOrder>(i, SortOrder.Ascending)).ToArray(),
            });
            
            return new Dictionary<BaseItem, List<BaseItem>>() { { result, collectionQuery.Items.ToList() } };

        }

There may be a better way of doing this, but this way was easiest for me to move a collection object around quickly.

I haven't tried to add things to a collection through the API... 

I'd have to take a look and see.

 

EDIT: I'm just wondering about keeping collection data in the PluginConfiguration like that?

I'm wondering if you can create a collection type and add it to the emby media dB?

 

Edited by chef
Link to comment
Share on other sites

  • 2 weeks later...
bakes82

@chef How do you make a channel and add items to it like in the new release.  I see in the new release you get a channel and return a list of items, but am I missing where it actually creates it?

 

var channel = LibraryManager.GetItemList(new InternalItemsQuery()
            {
                Name = "New Releases"
            });

 

Link to comment
Share on other sites

  • 7 months later...
pünktchen
On 10/7/2020 at 6:47 PM, chef said:

To query Collections I do this:


            var result = LibraryManager.GetItemIds(new InternalItemsQuery
            {
                IncludeItemTypes = new[] { "collections", "Boxset" },
                User             = user ////try using the user with policy.IsAdministator for testing               
            });

Now get each of the collections items:


            var collectionQuery = LibraryManager.QueryItems(new InternalItemsQuery()
            {
                ListIds        = new[] { result.InternalId },
                EnableAutoSort = true,
                OrderBy        = new[] { ItemSortBy.PremiereDate }.Select(i => new ValueTuple<string, SortOrder>(i, SortOrder.Ascending)).ToArray(),
            });

 

@Luke @chef How to get the items that are part of a collection with the new 4.6.x release?

Link to comment
Share on other sites

pünktchen
14 minutes ago, Luke said:

On an InternalItemsQuery, use the CollectionIds filter.

Aha, this is a new filter. Working!

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 3 weeks later...
chef

@pünktchen

Do you have a code sample here?

Something has messed up my collection query code and I don't know what it is... It might be this... Maybe.

 

Link to comment
Share on other sites

roaku
12 minutes ago, chef said:

@pünktchen

Do you have a code sample here?

Something has messed up my collection query code and I don't know what it is... It might be this... Maybe.

 

For 4.6, I had to update my querying for collection items to differentiate between collections and playlists.

'ListIds' still worked for Playlists, but the new collections had to be queried by 'CollectionIds'.

                    long[] collectionItemIds = new long[]{};
                    if (isPlaylist) {
                        collectionItemIds = _libraryManager.GetInternalItemIds(new InternalItemsQuery()
                        {
                            ListIds = new[] {collectionInternalId}
                        });
                    } else {
                        collectionItemIds = _libraryManager.GetInternalItemIds(new InternalItemsQuery()
                        {
                          	//this is a new thing for 4.6 collections
                            CollectionIds = new[] {collectionInternalId}
                        });
                    }

 

 

  • Thanks 1
Link to comment
Share on other sites

chef
15 minutes ago, roaku said:

For 4.6, I had to update my querying for collection items to differentiate between collections and playlists.

'ListIds' still worked for Playlists, but the new collections had to be queried by 'CollectionIds'.


                    long[] collectionItemIds = new long[]{};
                    if (isPlaylist) {
                        collectionItemIds = _libraryManager.GetInternalItemIds(new InternalItemsQuery()
                        {
                            ListIds = new[] {collectionInternalId}
                        });
                    } else {
                        collectionItemIds = _libraryManager.GetInternalItemIds(new InternalItemsQuery()
                        {
                          	//this is a new thing for 4.6 collections
                            CollectionIds = new[] {collectionInternalId}
                        });
                    }

 

 

Did you notice a change in the "SupportsPositionTicksResume" as well.

Looks like it changed to "SupportsPlayedStatus" with a minimum Duration value in seconds.

Link to comment
Share on other sites

roaku
2 minutes ago, chef said:

Did you notice a change in the "SupportsPositionTicksResume" as well.

Looks like it changed to "SupportsPlayedStatus" with a minimum Duration value in seconds.

Sorry, I've never used that myself, so I'm not familiar with how it might have changed.

  • Thanks 1
Link to comment
Share on other sites

chef
3 minutes ago, roaku said:

Sorry, I've never used that myself, so I'm not familiar with how it might have changed.

That change did fix my issue. Thank you 👍

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...