Created
November 9, 2024 18:34
-
-
Save jmcerrejon/d95249e805fc7a35046596dd28a490f5 to your computer and use it in GitHub Desktop.
Just output to console latest Python Reddit posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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