Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
lylyahiko
Level 7

Roku Search Bar

Does anyone have any example code for a search bar? 

I'm trying to setup a search bar for my app to filter and search through videos. I don't see anything on the Roku SDK Dev site.

Is that something I'm going to have to do custom?
0 Kudos
17 Replies
norcaljohnny
Level 7

Re: Roku Search Bar

Well first off you need to have a server side script that will perform the search itself.. I personally use PHP.

Assuming you do have a script, this is something you can use with the stock keyboard dialog.

sub tvshows()
    url = "http://theUrlwithScriptToSearch" + m.top.dialog.text
    regex = CreateObject("roRegex", " ", "") 'to replace any blank spaces.
    url = regex.ReplaceAll(url, "%20")
    m.readVideoContentTask = createObject("RoSGNode", "ContentReader")
    m.readVideoContentTask.observeField("content", "showvideolist")
    m.readVideoContentTask.contenturi = url
    m.readVideoContentTask.control = "RUN"
end sub

There is a little more to it than that like setting up key events on press or some sort of search button. If you need more let me know.
0 Kudos
destruk
Level 10

Re: Roku Search Bar

If your app downloads a master content table with everything in it, then you can search during runtime on the client side using brightscript.
0 Kudos
norcaljohnny
Level 7

Re: Roku Search Bar

"destruk" wrote:
If your app downloads a master content table with everything in it, then you can search during runtime on the client side using brightscript.

You are right but the Roku is extremely slower in parsing than server side.
0 Kudos
lylyahiko
Level 7

Re: Roku Search Bar

So to make sure I understand. 

There currently is no functionality that allows me to setup a search bar without doing it custom. 

Yes we will be using PHP for the backend part and thank you for the sample code.
0 Kudos
destruk
Level 10

Re: Roku Search Bar

You would make a minikeyboard, or a full keyboard, and handle the input to create a working "search bar".
And I think it depends a lot on your internet connection speed and server power, as far as which is faster.  The actual parsing on roku will be the pretty much the same depending on how many results you get, I think I have parsed 5000 entries from json in 3 or 4 seconds on a Roku 4.  If you have a cheap shared server which is running at capacity from GoDaddy or another typical cheap provider like rackspace then it just might be faster to have the roku client do the searching itself.  It's your choice which to do of course.
0 Kudos
norcaljohnny
Level 7

Re: Roku Search Bar

With all due respect 3-4 seconds is extremely slow. With 19,000 items my results return in .2 seconds via server side and php.
0 Kudos
norcaljohnny
Level 7

Re: Roku Search Bar

Here is a sample of how i do it

1st I have a master file in xml format, no database needed.
I named it "all.xml" and place it in the root folder of my server.

Sample:

<?xml version = "1.0" encoding = "utf-8" standalone = "yes" ?>
  <Content>
    <item url="a_bit_of_fry_and_laurie" title="A Bit of Fry and Laurie" year="1987" hdposterurl="yourPosterUrl"/>
  </Content>



2nd i create a search script that will return the results.
I name it "livesearch.php" and place it in the root folder of my server.
"\n" =  end of line and "i" =  not case sensitive
I then append the url to a base url that handles the actual video file on click of the results.

Sample:

<?php header("Content-type: text/xml")?>
<?php echo "<?xml version = \"1.0\" encoding = \"utf-8\" standalone = \"yes\" ?>\n" ?>
  <?php echo "<Content>\r\n" ?>
    <?php
      $q=$_GET["q"];
      $xml=file_get_contents("all.xml");
      preg_match_all("/.*title=\".*$q.*\"*\n/i", $xml, $output,PREG_SET_ORDER);
      foreach($output as $file){
      $file= $file[0];
      echo "$file";
      } 
<?php echo "</Content>" ?>

 
So now if I go to "https://myWebAddress.com/livesearch.php?q=searchTextInput" it gets returned with all results parsed in a Roku xml format. 
I also use a function so the results are returned as they type and not by a search button being pressed.

This method also allows all results returned regardless of where the word falls in the search and you dont have to worry about exact matches.

IE ... "https://myWebAddress.com/livesearch.php?q=brady" will return "the brady bunch" assuming that is a file.
0 Kudos
destruk
Level 10

Re: Roku Search Bar

"norcaljohnny" wrote:
With all due respect 3-4 seconds is extremely slow. With 19,000 items my results return in .2 seconds via server side and php.

And with all due respect it could take 3-4 seconds to download the data from your server to the client app.  So... you know, we can argue about which is better for months if you prefer, but really it's up to the individual channel developer to decide what they want to use. While results might return from your server it still takes the roku devices (low end and roku 4) 5 seconds to parse the results for 19,000 items, so that is still slow.  But since3 you can't make that part faster, well.... Maybe chill out, we're not fighting here, we are discussing different techniques to accomplish a goal dude.
0 Kudos
destruk
Level 10

Re: Roku Search Bar

I'm sure this code can be made faster, it's from about a decade ago.

'Create Searchresults Array
searchresults=[]
'Loop through content list categories
For x=0 To m.masterlist.count()-1
'Loop through items in each category
For y=0 To m.masterlist[x].count()-1
'If search term is 1 character long, then check for matches based on title only
If Len(FinalSearchTerm)=1
'search titles that begin with that character and skip other fields
If LCASE(m.masterlist[x][y].Title.Left(1))=LCASE(FinalSearchTerm)
'by default if it matches we will add it
oktoadd=1
'verify item doesn't already exist in the search results
For z=0 To searchresults.count()-1
If searchresults[z].contentID=m.masterlist[x][y].contentID
oktoadd=0
Exit For
End If
Next
If oktoadd=1 searchresults.push(m.masterlist[x][y])
End If
Else
If(Instr(1,LCASE(m.masterlist[x][y].Title),LCASE(FinalSearchTerm))>0) or (Instr(1,LCASE(m.masterlist[x][y].Director),LCASE(FinalSearchTerm))>0) or (Instr(1,LCASE(m.masterlist[x][y].Actors[0]),LCASE(FinalSearchTerm))>0) or (Instr(1,LCASE(m.masterlist[x][y].Actors[1]),LCASE(FinalSearchTerm))>0) or (Instr(1,LCASE(m.masterlist[x][y].Actors[2]),LCASE(FinalSearchTerm))>0)  or (Instr(1,LCASE(m.masterlist[x][y].Description),LCASE(FinalSearchTerm))>0) or (Instr(1,m.masterlist[x][y].ReleaseDate,FinalSearchTerm)>0)
oktoadd=1
For z=0 To searchresults.count()-1
If searchresults[z].contentID=m.masterlist[x][y].contentID
oktoadd=0
Exit For
End If
Next
If oktoadd=1 searchresults.push(m.masterlist[x][y])
End If
End If
Next
Next

0 Kudos