Skip to main content

Gets media URLs from Reddit Submission using Python Async PRAW

Project description

Reddit Media File Downloader

Tests passing Badge Supported Python versions Badge PyPI package version Badge

Gets media file URLs from Reddit submissions using Async PRAW library and downloads it. Supports video and single or gallery images. Can be used both as standalone package and library for Python scripts.

How to install

To use this package, you first have to install Python 3.7 or higher (versions below are not supported). Then open your terminal and type following command to install the latest version of the package from PyPI repository:

pip install reddit-media

Reddit API requires authentication for scripts that use it, so you will have to do some configuration beforehand. Specifically, it requires app Client ID and Client Secret. You can create both very easily following next steps:

  • Open this page: https://reddit.com/prefs/apps
  • If you are not logged in into your Reddit account, you will be prompted to do so
  • You will see 'authorized applications' page
  • Scroll to the very bottom and press 'create another app...' button
  • Select application type 'script'
  • Enter any name and redirect URL you want (you will be able to change it later)
  • Press 'create app' button. You will see your new app created
  • Copy Client ID just under 'personal use script' text and name of the app
  • Copy Client Secret next to 'secret' field

Example

Note: For details about authenticating, check out this page: Authenticating via OAuth

Using as a library

If your Python script needs to get URLs of some Reddit submission, but you don't want to do it yourself, you can use this package to do it for you. Once you installed the package, you can just import it and use as any other library:

import asyncio
from redditmedia import get_reddit, get_media, MediaType

async def main():
  async with get_reddit(client_id='your-client-id', client_secret='your-client-secret') as reddit:
    subreddit = await reddit.subreddit('cute')
    async for submission in subreddit.hot(limit=10):  # First 10 submissions on r/cute
      for media in get_media(submission).media:
        if media.type in [MediaType.jpg, MediaType.png]:  # Print URL only if it's an image
          print(media.uri)

asyncio.run(main())

Using as standalone program

If you want to download bunch of media files from some reddit submissions of subreddit, you can do this by using this package as standalone CLI program. You can do that by entering this into your terminal:

python -m redditmedia -c [CLIENT ID] [CLIENT SECRET] get [SUBMISSION IDS]

This will download all media files from specified submissions into reddit-media-downloads folder in current working directory. If you wish to change path of the folder where media should be downloaded to, you can specify it by adding -p [PATH TO FOLDER] option before get command above, or if you wish to have media files in separate folders depending on the submission they are from, you can do that by adding -s flag before get command.

For more info, enter:

python -m redditmedia --help

Storing credentials in a file

As you can see, both use cases require you to explicitly provide credentials, which can be security issue and generally inconvenient. Instead you can create praw.ini file inside your current working directory and paste this:

[redditmedia]
client_id=<Paste Client ID here>
client_secret=<Paste Client Secret here>

Now you can omit keyword arguments when calling get_reddit and credentials will be automatically picked up from the file:

from redditmedia import get_reddit
reddit = get_reddit()

And when using CLI tool, omit -c option:

python -m redditmedia get [IDS OF SUBMISSIONS SEPARATED WITH SPACE]

Note: You can do more things using this file, which is outside of the topic of this page. For details about praw.ini file, check out documentation: praw.ini Files

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reddit-media-0.0.3.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reddit_media-0.0.3-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file reddit-media-0.0.3.tar.gz.

File metadata

  • Download URL: reddit-media-0.0.3.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for reddit-media-0.0.3.tar.gz
Algorithm Hash digest
SHA256 40a150d5834d9001c1abf15f4f6fbef81a4bdf6629f1b596c78f764bcb4e12ea
MD5 f18e7c6d79bff0cdda6630a2cba4e3b0
BLAKE2b-256 2ce5670b9afcb7849cdc6ee48cfe8149218ad3b4c2fd232a62b3d3b9d9d83b9c

See more details on using hashes here.

File details

Details for the file reddit_media-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: reddit_media-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for reddit_media-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d04df71437cea4d54024486cb4aa6cc60fc9f128e9cddd4c9818ce05886d2fac
MD5 668b8699a9ddd6bdf6cfb38d499b8741
BLAKE2b-256 d1870da76095a4f9d79161407ef017fa729780f19042fd8ca7ed91163d1091a2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page