#!/usr/local/bin/env ruby
#
#   Created by Dale Taylor <daletaylor88@gmail.com>
#   Copyright 2006 nClass Software. All rights reserved.
#
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the GNU General Public License
#	as published by the Free Software Foundation; either version 2
#	of the License, or (at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#
# modified by Xiwen on 13 Jan 2008

require 'net/http'
require 'open-uri'
require 'pp'
require 'uri'


input=""
ARGV.each {|arg| input+= arg.to_s + " "}
input.chop!

identifier = false
input=""
#default value
num=10
#input-> query ^number
ARGV.each {|arg| 
if arg.include?("^")
identifier = true
end
if identifier
num= arg.to_s
num=num.gsub(/\^/, "")
num=num.to_i
else
input+= arg.to_s + " "
end}
input.chop!




def generate_search_url(query)

	query= URI.escape(query.chomp).gsub(/%20/, '+')
	#fixes e
	query = query.gsub(/%C3%A9|%C3%A8/, 'e')
    search_url = "http://www.amazon.com/s/ref=nb_ss_gw/103-8500264-6767842?url=search-alias%3Dpopular&field-keywords=" + query + "&x=0&y=0"
	return search_url
end

#returns array of ids
def parse_search_result(url, num)
	product_page=false
	temp = ""
	product_ids=Array.new
	#source = Net::HTTP.get(URI.parse(url))
	source = open(URI.parse(url)).read
	#finds image tag
	prod_exp=/\s\sregisterImage/
	prod_exp=~source
	if $& != nil
	product_page=true
	end
	if product_page
	re=/http:\/\/ecx.images-amazon.com\/images\/I\/.*_AA240_.jpg",\s/
	#puts "product page"
	else
	re=/http:\/\/www.amazon.com\/.*\/dp\/.*\/ref/
	#puts "Search list"
	end
	re =~ source
	if product_page
	product_ids[0]=$&.gsub(/",\s/, "")
	else
	product_ids[0]=$&
	end
	if !product_page
	(1...num).each { 
	|i|
	#find next match
	re =~  $'
	product_ids[i]=$&
	}
	else
	puts $&
	end
	if product_ids != nil
	product_ids.compact!
	end
	(0...product_ids.length).each{ |i|
	if !product_page
	temp2 = parse_search_result(product_ids[i], num)
	product_ids[i]= temp2[0]
	end}
	if product_ids != nil
	product_ids.uniq!
	product_ids.compact!
	end
	puts product_ids

	return product_ids	
end

parse_search_result(generate_search_url(input),num)
