Skip to content

Instantly share code, notes, and snippets.

@impshum
Created January 18, 2018 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save impshum/db01bd7b034fdc299d7a4e1dca6d5df2 to your computer and use it in GitHub Desktop.
Save impshum/db01bd7b034fdc299d7a4e1dca6d5df2 to your computer and use it in GitHub Desktop.
Get recent images from Reddit and post to Twitter
consumer_key = 'xxxx'
consumer_secret = 'xxxx'
access_key = 'xxxx-xxxx'
access_secret = 'xxxx'
client_id = 'xxxx'
client_secret = 'xxxx'
user_agent = 'New image posts from Reddit to twiiter (by /u/impshum)'
subreddits = ['comics', 'xxxx', 'xxxx']
import tweepy
import praw
import urllib.request
from config import *
import random
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
def redditpic():
reddit = praw.Reddit(client_id=client_id,
client_secret=client_secret,
user_agent=user_agent)
subs = subreddits
subs = random.choice(subs)
subs = reddit.subreddit(subs).new(limit=5)
try:
for sub in subs:
if sub.over_18:
print('NSFW - Skipping')
else:
if 'i.' in sub.url:
urllib.request.urlretrieve(sub.url, 'data/image.jpg')
picci = 'data/image.jpg'
print('Tweeting', sub.title)
api.update_with_media(picci, sub.title)
break
except:
print('Error')
redditpic()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment