/wishlistshuffler.py Secret
Last active
May 20, 2016 20:40
Star
You must be signed in to star a gist
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
| """Takes an Amazon wishlist and randomly promotes five items in it from the top. | |
| Usage: From the command line, run the following: | |
| python wishlistshuffler.py <your email> <your password> <wishlist name> | |
| Requires selenium to be installed as a python package and chromedriver ( | |
| https://sites.google.com/a/chromium.org/chromedriver/downloads) to be on | |
| your path. | |
| I believe this is not in violation of Amazon's Terms of Service based on | |
| my reading of them: It is a non-malicious use of your own account which | |
| does not republish any data. | |
| I release this source code under CC0 (https://creativecommons.org/choose/zero/) | |
| """ | |
| from selenium.webdriver import Chrome | |
| import re | |
| import random | |
| TRAILING_COUNT = re.compile(r'\s+\(\d+\)') | |
| def signed_in_chrome(email, password): | |
| chrome = Chrome() | |
| chrome.get("https://www.amazon.co.uk") | |
| chrome.find_elements_by_css_selector('div#nav-tools a')[0].click() | |
| chrome.find_elements_by_css_selector('input[type="email"]')[0].send_keys( | |
| ) | |
| chrome.find_elements_by_css_selector('input[type="password"]')[0].send_keys( | |
| password | |
| ) | |
| chrome.find_element_by_id('signInSubmit').click() | |
| return chrome | |
| def get_wishlists(chrome): | |
| chrome.get("https://www.amazon.co.uk/gp/registry/wishlist/") | |
| return [ | |
| (TRAILING_COUNT.sub("", l.text), l.get_attribute("href")) | |
| for l in chrome.find_elements_by_css_selector( | |
| "a[data-action='reg-nav-link']") | |
| if l.text and '/registry/wishlist/' in l.get_attribute('href') | |
| ] | |
| def random_wish_list_item(chrome, url): | |
| chrome.get(url) | |
| pagers = [ | |
| l for l in chrome.find_elements_by_css_selector( | |
| "div ul.a-pagination li a") if 'Next' not in l.text | |
| ] | |
| random.choice(pagers).click() | |
| chrome.implicitly_wait(1) | |
| items = [ | |
| link for link in chrome.find_elements_by_css_selector( | |
| "div.a-row h5 a.a-link-normal") | |
| ] | |
| items = [l for l in items if 'itemName' in l.get_attribute("id")] | |
| item = random.choice(items) | |
| return (item.text, item.get_attribute("href")) | |
| def add_item_to_wishlist(chrome, url, name): | |
| chrome.get(url) | |
| chrome.find_element_by_id('add-to-wishlist-button').click() | |
| chrome.implicitly_wait(1) | |
| wishlists = chrome.find_elements_by_css_selector( | |
| "#atwl-dd-ul > li > a > span > span.atwl-dd-list-name") | |
| for w in wishlists: | |
| if w.text == name: | |
| w.click() | |
| break | |
| else: | |
| raise ValueError("No such wishlist %r" % (name,)) | |
| if __name__ == '__main__': | |
| import sys | |
| _, email, password, wishlist = sys.argv | |
| chrome = signed_in_chrome(email, password) | |
| for name, url in get_wishlists(chrome): | |
| if name == wishlist: | |
| wishlist_url = url | |
| break | |
| else: | |
| raise ValueError("No such wishlist %r" % (wishlist,)) | |
| for _ in range(5): | |
| name, item = random_wish_list_item(chrome, wishlist_url) | |
| print("Promoting %r" % (name,)) | |
| add_item_to_wishlist(chrome, item, wishlist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment