-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewsletterCreator.py
107 lines (83 loc) · 3.12 KB
/
NewsletterCreator.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
import requests
import openai
from bs4 import BeautifulSoup
import json
def search_news(topic):
# Step 1: Create a Python script for news search
url = f"https://www.google.com/search?q={topic}&tbm=nws"
response = requests.get(url)
response.raise_for_status() # Check for any request errors
soup = BeautifulSoup(response.text, "html.parser")
articles = []
for article in soup.find_all("div", class_="g"):
title = article.find("h3").text
link = article.find("a")["href"]
description = article.find("div", class_="s").text
articles.append({"title": title, "link": link, "description": description})
return articles
def save_news_to_file(articles):
with open("news.json", "w") as file:
json.dump(articles, file)
def generate_news_summary(articles):
openai.api_key = 'YOUR_CHAT_GPT_API_KEY'
summaries = []
for article in articles:
prompt = f"Summarize the news article:\nTitle: {article['title']}\nDescription: {article['description']}"
response = openai.Completion.create(
engine='text-davinci-003',
prompt=prompt,
max_tokens=100,
temperature=0.7,
n=1,
stop=None,
)
summary = response.choices[0].text.strip()
summaries.append(summary)
return summaries
def generate_news_image(summary):
openai.api_key = 'YOUR_STABLE_DIFFUSION_API_KEY'
response = openai.Image.generate(
prompt=summary,
max_width=800,
max_height=600,
)
return response.url
def send_newsletter_email(recipients, sender, subject, content):
mailgun_api_key = 'YOUR_MAILGUN_API_KEY'
mailgun_domain = 'YOUR_MAILGUN_DOMAIN'
endpoint = f"https://api.mailgun.net/v3/{mailgun_domain}/messages"
data = {
'from': sender,
'to': recipients,
'subject': subject,
'html': content
}
response = requests.post(
endpoint,
auth=('api', mailgun_api_key),
data=data
)
return response.status_code
def main():
# Step 1: Create a Python script for news search
articles = search_news("your_topic_here")
save_news_to_file(articles)
# Step 2: Generate a conversational news summary using Chat GPT
generated_summaries = generate_news_summary(articles)
# Step 3: Create the newsletter content using Stable Diffusion API and ChatGPT-4
enhanced_content = "<h1>Newsletter Content</h1>"
for summary in generated_summaries:
image_url = generate_news_image(summary)
enhanced_content += f'<img src="{image_url}">'
enhanced_content += f'<p>{summary}</p>'
# Step 4: Distribute the newsletter using Mailgun API
recipients = ['recipient1@example.com', 'recipient2@example.com']
sender = 'sender@example.com'
subject = 'Your Newsletter Subject'
response_code = send_newsletter_email(recipients, sender, subject, enhanced_content)
if response_code == 200:
print('Newsletter email sent successfully!')
else:
print(f'Failed to send newsletter email. Status code: {response_code}')
if __name__ == '__main__':
main()