Created
September 11, 2023 14:07
-
-
Save MHaggis/fea939509930139eea307772cea6e414 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os | |
import json | |
file_path = "ids.txt" | |
base_url = "https://mockbin.org/bin" | |
log_directory = "logs" | |
script_directory = "scripts" | |
if not os.path.exists(log_directory): | |
os.mkdir(log_directory) | |
if not os.path.exists(script_directory): | |
os.mkdir(script_directory) | |
def safe_get_json(response): | |
try: | |
return response.json() | |
except Exception as e: | |
print(f"Error decoding JSON: {e}") | |
return None | |
with open(file_path, "r") as f: | |
for line in f: | |
bin_id = line.strip() | |
log_url = f"{base_url}/{bin_id}/log" | |
view_url = f"{base_url}/{bin_id}/view" | |
# Fetch logs | |
log_response = requests.get(log_url, headers={"Accept": "application/json"}) | |
log_content = safe_get_json(log_response) | |
if log_content: | |
with open(os.path.join(log_directory, f"{bin_id}.json"), "w") as log_file: | |
log_file.write(json.dumps(log_content, indent=4)) | |
else: | |
print(f"Error fetching logs for ID {bin_id}") | |
view_response = requests.get(view_url, headers={"Accept": "application/json"}) | |
script_content = safe_get_json(view_response) | |
if script_content: | |
with open(os.path.join(script_directory, f"{bin_id}_script.txt"), "w") as script_file: | |
script_file.write(str(script_content)) | |
else: | |
print(f"Error fetching script for ID {bin_id}") | |
print("Fetching logs and scripts completed!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment