File size: 755 Bytes
e5429bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import random
import os

def get_random_api_key(file_name='api_key.txt'):
    try:
        current_dir = os.path.dirname(os.path.abspath(__file__))
        project_root = os.path.abspath(os.path.join(current_dir, '..'))  # lên 1 cấp từ support/
        file_path = os.path.join(project_root, file_name)

        with open(file_path, 'r', encoding='utf-8') as f:
            keys = [line.strip() for line in f if line.strip()]
        if not keys:
            raise ValueError("File không chứa key hợp lệ.")
        return random.choice(keys)
    except FileNotFoundError:
        raise FileNotFoundError(f"Không tìm thấy file: {file_path}")
    except Exception as e:
        raise Exception(f"Lỗi khi đọc file: {e}")