incomestreamsurfer

Untitled

Jul 6th, 2023 (edited)
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. import requests
  2. import pandas as pd
  3. import base64
  4. import os
  5.  
  6. # Set your API credentials
  7. access_token = "YOUR_ACCESS_TOKEN"
  8. shop_id = "YOUR_SHOP_ID"
  9.  
  10. # Set the URL for the API endpoints
  11. base_url = "https://api.printify.com/v1"
  12. upload_url = f"{base_url}/uploads/images.json"
  13. product_url = f"{base_url}/shops/{shop_id}/products.json"
  14.  
  15. # Load the CSV file
  16. csv_path = "image_info.csv" # Update this to your CSV file path
  17. image_df = pd.read_csv(csv_path)
  18.  
  19. # Set headers for requests
  20. headers = {
  21. "Authorization": f"Bearer {access_token}",
  22. "Content-Type": "application/json"
  23. }
  24.  
  25. for idx, row in image_df.iterrows():
  26. # Convert the image to Base64
  27. with open(row['local_path'], "rb") as img_file:
  28. img_b64 = base64.b64encode(img_file.read()).decode('utf-8')
  29.  
  30. # Upload the image to the Printify media library
  31. data = {
  32. "file_name": row['file_name'],
  33. "contents": img_b64
  34. }
  35. response = requests.post(upload_url, headers=headers, json=data)
  36. image_id = response.json()["id"]
  37.  
  38. # Create the product with the uploaded image
  39. data = {
  40. "title": row['title'],
  41. "description": row['description'],
  42. "tags": row['tags'].split(', '), # Assuming tags are comma-separated in the CSV
  43. "blueprint_id": 9, # Replace with the actual blueprint ID
  44. "print_provider_id": 5,
  45. "variants": [
  46. {
  47. "id": 17887, # Replace with the actual variant ID
  48. "price": 1999,
  49. "is_enabled": True
  50. }
  51. ],
  52. "print_areas": [
  53. {
  54. "variant_ids": [17887], # Replace with the actual variant ID
  55. "placeholders": [
  56. {
  57. "position": "front",
  58. "images": [
  59. {
  60. "id": image_id,
  61. "x": 0.5,
  62. "y": 0.5,
  63. "scale": 1.0,
  64. "angle": 0
  65. }
  66. ]
  67. }
  68. ]
  69. }
  70. ]
  71. }
  72. response = requests.post(product_url, headers=headers, json=data)
  73. if response.status_code >= 200 and response.status_code < 300:
  74. print(f"Product {idx+1} created successfully!")
  75. else:
  76. print(f"Failed to create product {idx+1}. Server responded with: {response.text}")
  77.  
Add Comment
Please, Sign In to add comment