Text Share Online

# open server : python3 -m http.server 8080 –directory /path/to/your/folder

import os
import requests
from bs4 import BeautifulSoup

url = “http://192.168.8.149:8000/COD%20MW3/”
save_path = “D:\downloaded_files”

# Create the folder if it doesn’t exist
os.makedirs(save_path, exist_ok=True)

# Fetch the directory page
response = requests.get(url)
soup = BeautifulSoup(response.text, “html.parser”)

# Iterate over all the links on the page
for link in soup.find_all(“a”):
href = link.get(“href”)
if not href or href == “../”:
continue

file_url = url + href
file_name = os.path.join(save_path, href)

try:
# Send a HEAD request to get the file size
head_response = requests.head(file_url)
file_size = int(head_response.headers.get(“Content-Length”, 0))

# Check if the file already exists
if os.path.exists(file_name):
existing_file_size = os.path.getsize(file_name)
if existing_file_size == file_size:
print(f”File ‘{file_name}’ already exists and is complete. Skipping…”)
continue
else:
print(f”File ‘{file_name}’ exists but is incomplete. Redownloading…”)

# Download the file
print(f”Downloading {file_url}…”)
with requests.get(file_url, stream=True) as r:
r.raise_for_status()
with open(file_name, “wb”) as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)

# Verify the file size after download
downloaded_file_size = os.path.getsize(file_name)
if downloaded_file_size != file_size:
print(f”Warning: File ‘{file_name}’ download is incomplete. Please check.”)

except Exception as e:
print(f”Failed to process file ‘{file_name}’. Error: {e}”)

    Share This: