Skip to content

Commit dae1fb5

Browse files
committed
Added sample 11 for chapter 18
1 parent e199bad commit dae1fb5

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#
2+
# Example 18-11: Yahoo search visualization
3+
#
4+
load_library "pyahoo"
5+
import "pyahoo"
6+
7+
# The names to search
8+
NAMES = %w{ Aliki Cleopatra Penelope Daniel Peter }
9+
10+
def setup
11+
size 500, 300
12+
text_font create_font("Georgia", 20, true)
13+
smooth
14+
15+
# Search for all names
16+
# The search() function is called for each name in the array.
17+
@yahoo = []
18+
NAMES.each do |name|
19+
# Create a YahooSearch object.
20+
# You have to pass in the API key given to you by Yahoo.
21+
@yahoo << YahooSearch.new(self, "YOUR API KEY HERE")
22+
@yahoo.last.search(name)
23+
end
24+
25+
@bubbles = { }
26+
@search_count = 0
27+
end
28+
29+
def draw
30+
background 255
31+
32+
# poll all yahoo searches
33+
@yahoo.each do |yahoo|
34+
search_event(yahoo) if yahoo.available?
35+
end
36+
37+
# Show all bubbles
38+
@bubbles.values.each do |bubble|
39+
bubble.display
40+
end
41+
end
42+
43+
def search_event(yahoo)
44+
# Total # of results for each search
45+
# getTotalResultsAvailable() returns the total number of web pages that Yahoo found containing the search phrase.
46+
# These numbers can be quite large so they are scaled down before being used as an ellipse size.
47+
total = yahoo.get_total_results_available
48+
49+
# Scale down the number so that it can be viewable
50+
r = sqrt(total) / 75.0
51+
52+
# Make a new bubble object
53+
# The search data is used to make a Bubble object for the visualization.
54+
search = yahoo.get_search_string
55+
return if @bubbles.has_key? search
56+
57+
b = Bubble.new(search, r, 50 + @search_count * 100, $app.height / 2)
58+
@bubbles[search] = b
59+
@search_count += 1
60+
end
61+
62+
#
63+
# Simple "Bubble" class to represent each search
64+
#
65+
class Bubble
66+
def initialize(search, r, x, y)
67+
@search = search
68+
@r = r
69+
@x = x
70+
@y = y
71+
end
72+
73+
def display
74+
$app.stroke 0
75+
$app.fill 0, 50
76+
$app.ellipse @x, @y, @r, @r
77+
$app.text_align PApplet::CENTER
78+
$app.fill 0
79+
$app.text @search, @x, @y
80+
end
81+
end

0 commit comments

Comments
 (0)