praw-dev / praw

PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.

Home Page:http://praw.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to check if post contains video

eliich opened this issue · comments

Describe the solution you'd like

Hello i am trying to check if a post contains video, for logging purposes. Currently using something to do the same for photos

# Access the Reddit post using its post ID
post_id = '16uzdpd'  # Replace with the actual post ID
submission = reddit.submission(f'{post_id}')

# Function to download an image
def download_image(url):
    response = requests.get(url)
    if response.status_code == 200:
        filename = os.path.basename(url)
        with open(filename, 'wb') as file:
            file.write(response.content)
        print(f"Downloaded: {filename}")
        return True  # Return True if the image was downloaded
    return False  # Return False if the image was not downloaded

# Extract image URLs from the post's content
images_downloaded = False  # Initialize a flag to track if any images were downloaded

if submission.is_self:  # Check if the post is a self-post (text-only)
    print("This is a text-only post.")
else:
    # Check for image links in the post's URL and body
    if submission.url.endswith(('.jpg', '.png', '.gif', '.jpeg')):
        if download_image(submission.url):
            images_downloaded = True

    elif submission.selftext_html and '<img src="' in submission.selftext_html:
        start_index = submission.selftext_html.find('<img src="') + len('<img src="')
        end_index = submission.selftext_html.find('"', start_index)
        image_url = submission.selftext_html[start_index:end_index]
        if download_image(image_url):
            images_downloaded = True

# Print the message only if images were downloaded
if images_downloaded:
    print("Downloaded images saved in the current working directory.")
else:
    print("No images were downloaded.")

Describe alternatives you've considered

No response

Additional context

No response

I am going to use something like yt-dlp to download the mp4 but don't want to do that for every post as that is a lot of unnecessary usage(considering most posts wont have videos)

This issue is stale because it has been open for 30 days with no activity.

Remove the Stale label or comment or this will be closed in 30 days.

This issue was closed because it has been stale for 30 days with no activity.