Reputation: 46
I am using the following code snipped from Google Developers site. It's working fine but when I search for term shukareh alla
, I get ZERO results. The term is misspelled actually, but I want to get the results for the misspelled terms as well just like YouTube website is providing me results for the above term. I don't know whether I should specify some additional parameter for this to work or YouTube API doesn't provide this feature?
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser
DEVELOPER_KEY = "***************"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
def youtube_search(options):
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
developerKey=DEVELOPER_KEY)
# Call the search.list method to retrieve results matching the specified
# query term.
search_response = youtube.search().list(
q=options.q,
part="id,snippet",
maxResults=options.max_results
).execute()
videos = []
channels = []
playlists = []
# Add each result to the appropriate list, and then display the lists of
# matching videos, channels, and playlists.
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append("%s (%s)" % (search_result["snippet"]["title"],
search_result["id"]["videoId"]))
elif search_result["id"]["kind"] == "youtube#channel":
channels.append("%s (%s)" % (search_result["snippet"]["title"],
search_result["id"]["channelId"]))
elif search_result["id"]["kind"] == "youtube#playlist":
playlists.append("%s (%s)" % (search_result["snippet"]["title"],
search_result["id"]["playlistId"]))
print "Videos:\n", "\n".join(videos), "\n"
print "Channels:\n", "\n".join(channels), "\n"
print "Playlists:\n", "\n".join(playlists), "\n"
if __name__ == "__main__":
argparser.add_argument("--q", help="Search term", default="shukareh alla")
argparser.add_argument("--max-results", help="Max results", default=10)
args = argparser.parse_args()
try:
youtube_search(args)
except HttpError, e:
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
Upvotes: 0
Views: 183
Reputation: 46
As said by DaImTo, the Youtube API doesn't provide any spellcheck search ability. I was able to do a spell check using Google Custom Search Engine API. I have created a custom site search engine for site "youtube.com". Whenever I get zero results for Youtube search (using Youtube API), I use the Custom Search API to search for the same keyword. The best thing about the Custom Search Engine API is that, if it gets zero results for a search keyword and the search keyword is incorrect, it does a spell check and returns the corrected search query.
Upvotes: 0
Reputation: 116868
Search: list Returns a collection of search results that match the query parameters specified in the API request.
q string The q parameter specifies the query term to search for. Your request can also use the Boolean NOT (-) and OR (|) operators to exclude videos or to find videos that are associated with one of several search terms. For example, to search for videos matching either "boating" or "sailing", set the q parameter value to boating|sailing. Similarly, to search for videos matching either "boating" or "sailing" but not "fishing", set the q parameter value to boating|sailing -fishing. Note that the pipe character must be URL-escaped when it is sent in your API request. The URL-escaped value for the pipe character is %7C.
The YouTube API will return any results for the request you send. It is not going to spell check it for you. Your application will have to check the spelling then send the correct spelling to the YouTube API.
Answer: The YouTube API does not offer a spellcheck search ability.
Upvotes: 1