Commit af5ad07a authored by Benavides Ontiveros's avatar Benavides Ontiveros
Browse files

basic reddit fetch using praw library

parent 4d55a5dd
REDDIT_CLIENT_ID=""
REDDIT_CLIENT_SECRET=""
\ No newline at end of file
import praw
import json
import os
from dotenv import load_dotenv
# Reddit API credentials
# --- Load environment variables ---
load_dotenv()
# --- Reddit API credentials ---
reddit = praw.Reddit(
client_id="",
client_secret="",
user_agent="MyRedditApp/0.1 by DevTest0"
client_id=os.getenv("REDDIT_CLIENT_ID"),
client_secret=os.getenv("REDDIT_CLIENT_SECRET"),
user_agent="MyRedditScraper/1.0"
)
# Choose your subreddit
subreddit = reddit.subreddit("soccer")
# Get a subreddit object (e.g., "python")
subreddit = reddit.subreddit('python')
# Extract some useful data into a dictionary
subreddit_data = {
"id": subreddit.id,
"display_name": subreddit.display_name,
"title": subreddit.title,
"description": subreddit.public_description,
"subscribers": subreddit.subscribers,
"created_utc": subreddit.created_utc,
"over18": subreddit.over18,
"url": subreddit.url,
# Add more fields if you want
}
# Convert to JSON string
json_data = json.dumps(subreddit_data, indent=4)
print(json_data)
# Fetch top 5 hot posts
for post in subreddit.hot(limit=2):
print(f"\nPost Title: {post.title}")
print(f"URL: {post.url}")
print("Comments:")
# Load all comments
post.comments.replace_more(limit=0) # This removes "MoreComments" placeholders
for comment in post.comments[:5]: # Limit to top 5 comments
print(f"- {comment.body[:200]}") # Print first 100 characters of comment
# --- Choose subreddit and fetch posts ---
subreddit_name = "news"
subreddit = reddit.subreddit(subreddit_name)
data = []
for post in subreddit.hot(limit=3): # get top 3 hot posts
post.comments.replace_more(limit=0) # remove 'MoreComments' objects
post_data = {
"id": post.id,
"title": post.title,
"score": post.score,
"url": post.url,
"num_comments": post.num_comments,
"created_utc": post.created_utc,
"comments": []
}
# Collect top-level comments
for comment in post.comments.list()[:10]: # limit to first 10 comments
post_data["comments"].append({
"id": comment.id,
"body": comment.body,
"score": comment.score,
"created_utc": comment.created_utc
})
data.append(post_data)
# --- Convert to JSON string ---
json_output = json.dumps(data, indent=4)
# --- Print or save ---
print(json_output)
# Optionally, save to file:
with open("reddit_data.json", "w") as f:
json.dump(data, f, indent=4)
[
{
"id": "1oguyfk",
"title": "US airports report over 20 air traffic controller shortage triggers in one day | US news",
"score": 14619,
"url": "https://www.theguardian.com/us-news/2025/oct/26/air-traffic-controller-shortages-shutdown",
"num_comments": 509,
"created_utc": 1761510296.0,
"comments": [
{
"id": "nljcypt",
"body": "Imagine not paying people and expecting them to show up",
"score": 5998,
"created_utc": 1761510677.0
},
{
"id": "nljo64g",
"body": "*Duffy noted air traffic controllers have been taking on second jobs to try to supplement the lost income.*\n\n*\u201cThey\u2019re taking second jobs, they\u2019re out there looking,\u201d he said.*\n\nProbably not the best situation to have air traffic controllers, doing one of the most stressful jobs imaginable and further stressed out by pay issues, to be further burnt out by working a SECOND job.",
"score": 658,
"created_utc": 1761514124.0
},
{
"id": "nljdc5k",
"body": "This is how trump's last shutdown ended. No ATC.",
"score": 2502,
"created_utc": 1761510790.0
},
{
"id": "nlje40k",
"body": "October 28th is supposed to be the first \u201cpaycheck\u201d with $0, after ATCs got a <90% paycheck two weeks ago. I wonder if they\u2019ll even bother to print them; can you even see a direct deposit of $0 on your bank statement?\n\nIt sounds to me like the guardians of pedophiles have about two days left before things get significantly worse. Three days after that, there will be no SNAP payments *and* ACA premiums will skyrocket. I\u2019m sure they have a concept of a solution in the works. Any predictions?\n\nEdit: bank statement, not statue. Also, several people have confirmed they have already seen their $0 pay for an honest two weeks\u2019 work. What a fucking gut punch, my heart goes out to you guys (*not* the Nazi salute type, though).",
"score": 1616,
"created_utc": 1761511020.0
},
{
"id": "nljecje",
"body": "Between this and snap it really feels either cold and calculated or chaotic and experimental.",
"score": 859,
"created_utc": 1761511091.0
},
{
"id": "nljf89r",
"body": "I was actually considering an ATC job a few years back but when I looked into how much it wears you down I passed.",
"score": 327,
"created_utc": 1761511356.0
},
{
"id": "nljg0pb",
"body": "Airline and travel lobbies might be the biggest pressure against the Trump admin.\n\n31m Americans flew for Thanksgiving in 2024, and the average price of a plane ticket is ~$360. That's over $11.1b for flights alone each year for Thanksgiving travel, which doesn't include all the cargo flights every day.",
"score": 287,
"created_utc": 1761511593.0
},
{
"id": "nljdssy",
"body": "It\u2019s a mess today.\n\nLeft RDU for Dallas and ended up in Midland. From Midland to Dallas, Dallas to Albuquerque, and hopefully Albuquerque to Vegas. Who knows where I\u2019ll end up next!",
"score": 324,
"created_utc": 1761510928.0
},
{
"id": "nljosu2",
"body": "Don\u2019t worry, we gave Argentina $20B and will give them $20B more.",
"score": 86,
"created_utc": 1761514332.0
},
{
"id": "nljmgjv",
"body": "How long until they try to use a sycophantic LLM as air traffic control and it gets a bunch of people killed?",
"score": 58,
"created_utc": 1761513571.0
}
]
},
{
"id": "1ogo3kz",
"title": "Food benefits set to expire for 41 million people as US shutdown continues",
"score": 23381,
"url": "https://www.theguardian.com/us-news/2025/oct/26/food-benefits-federal-shutdown",
"num_comments": 2210,
"created_utc": 1761494012.0,
"comments": [
{
"id": "nlhuxsr",
"body": "The crazy thing is we have 12% of America on food assistance.",
"score": 5196,
"created_utc": 1761494872.0
},
{
"id": "nlhtfpo",
"body": "Looks like we'll unfortunately see how quickly public sentiment shifts towards those in power once people start missing meals.\u00a0",
"score": 3960,
"created_utc": 1761494412.0
},
{
"id": "nlhw5lh",
"body": "I\u2019m just reading the book The Jungle by Upton Sinclair. It\u2019s crazy how this book represents so many things today. It was written in 1906. We are heading down a slippery slope.",
"score": 1006,
"created_utc": 1761495244.0
},
{
"id": "nli7f6a",
"body": "Our system is catastrophically\u00a0 broken that 41 million need to be recieving a food benefit.\u00a0 Something is very, very wrong.",
"score": 204,
"created_utc": 1761498650.0
},
{
"id": "nlht4cs",
"body": "Cool, but the Orange has a ballroom now.",
"score": 2293,
"created_utc": 1761494316.0
},
{
"id": "nlih3zh",
"body": "This will affect you even if you do not receive food benefits. That is a significant amount of money to suddenly remove from the economy without causing widespread havoc. \n\nRegardless, the Democrats should not cave.",
"score": 297,
"created_utc": 1761501378.0
},
{
"id": "nlhuofk",
"body": "Every single one of my acquaintances and family members that are on food stamps are Trump-MAGA lovers. Interested to see how this plays out.",
"score": 753,
"created_utc": 1761494791.0
},
{
"id": "nlhv3sd",
"body": "Lots of rural folk that voted Trump are on SNAP, but they will be told to blame Biden, Obama, they may even blame the Hunter Biden penis. But at least Argentina is great again and Trump gets a ballroom and ICE gets cool weapons to use on Americans, and some cool planes.",
"score": 262,
"created_utc": 1761494923.0
},
{
"id": "nli10d8",
"body": "Make America Great ^depression Again",
"score": 75,
"created_utc": 1761496746.0
},
{
"id": "nlhshks",
"body": "Working as intended. DOGE 2.0.",
"score": 552,
"created_utc": 1761494126.0
}
]
},
{
"id": "1ogt27v",
"title": "Trump administration posts notice that no federal food aid will go out Nov. 1",
"score": 3164,
"url": "https://apnews.com/article/food-aid-government-shutdown-snap-trump-democrats-8a52a63b26a707ea676962226b090bb1",
"num_comments": 340,
"created_utc": 1761505740.0,
"comments": [
{
"id": "nljudbt",
"body": "There is a 5 billion dollar emergency fund. \n\n[https://www.politico.com/news/2025/10/24/snap-food-aid-shutdown-usda-00622690](https://www.politico.com/news/2025/10/24/snap-food-aid-shutdown-usda-00622690)",
"score": 1849,
"created_utc": 1761516092.0
},
{
"id": "nljz03d",
"body": "Democrats should run ads nationally talking about the $200 million SNAP cuts, the $300 million ballroom, $20 billion for Argentina, the new DHS planes, and so on. They should also say how they had agreed to an extension to prevent the shutdown.\n\nIs there anybody in charge at the DNC? How are they fucking up the messaging on this topic?",
"score": 829,
"created_utc": 1761517649.0
},
{
"id": "nljw0gb",
"body": "\u201cBottom line, the well has run dry,\u201d the USDA notice says. \u201cAt this time, there will be no benefits issued November 01. We are approaching an inflection point for Senate Democrats.\u201d\n\nBro, You can't say shit like that. The Republicans hate ACA so fucking bad they're willing to let kids starve. See how that can turn around.",
"score": 910,
"created_utc": 1761516634.0
},
{
"id": "nljv0di",
"body": "Everyone in the US who voted for Trump because he cares about American jobs and making America great again, remember this. He doesn\u2019t care about you. He never did and never will.",
"score": 588,
"created_utc": 1761516302.0
},
{
"id": "nljw9i7",
"body": "Meanwhile, he wants 230 million dollars for his \u201cpain and suffering\u201d experienced when he was being justifiably prosecuted for stealing classified government property.",
"score": 191,
"created_utc": 1761516718.0
},
{
"id": "nljucqh",
"body": "They spent it all on Argentina and the Epstein ballroom.",
"score": 338,
"created_utc": 1761516086.0
},
{
"id": "nljwi9d",
"body": "Meanwhile argentina gets $40 billion dollars, yaddy yada yada. Mericuh first!",
"score": 67,
"created_utc": 1761516798.0
},
{
"id": "nljyr0c",
"body": ">\u201cBottom line, the well has run dry,\u201d the USDA notice says. \u201cAt this time, there will be no benefits issued November 01. We are approaching an inflection point for Senate Democrats.\u201d\n\nIt has been [0] days since the last Hatch Act violation in this administration.",
"score": 80,
"created_utc": 1761517562.0
},
{
"id": "nljukd7",
"body": "\"But I just didn't like her laugh\"",
"score": 183,
"created_utc": 1761516156.0
},
{
"id": "nljw3pc",
"body": "Maybe some poor, white southerners will finally figure out a few brown people cleaning toilets and working in fields aren\u2019t their biggest problem after all. \n\nProbably not though.",
"score": 85,
"created_utc": 1761516665.0
}
]
}
]
\ No newline at end of file
praw==7.7.1
python-dotenv==1.0.1
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment