Coder Social home page Coder Social logo

google-cse's Introduction

google-cse Build Status

A wee Google CSE client. Use it to easily query your custom search engine. Supports image search.

Installation

Add this line to your application's Gemfile:

gem 'google-cse'

And then execute:

$ bundle

Or install it yourself as:

$ gem install google-cse

Usage

Set it up:

    # you will need a Google API account and have CSE enabled.
require 'google_cse'
GoogleCSE::CX = '<your-cx>'
GoogleCSE::KEY = '<your-app-key>'

If you are on a rails app, you should stick the constants in an initializer

Run a search and visit the first result, 'feeling lucky' style (on a mac):

g = GoogleCSE.search('Ian Kilminster')
`open -a Safari #{g.fetch.results.first.link}`

Do the same for an image:

g = GoogleCSE.image_search('Ian Kilminster')
img = g.fetch.results.first.link
file = img.split('/').last
File.open(file,'w') {|f| f.write(open(img).read)} 
`open -a Preview #{file}`

Paginated access:

g = GoogleCSE.image_search('Ian Kilminster')
g.fetch.results.first.link # fetches first page, first result
#=> http://en.wikipedia.org/wiki/Lemmy

g.total # number of total results
#=> 5400000
g.per_page # number of results per page
#=> 10

g.previous? # is there a previous page?
#=> false
g.previous # what did I just say just now?
#=> nil

g.next? # is there a next page?
#=> true
g.next.results.first.link # fetches next page, first result
#=> http://www.discogs.com/artist/Ian+Fraser+Kilmister
g.page # the current page number
#=> 2
g.current_index # the current page's starting index / or rank :)
=> 11

g.previous?
#=> true
g.previous.results.first.link # ...and back to first page
#=> http://en.wikipedia.org/wiki/Lemmy
g.page
#=> 1

You can specify any extra parameters the API allows. View a complete list here

Example:

# Only return large jpegs, I can't enjoy such high art otherwise
g = GoogleCSE.image_search('Flight of Icarus Cover', :fileType => :jpg, :imgSize => :large)
#=> http://musictrajectory.com/wp-content/uploads/2011/08/iron-maiden-flight-of-icarus-single-cover.jpg

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

google-cse's People

Stargazers

 avatar  avatar  avatar Alexander Kalinovsky avatar Jerel Johnson avatar John D. Pope avatar

Watchers

Achilles Charmpilas avatar James Cloos avatar

Forkers

dcheung

google-cse's Issues

:HTTPError: 400 Bad Request

sudo gem install google-cse
Password:
Successfully installed google-cse-1.0.1
1 gem installed
Installing ri documentation for google-cse-1.0.1...
Installing RDoc documentation for google-cse-1.0.1...
Johns-iMac:google-cse johnpope$ ./irb

Johns-iMac:google-cse johnpope$ irb

require 'google_cse'
=> true
GoogleCSE::CX = '004660295420853704579:8tths02yqhc'
=> "004660295420853704579:8tths02yqhc"
GoogleCSE::KEY = 'AIzaSyD_yJk60g--MHz5yQ39k6tIrG0S7zpKLLk'
=> "AIzaSyD_yJk60g--MHz5yQ39k6tIrG0S7zpKLLk"
g = GoogleCSE.search('korean girl')
=> #<GoogleCSE::Query:0x104796a08 @params={:q=>"korean girl", :key=>"AIzaSyD_yJk60g--MHz5yQ39k6tIrG0S7zpKLLk", :cx=>"004660295420853704579:8tths02yqhc"}>
g.res
g.respond_to? g.response g.response= g.results g.results=
g.results
=> nil
g.fetch
OpenURI::HTTPError: 400 Bad Request
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:277:in open_http' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:616:inbuffer_open'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:164:in open_loop' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:162:incatch'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:162:in open_loop' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:132:inopen_uri'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:518:in open' from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/open-uri.rb:30:inopen'
from /Library/Ruby/Gems/1.8/gems/google-cse-1.0.1/lib/google_cse/query.rb:19:in `fetch'
from (irb):6

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.