Skip to content

Instantly share code, notes, and snippets.

@chespinoza
Created September 4, 2020 19:53
Show Gist options
  • Save chespinoza/54f5c1dad7db83e76f291da2d9357812 to your computer and use it in GitHub Desktop.
Save chespinoza/54f5c1dad7db83e76f291da2d9357812 to your computer and use it in GitHub Desktop.
import os
import shutil
import timeit
from dotenv import load_dotenv
from aaapi import api
load_dotenv()
CLIENT_SECRET = os.environ["CLIENT_SECRET"]
CLIENT_ID = os.environ["CLIENT_ID"]
REFRESH_TOKEN = os.environ["REFRESH_TOKEN"]
PROFILE_ID = 1212121212121212
start_time = timeit.default_timer()
client = api.Client(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
refresh_token=REFRESH_TOKEN,
profile_id=PROFILE_ID,
marketplace="UK",
)
# Step 1 - Request Snapshot
request = client._session.post(
"https://advertising-api-eu.amazon.com/v2/sp/keywords/snapshot",
json={"stateFilter": "enabled"},
).json()
snapshotId = request.get("snapshotId")
while True:
# Step 2 - Check if Snapshot was generated
result = client._session.get(
f"https://advertising-api-eu.amazon.com/v2/sp/snapshots/{snapshotId}"
).json()
if result.get("status") == "SUCCESS":
# Step 3 - Download the snapshot
with client._session.get(result["location"], stream=True) as r:
r.raise_for_status()
r.raw.decode_content = True
with open("snapshot.json", "wb") as file:
shutil.copyfileobj(r.raw, file)
break
else:
print(f"status:{result.get('status')} - {(timeit.default_timer() - start_time)} secs passed")
time.sleep(1)
print(f"Done! {(timeit.default_timer() - start_time)} secs total")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment