RIG-Reddit Image grabber

 

RIG-Reddit Image grabber

Reddit image grabber logo

 

Please note that this post is here only for historical reasons, RIG is now on V2.0

 

Hi, I am well excited about this new project,  I have just started it today so why not follow my progress and add your ideas, code or whatever, It will be fun, honest.

Project RIG

Reddit Image Grabber is exactly what it sounds like, it will allow the user to download images from a specified Reddit subreddit.

If you have ever tried going through pages and pages of posts to see images it can be a bit of a pain in the arsenal to be honest, so let’s automate it and eventually give it one of my, now infamously bad GUI’s.

Well, I didn’t know where to start, so after some Googling , I found some scraps of related Python code, muddled them together, and fashioned the following bare-bones bit of code.

RIG  V0.2 Source Code

To run this code you will first need to: pip3 install praw

Double click inside code box to select all

"""Download x amount of images from any reddit subreddit
RIG- Reddit Image Grabber V0.2
By Steve Shambles 2018
updated Nov 2019
stevepython.wordpress.com

requires:
pip3 install praw
"""
import os
import shutil
from time import sleep
import urllib.request as web

import praw

# Change this to your desired download location.
DIR_PATH = 'reddit-images/'

# Check to see if folder exists, if not, create it.
check_folder = os.path.isdir(DIR_PATH)
if not check_folder:
    os.makedirs(DIR_PATH)

# Sign up with reddit and create an app to get his info.
# Instructions here:
# https://stevepython.wordpress.com/2018/08/09/rig-reddit-image-grabber
REDDIT = praw.Reddit(client_id='xxxxxxxx',
                     client_secret='xxxxxxx',
                     password='xxxxxxxxx',
                     user_agent='RIG V0.2',
                     username='xxxxxx')

# Change 'memes' to the subreddit and 'limit', to what you want.
SUBREDDIT = REDDIT.subreddit('memes').top(limit=5)

for submissions in SUBREDDIT:
    # Ignore stickie posts.
    if not submissions.stickied:
        fullfilename = os.path.join(DIR_PATH, '{}.jpg'.format(submissions))
        request = web.Request(submissions.url, headers={'User-Agent':'Mozilla/5.0 \
        (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'})

        with web.urlopen(request) as response, open(fullfilename, 'wb') as out_file:
            shutil.copyfileobj(response, out_file)
            dir_count = len(os.listdir(DIR_PATH))

            print("Downloaded. {} files saved in '{}'."
                  .format(dir_count, DIR_PATH))

            sleep(1) #not necessary, but prob good idea.

Alternate sources: Pastebin listing, or  MediaFire download.

Download Test

Yes, it works brilliantly. I’ve just downloaded 500 images from four different subreddits.

The above code is set to download two images from the “Memes” subreddit, just as a test, but you can easily change that of course, see the comments in the code.

Things couldn’t be that simple could they?

Nope, don’t be daft.

There is a bit of messing about with the Reddit API first. It’s no big deal though, especially if you are already signed up on Reddit, it will take seconds.

I’ll take you through it now.

  1. Sign up for a Reddit account, unless you already have one.
  2. Navigate to “preferences” then “create App”
  3. Complete the following form similar to the image below, it’s important that you select the “script”option.
  4. Click on “Create App”

Reddit create app

It’s Not So Bad At All

That wasn’t so bad really. Make a note of your client secret and client ID and enter those details in the appropriate parts of the code above, and you should be ready to rock.

Just to be clear, here is the section you need to enter your details:

REDDIT = praw.Reddit(client_id='enter your client id here',
                     client_secret='enter your secret here',
                     password='your own Reddit password',
                     user_agent='RIG V0.1',
                     username='your Reddit username')

Remember, don’t go too crazy downloading images or you could end up getting banned. I have read there is a one call a second limit, which seems fair, so I put a one second pause between each download, yes it slows things down to a crawl but do you really want to hit the API like a DDos attack?

rig-shell

Whatever!

In future updates I’ll probably make the pause a random 1-3 seconds or something, I’m still thinking on that.

OK I hope you feel inspired to mess with the code and learn something, maybe you could add support for GIFS, videos, posts whatever and write a GUI? Whatever you do, just enjoy it.

I promise to limit my use of the word  “whatever” from now on. 


Using Python V3.6.7 32bit, on Windows 7 64bit

and Python V3.67 on Linux Mint 19.1 Cinnamon 64bit

Home Page – Contact

A-Z Of My Projects 

Full list of my Python code snippets


Previous post: S-Py Full commented source

Next post: Reddit Image Grabber Help


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.