Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
CSC190 Group 6
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Imam M. Jah
CSC190 Group 6
Commits
7d8ca09c
Commit
7d8ca09c
authored
Oct 18, 2024
by
Imam M. Jah
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added urlof images
parent
ce9d5676
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
6 deletions
+28
-6
kroger.py
src/kroger.py
+28
-6
No files found.
src/kroger.py
View file @
7d8ca09c
...
@@ -2,8 +2,8 @@ import requests
...
@@ -2,8 +2,8 @@ import requests
from
requests.auth
import
HTTPBasicAuth
from
requests.auth
import
HTTPBasicAuth
# Replace these with your actual Kroger API credentials
# Replace these with your actual Kroger API credentials
CLIENT_ID
=
'
your_client_id
'
CLIENT_ID
=
'
bagandbyte-2432612430342431786a6a7a4144667543456c792f4433472f6f4f5965785957433762597543636d5752524f6b5554766233664f67703648413533654135037845033016591
'
CLIENT_SECRET
=
'
your_client_secret
'
CLIENT_SECRET
=
'
Yb24nJQRFG8yS73ivzSwTES9cBTfGe39UsjHgEAQ
'
# OAuth 2.0 token URL and Kroger API product search URL
# OAuth 2.0 token URL and Kroger API product search URL
TOKEN_URL
=
'https://api.kroger.com/v1/connect/oauth2/token'
TOKEN_URL
=
'https://api.kroger.com/v1/connect/oauth2/token'
...
@@ -39,7 +39,7 @@ def search_products(token, query):
...
@@ -39,7 +39,7 @@ def search_products(token, query):
params
=
{
params
=
{
'filter.term'
:
query
,
# Search term, e.g., "milk"
'filter.term'
:
query
,
# Search term, e.g., "milk"
'filter.locationId'
:
'01
234567
'
# Replace with a valid location ID
'filter.locationId'
:
'01
400943
'
# Replace with a valid location ID
}
}
# Send the GET request to search for products
# Send the GET request to search for products
...
@@ -51,6 +51,30 @@ def search_products(token, query):
...
@@ -51,6 +51,30 @@ def search_products(token, query):
print
(
f
"Failed to fetch products: {response.status_code}, {response.text}"
)
print
(
f
"Failed to fetch products: {response.status_code}, {response.text}"
)
return
None
return
None
def
display_products_with_images
(
products
):
for
product
in
products
[
'data'
]:
product_id
=
product
[
'productId'
]
name
=
product
[
'description'
]
# Check for images and get the first available image (front perspective)
image_url
=
None
if
'images'
in
product
and
len
(
product
[
'images'
])
>
0
:
for
image
in
product
[
'images'
]:
if
image
[
'perspective'
]
==
'front'
:
# Use front-facing image
if
len
(
image
[
'sizes'
])
>
0
:
image_url
=
image
[
'sizes'
][
0
][
'url'
]
# Fetch the URL of the first size available
break
print
(
f
"Product ID: {product_id}"
)
print
(
f
"Name: {name}"
)
if
image_url
:
print
(
f
"Image URL: {image_url}"
)
else
:
print
(
"No image available."
)
print
(
"-"
*
40
)
# Main code to test the API
# Main code to test the API
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
# Step 1: Get Access Token
# Step 1: Get Access Token
...
@@ -61,9 +85,7 @@ if __name__ == "__main__":
...
@@ -61,9 +85,7 @@ if __name__ == "__main__":
# Step 2: Search for products (e.g., search for "milk")
# Step 2: Search for products (e.g., search for "milk")
products
=
search_products
(
token
,
'milk'
)
products
=
search_products
(
token
,
'milk'
)
if
products
:
if
products
:
print
(
"Products found:"
)
display_products_with_images
(
products
)
for
product
in
products
[
'data'
]:
print
(
f
"Product ID: {product['productId']}, Description: {product['description']}"
)
else
:
else
:
print
(
"No products found or an error occurred."
)
print
(
"No products found or an error occurred."
)
else
:
else
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment