-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from nhuzaa/main
Hackernews Email Newsletter
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Hacker News Headlines Emailer | ||
|
||
This Python script scrapes the latest headlines from Hacker News and sends them via email. It can be used to receive the latest news updates from Hacker News in your email inbox. | ||
|
||
## Prerequisites | ||
|
||
Before using the script, ensure you have the following installed: | ||
|
||
- Python 3.x | ||
- Required Python libraries (you can install them using `pip`): | ||
- `requests` | ||
- `beautifulsoup4` | ||
|
||
## Usage | ||
|
||
1. Clone or download this repository to your local machine. | ||
|
||
2. Modify the script to include your email and SMTP server credentials: | ||
- Replace `smtp.example.com` with your SMTP server. | ||
- Replace `'your_email@example.com'` with your email. | ||
- Replace `'your_password'` with your email password. | ||
|
||
3. Run the script: | ||
|
||
```bash | ||
python hackernews_emailer.py | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import requests | ||
from bs4 import BeautifulSoup | ||
import smtplib | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
|
||
# Function to get the latest Hacker News headlines | ||
def get_hacker_news_headlines(): | ||
url = 'https://news.ycombinator.com' | ||
response = requests.get(url) | ||
soup = BeautifulSoup(response.text, 'html.parser') | ||
headlines = [] | ||
|
||
for item in soup.find_all('a', class_='storylink'): | ||
headlines.append(item.text) | ||
|
||
return headlines | ||
|
||
# Function to send an email | ||
def send_email(subject, body, to_email): | ||
smtp_server = 'smtp.example.com' # Replace with your SMTP server | ||
smtp_port = 587 | ||
smtp_user = 'your_email@example.com' # Replace with your email | ||
smtp_password = 'your_password' # Replace with your email password | ||
|
||
msg = MIMEMultipart() | ||
msg['From'] = smtp_user | ||
msg['To'] = to_email | ||
msg['Subject'] = subject | ||
msg.attach(MIMEText(body, 'plain')) | ||
|
||
try: | ||
server = smtplib.SMTP(smtp_server, smtp_port) | ||
server.starttls() | ||
server.login(smtp_user, smtp_password) | ||
server.sendmail(smtp_user, to_email, msg.as_string()) | ||
server.quit() | ||
print('Email sent successfully') | ||
except Exception as e: | ||
print('Email could not be sent. Error:', str(e)) | ||
|
||
# Main function | ||
if __name__ == '__main__': | ||
# Get the latest Hacker News headlines | ||
headlines = get_hacker_news_headlines() | ||
|
||
# Construct the email message | ||
subject = 'Latest Hacker News Headlines' | ||
body = '\n'.join(headlines) | ||
|
||
# Replace with your recipient's email address | ||
recipient_email = 'recipient@example.com' | ||
|
||
# Send the email | ||
send_email(subject, body, recipient_email) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
requests==2.26.0 | ||
beautifulsoup4==4.9.3 | ||
|