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
Here's the modified part of the script to only care for .gif, .gifv, and .mp4. This modification is achieved by adding a condition to the
write_posts_to_filefunction before writing the fetched post in the file.pythondef write_posts_to_file(file_path, sub_name, is_incomplete=False, after=None, before=None): # ... for post in post_generator: if interrupted: break # Add this line to filter only .gif, .gifv, and .mp4 posts elif not any(post['url'].endswith(ext) for ext in ('.gif', '.gifv', '.mp4')): continue # ... f.write(json_bytes) # ...This added condition ensures that only posts with URLs ending with .gif, .gifv, and .mp4 are written into the file. All other posts will be ignored.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.