incomestreamsurfer

this is the one that will work please jesus

Jun 6th, 2023 (edited)
3,475
1
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 1 0
  1. import os
  2. import openai
  3. import requests
  4. import pandas as pd
  5. from PIL import Image
  6. from io import BytesIO
  7. from tqdm import tqdm # Added tqdm for progress tracking
  8.  
  9. # Set OpenAI API key
  10. openai.api_key = 'YOUR_API_KEY'
  11.  
  12. print("OpenAI API key set...")
  13.  
  14. # Function to generate a clickable title using GPT-4
  15. def generate_clickable_title(title):
  16. print("Generating clickable title...")
  17. prompt = f"Generate a catchy and clickable title for a blog post based on the YouTube video titled '{title} Maximum 50 characters'."
  18. response = openai.ChatCompletion.create(
  19. model="gpt-3.5-turbo", # Updated to GPT-4
  20. messages=[
  21. {"role": "system", "content": "You are a helpful assistant."},
  22. {"role": "user", "content": prompt}
  23. ]
  24. )
  25. clickable_title = response['choices'][0]['message']['content'].strip()
  26. clickable_title = clickable_title.replace('"', '') # Remove double quotes
  27. print("Clickable title generated...")
  28. return clickable_title
  29.  
  30. # Function to generate blog post using GPT-4
  31. def generate_blog_post(row):
  32. print("Generating blog post...")
  33. original_title = row['youtube video title']
  34. body = row['body description']
  35. asin = row['ASIN']
  36.  
  37. # Generate a clickable title
  38. title = generate_clickable_title(original_title)
  39.  
  40. # Prepare a prompt for GPT-4.
  41. prompt = f'Write 1200 words of content, without considering the links, Write ONLY in HTML. Consider SEO ranking factors, so use as much HTML as possible. Unordered lists, ordered lists, bold, italiacs, quotes, underlines. Do not include links to the amazon products unless its a referral link. Never mention the ASIN except in the links. This is a blog post about a YouTube video originally titled "{original_title}" do not mention the YouTube video, or the host of the video, just use the information, with the following description: "{body}". The associated products ASIN is "{asin}". Please write a blog post about this. When you mention a brand, please add the following link <center><iframe sandbox="allow-popups allow-scripts allow-modals allow-forms allow-same-origin" style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//ws-na.amazon-adsystem.com/widgets/q?ServiceVersion=20070822&OneJS=1&Operation=GetAdHtml&MarketPlace=US&source=ss&ref=as_ss_li_til&ad_type=product_link&tracking_id=incomestre0d3-20&language=en_US&marketplace=amazon&region=US&placement={asin}&asins={asin}&linkId=ea5840420af22ff2e663be3eee42c688&show_border=true&link_opens_in_new_window=true"></iframe></center>Replace B07S9H1VRC with the ASIN of the product associated with that {asin}'
  42.  
  43. # Generate the blog post using GPT-4.
  44. response = openai.ChatCompletion.create(
  45. model="gpt-3.5-turbo", # Updated to GPT-4
  46. messages=[
  47. {"role": "system", "content": "You are a WordPress Writer. You write articles with introductions, brands, and conclusons, each section should have some content. Under each product name you ALWAYS put an embeded iframe link. You only write in HTML. Including headings, subheadings etc. You also always embed amazon iframe links after every mention of the brand"},
  48. {"role": "user", "content": prompt}
  49. ]
  50. )
  51.  
  52. blog_post = response['choices'][0]['message']['content'].strip()
  53. print("Blog post generated...")
  54. return title, blog_post
  55.  
  56. # Function to upload post to Wordpress
  57. def upload_to_wordpress(title, content):
  58. print("Uploading post to WordPress...")
  59. url = "https://yourwordpresswebsite.com/wp-json/wp/v2/posts"
  60. headers = {
  61. "Authorization": "Bearer your_auth_code",
  62. }
  63. data = {
  64. "title": title,
  65. "content": content,
  66. "status": "publish",
  67. }
  68. response = requests.post(url, headers=headers, json=data)
  69. print("Post uploaded to WordPress...")
  70. return response.json()
  71.  
  72. # Function to generate image using DALL-E
  73. def generate_image(prompt):
  74. print("Generating image using DALL-E...")
  75. response = openai.Image.create(
  76. prompt=prompt,
  77. n=1,
  78. size="512x512"
  79. )
  80. image_url = response['data'][0]['url']
  81. print("Image generated...")
  82. return image_url
  83.  
  84. # Function to download and save image
  85. def save_image(image_url, filename):
  86. print("Saving image...")
  87. response = requests.get(image_url)
  88. image = Image.open(BytesIO(response.content))
  89. image.save(filename)
  90. print("Image saved...")
  91.  
  92. # Function to upload image to WordPress
  93. def upload_image_to_wordpress(filename):
  94. url = "https://coffeesomnium.com/wp-json/wp/v2/media"
  95. headers = {
  96. "Authorization": "Bearer your_auth_code",
  97. "Content-Disposition": f"attachment; filename={filename}",
  98. "Content-Type": "image/jpeg",
  99. }
  100. with open(filename, "rb") as image_file:
  101. response = requests.post(url, headers=headers, data=image_file)
  102. return response.json() # Ensure .json() method is called
  103.  
  104.  
  105. # Load CSV
  106. print("Loading CSV...")
  107. df = pd.read_csv('file.csv')
  108.  
  109. # Select the first row
  110. first_row = df.iloc[0]
  111.  
  112. # Generate blog post for the first row
  113. print("Generating blog post...")
  114. title, blog_content = generate_blog_post(first_row)
  115. print("Blog post generated...")
  116.  
  117. # Generate image using DALL-E
  118. print("Generating image...")
  119. image_prompt = "A patterned repeated background image-only with coffee" # Replace this with the appropriate prompt for your blog post
  120. image_url = generate_image(image_prompt)
  121.  
  122. # Save the generated image to your local system
  123. print("Saving image...")
  124. filename = "generated_image.jpg"
  125. save_image(image_url, filename)
  126. print("Image saved...")
  127.  
  128. # Upload the image to WordPress
  129. print("Uploading image to WordPress...")
  130. uploaded_image = upload_image_to_wordpress(filename)
  131. print(f"Uploaded image: {uploaded_image}") # Debugging line
  132. print("Image uploaded to WordPress...")
  133.  
  134.  
  135.  
  136. # Add the uploaded image URL to the blog post content
  137. image_url = uploaded_image['source_url']
  138. blog_content_with_image = f'<img src="{image_url}" style="max-width:100%;height:auto;" alt="{image_prompt}"><br>{blog_content}'
  139.  
  140.  
  141. # Upload the blog post with the image
  142. print("Uploading post to WordPress...")
  143. upload_to_wordpress(title, blog_content_with_image)
  144. print("Post uploaded to WordPress...")
  145.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment