-
Notifications
You must be signed in to change notification settings - Fork 0
/
rouleur.rb
272 lines (208 loc) · 11.1 KB
/
rouleur.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# encoding: utf-8
require 'rubygems'
require 'haml'
require 'sass'
require 'sinatra'
require 'curb'
require 'nokogiri'
require 'maruku'
require 'open-uri'
require 'json'
configure :development do
require "sinatra/reloader"
end
# Default is xhtml, do not want!
set :haml, {:format => :html5, :escape_html => false}
# Conversion of the SCSS to regular CSS
get '/style.css' do
scss :style
end
# Homepage
get '/' do
# Render the HAML template
haml :home
end
# About page
get '/about' do
# Render the HAML template
haml :about
end
# Process a search perform
post '/search' do
# Convert the search term and redirect
redirect to('/search/' + params[:term].gsub(/\s/,'+'))
end
# Search results page
get '/search/:term' do
# The search term
@search = params[:term].gsub(/\+/, ' ')
@searchconcat = params[:term]
# Render the HAML template
haml :search
end
# Takes in arguments for site, search term and number of results to return and returns JSON
# This will be called by JavaScript so that we can progressively show each site's results
get '/json/:site/:term/:number' do
# We'll be returning JSON
content_type 'application/javascript'
# New JSON object for holding the results
results = "{\"Results\": ["
# Multi-word search term parsing
search = params[:term].gsub(/\s/, "+")
# ========================================================================
# CHAIN REACTION
# ========================================================================
# Different rules for parsing the returned doc depending on the site
if params[:site] == "Chain Reaction" then
# For chain reaction we have to use curb because it doesn't return
# images if we don't specify a graphical user agent
connection = Curl::Easy.new
connection.useragent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7"
connection.url = "http://www.chainreactioncycles.com/Mobile/MobileSearchResults.aspx?Search=" + search
connection.cookies = "UserSettings=GUID=4961162a-b9f0-45d4-9741-cc7b5f9f17dc&CurrencyISO=AUD&LanguageISO=en&NavigationID=0&PartnerID=0&PollID=0&PreferredUserLanguageISO=All&VatFree=True&ShippingCountryID=1712&ShowCategoryPictures=True&ListDisplayStyle=0&SuperCategoryID=2189&UseDefaultLanguage=True&Gender=0&PartnerIDExpiry=2011-05-15 08:12:57; expires=Thu, 12-May-2012 12:58:52 GMT; path=/; domain=www.chainreactioncycles.com; httponly"
connection.http_get
# Parse the doc into Nokogiri
doc = Nokogiri::HTML(connection.body_str)
# Get the number of results found
num_results = doc.css("#LblProductCount")[0].content.match(/Showing [0-9]+ \- [0-9]+ of ([0-9]+) Products/)[1]
num_results_string = num_results.to_i == 1 ? num_results.to_s + " result" : num_results.to_s + " results"
# Loop through the number of items we want returned creating a little JSON object for each
for i in 1..params[:number].to_i do
# If there's just one result the structure is a little different
single_result = doc.css(".Div11").count == 1 ? true : false
# Set the loop incrementing value
increment = single_result ? i : (i+1)
# Check that there's some results
if (!doc.css("#Form1 table:nth-of-type(#{increment}) .Div11")[0].nil?) then
# Create affiliate link
# http://www.awin1.com/cread.php?awinmid=2698&awinaffid=121196&clickref=&p=http%3A%2F%2Fwww.chainreactioncycles.com%2FModels.aspx%3FModelID%3D51432
link = "http://www.awin1.com/cread.php?awinmid=2698&awinaffid=121196&clickref=&p=http%3A%2F%2Fwww.chainreactioncycles.com" + doc.css("#Form1 table:nth-of-type(#{increment}) .Div11")[0].attribute("href").value.gsub(/\/Mobile\/MobileModels.aspx/, '/Models.aspx').gsub(/\:/, "%3A").gsub(/\//, "%2F")
# Create object
results += "{"
results += "\"name\": \"" + doc.css("#Form1 table:nth-of-type(#{increment}) .Div11")[0].content + "\","
results += "\"price\": \"" + doc.css("#Form1 table:nth-of-type(#{increment}) .Div12")[0].content.gsub(/Now |From |AUD/, '').strip + "\","
results += "\"url\": \"" + link + "\","
results += "\"image\": \"" + "http://chainreactioncycles.com" + doc.css("#Form1 table:nth-of-type(#{increment}) .Div29 img")[0].attribute("src").value + "\""
results += "},"
end
end
# ========================================================================
# WIGGLE
# ========================================================================
elsif params[:site] == "Wiggle" then
# User Curb so we can pass in the cookie data
connection = Curl::Easy.new
connection.url = "http://www.wiggle.co.uk/?s=" + search
connection.cookies = "browsingCustomer2=Name=&DType=None&CID=0&Last=15/04/2011 08:33:08&Cur=AUD&Dest=27&Language=en&SiteDomainName=wiggle.co.uk; expires=Thu, 12-May-2012 12:58:52 GMT; path=/; domain=www.wiggle.co.uk/; httponly"
connection.http_get
# Parse the doc into Nokogiri
doc = Nokogiri::HTML(connection.body_str)
# Get the number of results found
num_results = doc.css("#pager5")[0].content.match(/Showing\s+[0-9]+\s+-\s+[0-9]+\s+of\s+([0-9]+)/)[1]
num_results_string = num_results.to_i == 1 ? num_results.to_s + " result" : num_results.to_s + " results"
# Loop through the number of items we want returned creating a little JSON object for each
for i in 1..params[:number].to_i do
# Check that there's some results
if (!doc.css(".categoryListItem:nth-child(#{i})")[0].nil?) then
# Create affiliate link
# http://www.awin1.com/cread.php?awinmid=1857&awinaffid=121196&clickref=&p=http%3A%2F%2Fwww.wiggle.co.uk%2Fgarmin-edge-500-with-heart-rate-and-cadence%2F
link = "http://www.awin1.com/cread.php?awinmid=1857&awinaffid=121196&clickref=&p=" + doc.css(".categoryListItem:nth-child(#{i}) h2 a")[0].attribute("href").value.gsub(/\:/, "%3A").gsub(/\//, "%2F")
# Create object
results += "{"
results += "\"name\": \"" + doc.css(".categoryListItem:nth-child(#{i}) h2 a")[0].content + "\","
results += "\"price\": \"" + doc.css(".categoryListItem:nth-child(#{i}) .youpay strong")[0].content.gsub(/\$/, '') + "\","
results += "\"url\": \"" + link + "\","
results += "\"image\": \"" + doc.css(".categoryListItem:nth-child(#{i}) .productimage img")[0].attribute("src").value + "\""
results += "},"
end
end
# ========================================================================
# PRO BIKE KIT
# ========================================================================
elsif params[:site] == "ProBikeKit" then
# For PBK we have to use curb because we have to send the search term as a POST var
connection = Curl::Easy.new
connection.url = "http://www.probikekit.com/au/factfinder/search/result/?q=" + search
connection.cookies = "geolc=aud; expires=Thu, 12-May-2011 12:58:52 GMT; path=/; domain=www.probikekit.com; httponly"
connection.http_get
# Parse the doc into Nokogiri
doc = Nokogiri::HTML(connection.body_str).css(".item")
# Get the number of results found, have to do some maths because PBK sucks
# Check if there's more than one page
if Nokogiri::HTML(connection.body_str).css(".pages").count > 0 then
# Don't want to do another HTTP call and count how many results are on the last page
# so I'll estimate by multipling the number of results they show per page (15) with how many pages
num_results = (Nokogiri::HTML(connection.body_str).css(".pager ol li").count - 1) * 15
else
# Otherwise just count the number of results on the page
num_results = Nokogiri::HTML(connection.body_str).css(".product-name").count
end
num_results_string = num_results.to_i == 1 ? num_results.to_s + " result" : num_results.to_s + " results"
# Loop through the number of items we want returned creating a little JSON object for each
for i in 1..params[:number].to_i do
# Check that there's some results
if (!doc.css(".item")[0].nil?) then
# Get the right item
item = doc.css(".item")[i-1]
# Create object
results += "{"
results += "\"name\": \"" + item.css(".product-name a")[0].content + "\","
# Sometimes the price element is different for unknown reasons
if (item.css(".price")[0].nil?)
results += "\"price\": \"" + item.css(".price_actual")[0].content.strip.gsub!(/AU\$/, '') + "\","
else
results += "\"price\": \"" + item.css(".price")[0].content.strip.gsub!(/AU\$/, '') + "\","
end
results += "\"url\": \"http://scripts.affiliatefuture.com/AFClick.asp?affiliateID=252357&merchantID=4100&programmeID=10236&mediaID=0&tracking=&url=" + item.css(".product-name a")[0].attribute("href").value + "\","
results += "\"image\": \"" + item.css(".product-image img")[0].attribute("src").value + "\""
results += "},"
end
end
# ========================================================================
# RIBBLE CYCLES
# ========================================================================
elsif params[:site] == "Ribble Cycles" then
# Multi-word search term parsing
search.gsub!(/\+/, "%20")
# Create and grab the search results
# Unfortunuately Ribble does some weird server side thing to remember your current location and currency
# The change currency page returns a 301 permanent redirect which redirects properly in a browser but I
# can't get Curl to do it. Oh well.
connection = Curl::Easy.new
# connection.url = "http://www.ribblecycles.co.uk/ChangeCurrency.asp?C=AUD&CC=AU&from=http://www.ribblecycles.co.uk/product/t/" + search
connection.url = "http://www.ribblecycles.co.uk/product/t/" + search
connection.http_get
# Parse the doc into Nokogiri
doc = Nokogiri::HTML(connection.body_str)
# Get the number of results found
num_results = doc.css(".productListItem").count
num_results_string = num_results.to_i == 1 ? num_results.to_s + " result" : num_results.to_s + " results"
# Loop through the number of items we want returned creating a little JSON object for each
for i in 1..params[:number].to_i do
# Check that there's some results
if (!doc.css("#listItemTitle#{i}")[0].nil?) then
# Convert the price to AUD
amount = doc.css(".productListItem:nth-child(#{(i*2)-1}) .price4")[0].content.gsub(/£([0-9]+\.[0-9]+) a saving of [0-9]+\.[0-9]+%/, '\1').strip.to_f * 0.8
currency = open("http://www.google.com/ig/calculator?hl=en&q=" + amount.to_s + "GBP%3D%3FAUD")
australian = (currency.string.match(/([0-9]+.[0-9]+) Australian dollars/)[1].to_f * 100).round/100.00
# Create object
results += "{"
results += "\"name\": \"" + doc.css("#listItemTitle#{i}")[0].content + "\","
results += "\"price\": \"" + australian.to_s + "\","
results += "\"url\": \"" + doc.css("#listItemTitle#{i}")[0].attribute("href").value + "\","
results += "\"image\": \"" + doc.css("#pimage#{i}")[0].attribute("data-src").value + "\""
results += "},"
end
end
end
# Check for results and return
if results == "{\"Results\": [" then
return "{\"Results\": \"None\"}"
else
if num_results then
return results.chop! << "], \"Number\": \"" + num_results.to_s + "\", \"NumberString\": \"" + num_results_string + "\"}"
else
return results.chop! << "]}"
end
end
end