-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB_downloadFiles.py
41 lines (31 loc) · 1.1 KB
/
B_downloadFiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import json
import requests
# Path to the JSON file
json_file_path = "episodes.json"
# Destination folder for downloaded files
download_folder = "./downloads"
os.makedirs(download_folder, exist_ok=True)
# Load data from the JSON file
with open(json_file_path, "r", encoding="utf-8") as json_file:
episodes = json.load(json_file)
# Download each file
for episode in episodes:
url = episode["url"]
filename = episode["fileName"]
filepath = os.path.join(download_folder, filename)
if os.path.exists(filepath):
print(f"File already exists: {filename}")
continue
print(f"Downloading: {filename} from {url}")
try:
response = requests.get(url, stream=True)
response.raise_for_status() # Check for HTTP errors
# Write the file
with open(filepath, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print(f"File downloaded: {filepath}")
except requests.exceptions.RequestException as e:
print(f"Error downloading {filename}: {e}")
print("Download complete.")