Advertisement
Friendsincode

Untitled

Nov 16th, 2023
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. import requests
  2. import os
  3.  
  4. def download_mp3s(download_folder='downloadfree', log_file='free_log.txt'):
  5.     input_file = 'free-output.txt'  # Name of the file containing MP3 URLs
  6.     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
  7.  
  8.     # Create the download folder if it doesn't exist
  9.     if not os.path.exists(download_folder):
  10.         os.makedirs(download_folder)
  11.  
  12.     with open(input_file, 'r', encoding='utf-8') as file:
  13.         mp3_urls = file.readlines()
  14.  
  15.     with open(log_file, 'w', encoding='utf-8') as log:
  16.         for url in mp3_urls:
  17.             url = url.strip()  # Remove any extra whitespace
  18.             file_name = url.split('/')[-1]  # Extract the file name from the URL
  19.  
  20.             try:
  21.                 response = requests.get(url, stream=True, headers=headers)
  22.                 response.raise_for_status()
  23.  
  24.                 file_path = os.path.join(download_folder, file_name)
  25.                 with open(file_path, 'wb') as f:
  26.                     for chunk in response.iter_content(chunk_size=8192):
  27.                         f.write(chunk)
  28.  
  29.                 log.write(f"Downloaded: {url}\n")
  30.                 print(f"Downloaded: {url}")
  31.  
  32.             except Exception as e:
  33.                 log.write(f"Failed to download {url}: {e}\n")
  34.                 print(f"Failed to download {url}: {e}")
  35.  
  36. if __name__ == "__main__":
  37.     download_mp3s()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement