Skip to content

Instantly share code, notes, and snippets.

@butlermh
Created June 16, 2011 19:29
Show Gist options
  • Save butlermh/1030033 to your computer and use it in GitHub Desktop.
Save butlermh/1030033 to your computer and use it in GitHub Desktop.
Find out what books are available on your Amazon wishlist on Kindle
#!/usr/bin/ruby
require 'rubygems'
require 'open-uri'
require 'nokogiri'
require 'net/http'
require 'uri'
require 'amazon/aws/search'
include Amazon::AWS
include Amazon::AWS::Search
$KCODE = "UTF8"
class KindleEdition
attr_reader :author, :title, :url, :exists
def initialize(title, author, locale)
is = ItemSearch.new( 'KindleStore', { 'Author' => author, "Title" => title } )
is.response_group = ResponseGroup.new( 'Large' )
req = Request.new
req.locale = locale
begin
resp = req.search( is )
items = resp.item_search_response[0].items[0].item
items.each do |item|
attribs = item.item_attributes[0]
@author = attribs.author
@title = attribs.title
links = item.item_links.item_link
@url = links[0].url
@exists = true
return
end
rescue Amazon::AWS::Error::NoExactMatches
@exists = false
end
@exists = false
end
end
String.class_eval do
def trim
chomp.strip
end
end
class WishlistItem
attr_reader :authors, :title, :id
def initialize (authors, title, id)
@authors = authors
@title = title
@id = id
end
end
class Wishlist
attr_reader :numberOfPages, :items, :baseUri, :sid, :gid, :amazonURL, :wishlistID
PATH_TO_FORM = "//html/body/div/div/div/div/div/div/form"
def initialize(email, locale)
@items = Array.new
# set Amazon URL
@amazonURL = "http://www.amazon.co.uk"
if locale == "us"
@amazonURL = "http://www.amazon.com"
end
# find wishlist ID from email
u = @amazonURL + '/gp/registry/search.html?type=wishlist&field-name=' + email
url = URI.parse(u)
full_path = "#{url.path}?#{url.query}"
the_request = Net::HTTP::Get.new(full_path)
res = Net::HTTP.start(url.host, url.port) { |http|
http.request(the_request)
}
redirect = res['location']
if redirect == nil
raise "Cannot find wishlist for #{email} at #{@amazonURL}"
end
@wishlistID = redirect[redirect.index("&id=") + 4, redirect.length]
# set base URI to retrieve wishlist
@baseUri = @amazonURL + "/registry/wishlist/" + "#{@wishlistID}"
@uriWithParams = @baseUri + "?reveal=unpurchased&filter=3&sort=date-added"
@compactBaseUri = @uriWithParams + "&layout=compact&x=15&y=4"
doc = Nokogiri::HTML(open(@compactBaseUri))
setNumberOfPages(doc)
@sid = doc.xpath("//html/body/form/input[@name=\"sid\"]/@value")
@gid = doc.xpath("//html/body/form/input[@name=\"gid\"]/@value")
# process each wishlist page
for pages in 1..@numberOfPages
if pages > 1
doc = Nokogiri::HTML(open(@compactBaseUri + "&page=#{pages}"))
end
doc.xpath(PATH_TO_FORM + '/table/tbody/tr').each do | row |
item = row.parent['name']
title = getTitle(row)
authors = getAuthors(row)
if title != nil
if title.trim.length > 0
@items << WishlistItem.new(authors, title, item)
end
end
end
end
end
protected
def setNumberOfPages(doc)
doc.xpath(PATH_TO_FORM + '/div/div/table/tr/td/span').each do | page_text |
page_number_string = "Page 1 of"
p = page_text.content
if p.index(page_number_string) != nil
@numberOfPages = Integer(p[page_number_string.length, p.length].trim)
return
end
end
end
def getAuthors(row)
authors = Array.new
row.xpath('td/span[2]').text.trim.split(",").each do | author |
author = author.trim
if author=~/^by\s/
author = author[3, author.length]
end
if author=~/\(Author\)$/
author = author[0, author.length - 9]
end
authors << author
end
return authors
end
def getTitle(row)
title = row.xpath('td/span[1]/strong/a').text
if (title.index("(") != nil)
title = title[0, title.index("(")]
end
colon = title.index(":")
if colon != nil
if (title.index(":", colon + 1) != nil)
title = title[0, title.index(":", colon + 1)]
end
end
if title.index("[") != nil
title = title[0, title.index("[")]
end
return title.chomp
end
end
unless ARGV.length == 1 || ARGV.length == 2
puts "Usage: #{$0} [email] [locale]"
puts "For amazon.com locale is us"
puts "For amazon.co.uk locale is uk"
exit
end
locale = "uk"
if ARGV.length == 2
locale = ARGV[1]
end
wishlist = Wishlist.new(ARGV[0], ARGV[1])
puts "<html><head></head><body>"
puts "<table>"
form = 1
wishlist.items.each do | item |
id = item.id.split(".")
ke = KindleEdition.new(item.title, item.authors[0], locale)
if ke.exists
puts "<tr><td>#{ke.title}</td>"
puts "<td>#{item.authors[0]}</td>"
puts "<td><a href=\"#{ke.url}\"><input type=\"submit\" value=\"Add eBook to Wishlist\" class=\"textLink\" /></a></td>"
puts "</tr>"
form = form + 1
end
end
puts "</table></body></html>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment