change this script so it loops every .txt in the same folder and creates a new folder…
- User
change this script so it loops every *.txt in the same folder and creates a new folder with the name of the *.txt (example: awwww.txt would create an awwww folder for it's output and so on)
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)
# Limit the filename length to 20 characters if len(filename) > 20: filename = filename[:20] + '.gif' else: filename += '.gif' 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 - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.