Skip to content

Instantly share code, notes, and snippets.

@tin2tin
Created January 24, 2025 06:54
Show Gist options
  • Save tin2tin/d330e58ec7d838fabfc5df2fe9d7cabe to your computer and use it in GitHub Desktop.
Save tin2tin/d330e58ec7d838fabfc5df2fe9d7cabe to your computer and use it in GitHub Desktop.
A downloader for csv-files containing file IDs.
import csv
import requests
def debug_print(*args):
print(*args)
def fetch_video_result(file_id, api_key, output_file_name):
debug_print("File ID:", file_id)
debug_print("Out file name:", output_file_name)
debug_print("---------------Video generated successfully, downloading now---------------")
url = f"https://api.minimaxi.chat/v1/files/retrieve?file_id={file_id}"
debug_print("Retrieve URL:", url)
headers = {
'authorization': f'Bearer {api_key}',
}
response = requests.request("GET", url, headers=headers)
debug_print("Response text:", response.text)
if response.status_code != 200:
print(f"Failed to retrieve file info for file_id {file_id}. HTTP Status Code: {response.status_code}")
return
try:
download_url = response.json()['file']['download_url']
except (KeyError, ValueError):
print(f"Error parsing response for file_id {file_id}. Response: {response.text}")
return
debug_print("Download URL:", download_url)
print("Video download link:" + download_url)
try:
with open(output_file_name, 'wb') as f:
video_content = requests.get(download_url).content
f.write(video_content)
debug_print("Video content written to:", output_file_name)
print("The video has been downloaded in:" + output_file_name)
except Exception as e:
print(f"Error downloading or saving file for file_id {file_id}. Error: {e}")
return output_file_name
def read_api_key(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as f:
return f.read().strip()
except FileNotFoundError:
print(f"API key file {file_path} not found.")
raise
except Exception as e:
print(f"An error occurred while reading the API key file: {e}")
raise
def download_files_from_csv(csv_file_path, api_key, output_dir):
try:
with open(csv_file_path, mode='r', newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
file_id = row.get("video_file_id")
if not file_id:
print(f"No file_id found in row: {row}")
continue
output_file_name = f"{output_dir}/{file_id}.mp4"
fetch_video_result(file_id, api_key, output_file_name)
except FileNotFoundError:
print(f"The file {csv_file_path} does not exist.")
except Exception as e:
print(f"An error occurred while processing the CSV file: {e}")
# Example usage
if __name__ == "__main__":
API_KEY_FILE = r"C:/Users/peter/AppData/Roaming/Blender Foundation/Blender/4.4/extensions/user_default/pallaidium_generative_ai/MiniMax_API.txt" # Replace with your actual API key file path
CSV_FILE_PATH = r"C:/Users/peter/Documents/1880470278346117648_video.txt" # Replace with your actual CSV file path
OUTPUT_DIR = r"C:/Users/peter/Documents/MiniMax" # Replace with your desired output directory
import os
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
try:
API_KEY = read_api_key(API_KEY_FILE)
download_files_from_csv(CSV_FILE_PATH, API_KEY, OUTPUT_DIR)
except Exception as e:
print(f"An error occurred during the process: {e}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment