getDirectoryListing.brs (2281B)
1 '******************************************************************** 2 '** Home Media Server Application - Directory listing functions 3 '** Copyright (c) 2010 Brian C. Lane All Rights Reserved. 4 '******************************************************************** 5 6 ' ******************************************************************** 7 ' Parse an HTML directory listing 8 ' 9 ' Pass the server url, it returns an array of all the a href paths. 10 ' ******************************************************************** 11 Function getDirectoryListing(url As String) As Object 12 result = getHTMLWithTimeout(url, 60) 13 14 if result.error or result.str = invalid then 15 ' title = "Directory Listing Error" 16 ' text = "There was an error fetching the directory listing." 17 ' print text 18 ' ShowErrorDialog(text, title) 19 return invalid 20 end if 21 22 ' NOTE: I can't find a way to escape a " character with Instr so we have to ASSume it's there. 23 dir = CreateObject("roArray", 10, true) 24 next_href = 0 25 next_quote = -1 26 while true 27 next_href = result.str.Instr(next_href, "href=") 28 if next_href = -1 then 29 return dir 30 end if 31 next_href = next_href + 6 32 next_quote = result.str.Instr(next_href, ">") 33 if next_quote = -1 then 34 return dir 35 end if 36 next_quote = next_quote - 1 37 dir.Push(result.str.Mid(next_href, next_quote-next_href)) 38 next_href = next_quote + 2 39 end while 40 End Function 41 42 '****************************************************** 43 '** Return a list of the Videos and directories 44 '** 45 '** Videos end in the following extensions 46 '** .mp4 .m4v .mov .wmv 47 '****************************************************** 48 Function displayFiles(files As Object, fileTypes As Object, dirs=false As Boolean) As Object 49 list = [] 50 for each f in files 51 ' This expects the path to have a system volume at the start 52 p = CreateObject("roPath", "pkg:/" + f) 53 if p.IsValid() and f.Left(1) <> "." then 54 fileType = fileTypes[p.Split().extension.mid(1)] 55 if (dirs and f.Right(1) = "/") or fileType = true then 56 list.push([f, p.Split()]) 57 end if 58 end if 59 end for 60 61 return list 62 End Function