Skip to content

Instantly share code, notes, and snippets.

@jmcerrejon
Created November 9, 2024 18:34
Show Gist options
  • Save jmcerrejon/d95249e805fc7a35046596dd28a490f5 to your computer and use it in GitHub Desktop.
Save jmcerrejon/d95249e805fc7a35046596dd28a490f5 to your computer and use it in GitHub Desktop.
Just output to console latest Python Reddit posts
from typing import List, Tuple
import requests
def get_reddit_posts(subreddit: str) -> List[Tuple[str, str]]:
url = f"https://www.reddit.com/r/{subreddit}/new.json"
headers = {"User-agent": "Mozilla/5.0"}
response = requests.get(url, headers=headers)
data = response.json()
return [
(post.get("data", {}).get("title"), post.get("data", {}).get("url"))
for post in data.get("data", {}).get("children", [])
]
if __name__ == "__main__":
for title, url in get_reddit_posts("python"):
print(f"⇴ {title} - {url}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment