Last active
April 10, 2020 20:28
-
-
Save dhruvkar/d2a06e7458e51f59d5113ffeb62051fc to your computer and use it in GitHub Desktop.
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
import os | |
import requests | |
from bs4 import BeautifulSoup as bs | |
USERNAME = "" # for instacart | |
PASSWORD = "" # for instacart | |
EMAIL = "" # where to send alert | |
STORE = "costco" | |
#STORE = "fresh-thyme-farmers-market" | |
#STORE = "schnucks" | |
#STORE = "sams-club" | |
#get and parse result for <meta name="csrf-token" content= | |
headers0 = { | |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | |
"Accept-Encoding": "gzip, deflate, br", | |
"Accept-Language": "en-US,en;q=0.5", | |
"Connection": "keep-alive", | |
"Host": "www.instacart.com", | |
"User-Agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148", | |
} | |
url0 = "https://www.instacart.com/" | |
#get - this sets cookies | |
headers1 = { | |
"Host": "www.instacart.com", | |
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148", | |
"Accept": "application/json", | |
"Accept-Language": "en-US,en;q=0.5", | |
"Accept-Encoding": "gzip, deflate, br", | |
"Referer": "https://www.instacart.com/", | |
"content-type": "application/json", | |
"X-Requested-With": "XMLHttpRequest", | |
"x-client-identifier": "web", | |
"X-CSRF-Token": "undefined", | |
"Connection": "keep-alive", | |
"TE": "Trailers", | |
} | |
url1 = "https://www.instacart.com/v3/containers/next_gen/authenticate?&source=web&cache_key=undefined" | |
#post - to login | |
data2 = { | |
#"address":None, | |
"authenticity_token":"", | |
"email":USERNAME, | |
"grant_type":"password", | |
"password":PASSWORD, | |
#"scope":"", | |
#"signup_v3_endpoints_web": None, | |
} | |
headers2 = { | |
"Host": "www.instacart.com", | |
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148", | |
"Accept": "application/json", | |
"Accept-Language": "en-US,en;q=0.5", | |
"Accept-Encoding": "gzip, deflate, br", | |
"Referer": "https://www.instacart.com/", | |
"content-type": "application/json", | |
"X-Requested-With": "XMLHttpRequest", | |
"x-client-identifier": "web", | |
"X-CSRF-Token": "undefined", | |
"Origin": "https://www.instacart.com", | |
"Connection": "keep-alive", | |
} | |
url2 = "https://www.instacart.com/v3/dynamic_data/authenticate/login?source=web&cache_key=undefined" | |
s = requests.session() | |
s.headers.update(headers0) | |
r0 = s.get(url0) | |
soup0 = bs(r0.content, "lxml") | |
metas = soup0.find_all("meta") | |
for meta in metas: | |
try: | |
if meta.attrs["name"] == "csrf-token": | |
data2["authenticity_token"] = meta.attrs["content"] | |
except KeyError: | |
pass | |
s.headers.update(headers1) | |
r1 = s.get(url1) | |
s.headers.update(headers2) | |
r2 = s.post(url2, json=data2) | |
###.... last step. get | |
headers_final = { | |
"Host": "www.instacart.com", | |
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148", | |
"Accept": "application/json", | |
"Accept-Language": "en-US,en;q=0.5", | |
"Accept-Encoding": "gzip, deflate, br", | |
"Referer": "https://www.instacart.com/store/{0}/info?tab=delivery".format(STORE), | |
"content-type": "application/json", | |
"X-Requested-With": "XMLHttpRequest", | |
"x-client-identifier": "web", | |
"Connection": "keep-alive", | |
"TE": "Trailers", | |
} | |
url_final = "https://www.instacart.com/v3/containers/{0}/next_gen/retailer_information/content/delivery".format(STORE) | |
## then check containers -> modules -> 1st item in list ["id"]. if it contains "error_no_availability", then no delivery times | |
s.headers.update(headers_final) | |
rfinal = s.get(url_final) | |
if len(rfinal.json()["container"]["modules"]) == 1: | |
try: | |
print (rfinal.json()["container"]["modules"][0]["data"]["title"]) | |
except KeyError: | |
print ("Delivery times might be available") | |
elif len(rfinal.json()["container"]["modules"]) == 2: | |
os.system("mutt -s 'delivery available at {0}!' {1} < /dev/null".format(STORE, EMAIL)) | |
av_days = rfinal.json()["container"]["modules"][1]["data"]["service_options"]["service_options"]["days"] | |
for ad in av_days: | |
options = ad["options"] | |
for option in options: | |
print (option["delivery_full_window"]) | |
else: | |
print ("didn't write code for this eventuality, check rfinal") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment