Skip to content

Instantly share code, notes, and snippets.

@waynegraham
Created October 27, 2010 17:10
Show Gist options
  • Save waynegraham/649480 to your computer and use it in GitHub Desktop.
Save waynegraham/649480 to your computer and use it in GitHub Desktop.
require 'open3'
require 'open-uri'
require 'rubygems'
require 'nokogiri'
# The program takes an intital keyword or phrase and performs a
# search against a Blacklight-based OPAC, reading the results
# back using the built in voice commands in OS X.
#
# Author:: Wayne Graham (based on code written by Matt Mitchell)
# Copyright:: Copyright (c) 2010
# License:: Apache 2
# This class provides a search interface to read back results from
# a Blacklight-based library OPAC. The <tt>read_results</tt> method
# will read back the results of a query to Blacklight in OS X using
# any of the voices available on the system
#
# Usage::
# searcher = Virgo.new
# searcher.read_results('rocket science')
class Virgo
# Set up the defaults
#
# @max_results will set the maximum number of result to return from the search
# @search_base is the base URL for performing searches
# @voice OS X voice to use to read results
def initialize
@max_results = 5
@search_base = 'http://search.lib.virginia.edu/catalog'
@voice = 'Vicki'
end
def max_results
@max_results
end
def max_results! val
@max_results = val
end
def voice
@voice
end
# Set the voices that are availabe in OS X
# Valid voices include:
# * Alex (male)
# * Bruce (male)
# * Fred (male)
# * Junior (male)
# * Ralph (male)
# * Agnes (female)
# * Kathy (female)
# * Princess (female)
# * Vicky (female)
# * Victoria (famale)
#
# If you're really in to fun, try these others:
# * Albert
# * Bad News
# * Bahh
# * Bells
# * Boing
# * Bubbles
# * Cellos
# * Deranged
# * Good News
# * Hysterical
# * Pipe Organ
# * Trinoids
# * Whisper
# * Zarvox
def voice! voice
@voice = voice
end
def search_base! val
@search_base = val
end
# Construct the search URL for the catalog search
def search_string (query)
return "#{@search_base}?q=#{URI.escape(query)}&per_page=#{@max_results}"
end
# Read the results back to the system voice command
#
#
def read_results(query)
results = open search_string(query)
Open3.popen3("say -v #{@voice}") do |stdin, stdout, stderr|
stdin << "Virgo results for #{query}...using reusable code"
Nokogiri::XML(results).search('.document').each_with_index do |d,i|
stdin << "Item #{i+1},"
stdin << "Catalog key, #{d['id'].gsub(/^Doc/,'')},"
stdin << "Title, #{d.at('.titleField a').text},"
end
stdin.close
end
end
end
searcher = Virgo.new
searcher.voice! 'Zarvox'
puts searcher.search_string('rocket science')
searcher.max_results! 1
puts searcher.max_results
searcher.read_results('rocket science')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment