Last active
October 18, 2024 21:56
-
-
Save BFTrick/89b07fda34c218e9d989c0ba2dd7db24 to your computer and use it in GitHub Desktop.
Revisions
-
BFTrick revised this gist
Oct 15, 2024 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -23,6 +23,7 @@ sku active warehouse_products { warehouse_id available } } @@ -57,7 +58,10 @@ f.write("SKU,Available\n") for product in result['products']['data']['edges']: if product['node']['active']: for warehouse_product in product['node']['warehouse_products']: if warehouse_product['warehouse_id'] == "V2FyZWhvdXNlOjc4OTMx": f.write("{},{}\n".format(product['node']['sku'], warehouse_product['available'])) break f.write(json.dumps(result, indent=2)) print(f"Successfully wrote to {filename}") batch_number += 1 -
BFTrick created this gist
Oct 14, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,80 @@ #!/usr/bin/env python3 from gql import gql, Client from gql.transport.requests import RequestsHTTPTransport from gql.transport.exceptions import TransportQueryError import json import time import os bearer = ADDYOURTOKENHERE query_template = """query($cursor: String) { products { request_id complexity data(first: 500, after: $cursor) { pageInfo { endCursor hasNextPage } edges { node { id sku active warehouse_products { available } } } } } }""" _transport = RequestsHTTPTransport( url='https://public-api.shiphero.com/graphql', use_json=True, ) _transport.headers = { "User-Agent": "Mozilla/5.0 (X11; buntu; Linux x86_64; rv:58.0) Gecko/0100101 Firefox/58.0", "Authorization": "Bearer {}".format(bearer), "content-type": "application/json", } client = Client(transport=_transport, fetch_schema_from_transport=True) query = gql(query_template) batch_number = 1 cursor = None while True: try: print(f"Executing query for batch {batch_number}") variables = {"cursor": cursor} result = client.execute(query, variable_values=variables) filename = f'output-{batch_number}.txt' print(f"Writing to {filename}") with open(filename, 'w') as f: f.write("SKU,Available\n") for product in result['products']['data']['edges']: if product['node']['active']: f.write("{},{}\n".format(product['node']['sku'], product['node']['warehouse_products'][0]['available'])) f.write(json.dumps(result, indent=2)) print(f"Successfully wrote to {filename}") batch_number += 1 # Update cursor for the next batch cursor = result['products']['data']['pageInfo']['endCursor'] if not result['products']['data']['pageInfo']['hasNextPage']: print("No more products to fetch.") break except TransportQueryError as e: error_data = e.errors[0] print(f"Error occurred: {error_data}") if error_data['code'] == 30: print(f"Rate limit hit. Waiting for {error_data['time_remaining']} seconds.") time.sleep(120) else: raise e except Exception as e: print(f"An unexpected error occurred: {e}") break