Skip to content

Instantly share code, notes, and snippets.

@polarblau
Last active March 17, 2021 13:16
Show Gist options
  • Save polarblau/cf6d62bdbc23172d705e to your computer and use it in GitHub Desktop.
Save polarblau/cf6d62bdbc23172d705e to your computer and use it in GitHub Desktop.
Transferring Amazon wish list entries the hard way between international stores.
#!/usr/bin/env ruby
require "rubygems"
require "mechanize"
SOURCE_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"
TARGET_LOGIN_URL = "https://www.amazon.de/ap/signin?_encoding=UTF8&openid.assoc_handle=deflex&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.de%3Fie%3DUTF8%26ref_%3Dnav_ya_signin"
WISHLIST_URL = "http://www.amazon.com/gp/registry/wishlist/ref=nav_wishlist_btn"
TARGET_ROOT = "http://www.amazon.de"
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(".g-item-sortable 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