Skip to content

Commit

Permalink
Merge pull request #284 from nhuzaa/main
Browse files Browse the repository at this point in the history
Hackernews  Email Newsletter
  • Loading branch information
Techiral authored Jul 12, 2024
2 parents 0886345 + b316ed6 commit 911e627
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
27 changes: 27 additions & 0 deletions H/hackernews/README.md
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

56 changes: 56 additions & 0 deletions H/hackernews/main.py
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)

3 changes: 3 additions & 0 deletions H/hackernews/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
requests==2.26.0
beautifulsoup4==4.9.3

0 comments on commit 911e627

Please sign in to comment.