#GPT-4 Conversation #1096112054433218671

1 messages · Page 1 of 1 (latest)

sinful stratusBOT
mystic maple
#

Write a Python script that uses the Reddit API to 1. ask for the user to input a Reddit username then 2. use matplotlib and the API to output a line graph showing the user's karma overtime since the account was created to the current day.

sinful stratusBOT
# mystic maple Write a Python script that uses the Reddit API to 1. ask for the user to input a...

To create a Python script that uses the Reddit API to query a user's karma over time and create a matplotlib line graph, you need to follow these steps:

  1. Install the required packages:

    • praw (Python Reddit API Wrapper)
    • matplotlib

    You can do this using pip:

    pip install praw matplotlib
    
  2. Register your script as a Reddit application: https://www.reddit.com/prefs/apps, get the client_id, client_secret, and user-agent.

  3. Replace the placeholders in the following script with the credentials obtained from Step 2.

Here's a sample script:

import datetime
import matplotlib.pyplot as plt
import praw

# Replace the placeholders with your own Reddit application credentials
client_id = 'your_client_id'
client_secret = 'your_client_secret'
user_agent = 'your_user_agent'

reddit = praw.Reddit(client_id=client_id,
                     client_secret=client_secret,
                     user_agent=user_agent)

# Get user input
username = input("Enter a Reddit username: ")

# Fetch user's account
user = reddit.redditor(username)

# Karma tracking variables
karma_by_date = {}
creation_date = datetime.date.fromtimestamp(user.created_utc)
today = datetime.date.today()
current_date = creation_date

while current_date <= today:
    karma_by_date[current_date] = 0
    current_date += datetime.timedelta(days=1)

# Get user's post and comment karma
for submission in user.submissions.new(limit=None):
    date = datetime.date.fromtimestamp(submission.created_utc)
    karma_by_date[date] += submission.score

for comment in user.comments.new(limit=None):
    date = datetime.date.fromtimestamp(comment.created_utc)
    karma_by_date[date] += comment.score

# Graph preparation
dates = sorted(karma_by_date.keys())
karma = [karma_by_date[date] for date in dates]
cumulative_karma = [sum(karma[:i+1]) for i in range(len(karma))]

# Plotting
plt.plot(dates, cumulative_karma)
plt.xlabel('Date')
plt.ylabel('Karma')
plt.title(f"Karma overtime for {usern