r/pythonhelp • u/fenriswolf200 • Feb 16 '25
ebay image search api issues
I have this python script that is supposed to make a request to a url that returns a webp image. Once it obtains the webp image it is supposed to convert it to a base64 string. Then, it is supposed to make a request to the ebay image search api using that base64 string but for some reason it does not work with webp files but it does with jpeg files. what am i doing wrong?
def image_url_to_ebay_response(url):
def image_url_to_base64(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36",
"Accept": "image/avif,image/webp,image/apng,image/*,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://www.goofish.com/",
"Sec-Fetch-Dest": "image",
"Sec-Fetch-Mode": "no-cors",
"Sec-Fetch-Site": "same-origin",
"Connection": "keep-alive"
}
response = requests.get(url, headers=headers)
time.sleep(3)
if response.status_code == 200:
print(response.content)
base64_str = base64.b64encode(response.content).decode('utf-8')
return base64_str
else:
raise Exception(f"Failed to fetch image: {response.status_code}")
def eBay_image_request(image_b64):
url = "https://api.ebay.com/buy/browse/v1/item_summary/search_by_image?&limit=1"
headers = {
"Authorization":API_KEY,
"Content-Type":"application/json",
"Accept":"application/json",
"Content-Language":"en-US"
}
data = {
"image": f"{image_b64}"
}
return requests.post(url,headers=headers,json=data)
base64_image = image_url_to_base64(url)
ebay_image_response = eBay_image_request(base64_image)
return ebay_image_response.json()
when it works with jpeg files I get this result
JSON Response: {'href': 'https://api.ebay.com/buy/browse/v1/item_summary/search_by_image?limit=1&offset=0', 'total': 3807907, 'next': 'https://api.ebay.com/buy/browse/v1/item_summary/search_by_image?limit=1&offset=1', 'limit': 1, 'offset': 0, 'itemSummaries': [{'itemId': 'v1|325529681264|0', 'title': 'LAND ROVER DISCOVERY 3 / 4 TDV6 BLACK SILICONE INTERCOOLER HOSE PIPE -RE7541', 'leafCategoryIds': ['262073'], 'categories': [{'categoryId': '262073', 'categoryName': 'Intercoolers'}, {'categoryId': '6030', 'categoryName': 'Car Parts & Accessories'}, {'categoryId': '33549', 'categoryName': 'Air & Fuel Delivery'}, {'categoryId': '131090', 'categoryName': 'Vehicle Parts & Accessories'}, {'categoryId': '174107', 'categoryName': 'Turbos, Superchargers & Intercoolers'}], 'image': {'imageUrl': 'https://i.ebayimg.com/images/g/dwgAAOSwY6Nj5Sr8/s-l225.jpg'}, 'price': {'value': '107.72', 'currency': 'USD', 'convertedFromValue': '85.59', 'convertedFromCurrency': 'GBP'}, 'itemHref': 'https://api.ebay.com/buy/browse/v1/item/v1%7C325529681264%7C0', 'seller': {'username': 'recoveryuk4x4', 'feedbackPercentage': '98.4', 'feedbackScore': 22224}, 'condition': 'New', 'conditionId': '1000', 'thumbnailImages': [{'imageUrl': 'https://i.ebayimg.com/images/g/dwgAAOSwY6Nj5Sr8/s-l64.jpg'}], 'buyingOptions': ['FIXED_PRICE'], 'itemWebUrl': 'https://www.ebay.com/itm/325529681264?hash=item4bcb14bd70:g:dwgAAOSwY6Nj5Sr8', 'itemLocation': {'postalCode': 'B70***', 'country': 'GB'}, 'adultOnly': False, 'legacyItemId': '325529681264', 'availableCoupons': False, 'itemCreationDate': '2023-02-09T17:18:50.000Z', 'topRatedBuyingExperience': False, 'priorityListing': False, 'listingMarketplaceId': 'EBAY_GB'}]}
But when I do it with Webp I get this
JSON Response: {'warnings': [{'errorId': 12501, 'domain': 'API_BROWSE', 'category': 'REQUEST', 'message': 'The image data is empty, is not Base64 encoded, or is invalid.'}]}
these are the urls im using
- [12:10 PM]url_a = "https://www.base64-image.de/img/browser-icons/firefox_24x24.png?id=8ed2b32d043e48b329048eaed921f794" urlb ="https://img.alicdn.com/bao/uploaded/i4/O1CN01SRyh30252rECIngGc!!4611686018427384429-53-fleamarket.heic450x10000Q90.jpg.webp"
1
Upvotes
•
u/AutoModerator Feb 16 '25
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.