Browse files

remove '#' in trend instead of replacing with '+'. Also take care of …

…no google image result scenario
  • Loading branch information...
1 parent e642ab8 commit 77f525a6b715270422b54b09a5498f858c3bc61c @stevenwilkin committed Sep 1, 2009
Showing with 7 additions and 2 deletions.
  1. +7 −2 lib/get_trends.rb
View
9 lib/get_trends.rb
@@ -12,11 +12,16 @@
# peforms a google image search for `q` and returns the url of the first image returned
def image_search(q)
- term = q.gsub(/[\W]/, '+')
+ term = q.gsub(/#/, '').gsub(/[\W]/, '+')
open(SEARCH + term) do |f|
return nil unless f.status[0] == '200'
h = Hpricot(f.read)
- h.at('#ImgCont//img')['src']
+ # in the rare case there are no results...
+ if (i = h.at('#ImgCont//img'))
+ i['src']
+ else
+ 'http://tbn0.google.com/images?q=tbn:WgkIHy0O9DyKJM:http://www.kreiseder.at/wp-content/2008/07/lolcat404.png'
+ end
end
end

0 comments on commit 77f525a

Please sign in to comment.