add error handling so the script doesn't hang it self up and add prints for what it is…
- User
add error handling so the script doesn't hang it self up and add prints for what it is doing. also don't download the gif if the name already exists
import requests import os import re from PIL import Image
Set the directory where the GIFs will be saved
output_dir = 'output/'
Define the function to download the GIF
def download_gif(url, subreddit, title): response = requests.get(url) filename = f"{subreddit}{title}.gif" filename = re.sub(r'[^\w\s-]', '', filename) filename = re.sub(r'^\s+|\s+$', '', filename) filename = re.sub(r'[-\s]+', '', filename) filepath = os.path.join(output_dir, subreddit, filename) os.makedirs(os.path.dirname(filepath), exist_ok=True) with open(filepath, 'wb') as f: f.write(response.content)
# Check if the downloaded file is a valid GIF try: with Image.open(filepath) as img: if img.format != 'GIF': # Delete the file if it is not a GIF os.remove(filepath) except Exception: # Delete the file if an exception occurred while checking if it is a valid GIF os.remove(filepath)Read the list of subreddits from a text file
subreddits = [] with open('subreddits.txt', 'r') as f: subreddits = [line.strip() for line in f.readlines()]
Loop through each subreddit
for subreddit in subreddits: # Set the URL to search for GIFs in the subreddit url = 'https://www.reddit.com/r/{}/search.json?q=url%3A.gif&restrict_sr=1&sort=top&t=all'.format(subreddit) headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# Loop through each page of search results after = None count = 0 # Initialize count while True: params = {'after': after} if after else {} response = requests.get(url, headers=headers, params=params) data = response.json() # Loop through each post and download the GIF for post in data['data']['children']: url = post['data']['url'] title = post['data']['title'] download_gif(url, subreddit, title) count += 1 # Increment count if count >= 250: # Check if count reaches the limit of 250 break # Check if there are more search results after = data['data']['after'] if not after or count >= 250: break - Assistant
import requests import os import re from PIL import Image # Set the directory where the GIFs will be saved output_dir = 'output/' # Define the function to download the GIF def download_gif(url, subreddit, title): print(f"Downloading from URL: {url}") response = requests.get(url) filename = f"{subreddit}_{title}.gif" filename = re.sub(r'[^\w\s-]', '', filename) filename = re.sub(r'^\s+|\s+$', '', filename) filename = re.sub(r'[-\s]+', '_', filename) filepath = os.path.join(output_dir, subreddit, filename) os.makedirs(os.path.dirname(filepath), exist_ok=True) # Check if the file already exists if not os.path.isfile(filepath): with open(filepath, 'wb') as f: f.write(response.content) # Check if the downloaded file is a valid GIF try: with Image.open(filepath) as img: if img.format != 'GIF': # Delete the file if it is not a GIF os.remove(filepath) except Exception as e: print(f"Error while checking if the file is a valid GIF: {e}") # Delete the file if an exception occurred while checking if it is a valid GIF os.remove(filepath) else: print(f"File {filename} already exists") # Read the list of subreddits from a text file subreddits = [] with open('subreddits.txt', 'r') as f: subreddits = [line.strip() for line in f.readlines()] # Loop through each subreddit for subreddit in subreddits: print(f"Processing subreddit: {subreddit}") # Set the URL to search for GIFs in the subreddit url = 'https://www.reddit.com/r/{}/search.json?q=url%3A.gif&restrict_sr=1&sort=top&t=all'.format(subreddit) headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} # Loop through each page of search results after = None count = 0 # Initialize count while True: params = {'after': after} if after else {} try: response = requests.get(url, headers=headers, params=params) data = response.json() except Exception as e: print(f"Error while getting search results: {e}") break # Loop through each post and download the GIF for post in data['data']['children']: url = post['data']['url'] title = post['data']['title'] download_gif(url, subreddit, title) count += 1 # Increment count if count >= 250: # Check if count reaches the limit of 250 break # Check if there are more search results after = data['data']['after'] if not after or count >= 250: break
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.