0
votes

I first tried

class Youtube
   def search_url(term)
     url = "https://gdata.youtube.com/feeds/api/videos"
     url += "?q=#{term}&alt=json&restriction=US&max-results=50&orderby=viewCount"
 url += "&fields=entry(id,title,yt:noembed,media:group(media:description),author(name),yt:statistics(@viewCount))"
     url += "&key=#{DEV_KEY}"

But it wasn't able to handle queries like "Macklemore & Ryan Lewis"

Then someone suggested

require 'cgi'
class Youtube
  def search_url(term)
    term = CGI.escape(term)
    url = "https://gdata.youtube.com/feeds/api/videos"
    url += "?q=#{term}&alt=json&restriction=US&max-results=50&orderby=viewCount"
    url += "&fields=entry(id,title,yt:noembed,media:group(media:description),author(name),yt:statistics(@viewCount))"
    url += "&key=#{DEV_KEY}"

But it didn't solve the problem. But it just made queries that worked before not work anymore.

Opening these two links in Firefox (other browsers don't seem to be able to open them)

https://gdata.youtube.com/feeds/api/videos?q=Macklemore+%26+Ryan+Lewis&restriction=US&max-results=50&orderby=viewCount https://gdata.youtube.com/feeds/api/videos?q=Macklemore & Ryan Lewis&restriction=US&max-results=50&orderby=viewCount

tells me that "&" in the query really affects badly.

How can this be really solved?

1

1 Answers

0
votes

You can't use & in q= without escaping it. The escape of & with %26 in the URL seems to be correct. And if & is left out ?

When i search on youtube.com on only 1 term: & Then my browser shows the url: http://www.youtube.com/results?search_query=%26