-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_blog_post_top_hackernews_post.py
80 lines (64 loc) · 2.44 KB
/
write_blog_post_top_hackernews_post.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
import requests
from bs4 import BeautifulSoup
import openai
from datetime import datetime
import os
import boto3
# Replace with your OpenAI API key
openai.api_key = os.environ.get("OPENAI_KEY")
# Get the first link from the 'Best' page on Hacker News
url = "https://news.ycombinator.com/best"
response = requests.get(url)
soup = BeautifulSoup(response.text, "lxml")
first_link = soup.find("span", class_="titleline").find("a")["href"]
# Visit the first link and extract text
response = requests.get(first_link)
soup = BeautifulSoup(response.text, "lxml")
text = ' '.join([p.get_text() for p in soup.find_all("p")])
# Send the text to ChatGPT via the OpenAI API to generate a one-word name for the blog post
response = openai.Completion.create(
engine="text-davinci-002",
prompt=f"Generate a one-word name for a blog post based on the following information:\n\n{text}",
max_tokens=20,
n=1,
stop=None,
temperature=0.5,
)
# Extract the blog post name
blog_post_name = response.choices[0].text.strip().replace(" ", "_")
# Create a "posts" directory if it doesn't already exist
os.makedirs("posts", exist_ok=True)
# Save the blog post to a Markdown file with today's date, the word, and an increment in the title
today = datetime.now().strftime("%Y-%m-%d")
increment = 1
while True:
filename = f"{today}_{blog_post_name}_{increment}.md"
file_path = os.path.join("posts", filename)
if not os.path.exists(file_path):
break
increment += 1
# Send the text to ChatGPT via the OpenAI API to generate a blog post with strong opinions
response = openai.Completion.create(
engine="text-davinci-002",
prompt=f"Write a 500-word blog post with strong opinions discussing the following information:\n\n{text}",
max_tokens=800, # Increasing max_tokens to accommodate a longer blog post
n=1,
stop=None,
temperature=0.7,
)
# Extract the blog post
blog_post = response.choices[0].text.strip()
with open(file_path, "w", encoding="utf-8") as file:
file.write(blog_post)
print(f"Blog post saved to {file_path}")
# Save the blog post to an S3 bucket
s3 = boto3.client(
's3',
aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY'),
)
s3_bucket = os.environ.get('S3_BUCKET')
s3_key = f"{filename}"
with open(file_path, "rb") as file:
s3.upload_fileobj(file, s3_bucket, s3_key)
print(f"Blog post uploaded to S3 bucket '{s3_bucket}' with key '{s3_key}'")