This Python script demonstrates interaction with the Reddit API to fetch the latest posts from a specified subreddit using PRAW (Python Reddit API Wrapper).
- OAuth authentication with Reddit API
- Fetches 5 latest posts from a specified subreddit
- Displays post title, author, and upvote count
- Includes error handling and logging
- Uses environment variables for secure credential management
- Python 3.7 or higher
- pip (Python package installer)
-
Clone this repository or download the script files
-
Install required dependencies:
pip install praw python-dotenv
- Create a
.env
file in the same directory as the script with your Reddit API credentials:
REDDIT_CLIENT_ID=your_client_id
REDDIT_CLIENT_SECRET=your_client_secret
REDDIT_USER_AGENT=Python/PRAW Latest Posts Fetcher v1.0
To obtain Reddit API credentials:
- Visit https://www.reddit.com/prefs/apps
- Click "create application"
- Select "script"
- Fill in the required information
- Once created, you'll get the client_id and client_secret
- Create a
.env
file:- Copy
.env.example
to.env
- Fill in your Reddit API credentials
- Copy
Run the script using Python:
python main.py
- Fetches the 5 latest posts from a specified subreddit
- Displays post title, author, and upvote count
- Includes error handling and logging
- Uses environment variables for secure credential management
The script includes comprehensive error handling for:
- API authentication failures
- Network errors
- Invalid subreddit names
- General exceptions
Logs are printed to the console with timestamps and appropriate log levels.