Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
streamotor
Visitor

Writing a separate search function

Since we can no longer use the roSearch, I am working on writing a search algorithm for my code based on the heroScreen example. I have the keyboard and can get what the user types in, but after that I could use some help on where to begin with the algorithm
0 Kudos
6 REPLIES 6
destruk
Binge Watcher

Re: Writing a separate search function

If you are using a server, send the search term to a script on the server and have the server return matches.
If you're doing this on the end user device, you would need to loop through the content list and run your matches on the keys for the content like title, description, actors, etc.  Any match found would be copied into a results string, or copied into a result content set for display.
0 Kudos
streamotor
Visitor

Re: Writing a separate search function

"destruk" wrote:
If you are using a server, send the search term to a script on the server and have the server return matches.
If you're doing this on the end user device, you would need to loop through the content list and run your matches on the keys for the content like title, description, actors, etc.  Any match found would be copied into a results string, or copied into a result content set for display.

Do you have any sample code as an example? I can picture it in my head but still am wondering where to get started.
0 Kudos
hugetv
Visitor

Re: Writing a separate search function

I am also trying to be able to make this possible I would like to know of some example to be able to begin
Our system http://www.rokumanager.com
0 Kudos
destruk
Binge Watcher

Re: Writing a separate search function

It is a complicated process - if you can understand this then you can do just about any string operation you need.
Here is code, but I won't have the time to explain everything or help further on this -

searchresults=[]
For x=0 To m.masterlist.count()-1
For y=0 To m.masterlist[x].count()-1 'search current category
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)
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
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
If searchresults.count()>1 'only sort titles if there is more than 1 match
'sort results by title alphabetical
titles=[]
contentids=[]
For x=0 To searchresults.count()-1
titles.push(LCASE(searchresults[x].title+"**********"+searchresults[x].contentid))
Next
Sort(titles) 'alphabetize
searchresults2=[]
For T=0 To titles.count()-1 'initial count of sorted results array
For X=0 To searchresults.count()-1
svt=Instr(1,titles[T],"**********")
If LCASE(Mid(titles[T],(svt+10)))=LCASE(searchresults[X].contentid)
searchresults2.Push(searchresults[X])
Exit For
End If
Next
Next
SearchResults=searchresults2
End If
If SearchResults.Count()>0
result=ShowResults(FinalSearchTerm,SearchResults) 'This sends the search term used as well as the content list that was found to another routine to display the results
If result=9999 Return 9999
Else
ShowErrorDialog("There were no videos found.","")
End If

0 Kudos
destruk
Binge Watcher

Re: Writing a separate search function

A minimal server-side sample with a database -

<?php
include 'includevars.php';

//this file is only utilized by Roku Interface on back end of server using a GET request.

if ($_SERVER["REQUEST_METHOD"] == "GET")
{
$con=@new mysqli($sitename,$masteraccessname,$masteraccesspw,$databasename);
if (mysqli_connect_errno())
{
exit("Unable to connect to database.");
}

// read the post from ROKU
$value2 = urldecode(stripslashes($_GET["Term"])); //search term

if($value2=="")
{
$_xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>".chr(13);
$_xml.="<feed>".chr(13);
$_xml.="<item>".chr(13);
$_xml.="<thumbnail>".$rootdir."Empty.jpg</thumbnail>".chr(13);
$_xml.="<title>Placeholder</title>".chr(13);
$_xml.="<streamUrl>".$baseserverip."Empty.mp4</streamUrl>".chr(13);
$_xml.="</item>".chr(13);
$_xml.="</feed>".chr(13);
echo $_xml;
exit();
}

if (strlen($value2)>2)
{
$sql="SELECT * FROM ".$maindbname." WHERE title LIKE '%".$value2."%' OR description LIKE '%".$value2."%' ORDER BY title"; //Do search query
}
else
{
$sql="SELECT * FROM ".$maindbname." WHERE title LIKE '".$value2."%' ORDER BY title"; //Do search query based on first letters of filename
}

$res=mysqli_query($con, $sql)
or die("Error: ".mysqli_error($con));
if (mysqli_num_rows($res)==0) //no shows
{
$_xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>".chr(13);
$_xml.="<feed>".chr(13);
$_xml.="<item>".chr(13);
$_xml.="<thumbnail>".$rootdir."Empty.jpg</thumbnail>".chr(13);
$_xml.="<title>Placeholder</title>".chr(13);
$_xml.="<streamUrl>".$baseserverip."Empty.mp4</streamUrl>".chr(13);
$_xml.="</item>".chr(13);
$_xml.="</feed>".chr(13);
echo $_xml;
}
else
{
$mx=0;
while ($info=mysqli_fetch_array($res,MYSQLI_NUM))
{
$title[$mx]=$info[0]; //title
$thumbnail[$mx]=$info[1]; //thumbnail
$streamurl[$mx]=$info[2]; //streamurl
$mx++;
}

$_xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>".chr(13);
$_xml.="<feed>".chr(13);
For ($i=0;$i<$mx;$i++)
{
$_xml.="<item>".chr(13);
$_xml.="<title>".$title[$i]."</title>".chr(13);
$_xml.="<thumbnail>".$thumbnail[$i]."</thumbnail>".chr(13);
$_xml.="<streamUrl>".$streamurl[$i]."</streamUrl>".chr(13);
$_xml.="</item>".chr(13);
}
$_xml.="</feed>".chr(13);
echo $_xml;
}
mysqli_close($con);
}

?>

0 Kudos
destruk
Binge Watcher

Re: Writing a separate search function

Another tip to keep in mind -
If you have a whole lot of TV shows for content and they are named SxxExx (Season/Episode) then you don't want those to be spammed titles when a user searches for a title starting with "S", so you'll want to check if it's an S and eliminate those from the results, or check for S and require two consecutive letters to search for title matches.  Alternately, if your episodes are all numeric then you'll want to ignore title matches if the search term starts with a number.  I'm sure there are more efficient/better methods of searching but the code posted is what I use and it gets the job done for what I required.
0 Kudos