-
Notifications
You must be signed in to change notification settings - Fork 4
Switching images to use Amazon instead #698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shashilo
wants to merge
2
commits into
develop
Choose a base branch
from
slo/images-using-amazonapi
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 characters
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 characters
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 characters
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 characters
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 characters
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 characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,6 @@ | ||
| // Copyright (c) Gridiron Survivor. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { GoogleError, BackendError } from './errors/CustomErrors'; | ||
|
|
||
| /** | ||
| * Amazon Image | ||
| * @param {string} title - The title of the search item | ||
|
|
@@ -11,50 +9,9 @@ import { GoogleError, BackendError } from './errors/CustomErrors'; | |
| */ | ||
| export const getAmazonImage = async ( | ||
| title: string, | ||
| ): Promise<{ imageUrl: string | null }> => { | ||
| const API_KEY = process.env.GOOGLE_API_KEY; | ||
| const CSE_ID = process.env.GOOGLE_CSE_ID; | ||
|
|
||
| const cleanTitle = title | ||
| .replace(/[^\w\s-]/g, '') | ||
| .replace(/\s+/g, ' ') | ||
| .trim(); | ||
|
|
||
| const searchQuery = `${cleanTitle} amazon.com product`; | ||
|
|
||
| try { | ||
| const response = await fetch( | ||
| `https://customsearch.googleapis.com/customsearch/v1?key=${API_KEY}&cx=${CSE_ID}&q=${encodeURIComponent(searchQuery)}&searchType=image&num=10`, | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| throw new GoogleError( | ||
| 'Error fetching Google Search results:', | ||
| response.status, | ||
| ); | ||
| } | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const amazonItems = data.items?.filter((item: any) => { | ||
| try { | ||
| const domain = new URL(item.image.contextLink).hostname; | ||
| return domain.includes('amazon.'); | ||
| } catch { | ||
| throw new BackendError('Failed to fetch valid Amazon product url', 404); | ||
| } | ||
| }); | ||
|
|
||
| // Get the first Amazon item if available | ||
| if (amazonItems?.length > 0) { | ||
| return { | ||
| imageUrl: amazonItems[0].link, | ||
| }; | ||
| } | ||
|
|
||
| throw new BackendError('Failed to fetch Amazon image', 404); | ||
| } catch (error) { | ||
| throw error; | ||
| } | ||
| ): Promise<{ imageUrl: string | null; productUrl: string | null }> => { | ||
| const affiliateTag = process.env.NEXT_PUBLIC_AMAZON_AFFILIATE_TAG; | ||
| const encodedSearch = encodeURIComponent(title).replace(/%20/g, '+'); | ||
| const productUrl = `https://www.amazon.com/s?k=${encodedSearch}${affiliateTag ? `&tag=${affiliateTag}` : ''}`; | ||
| return { imageUrl: null, productUrl }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
titleis just the name of a gift to be searched. This just usestitleas if it's a product id with the goal of creating an Amazon product page. But by using/s?k=it just creates a search page instead.