-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentGenerator.py
484 lines (425 loc) · 23.8 KB
/
ContentGenerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import openai
import os
import json
from wp_content_uploader import ContentUploader
from amazon_product_scraper import AmazonProductScraper
openai.api_key = os.environ.get('OPENAI_API_KEY')
SITE_URL = os.environ.get("SITE_URL")
WP_USERNAME = os.environ.get("WP_USERNAME")
WP_PASSWORD = os.environ.get("WP_PASSWORD")
USERNAME = os.environ.get("USERNAME")
PASSWORD = os.environ.get("PASSWORD")
CHROME_DRIVER_PATH = os.environ.get("CHROME_DRIVER_PATH")
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
def create_path_if_not_exists(path):
if not os.path.exists(path):
os.makedirs(path)
print(f"Created path: {path}")
else:
print(f"Path already exists: {path}")
class StoreContentGenerator:
def __init__(self, store, products, content_size=2):
self.store = store
self.products = products
self.content = {}
self.content_size = content_size
self.content_uploader = ContentUploader(WP_USERNAME, WP_PASSWORD, SITE_URL)
self.checkpoint = {
"start_content_structure": 0,
"home_page_set": 0,
"set_categories": 0,
"set_subcategories": {
"section_idx": 0,
"category_idx": 0,
"idx": 0
},
"set_products": {
"category_idx": 0,
"subcategory_idx": 0,
"idx": 0
},
"set_products_articles": {
"category_idx": 0,
"subcategory_idx": 0,
"idx": 0
},
"set_blog": {
"category_idx": 0,
"subcategory_idx": 0,
"idx": 0
},
"set_blog_articles": {
"category_idx": 0,
"subcategory_idx": 0,
"topic_idx": 0,
"idx": 0
},
"set_product_reviews": {
"category_idx": 0,
"subcategory_idx": 0,
"product_idx": 0,
"idx": 0,
"set": 0
}
}
def start_content_structure(self):
print("\n--------INITIAL STRUCTURE SET-------")
if not self.checkpoint["start_content_structure"]:
main_menu_prompt = f"""
Instrucciones:
Escribe como un experto SEO.
Este es un diccionario que representa un listado de las secciones que debe tener el menu principal de una {self.store}. Tu objetivo es proporcionar la estructura del diccionario en formato JSON. solo el JSON
- El diccionario principal se llama "menu".
- Cada nombre de sección debe ser una clave dentro del diccionario principal.
- Para cada sección, incluye las siguientes claves:
- "titulo": nombre de la seccion.
- "descripcion": nombre completo relacionado con {self.store}.
- "categorizable" : 1 si es categorizable y 0 si no.
"""
main_menu_response = get_completion(main_menu_prompt)
main_menu_dict = json.loads(main_menu_response)
if main_menu_dict['menu'].get('blog'):
main_menu_dict['menu']['blog']['categorizable'] = 1
self.content = main_menu_dict
self.checkpoint["start_content_structure"] = 1
self.save_checkpoint()
self.save_content()
print("Completed ✓\n")
def set_categories(self):
print("\n-------CATEGORIES SET-------")
if not self.checkpoint["set_categories"]:
idx = 0
for section, characteristics in self.content['menu'].items():
if characteristics['categorizable']:
categories_promt = f"""
Escribe como un Experto SEO
{self.content_size} categorias relevantes para la seccion de {section} de una {self.store}.
En formato de lista de python. solo la lista de python.
siguiendo el siguiente formato: [categoria1, categoria2]
"""
categories_response = get_completion(categories_promt)
categories_list = eval(categories_response)
characteristics["categorias"] = []
for category in categories_list:
characteristics["categorias"].append({
"nombre": category
})
print(idx+1, "category set")
idx += 1
self.checkpoint["set_categories"] = 1
self.save_content()
self.save_checkpoint()
print("Completed ✓\n")
def set_subcategories(self):
print("\n-------SUBCATEGORIES SET-------")
section_idx = self.checkpoint["set_subcategories"]["section_idx"]
category_idx = self.checkpoint["set_subcategories"]["category_idx"]
idx = self.checkpoint["set_subcategories"]["idx"]
for section, characteristics in list(self.content['menu'].items())[section_idx:]:
if characteristics['categorizable']:
characteristics["category_id"] = self.content_uploader.new_category(characteristics)
for category in characteristics["categorias"][category_idx:]:
category["parent_id"] = characteristics["category_id"]
category["category_id"] = self.content_uploader.new_subcategory(category)
subcategories_prompt = f"""
Escribe como un Experto SEO
{self.content_size} Subcategorias relevantes para la categoria de "{category["nombre"]}" de la seccion de "{section}" de una {self.store}.
En formato de lista de python. solo la lista de python.
siguiendo el siguiente formato: [subcategoria1, subcategoria2]
"""
subcategories_response = get_completion(subcategories_prompt)
subcategories_list = eval(subcategories_response)
category["subcategorias"] = []
for subcategory in subcategories_list:
new_subcategory = {
"nombre": subcategory,
"parent_id": category["category_id"]
}
new_subcategory["category_id"] = self.content_uploader.new_subcategory(new_subcategory)
category["subcategorias"].append(new_subcategory)
print(idx+1, "subcategory set")
idx += 1
category_idx += 1
self.checkpoint["set_subcategories"]["category_idx"] = category_idx
self.checkpoint["set_subcategories"]["idx"] = idx
self.save_checkpoint()
self.save_content()
category_idx = 0
self.checkpoint["set_subcategories"]["category_idx"] = category_idx
self.save_checkpoint()
section_idx += 1
self.checkpoint["set_subcategories"]["section_idx"] = section_idx
self.save_checkpoint()
print("Completed ✓\n")
def set_products(self):
print("\n-------PRODUCTS SET-------")
category_idx = self.checkpoint["set_products"]["category_idx"]
subcategory_idx = self.checkpoint["set_products"]["subcategory_idx"]
idx = self.checkpoint["set_products"]["idx"]
if self.content['menu']['productos']:
for category in self.content['menu']['productos']['categorias'][category_idx:]:
for subcategory in category['subcategorias'][subcategory_idx:]:
products_prompt = f"""
Escribe como un Experto en la Busqueda de {self.products} en Amazon.
{self.content_size*2} productos relevantes para la subcategoria de "{subcategory["nombre"]}" de la categoria de "{category["nombre"]}" de la seccion de "productos" de una {self.store}.
Asegurate de que sean productos que puedan encontrarse en Amazon.
En formato de lista de python. solo la lista de python.
siguiendo el siguiente formato: ["producto1", "producto2"]
"""
products_response = get_completion(products_prompt)
products_list = eval(products_response)
subcategory["productos"] = []
for product in products_list:
subcategory["productos"].append({
"nombre": product,
})
print(idx+1, "subcategory products set")
idx += 1
subcategory_idx += 1
self.checkpoint["set_products"]["subcategory_idx"] = subcategory_idx
self.checkpoint["set_products"]["idx"] = idx
self.save_checkpoint()
self.save_content()
subcategory_idx = 0
self.checkpoint["set_products"]["subcategory_idx"] = subcategory_idx
self.save_checkpoint()
category_idx += 1
self.checkpoint["set_products"]["category_idx"] = category_idx
self.save_checkpoint()
print("Completed ✓\n")
def set_product_reviews(self):
print("\n-------PRODUCT REVIEWS SET-------")
category_idx = self.checkpoint["set_product_reviews"]["category_idx"]
subcategory_idx = self.checkpoint["set_product_reviews"]["subcategory_idx"]
product_idx = self.checkpoint["set_product_reviews"]["product_idx"]
idx = self.checkpoint["set_product_reviews"]["idx"]
if self.content['menu']['productos'] and not self.checkpoint["set_product_reviews"]["set"]:
scraper = AmazonProductScraper(CHROME_DRIVER_PATH, USERNAME, PASSWORD)
scraper.login()
for category in self.content['menu']['productos']['categorias'][category_idx:]:
for subcategory in category['subcategorias'][subcategory_idx:]:
for product in subcategory["productos"][product_idx:]:
download_image_path = f"product_images/{category['nombre']}/{subcategory['nombre']}/"
create_path_if_not_exists(download_image_path)
title, ref_url, image_path = scraper.search_product(product["nombre"], download_image_path)
product["titulo"] = title
product["ref_url"] = ref_url
product["image_path"] = image_path
scraper.process_images(download_image_path)
if product["titulo"]:
review_prompt = """
Escribe como un Experto en Ventas Online.
una reseña relevante para el producto "%s" de la subcategoria de "%s" de la categoria de "%s" de la seccion de "productos" de una %s (2100 palabras).
En formato JSON. solo el JSON.
siguiendo el siguiente formato:
{
"titulo": titulo,
"meta-descripcion": meta descripcion,
"contenido": contenido de la reseña en formato HTML,
"llamada a la accion": llamada a la accion con boton de comprar con la url: "%s" en formato HTML
}
""" % (product["titulo"], subcategory["nombre"], category["nombre"], self.store, product["ref_url"])
review_response = get_completion(review_prompt)
review = json.loads(review_response)
product["reseña"] = review
if product["image_path"]:
product["image_id"] = self.content_uploader.upload_image(product["image_path"])
self.content_uploader.new_product_post(product, subcategory["category_id"])
print(idx + 1, "product review set")
idx += 1
product_idx += 1
self.checkpoint["set_product_reviews"]["idx"] = idx
self.checkpoint["set_product_reviews"]["product_idx"] = product_idx
self.save_checkpoint()
self.save_content()
product_idx = 0
self.checkpoint["set_product_reviews"]["product_idx"] = product_idx
self.save_checkpoint()
subcategory_idx += 1
self.checkpoint["set_product_reviews"]["subcategory_idx"] = subcategory_idx
self.checkpoint["set_product_reviews"]["idx"] = idx
self.save_checkpoint()
subcategory_idx = 0
self.checkpoint["set_product_reviews"]["subcategory_idx"] = subcategory_idx
self.save_checkpoint()
category_idx += 1
self.checkpoint["set_product_reviews"]["category_idx"] = category_idx
self.save_checkpoint()
self.checkpoint["set_product_reviews"]["set"] = 1
self.save_checkpoint()
print("Completed ✓\n")
def set_products_articles(self):
print("\n-------PRODUCTS ARTICLES SET-------")
category_idx = self.checkpoint["set_products_articles"]["category_idx"]
subcategory_idx = self.checkpoint["set_products_articles"]["subcategory_idx"]
idx = self.checkpoint["set_products_articles"]["idx"]
if self.content['menu']['productos']:
for category in self.content['menu']['productos']['categorias'][category_idx:]:
for subcategory in category['subcategorias'][subcategory_idx:]:
article_prompt = """
Escribe como un Experto SEO.
un articulo relevante para la subcategoria de "%s" de la categoria de "%s" de la seccion de "productos" de una %s (2100 palabras).
En formato JSON. solo el JSON.
siguiendo el siguiente formato:
{
"titulo": nombre del titulo,
"meta-descripcion": meta descripcion,
"ventajas": 7 ventajas desarrolladas de consumir "%s". en formato HTML,
"preguntas-frecuentes": 7 preguntas frecuentes con sus respuestas antes de comprar un producto de "%s" en formato HTML,
}
""" % (subcategory["nombre"], category["nombre"], self.store, subcategory["nombre"], subcategory["nombre"])
article_response = get_completion(article_prompt)
article = json.loads(article_response)
subcategory["articulo"] = article
self.content_uploader.new_page_with_gallery(subcategory["nombre"], subcategory["productos"], subcategory["articulo"], subcategory["parent_id"])
print(idx + 1, "products article set")
idx += 1
subcategory_idx += 1
self.checkpoint["set_products_articles"]["subcategory_idx"] = subcategory_idx
self.checkpoint["set_products_articles"]["idx"] = idx
self.save_checkpoint()
self.save_content()
subcategory_idx = 0
self.checkpoint["set_products_articles"]["subcategory_idx"] = subcategory_idx
self.save_checkpoint()
category_idx += 1
self.checkpoint["set_products_articles"]["category_idx"] = category_idx
self.save_checkpoint()
print("Completed ✓\n")
def set_blog(self):
print("\n-------BLOG SET-------")
category_idx = self.checkpoint["set_blog"]["category_idx"]
subcategory_idx = self.checkpoint["set_blog"]["subcategory_idx"]
idx = self.checkpoint["set_blog"]["idx"]
if self.content['menu']['blog']:
for category in self.content['menu']['blog']['categorias'][category_idx:]:
for subcategory in category['subcategorias'][subcategory_idx:]:
topics_prompt = f"""
Escribe como un Experto en SEO.
{self.content_size} temas relevantes para la subcategoria de "{subcategory["nombre"]}" de la categoria de "{category["nombre"]}" de la seccion de "blog" de una {self.store}.
En formato de lista de python. solo la lista de python.
siguiendo el siguiente formato: [tema1, tema2]
"""
topics_response = get_completion(topics_prompt)
topics_list = eval(topics_response)
subcategory["temas"] = []
for topic in topics_list:
subcategory["temas"].append({
"nombre": topic
})
print(idx + 1, "subcategory topics set")
idx += 1
subcategory_idx += 1
self.checkpoint["set_blog"]["subcategory_idx"] = subcategory_idx
self.checkpoint["set_blog"]["idx"] = idx
self.save_checkpoint()
self.save_content()
subcategory_idx = 0
self.checkpoint["set_blog"]["subcategory_idx"] = subcategory_idx
self.save_checkpoint()
category_idx += 1
self.checkpoint["set_blog"]["category_idx"] = category_idx
self.save_checkpoint()
print("Completed ✓\n")
def set_blog_articles(self):
print("\n-------BLOG ARTICLES SET-------")
category_idx = self.checkpoint["set_blog_articles"]["category_idx"]
subcategory_idx = self.checkpoint["set_blog_articles"]["subcategory_idx"]
topic_idx = self.checkpoint["set_blog_articles"]["topic_idx"]
idx = self.checkpoint["set_blog_articles"]["idx"]
if self.content['menu']['blog']:
scraper = AmazonProductScraper(CHROME_DRIVER_PATH, USERNAME, PASSWORD)
for category in self.content['menu']['blog']['categorias'][category_idx:]:
for subcategory in category['subcategorias'][subcategory_idx:]:
for topic in subcategory["temas"][topic_idx:]:
download_image_path = f"post_images/{category['nombre']}/{subcategory['nombre']}/"
create_path_if_not_exists(download_image_path)
image_path = scraper.get_post_image(topic["nombre"], download_image_path)
topic["image_path"] = image_path
scraper.process_images(download_image_path)
article_prompt = """
Escribe como un Experto en SEO.
un articulo relevante para el tema de "%s" de la subcategoria de "%s" de la categoria de "%s" de la seccion de "blog" de una %s (2100 palabras).
En formato JSON. solo el JSON.
siguiendo el siguiente formato:
{
"titulo": nombre del titulo,
"meta-descripcion": meta descripcion,
"contenido": contenido del articulo en formato HTML,
}
""" % (topic["nombre"], subcategory["nombre"], category["nombre"], self.store)
article_response = get_completion(article_prompt)
article = json.loads(article_response)
topic["articulo"] = article
if topic["image_path"]:
topic["image_id"] = self.content_uploader.upload_image(topic["image_path"])
self.content_uploader.new_blog_post(topic, subcategory["category_id"])
print(idx + 1, "topic article set")
idx += 1
topic_idx += 1
self.checkpoint["set_blog_articles"]["idx"] = idx
self.checkpoint["set_blog_articles"]["topic_idx"] = topic_idx
self.save_checkpoint()
self.save_content()
topic_idx = 0
self.checkpoint["set_blog_articles"]["topic_idx"] = topic_idx
self.save_checkpoint()
subcategory_idx += 1
self.checkpoint["set_blog_articles"]["subcategory_idx"] = subcategory_idx
self.save_checkpoint()
subcategory_idx = 0
self.checkpoint["set_blog_articles"]["subcategory_idx"] = subcategory_idx
self.save_checkpoint()
category_idx += 1
self.checkpoint["set_blog_articles"]["category_idx"] = category_idx
self.save_checkpoint()
print("Completed ✓\n")
def set_homepage(self):
print("\n-------HOMEPAGE SET-------")
if not self.checkpoint["home_page_set"]:
homepage_prompt = """
Escribe como un Experto SEO
un articulo relevante e interesante sobre %s en general (3500 palabras).
En formato JSON. solo el JSON.
siguiendo el siguiente formato:
{
"titulo": nombre del titulo,
"meta-descripcion": meta descripcion,
"contenido": contenido del articulo en formato HTML,
}
""" % self.products
homepage_response = get_completion(homepage_prompt)
homepage_dict = json.loads(homepage_response)
self.content["menu"]["inicio"]["articulo"] = homepage_dict
self.content_uploader.new_page(self.content["menu"]["inicio"]["articulo"])
self.checkpoint["home_page_set"] = 1
self.save_checkpoint()
self.save_content()
print("Completed ✓\n")
def get_current_content(self):
return self.content
def load_checkpoint(self):
try:
with open('checkpoint.json', 'r') as file:
self.checkpoint = json.load(file)
except FileNotFoundError:
self.save_checkpoint()
def save_checkpoint(self):
with open('checkpoint.json', 'w') as file:
json.dump(self.checkpoint, file)
def load_content(self):
try:
with open('content.json', 'r') as file:
self.content = json.load(file)
except FileNotFoundError:
self.save_content()
def save_content(self):
with open('content.json', 'w') as file:
json.dump(self.content, file)