Forked from polarblau/amzn_wish_list_transfer.rb
Last active
November 18, 2015 21:46
-
-
Save vesan/84e7cfd7656ecbe914b1 to your computer and use it in GitHub Desktop.
Transferring Amazon wish list entries the hard way between international stores.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "rubygems" | |
require "mechanize" | |
TARGET_LOGIN_URL = "https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fcss%2Fhomepage.html%3Fie%3DUTF8%26*Version*%3D1%26*entries*%3D0%26ref_%3Dnav_signin" | |
SOURCE_LOGIN_URL = "https://www.amazon.co.uk/ap/signin?_encoding=UTF8&openid.assoc_handle=gbflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.co.uk%2F%3Fref_%3Dnav_ya_signin" | |
WISHLIST_URL = "http://www.amazon.co.uk/gp/registry/wishlist/ref=nav_wishlist_btn" | |
TARGET_ROOT = "http://www.amazon.com" | |
Credentials = Struct.new(:user, :password) | |
amzn = Credentials.new(ENV["AMZN_USER"], ENV["AMZN_PASSWORD"]) | |
# Set-up | |
agent = Mechanize.new { |a| | |
a.user_agent_alias = "Mac Safari" | |
a.follow_meta_refresh = true | |
} | |
# Login at source store | |
agent.get(SOURCE_LOGIN_URL) do |page| | |
page.form_with(:name => "signIn") do |login| | |
login.email = amzn.user | |
login.password = amzn.password | |
end.submit | |
end | |
# Login at target store | |
agent.get(TARGET_LOGIN_URL) do |page| | |
page.form_with(:name => "signIn") do |login| | |
login.email = amzn.user | |
login.password = amzn.password | |
end.submit | |
end | |
# Scrape wish list pages recursively | |
def scrape_page(agent, page, items) | |
items += page.search(".a-column h5 .a-link-normal") | |
.map {|link| [link['title'], TARGET_ROOT + link['href']] } | |
next_page_link = page.at("#wishlistPagination .a-last:not(.a-disabled) a") | |
if next_page_link | |
puts "Scraping next page ..." | |
next_page = agent.click(next_page_link) | |
items = scrape_page(agent, next_page, items) | |
else | |
return items | |
end | |
end | |
agent.get(WISHLIST_URL) do |page| | |
puts "Scraping first page ..." | |
items = scrape_page(agent, page, []) | |
items.each_with_index do |(title, url), index| | |
puts "Transferring '#{title}' ..." | |
begin | |
agent.get(url) do |item_page| | |
item_page.form_with(:id => "addToCart") do |form| | |
form.submit(form.button_with(:name => "submit.add-to-registry.wishlist")) | |
puts "Done!" | |
end | |
end | |
rescue | |
puts "Item not found: #{title} (#{url})" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment