-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.py
320 lines (267 loc) · 9.97 KB
/
main.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
import os
import sys
import json
from dotenv import load_dotenv
import openai
import toml
from halo import Halo
import colorama
from termcolor import colored
import time
from colorama import Fore, Style
colorama.init(autoreset=True)
from pyfiglet import Figlet
from config import Config
from initiation import kickoff
import articles.writing
from articles import skeleton
from articles import writing
from researcher import search
from orchestrar import gutenberg
from orchestrar import wp
from orchestrar import blogs
from enhancer import midjourney_ai
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
# Initialize colorama
colorama.init(autoreset=True)
# Load the .env file
load_dotenv()
# Configure OpenAI API key
cfg = Config()
try:
openai.api_key = cfg.openai_api_key
except KeyError:
sys.stderr.write("OpenAI key configuration failed.")
exit(1)
# Global variables for article content
article_content = ""
article_title = ""
article_description = ""
findings = ""
wp_admin = ""
wp_pass = ""
wp_url = ""
published_articles = ""
tone = ""
keywords = ""
def process_section(section, level=2, max_depth=5):
"""
Process a section or subsection of the article.
"""
section_type = 'section' if level == 2 else 'subsection'
section_content = ""
spinner = Halo(text=f'Processing {section_type}: {section[f"Heading_H{level}"]}', spinner='dots')
spinner.start()
# Write section or subsection
section_content = articles.writing.write_section(article_title,
article_description,
section[f'Heading_H{level}'],
section['Description'],
tone)
spinner.succeed(f"Finished processing {section_type}: {section[f'Heading_H{level}']}")
# Process subsections if they exist and the maximum depth has not been reached
if 'SubSections' in section and level < max_depth:
for sub_section in section['SubSections']:
section_content += process_section(sub_section, level + 1)
return "\n\n" + f"<h{level}>" + section[f'Heading_H{level}'] + f"</h{level}>" + "\n\n" + section_content
def process_json(json_string):
"""
Process the JSON string to generate the article content.
"""
global article_content
global article_title # Declare article_title as global
spinner = Halo(text='Parsing JSON', spinner='dots')
spinner.start()
data = json.loads(json_string)
article_title = data['Title'] # This now refers to the global variable
if findings.strip():
article_description = data['Description'] + " " +f"""{findings}"""
#print("\n\n\n\nArticle_description: ", article_description)
#print("\n\n\n\n")
else:
article_description = data['Description']
spinner.succeed('Finished parsing JSON')
# Add the intro to the article content
article_content += writing.write_intro(article_title, tone) + "\n\n"
for section in data['Sections']:
article_content += process_section(section)
return article_content
def is_json(json_string):
"""
Check if a string is valid JSON.
"""
try:
json.loads(json_string)
except ValueError:
return False
return True
def wait_for_image(image_name):
"""
Function to wait for the image file to appear in the specified directory
"""
# Define the base directory
base_dir = "./temp/imgs/"
# Use os.path.join to create the full path
image_path = os.path.join(base_dir, image_name)
print(f"Looking for image at: {image_path}")
# Keep checking for the image file until it appears
while True:
try:
if os.path.isfile(image_path):
print("Image file found.")
break
else:
print("Image file not found. Waiting...")
time.sleep(5) # wait for 5 seconds before checking again
except Exception as e:
print(f"Error while checking for image: {e}")
break
def article(title=None, category=None):
"""
Main function to generate the article.
"""
global findings
global article_content
global article_title
# Reset article_content
article_content = ""
if title is None:
title = input("Please enter the article title: ")
else:
print(f"Article Title: {title}")
if category is None:
category = input("Please enter the article category: ")
else:
print(f"Article Category: {category}")
# RESEARCH
if title is not None and category is not None:
research = 'y'
else:
research = input("Do you want me to research the internet? (y/n): ")
if research == 'y':
search_results = search.go(title)
findings = f"""This is additional info from user you need to incorporate: {search_results}"""
print(colored("\n" + "################### RESEARCH FINISHED ###################\n", "green", attrs=["bold"]))
# ARTICLE TYPE
if title is not None and category is not None:
article_type = 'a'
else:
article_type = input("Do you want Article or Product Review? (a/p): ")
spinner = Halo(text='Preparing Structure of Article', spinner='dots')
spinner.start()
article_skeleton = ""
while not is_json(article_skeleton):
try:
if article_type == 'a':
article_skeleton = skeleton.write_skeleton(title)
elif article_type == 'p':
article_skeleton = skeleton.write_skeleton_product_review(title)
except Exception as e:
spinner.fail(str(e))
else:
spinner.succeed("Finished writing the article skeleton")
# PROCESS SECTIONS
try:
article_content += process_json(article_skeleton)
except Exception as e:
spinner.fail(str(e))
else:
spinner.succeed("Finished processing JSON")
print(colored("\n" + "################### ARTICLE GENERATED ###################", "green",
attrs=["bold"]))
# SAVE TO TXT FILE
if title is not None and category is not None:
save_to_txt = 'y'
else:
save_to_txt = input("Do you want to save this article to a txt file? (y/n): ")
if save_to_txt == 'y':
with open(f"./temp/articles/{title}.txt", "w") as file:
file.write(article_content)
print(colored("\nArticle saved to txt file.", "green", attrs=["bold"]))
# GENERATE IMAGES
featured_image_name = midjourney_ai.generate_image(title)
# Wait for the image file to appear
wait_for_image(featured_image_name + "_small.jpg")
# Define the base directory
base_dir = "/temp/imgs"
# Use os.path.join to create the full path
featured_image_path = os.path.join(base_dir, featured_image_name + "_small.jpg")
featured_image_path = "." + featured_image_path
# WORDPRESS IMPORT
if title is not None and category is not None:
wp_import = 'y'
else:
wp_import = input("Do you want to import this article to WordPress? (y/n): ")
if wp_import == 'y':
print(colored("\n" + "################### WORDPRESS IMPORT ###################", "green",
attrs=["bold"]))
spinner = Halo(text='Preparing article for WordPress import', spinner='dots')
spinner.start()
try:
to_wordpress = gutenberg.convert_to_gutenberg_blocks(article_content)
tags = [category]
wp.post_to_wordpress(article_title, to_wordpress, category, tags, featured_image_path, wp_admin, wp_pass, wp_url)
except Exception as e:
spinner.fail(str(e))
else:
spinner.succeed("Article imported to WordPress\n\n")
def parse_blog_articles():
data = json.loads(get_blog_details())
for item in data:
#print(f'Category: {item["Category"]}, Title: {item["Title"]}')
article(item["Title"], item["Category"])
def get_blog_details():
global wp_admin
global wp_pass
global wp_url
global tone
global keywords
data = toml.load("blogs/blogs.toml")
# print all the blogs and ask user to choose one
print("List of blogs:")
for i, blog in enumerate(data["blog"], start=1):
print(f"{i}. {blog['name']}")
# ask user for blog number
blog_number = int(input("\nEnter the number of the blog: "))
# get the chosen blog
chosen_blog = data["blog"][blog_number - 1]
# get the WP admin username and password from the .env file
wp_admin = os.getenv(f"WP_ADMIN_USERNAME_{chosen_blog['id']}")
wp_pass = os.getenv(f"WP_ADMIN_PASSWORD_{chosen_blog['id']}")
wp_url = os.getenv(f"WP_URL_{chosen_blog['id']}")
print(f"\nBlog Name: {chosen_blog['name']}")
print(f"Description: {chosen_blog['description']}")
print(f"URL: {chosen_blog['url']}")
print(f"Tone: {chosen_blog['tone']}")
print(f"Keywords: {chosen_blog['keywords']}")
#print(f"WordPress Admin: {wp_admin}")
#print(f"WordPress Password: {wp_pass}")
print("\n")
tone = chosen_blog['tone']
keywords = chosen_blog['keywords']
# get the published articles from the blog
spinner = Halo(text='Loading existing articles...', spinner='dots')
spinner.start()
try:
published_articles = wp.get_all_posts_titles(chosen_blog['url'])
except Exception as e:
spinner.fail(str(e))
else:
spinner.succeed("Articles loaded")
# generate the JSON for the articles
spinner = Halo(text='Generating articles JSON', spinner='dots')
spinner.start()
try:
json_articles = blogs.init_blog(chosen_blog['description'],published_articles, tone, keywords)
except Exception as e:
spinner.fail(str(e))
else:
spinner.succeed("JSON Generated")
print(json_articles)
return json_articles
if __name__ == "__main__":
f = Figlet(font='big', width=300)
print(Fore.CYAN + Style.BRIGHT + f.renderText('AI BLOG PILOT'))
#article()
parse_blog_articles()