Thursday, February 22, 2024

Using Python to post to reddit programmatically

My reddit account was banned after testing, so I'll leave experimenting and integration aside for some time. I am posting this short code snippet here anyway, just for my own convenience when I decide to pull it out again.

import praw

from config import REDDIT_CLIENT_ID
from config import REDDIT_CLIENT_SECRET
from config import REDDIT_USERNAME
from config import REDDIT_PASSWORD

reddit = praw.Reddit(
    client_id     = REDDIT_CLIENT_ID,
    client_secret = REDDIT_CLIENT_SECRET,
    user_agent    = "web:com.example:v1.0.0 (by u/your_username)",
    username      = REDDIT_USERNAME,
    password      = REDDIT_PASSWORD,
)

submission = reddit.subreddit("sub_name").submit(
    title    = "Post (link) title",
    url      = "https://example.com/url")

print(submission.permalink)

No comments:

Post a Comment