which part of the script has to be changed, make a code block modify this script so it…
- User
which part of the script has to be changed, make a code block
modify this script so it only cares for .gif and .gifv and .mp4
import requests import urllib.parse from datetime import datetime import json import gzip import os import shutil import signal from contextlib import contextmanager import time import argparse import configparser import concurrent.futures from contextlib import contextmanager from queue import Queue
config = configparser.ConfigParser() config.read("config") #maxWorkers = int(config["CONFIG"]["MAX_WORKERS"]) maxWorkers = 4 # Go easy on Push shift root_folder = config["CONFIG"]["MEDIA_FOLDER"] global interrupted interrupted = False
last_character = root_folder[-1] if last_character != "/": root_folder = root_folder + "/"
global successful_subs global failed_subs successful_subs = Queue() failed_subs = Queue()
class NoQuotedCommasSession(requests.Session): """ A custom session class that inherits from requests.Session and overrides the send method to avoid URL encoding of commas and allows setting a custom timeout. """ def init(self, timeout=None): super().init() self.timeout = timeout
codedef send(self, *a, **kw): a[0].url = a[0].url.replace(urllib.parse.quote(','), ',') if self.timeout is not None: kw['timeout'] = self.timeout return super().send(*a, **kw)class GracefulInterrupt(Exception): """ A custom exception class to handle graceful interruption of the script. """ pass
#@contextmanager class handle_interrupt: def init(self, func): self.func = func
codedef __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): global interrupted if exc_type == KeyboardInterrupt: interrupted = True print("Interrupt requested. Waiting for tasks to finish...") time.sleep(1) return True return False def __call__(self, *args, **kwargs): return self.func(*args, **kwargs)def displayOutput(): global sub_status_dict for sub in sub_status_dict: if sub_status_dict[sub]['completed'] == True: print(f"\rDownloading {sub} complete",end="",flush=True) else: print(f"\rDownloading {sub}...chunk {sub_status_dict[sub]['chunks']}",end="",flush=True)
- Assistant
To modify the script so it only cares for .gif, .gifv, and .mp4 files, you can add a condition in the
fetch_all_subreddit_posts()function. Update the function as follows:pythondef fetch_all_subreddit_posts(sub_name, after=None, before=None): ... i = 1 while True: ... yield from (post for post in chunk if any(post['domain'].endswith(ext) for ext in ('.gif', '.gifv', '.mp4'))) ... i += 1 ...This change adds a generator expression
(post for post in chunk if any(post['domain'].endswith(ext) for ext in ('.gif', '.gifv', '.mp4')))which only yields the posts where the domain ends with .gif, .gifv, or .mp4.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.