# Object Sorting, Storing Settings and Metadata

**URL:** https://forums.plex.tv/t/object-sorting-storing-settings-and-metadata/34956
**Category:** Dev/API Corner
**Tags:** plugin-dev
**Created:** [May 28, 2013, 6:34am UTC](https://forums.plex.tv/t/object-sorting-storing-settings-and-metadata/34956 "2013-05-28T06:34:00Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![kheading\_me.com](https://avatars.discourse-cdn.com/v4/letter/k/df788c/32.png) [@kheading\_me.com](https://forums.plex.tv/u/kheading_me.com)
#### Post date: [May 28, 2013, 6:34am UTC](https://forums.plex.tv/t/object-sorting-storing-settings-and-metadata/34956/1 "2013-05-28T06:34:00Z")

</div>

Hi All,

&nbsp;

Im working on some plugins for OZ Catch up TV among others and I have the need to sort a list of directory object by title see below code....

&nbsp;

> @route('/video/aubci/series/{category}')
> 
> def GetSeriesByCaegory(category):
> 
> &nbsp;
> 
> cat = iView\_Category(category)
> 
> &nbsp;
> 
> oc = ObjectContainer(view\_group='List', title2 = cat.title)
> 
> &nbsp;
> 
> series = cat.series\_list
> 
> &nbsp;
> 
> for item in series:
> 
> &nbsp;
> 
> oc.add(DirectoryObject(
> 
> key = Callback(GetEpisodesBySeries, series=item[0]),
> 
> title = item[1]
> 
> ))
> 
> &nbsp;
> 
> return oc

&nbsp;

This works fine, however the DirectoryObjects are all out of order and not sort alphabetically, is there an easy way to sort these items? I have tried a few different thing I found on the forums here but none seem to work, I think they are for older versions of PMS

&nbsp;

Also im building another plugin that I need to be able to save settings for (e.g. favourite sites ect ect) is there anyway to save a setting so that it can be stored and used again after the server has been restarted?

&nbsp;

&nbsp;

And one last thing, im looking to have this plugin display MetaData about a specific Movie or TVShow all it needs is &nbsp;title, summary and a banner. This page just needs a simple button or item that will allow the user to add this show/movie as a favourite (see above) what is the best object type to do this?

&nbsp;

Im only starting out with channel developing for Plex so im pretty new to this, and I have been searching for a while now with no luck...

&nbsp;

Thanks :D

&nbsp;

&nbsp;

---

<div class="post-metadata">

### Author: ![Dan\_J](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Dan\_J](https://forums.plex.tv/u/Dan_J)
#### Post date: [May 28, 2013, 11:03am UTC](https://forums.plex.tv/t/object-sorting-storing-settings-and-metadata/34956/2 "2013-05-28T11:03:50Z")

</div>

**Sorting Lists**

The trick is to sort the list before adding them to the object container. The Python docs are a brilliant place to start, take a look at&nbsp;[http://wiki.python.org/moin/HowTo/Sorting/](http://wiki.python.org/moin/HowTo/Sorting/). I imagine something like the following is what you are after:

```
series = sorted(series, key=lambda item: item[1])
```

Take a look at the&nbsp;[Plex Framework documentation](http://dev.plexapp.com/docs/), it's certainly worth a read and has all of the information you are after.&nbsp;

**Setting and accessing preferences**

Create a DefaultPrefs.json file in the Contents directory.

Add the default&nbsp;preferences as a a JSON array, see [Page 85](http://dev.plexapp.com/docs/api/prefskit.html#preffiles)&nbsp;of the docs.

In the main menu of your application create the following object to allow the user to access it, Plex will take care of updating the user preferences.

```
oc.add(PrefsObject(title = "Preferences"))
```

And you can access the preferences in the python code via

```
x = Prefs[id]
```

**Setting metadata**

I'll simply point you to the docs again, [Page 54](http://dev.plexapp.com/docs/api/objectkit.html#metadata-objects)

In particular you want to set the following arguments:

```
title
  A string specifying the movie’s title.
summary
  A string specifying the movie’s summary.
thumb
  A string specifying an image resource to use as the movie’s thumbnail.
art
  A string specifying an image resource to use as the movie’s background art.
```

---

<div class="post-metadata">

### Author: ![mikedm139](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/mikedm139/32/96909_2.png) [@mikedm139](https://forums.plex.tv/u/mikedm139)
#### Post date: [May 28, 2013, 9:14pm UTC](https://forums.plex.tv/t/object-sorting-storing-settings-and-metadata/34956/3 "2013-05-28T21:14:51Z")

</div>

Sorting before adding to the Object Container works well but, may not always be the best approach. It is possible to sort the contents of a Object Container by title (or pretty much any of the attributes). Like so,

```
oc.objects.sort(key = lambda obj: obj.title)
```
  
  
  
The last bit “obj.title” can be changed to sort based on other attributes such as obj.duration, obj.originally\_available\_at, obj, etc. You can also add the “reverse=True” argument if you want the list in the reverse order.

---

<div class="post-metadata">

### Author: ![kheading\_me.com](https://avatars.discourse-cdn.com/v4/letter/k/df788c/32.png) [@kheading\_me.com](https://forums.plex.tv/u/kheading_me.com)
#### Post date: [May 29, 2013, 12:16am UTC](https://forums.plex.tv/t/object-sorting-storing-settings-and-metadata/34956/4 "2013-05-29T00:16:27Z")

</div>

Hi All

Thanks again for the quick response!

The sorting work great! I used Mikedm139's method, but im sure the other way would have worked just fine.

I havent tried writing preferences yet, but after reading the doc'o this should be what im after.

I also found&nbsp;[http://dev.plexapp.com/docs/api/datakit.html?highlight=dict#Dict](http://dev.plexapp.com/docs/api/datakit.html?highlight=dict#Dict)&nbsp;in the doco which looks like it will do what I need as well.

As far as the MetaData goes, you might have misunderstood the question...

I need to retrieve metadata, say from TheTVDB.com or IMDB.com and display it within the plugin, I can see there are already agents for this within Plex im just not sure how to access them from within my plugin

Thanks!!!!

---

<div class="post-metadata">

### Author: ![shopgirl284](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@shopgirl284](https://forums.plex.tv/u/shopgirl284)
#### Post date: [May 29, 2013, 2:26am UTC](https://forums.plex.tv/t/object-sorting-storing-settings-and-metadata/34956/5 "2013-05-29T02:26:27Z")

</div>

> Sorting before adding to the Object Container works well but, may not always be the best approach. It is possible to sort the contents of a Object Container by title (or pretty much any of the attributes). Like so,  
> &nbsp;
> 
> ```
> oc.objects.sort(key = lambda obj: obj.title)
> ```
>   
> The last bit "obj.title" can be changed to sort based on other attributes such as obj.duration, obj.originally\_available\_at, obj, etc. You can also add the "reverse=True" argument if you want the list in the reverse order.

The sort command seems like a good one to know and use. Just one question, where would you put the line above within your code? You say that with this approach, you would not be sorting before adding to the object container. So would you place this command outside of any loop you are using to pull data and place it between the oc.add() and the return oc commands?

---

<div class="post-metadata">

### Author: ![mikedm139](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/mikedm139/32/96909_2.png) [@mikedm139](https://forums.plex.tv/u/mikedm139)
#### Post date: [May 29, 2013, 7:50am UTC](https://forums.plex.tv/t/object-sorting-storing-settings-and-metadata/34956/6 "2013-05-29T07:50:30Z")

</div>

> need to retrieve metadata, say from TheTVDB.com or IMDB.com and display it within the plugin, I can see there are already agents for this within Plex im just not sure how to access them from within my plugin.

Metadata from agents is not exposed for use by channel plugins. It \*might\* be possible to hack some sort of workaround but it would definitely be unsupported and liable to break at any time.  
  

> So would you place this command outside of any loop you are using to pull data and place it between the oc.add() and the return oc commands?

That's right. You can see it used in the CBC channel code here: [https://github.com/plexinc-plugins/CBC.bundle/blob/master/Contents/Code/\_\_init\_\_.py#L106](https://github.com/plexinc-plugins/CBC.bundle/blob/master/Contents/Code/ __init__.py#L106)

---

<div class="post-metadata">

### Author: ![kheading\_me.com](https://avatars.discourse-cdn.com/v4/letter/k/df788c/32.png) [@kheading\_me.com](https://forums.plex.tv/u/kheading_me.com)
#### Post date: [June 3, 2013, 2:30am UTC](https://forums.plex.tv/t/object-sorting-storing-settings-and-metadata/34956/7 "2013-06-03T02:30:35Z")

</div>

Thanks everyone for you help! I managed to get the sorting and adding items to favourites working and im just using the TVDB and IMDB api's to pull the data i need for banners ect ect

---

<div class="post-metadata">

### Author: ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)
#### Post date: [December 20, 2019, 11:24pm UTC](https://forums.plex.tv/t/object-sorting-storing-settings-and-metadata/34956/8 "2019-12-20T23:24:58Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
