-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_ids.py
60 lines (43 loc) · 1.6 KB
/
get_ids.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import requests
import json
import os
from dotenv import load_dotenv
load_dotenv()
company_id = 'mnml-la'
url = f"https://api.convictional.com/buyer/products?page=0&limit=50&companyId={company_id}"
headers = {
"accept": "application/json",
"Authorization": os.getenv('CONVICTIONAL_API_KEY') # flipshop api key
}
def get_product_details(url):
product_details = {}
while url:
response = requests.get(url, headers=headers)
if response.status_code != 200:
print(f"Error: Received status code {response.status_code}")
break
data = response.json()
# extract product and variant details
for product in data['data']:
parent_id = product.get('id', 'n/a')
if parent_id not in product_details:
product_details[parent_id] = []
variants = product.get('variants', [])
for variant in variants:
variant_id = variant.get('id', 'n/a')
product_details[parent_id].append(variant_id)
# check if there's more pages of data
if data.get("hasMore", False):
url = data.get('next', None)
else:
break
return product_details
# get all product details
product_details = get_product_details(url)
# print the results
print(json.dumps(product_details, indent=4))
# save the results to a file
output_file = f"{company_id}_ids.json"
with open(output_file, 'w') as f:
json.dump(product_details, f, indent=4)
print(f"Parent and variant IDs for {company_id} saved to {output_file}")