How to 'filter' directory items ?

  Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
Swifty Offline
Fan
Posts: 619
Joined: Nov 2008
Reputation: 5
Post: #1
Hey all,

I'm new to python and i'm trying to write my first xbmc addon.
I've got most of the addon working, but there is one bit I'm stuck with;

I'd like to get a directory (from an NFS share) and list the largest 3 files in that directory - I can list the directory fine in XBMC, but I'm struggling to work out how to 'filter' out the stuff I don't want.
I found some sample python which gets the largest file in a directory but it's working at an OS level, so it knows nothing about the NFS:// path I give it (which XBMC seems to handle just fine, thanks to Memphiz!)

Could anyone with some python experience point me in the right direction?

Thanks
find quote
giftie Offline
Skilled Python Coder
Posts: 2,364
Joined: Mar 2010
Reputation: 54
Location: Calgary, Alberta
Post: #2
Best way would to use JSON RPC:

Code:
json_query = '{"jsonrpc": "2.0", "method": "Files.GetDirectory", "params": {"directory": "%s", "media": "%s", "properties": ["size"]}, "id": 1}' % ( dir_name , media_type )
json_response = xbmc.executeJSONRPC( json_query )

the dir_name = the folder you want to look in
media_type = can limit the type of file return("video", "music", "pictures", "files", "programs")

[Image: e4f63e45ba34fe4695b3bb08eb2499d8e4ee484e...4c076g.jpg]
For troubleshooting and bug reporting please make sure you read this first you can also use XBMC Log Uploader Script.
Cinema Experience
Cinema Experience Wiki
cdART Manager
fanart.tv


find quote
Swifty Offline
Fan
Posts: 619
Joined: Nov 2008
Reputation: 5
Post: #3
Thanks very much, there is no way I would have come up with that Smile

I've just tried it out and it's coming back with an error;

22:35:15 T:5356 DEBUG: JSONRPC: Incoming request: {"jsonrpc": "2.0", "method": "Files.GetDirectory", "params": {"directory": "nfs://192.168.1.251/Media/Videos/Misc", "media": "files", "properties": ["size"]}, "id": 1}
22:35:15 T:5356 DEBUG: JSONRPC: Calling files.getdirectory
22:35:15 T:5356 DEBUG: JSONRPC: Value does not match any of the enum values in type
22:35:15 T:5356 DEBUG: JSONRPC: Array element at index 0 does not match in type properties

I'm not sure what to make of that.. I just found the json wiki pages and it seems to have all the correct parameters for Eden.
find quote
giftie Offline
Skilled Python Coder
Posts: 2,364
Joined: Mar 2010
Reputation: 54
Location: Calgary, Alberta
Post: #4
(2012-06-17 23:33)Swifty Wrote:  Thanks very much, there is no way I would have come up with that Smile

I've just tried it out and it's coming back with an error;

22:35:15 T:5356 DEBUG: JSONRPC: Incoming request: {"jsonrpc": "2.0", "method": "Files.GetDirectory", "params": {"directory": "nfs://192.168.1.251/Media/Videos/Misc", "media": "files", "properties": ["size"]}, "id": 1}
22:35:15 T:5356 DEBUG: JSONRPC: Calling files.getdirectory
22:35:15 T:5356 DEBUG: JSONRPC: Value does not match any of the enum values in type
22:35:15 T:5356 DEBUG: JSONRPC: Array element at index 0 does not match in type properties

I'm not sure what to make of that.. I just found the json wiki pages and it seems to have all the correct parameters for Eden.

looks like the size property is not available in Eden, It is in the nightlies though...

[Image: e4f63e45ba34fe4695b3bb08eb2499d8e4ee484e...4c076g.jpg]
For troubleshooting and bug reporting please make sure you read this first you can also use XBMC Log Uploader Script.
Cinema Experience
Cinema Experience Wiki
cdART Manager
fanart.tv


find quote
Swifty Offline
Fan
Posts: 619
Joined: Nov 2008
Reputation: 5
Post: #5
Thanks giftie, it's working ok on the latest nightly Smile

What's the best way of parsing those results from the json query so that I only get the largest 3 filenames and urls?

I'm struggling to figure out how to select the various properties from the results and shove them into an array or something I can use later
find quote