DEV Community

Cover image for Day 15 - Display Random Reddit posts - 100 days 100 python scripts
Ganesh Raja
Ganesh Raja

Posted on

Day 15 - Display Random Reddit posts - 100 days 100 python scripts

Day 15: notify_random_reddit

This script will randomly pick a sub_reddit from the list of your favs and randomly pick one of the top posts and notify it. You can add it to cron and get random posts hourly.

import json,requests
from random import randint,choice
import os,constants

subreddit_list= constants.fav_subreddit_list # List of favorite sub reddit name

subreddit=choice(subreddit_list)

url=r"https://json.reddit.com/r/"+subreddit+r"/top/?sort=top&t=day"

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
data=json.loads(requests.get(url,headers=headers).text)

response_data=data["data"]
count= int(response_data["dist"])


random_post=(response_data["children"][randint(0,count)]["data"])

text=random_post["selftext"]
subreddit=random_post["subreddit"].title()

os.system('notify-send "'+subreddit+'" "'+text+'"')
Enter fullscreen mode Exit fullscreen mode

Please Visit my Git Repo to check out all the previous day challenges.

https://github.com/ganeshraja10/automated-python-scripts

Top comments (0)