Sorting within my first plugin

Hi



This is my first plugin. I’ve searched the forum and the API, but unfortunately found nothing.

I want to get video clips from a specific webpage. So far everything works. I have a list of videos and can play the videos.



But I have a question:

How is it possible to provide a ‘sort by’ button on the iPad plex client. I mean such a sort button which exists for local media (in the upper right corner).



My first attempt was:



<br />
dir = ObjectContainer(view_group = 'InfoList')<br />
list = []<br />
for movie in movies<br />
    some code<br />
    list.append([(title, VideoClipObject(title = title, thumb = thumb, rating = rating, summary = summary, url = url))])<br />
    list.sort()<br />
for sortedMovies in list:<br />
    dir.add(sortedMovies[0][1])<br />
return dir



As a result the video clips are listed alphabetically. But it would be perfect if the order can be chosen freely (like sort by date, recently added, etc.).

I'm grateful for any help!
Best Regards

You can use python’s “sorted()” method on the ObjectContainer and specify which attribute you want used for the sort.



<br />
oc = ObjectContatiner()<br />
''' insert code to build VideoClipObjects and add them to the container'''<br />
oc = sorted(oc, key=lambda media: media.title) ### any attribute of the Object should work for sorting<br />
return oc<br />




You can also specify that you want the list in the reverse order adding the "reverse" argument.



<br />
oc = sorted(oc, key=lambda media: media.originally_available_at, reverse=True)<br />


Hi



Thx for your reply!



Unfortunately it doesn’t work. I get the following error:

TypeError: ‘MediaContainer’ object is not iterable



My code with your suggestion looks like:


<br />
def getMovies(url):<br />
    dir = ObjectContainer(view_group = 'InfoList')<br />
    "get page informations"<br />
    for movie, thumbnail in map(None, movies, thumbs):<br />
        thumb = thumbnail.xpath(....)[0].get('src')<br />
        title = ...<br />
        etc.<br />
        dir.add(PopupDirectoryObject(key=Callback(getStreams, page=streams), thumb = thumb, title = title, etc.)<br />
    dir = sorted(dir, key=lambda media: media.title)<br />
    return dir<br />
<br />
def getStreams(page):<br />
    oc = ObjectContainer()<br />
    some code<br />
    oc.add(VideoClipObject(title=title, url = url, rating = rating, ...))<br />
    return oc<br />




My ServiceCode.pys:

<br />
def MetadataObjectForURL(url):<br />
    ....<br />
    return VideoClipObject(title = title, ....)<br />
<br />
def MediaObjectsForURL(url):<br />
    return[<br />
        MediaObject(<br />
            video_codec = VideoCodec.H264, <br />
            audio_codec = AudioCodec.AAC, <br />
            parts = [PartObject(key=Callback(PlayVideo, url = url))]<br />
        )<br />
    ]<br />


Sorry, the code snippet I gave you isn’t quite right. Try this:


<br />
oc.objects.sort(key=lamda obj: obj.attribute) # where "attribute" is the parameter you wish to sort by<br />



Hi



Thx for your answer! Your snipet works (apart from a typo: lamda <-> lambda).

But with this solution I have to determine the sorting. I meant that the user can determine the sorting on runtime (see attached image).

Hopefully not, but I think this is only possible with existing XML metadata files. But I don’t know.



Best regards



If you are willing to add an extra menu layer you can still have the user determine their choice for sorting at runtime. Something like this might work...


<br />
def SortMenu(url):<br />
	oc = ObjectContainer()<br />
	oc.add(DirectoryObject(key=Callback(GetMovies, url=url, sort_attribute="title"), title="Alphabetical"))<br />
	oc.add(DirectoryObject(key=Callback(GetMovies, url=url, sort_attribute="originally_available_at"), title="Most Recent"))<br />
	... #add whatever options you deem necessary<br />
	return oc<br />
<br />
def GetMovies(url, sort_attribute):<br />
	oc = ObjectContainer()<br />
	''' page-parsing/list-building code'''<br />
	oc.objects.sort(key=lambda obj: obj.sort_attribute)<br />
	return oc<br />



Hi

Hmm, ok. This is a possible solution. But it’s not a really nice one :slight_smile: (regarding design / usability). There is no possibility to do it like in the screenshot? Is it because there is no XML metadata file? If so, is there a possibility to call a existing agent (like imdb, themoviesdb, etc.) and save the metadata on disk? If the user navigates to a video the metadata is loaded from the hard drive, but the video is still streamed over HTTP.



Plugins do not have access to client level controls like the Sort/Filter menu. Another option would be to add an item to the list which allows the user to change the sort order by selecting it, but it would show up as within the list of movies/videos/etc. rather than as a client level option.

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