The goal is to:
Install the necessary libraries:
pip install openai requests
You’ll need:
import openai
import requests
import json
# Set your API keys
OPENAI_API_KEY = "your_openai_api_key"
ETSY_API_KEY = "your_etsy_api_key"
ETSY_SHOP_ID = "your_shop_id" # Replace with your Etsy shop ID
# Set API endpoints
ETSY_BASE_URL = "https://openapi.etsy.com/v3/application"
ETSY_LISTINGS_URL = f"{ETSY_BASE_URL}/shops/{ETSY_SHOP_ID}/listings"
# OpenAI API configuration
openai.api_key = OPENAI_API_KEY
def generate_product_description(product_name, product_details):
"""Use ChatGPT to generate a product description."""
prompt = f'''
Write an engaging and SEO-optimized Etsy product description for the following:
- Product Name: {product_name}
- Details: {product_details}
'''
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=250,
temperature=0.7
)
return response["choices"][0]["text"].strip()
def fetch_etsy_listings():
"""Fetch active Etsy listings."""
headers = {"x-api-key": ETSY_API_KEY}
response = requests.get(ETSY_LISTINGS_URL, headers=headers)
if response.status_code == 200:
return response.json()["results"]
else:
print(f"Error: {response.status_code}, {response.text}")
return []
def update_etsy_listing(listing_id, title=None, description=None, price=None, tags=None):
"""Update an Etsy listing."""
headers = {
"x-api-key": ETSY_API_KEY,
"Content-Type": "application/json"
}
data = {
"title": title,
"description": description,
"price": price,
"tags": tags
}
# Remove None values
data = {k: v for k, v in data.items() if v is not None}
response = requests.put(f"{ETSY_LISTINGS_URL}/{listing_id}", headers=headers, json=data)
if response.status_code == 200:
print(f"Successfully updated listing {listing_id}")
else:
print(f"Error: {response.status_code}, {response.text}")
def optimize_and_update():
"""Fetch, optimize, and update Etsy listings."""
listings = fetch_etsy_listings()
for listing in listings:
# Extract details for ChatGPT
product_name = listing["title"]
product_details = listing.get("description", "")
# Generate new description and tags
new_description = generate_product_description(product_name, product_details)
new_tags = ["customized", "unique", "handmade"] # Add ChatGPT-generated tags here
# Update the listing
update_etsy_listing(
listing_id=listing["listing_id"],
description=new_description,
tags=new_tags
)
if __name__ == "__main__":
# Fetch listings and optimize
optimize_and_update()