Created
June 25, 2021 18:57
-
-
Save jftuga/262782df2ce3ed062a7f0724e1f7d0aa to your computer and use it in GitHub Desktop.
reddit asyncpraw example
This file contains 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
import asyncio | |
import asyncpraw | |
import sys | |
class AsPr: | |
def __init__(self, sr: str): | |
self.sr = sr # subreddit | |
self.reddit = asyncpraw.Reddit(client_id=reddit_client_id, client_secret=reddit_client_secret, | |
user_agent=user_agent) | |
async def close(self): | |
await self.reddit.close() | |
async def stream(self): | |
self.subreddit = await self.reddit.subreddit(self.sr) | |
async for submission in self.subreddit.hot(limit=5): | |
print(submission.title) | |
### | |
async def main(): | |
aspr = AsPr("nfl") | |
await aspr.stream() | |
await aspr.close() | |
if "__main__" == __name__: | |
loop = asyncio.get_event_loop() | |
try: | |
loop.run_until_complete(main()) | |
except Exception as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment