Last active
March 23, 2023 18:22
-
-
Save danwalkeruk/7b471a095c645f96456ec2dd3d4bc87f to your computer and use it in GitHub Desktop.
Script to post new subreddit posts to a Discord channel via webhook
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
# reddit-feed.py - posts new subreddit posts to a Discord channel via webhook | |
# author: Dan Walker | |
# email: code@danwalker.com | |
# url: https://danwalker.netlify.app/python-discord-reddit-feed/ | |
# created: 2020-08-13 | |
import requests | |
import json | |
import time | |
from discord_webhook import DiscordWebhook, DiscordEmbed | |
# open the cache, or start from a blank list | |
try: | |
with open('db.json') as json_file: | |
db = json.load(json_file) | |
except FileNotFoundError: | |
db = [] | |
webhook_url = "https://discord.com/api/webhooks/YOUR_WEBHOOK_HERE" | |
subreddit = 'discordapp' # can be chained with + (example: python+webdev) | |
req = requests.get(f'https://www.reddit.com/r/{subreddit}/new/.json', headers={ | |
"Cache-Control": "no-cache", | |
"Pragma": "no-cache", | |
"User-Agent": "discord-feed-bot"}) | |
posts = req.json()['data']['children'] | |
# for each new post found in this request | |
for post in posts: | |
if post['data']['name'] not in db: | |
webhook = DiscordWebhook(url=webhook_url) | |
permalink = f"https://www.reddit.com{post['data']['permalink']}" | |
# create an appropriate embed object | |
if post['data']['thumbnail'] == 'self': # text post | |
embed = DiscordEmbed(title=post['data']['title'], url=permalink, description=post['data']['selftext']) | |
embed.set_footer(text=f"Posted by {post['data']['author']}") | |
elif post['data']['is_video']: # video post | |
embed = DiscordEmbed(title=post['data']['title'], url=permalink) | |
embed.set_image(url=post['data']['thumbnail']) | |
embed.set_footer(text=f"Video posted by {post['data']['author']}") | |
else: # image post | |
embed = DiscordEmbed(title=post['data']['title'], url=permalink) | |
embed.set_image(url=post['data']['url']) | |
embed.set_footer(text=f"Image posted by {post['data']['author']}") | |
# attach the embed to the webhook request and go! | |
webhook.add_embed(embed) | |
webhook.execute() | |
time.sleep(1) # to prevent Discord webhook rate limiting | |
# add post name to DB so we don't display it again | |
db.append(post['data']['name']) | |
# save the cache of (at least) the last 50 posts seen | |
with open('db.json', 'w') as outfile: | |
json.dump(db[-50:], outfile) |
also changed to 2 second wait time as I was still getting issues.
Will this automatically post new content or will i need to code a task?
Will this automatically post new content or will i need to code a task?
Hi @killaxia, I'm not entirely sure what you mean? You will need to run the script on a regular interval, each time it runs it checks for new posts and if there are any, it submits them to your Discord. Ideally you'd run this as a cronjob.
I added a task to run the code every 3 minutes
Works great :)
…On Wed, Oct 27, 2021, 6:17 AM Dan Walker ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
Will this automatically post new content or will i need to code a task?
Hi @killaxia <https://github.com/killaxia>, I'm not entirely sure what
you mean? You will need to run the script on a regular interval, each time
it runs it checks for new posts and if there are any, it submits them to
your Discord. Ideally you'd run this as a cronjob.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/7b471a095c645f96456ec2dd3d4bc87f#gistcomment-3941429>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AVMIQOWUMK2HKVE62ZZTQMLUI7NVPANCNFSM42ERUNJA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great script, I have forked and added a loop so you can specify in an array: 1. Subredit name 2. Discord webhook link 3. Type: Hot | New | Top | Rising. making it a little more modular and easier to add to.