faroo 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/faroo.rb +21 -7
  2. metadata +1 -1
data/lib/faroo.rb CHANGED
@@ -21,12 +21,15 @@ class Faroo
21
21
  # num_results: (Integer+)
22
22
 
23
23
  API_PATH = 'http://www.faroo.com/instant.json?'
24
+ CHUNK_SIZE = 10
25
+ MAX_TTL = 2
24
26
 
25
27
  attr_accessor :referer, :num_results
26
28
 
27
29
  def initialize(referer='', num_results=100)
28
30
  @referer = referer
29
31
  @num_results = num_results
32
+ @chunk_size = 10
30
33
  end
31
34
 
32
35
  def web(query, start=1, language='en')
@@ -45,15 +48,26 @@ class Faroo
45
48
  # l=language (en, de, zh)
46
49
  # src=source (web, news)
47
50
 
48
- params = "start=#{start}&length=#{@num_results}&q=#{CGI.escape(query)}"
49
- params += "&src=#{src}&l=#{language}"
50
- response = open(API_PATH + params, { 'Referer' => @referer })
51
- return nil if response.class.superclass == Net::HTTPServerError
52
- doc = JSON.load(response)
51
+ # faster with 10
52
+ params = "length=#{CHUNK_SIZE}&q=#{CGI.escape(query)}&src=#{src}&l=#{language}"
53
+ results = []
54
+ threads = []
53
55
 
54
- return [] if doc['length'] == 0
56
+ # Faroo API significantly faster when results are of size 10
57
+ 1.upto(@num_results / 10) do |start|
58
+ threads << Thread.new(start) do |_start|
59
+ url = "#{API_PATH}#{params}&start=#{(_start - 1) * CHUNK_SIZE + 1}"
60
+ response = open(url, { 'Referer' => @referer })
61
+ unless response.class.superclass == Net::HTTPServerError
62
+ doc = JSON.load(response)
63
+ results += doc['results']
64
+ end
65
+ end
66
+ end
67
+
68
+ threads.each { |thread| thread.join(MAX_TTL) }
55
69
 
56
- doc['results'].map do |result|
70
+ results.map do |result|
57
71
  FarooResult.new(
58
72
  result['title'],
59
73
  result['kwic'],
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faroo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: