SUSI.AI now has the ability to search and play any song or video directly in the webclient and the speaker. When a user asks for a query regarding playing a song, the clients sends a search request to the server. In this post I will discuss about the server side implementation of the Youtube search. Every time a request is made by any client, the client sends a query request to the server in the form of a json object. For more on the working on the webclient side can be seen here.
The endpoint for youtube search is http://api.susi.ai/susi/console.json and an example query for this can be as shown below:
This endpoint case is handled by the ConsoleService.java file on susi server here. The codeblock given below from ConsoleService.java is responsible for our youtube service.
From this code we can see that whenever a call is made at the above endpoint the above code is executed. First of all the query parameter is extracted from the url, which contains the value of the search term which will be search on youtube. Then three pattern variable are defined namely videoPattern, keyPattern and titlePattern. These are java pattern objects which are used to compile and search for a particular pattern in the file. More on java pattern and matcher can be read here. Lets try to understand each one of them.
This pattern is used to search for all sections with the aria-describedby . The aria-describedby attribute is used to indicate the IDs of the elements that describe the object. More on aria-describedby can be found here.
This pattern is used to search for all links with the search term ‘watch’ and extract the video ID of each video from the search result. This video ID is unique for every video and its corresponding video can be played directly with this syntax url below:
This pattern is used to search for all links with the term ‘title’ which contains the title of the video.
- aria-describedby – https://www.w3.org/TR/wai-aria/#aria-describedby
- Regex in Java – https://www3.ntu.edu.sg/home/ehchua/programming/java/Java_Regexe.html
- Matcher – https://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html
- Java sevlets – https://en.wikipedia.org/wiki/Java_servlet