Jump to content

Setting Folder Sort Sequence via API


Recommended Posts

steamhead

I'm chugging away quite nicely now with the API, having great fun getting my Audio  Dramas in shape.

One key shortfall with Audio Books is that the default sort sequence is ByFolder, but to listen to them in the narrative order you need to listen to them in Ascending Release Date.  As yet there is no way to set the default sort sequence by Library, I've seen that there is a requiement in for that, so that gets my vote, but until that arrives I wondered if there was a way to set the sort sequence for each folder, for each user by the API?

I've seen already that you can get the folders themselves, but can't see any way of setting the sort sequence, does anyone know if there a way to manipulate that setting in the API?

 

 

Link to post
Share on other sites
Luke

Hi, no you can't set the default sort order. The app has to request a specific sort order at the time of display.

Link to post
Share on other sites
steamhead

Hi Luke, It wasn't so much setting the default order, I can see there's a requirement in for that - which will really help.

It was more the case that when you change the sort order for a folder in the app, it stores that somewhere, and remembers it, by folder by user.  What I was after was can I use the API to get at where it has stored the individual (not default) sort order for each folder, and change it, so when the app requests the specific sort order, it's already been set by an automated process that has looped through all the folders.

To put things in a little context as to how useful it would be to be able to automate this, I've converted a small section of my library to use Audio Folders. Already I have 85 folders (there will be many more when I'm finished), which I have to visit one at a time, click twice to get to ascending Release Date, and then repeat that process for each of my 6 users.  And remember to do that every time I add a new folder.

The underlying problem is that Audio Dramas are more like TV shows, where they need to be presented in chronological order to make sense.  Right now I'm looking at how much I can automate around, eventually I'll post feedback on here as to my results, and what would really help for the future.

Cheers

Dean

Link to post
Share on other sites
  • 2 weeks later...
steamhead

I did initially try that before looking at trying to set the sort sequence. I set ForcedSortName on Folders and Albums to be prefixed by the release date in YYYYMMDD format.  It worked for pure folders, but for folders containing albums it was hit and miss.  Even though I could confirm by checking the Meta Data that the sort name was set, it would not always sort them in that order.  I couldn't fathom out any common ground as to why, but it wasn't working.

I also hit some odd behaviour actually setting ForcedSortName, if I tried to set it in the same pass as any other fields, it was not set, only if I set ForcedSortName in it's own API call did it actually make the change.  Further still, on albums when I set ForcedSortName the Year and PermiereDate fields occaisionally got wiped out, even though when I printed the JSON I was setting, those fields were correct.  Again I couldn't see any rhyme or reason as to why some times it did this and some times it didn't. so eventually I gave up and went for Sorting By Release date, which for now I've had to set manually for every folder for every user, but at least the library is functioning now.

Now it's functioning I may go back and try and figure out why ForcedSortName didn't work.

 

Link to post
Share on other sites
Luke
Quote

I also hit some odd behaviour actually setting ForcedSortName, if I tried to set it in the same pass as any other fields, it was not set, only if I set ForcedSortName in it's own API call did it actually make the change.

You can send back multiple fields at once, in fact it is required to send the whole object back, not just the fields you're editing. I would use the chrome debugger to monitor what the web app does and compare to your requests.

Link to post
Share on other sites
PenkethBoy

For info - its Powershell but you should be able to adapt to your chosen language

# Get Indivdual Movie - Full Metadata
$MediaUrl = $embyServerUrl + "/emby/users/" + $User.User.Id + "/items/"+ $TVMovieResult.Items[$j].ID + "?api_key=" + $User.AccessToken
$Result = Invoke-WebRequest -Uri $MediaUrl -Method Get
$Result = $Result.Content | ConvertFrom-Json


# once you have done the edits to the $Result object

# you send back the whole record to Emby

$MediaUrl = $embyServerUrl + "/emby/Items/"+ $TVMovieResult.Items[$j].ID + "?api_key=" + $User.AccessToken
$result = Invoke-Webrequest -uri $MediaUrl -Method POST -Body ($Result | ConvertTo-Json) -ContentType "application/json; charset=utf-8"

# depending on what happened when you do the post to emby will depend on what $Result.StatusCode is returned - 200 is what you want

 

Link to post
Share on other sites
steamhead

Yes I was already fetching the full record, making updates to said record and posting it back.  The API would quite happily let me update multiple fields at once, except when I updated ForcedSortName.  Whenever I updated that field at the same time as other fields, it ignored the change for ForcedSortName.  Though I did find if I updated the record with all the other fields, posted that back, then updated ForcedSortName and posted that back, in that case everything worked.

I had assumed that was one of those undocumented "yeah you have to do that" features of Emby, but from what Luke says, apparently not.  So now I'm thinking maybe it's one of those "Emby has got itself befuddled and needs a restart" things that I'm also getting used to, so I restarted Emby, removed the interemediate post and now it happily updates ForcedSortName along with other fields.

I might go back now and see if it sorts properly, and doesn't randomly lose PremiereDate and Year, which was something else I was seeing occasionally when setting ForcedSortName.

  • Like 1
Link to post
Share on other sites
PenkethBoy

ah ok - understand better now what you meant - i will try forcedSortname a bit later

not had any problems with premieredate and year - do it all the time to update album dates etc after i add them - via the api

 

glad the reboot worked for you

just a thought - is you db old? possibly an upgrade from 3.x or early 4.x

might have issues so a rebuild might be worth the hassle - keep the old one just in case things go pear shaped :)

 

Link to post
Share on other sites
PenkethBoy

Ok - had quick play

ForcedSortname only appears to comeback if in the UI you add a "sortname" to an item -  just tested an album

so kind of optional?

If you edit an item via api and change the ForcedSortName it updates in the UI sortname

when you access the item a second time Sortname and ForcedSortanem are the same

if you then update the forcedsortname it is reflected in the UI

 

So my initial thought is that an item has to have a forcedsortname added before you can play with it via the api - i dont think just adding it works

also just amending the sortname field via the api appearded to have no effect

 

So unless i missed something you are kind of stumped

Link to post
Share on other sites
steamhead

This is kind of what I was seeing, though my observation was that Sortname is there all the time, and is essentially the Name field, converted to lower case, with leading zeroes added to any numeric elements.  As you saw ForcedSortName is only in the input record if "Sort Title" is set in the UI, but if it's not in the record, adding ForcedSortName to the end of the record seems to work, and when you read it back it's in the right place, and then the sortname field matches it. 

The ForecedSortName being already present or not might be what was causing my issue with having to set ForcedSortName in a POST on its own, I can't really tell now as most folders have one.

As for actual sorting, it seemed to work perfectly for actual folders, and in fact I am actively using that to get my folder structure in exactly the order I want by numerically prefixing them, and using the API to strip the prefix off the title, and put it in ForcedSortName.  The area that is flakey appears to be setting ForcedSortName for albums, where it seems intemittantly reliable as a sort sequence 😉

The disappearing PremiereDate and Year is not around setting those fields, I can set them no problem, quite reliably, the problem with them happens "sometimes" when I set ForcedSortName for an album, when previously set PremiereDate and Year can occasionally go missing.  I'm assuming those fields potentially get hit as they are part of potential sort sequences.  Nothing else seems to get hit, and I can't figure out any correlation for ones the disappear, and ones that don't.  I might have another go at this after reboot appears to have cleared one issue.

As for age of the database my first version of Emby on this server was 3.5.2, though my Audio Books library is currently in "Slash and Burn" status, having dropped it and recreated it many times in the last few weeks.  Can't say I'd know how to rebuild the database though, though not something I'd relish putting back all the little data tweaks from over the versions.

 

Link to post
Share on other sites
PenkethBoy

ok similar experience then - did not try every combination as only spent 10 mins on it

 

As for db - shut emby down, move or rename library.db - restart emby - it will create a new library.db file on its own

sit back and wait as it rebuilds everything - users etc will be maintained that way as in diff db file - anything not in an nfo file will be lost

done it loads of times on my test system - but that only takes 5 mins to rebuild

Link to post
Share on other sites
steamhead

Well I think I've discovered why I can't manipulate Sort By Folders to do what I want.  Quite simply, Sort By Folders doesn't actually appear to be sorting by folders, so I was starting from an entirely false premise.

Having failed with the API, I decided to come at this both sideways and old school...

As I knew when Emby is presenting a folder containing an album, it uses the Album Title for display purposes instead of the name of the folder containing the album, I reorganised a set of folders like this with the release date as the folder prefix.

drwxrwxrwx  2 nobody nogroup  4096 Sep 15 11:30 '20130101 Destiny of the Doctor'
drwxrwxrwx  2 nobody nogroup  4096 Sep  4 19:49 '20160101 The Churchill Years'
drwxrwxrwx  2 nobody nogroup  4096 Sep  4 19:47 '20190308 Eighth of March'
drwxrwxrwx  2 nobody nogroup  4096 Sep  5 06:03 '20190901 Dimension Canon #01'
drwxrwxrwx  2 nobody nogroup  4096 Sep  5 01:48 '20200301 Donna #01 Kidnapped'
drwxrwxrwx  2 nobody nogroup  4096 Sep  5 06:06 "20200401 Susan's War"

Within each folder the underlying audio book has an album title that is stored in the mp3 file is the folder name minus the release date prefix, or something close.

So you would expect (or at least I would) that Sort By Folders would present them in that order, but showing the album names instead of the folder names.  So in theory I get my stories presented in the correct listening order, with sensible titles,  I don't have do any manipulating of meta data via API and I don't have to go and manually set the sort sequence for every folder, for every user - that would be just perfect.

Except they appear in this order - The Churcill Years, Destiny of the Doctor, Dimension Canon 1, The Eighth of March, Kidnapped! then Susan's War.  So Sort By Folders is actually Sort By Title, not in any conceivable way sorting by folders. 😞

Link to post
Share on other sites
PenkethBoy

sort by folders works for me without any additional stuff

1737145013_Annotation2020-09-16170505.thumb.jpg.e2be7d3e7d44758b6cf556abd12e7793.jpg

 

also sort by release date for albums works fine

1785539575_Annotation2020-09-16170945.thumb.jpg.4a850926a0198a4df532600d89c91a94.jpg

only the ones without one are funky but at the end of the screen to easy to spot / sort

 

no forced/sortnames used - just basic metadata from MBz/audiodb - if they have the audiobooks

Edited by PenkethBoy
Link to post
Share on other sites
steamhead

Sort by folders appears to work if your folder names are close to the name of the underlying content.  But if you look at the example I tried, by prefixing the folder names with the actual sort sequence you want, you quickly see that Sort By Folders isn't actually doing the sort by data from the name of the folder, but instead the name of the Title within the folder.

Link to post
Share on other sites
steamhead

One subtle distinction might be in play here that may be the difference between an Audio Book and and Audio Drama, is that an Audio Drama can consist of may episodes, as separate files, so each audio drama is in it's own folder like an album, and then that range of audio dramas are together in a folder to logically group them.  Whereas I suspect an Audiobook may be a single file.

What I'm trying to do is to get each drama within a folder presented chronologically, not alphabetically.  To use Release Date you have to set that attribute for every folder per user, so I was trying to make the default "By Folders" work for me by prefixing the folder name for each drama with it's release date.  However when I sort the contents of a folder containing lots of folders prefixed by release dates, it uses the name of the albums within those folders to sort NOT the chronologically prefixed folder.  The problem only happens where a folder becomes an album, but nevertheless it is performing Sort By Title rather than Sort By Folders when that happens, completely ignoring the folder name.

Link to post
Share on other sites
PenkethBoy
40 minutes ago, steamhead said:

Sort by folders appears to work if your folder names are close to the name of the underlying content.  But if you look at the example I tried, by prefixing the folder names with the actual sort sequence you want, you quickly see that Sort By Folders isn't actually doing the sort by data from the name of the folder, but instead the name of the Title within the folder.

i think this might be because you have been playing with forcedsortname and what you have added is not the same as what the sqlite db/emby adds as forcedsortname

compare with whats in the db for sortname /forcedsortname - especially those that start with numbers - emby adds a lot of zero as prefix etc

Link to post
Share on other sites
Luke
2 minutes ago, PenkethBoy said:

emby adds a lot of zero as prefix etc

On a fresh install of 4.4+ this doesn't happen anymore.

Link to post
Share on other sites
Luke
4 minutes ago, PenkethBoy said:

ok - where in the release notes would i find that?

That was a while ago, and that was discussed at length in the testing area.

Link to post
Share on other sites
PenkethBoy

think OP is on a 3.5 vintage upgrade which i why a mentioned it - as he is going to have a mixture - plus whatever he has changed via api

Link to post
Share on other sites
steamhead

For this particular set of folders they are brand new, untarnished by API-dickery, so just the DB left to eliminate.  I'll have to find a suitable time over the next few days to get that done.

Thanks for all the suggestions so far

Watch this space...

Link to post
Share on other sites
PenkethBoy

dont confuse - removing/re add library as = new db structure - that is not the case

complete rebuild of library needed

i think you said the db is 3.5+ vintage with an upgrade to 4.x - thats an old db - my main Emby server is the same - works fine - and might (might not) be your problem

rebuilding a new db - as i outlined before - if painful in time but if you have premiere and use the config backup plugin - its fairly painly appart from the time waiting for it to happen etc etc

 

Link to post
Share on other sites
steamhead

Not confused, that's why I said I need to find a suitable time to get it done.

Not on Emby Premiere as I've not seen a single feature that I wanted (though backup and restore now looks attractive) and was waiting to see if I could finally get a decent structure for Audio Dramas before taking the plunge. Might sign up for a month to try out the Backup and Restore

Link to post
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...