import os # import requests # from bs4 import BeautifulSoup from selenium.webdriver.common.by import By # import re import time # from fake_useragent import UserAgent import undetected_chromedriver as uc from datetime import datetime import random import string # from selenium.webdriver.common.action_chains import ActionChains # from selenium.webdriver.common.keys import Keys # from selenium.webdriver.support.ui import WebDriverWait # from selenium.webdriver.support import expected_conditions as EC # from ga4mp import FirebaseMP from dotenv import load_dotenv import os import shutil import logging from fastapi import APIRouter, FastAPI import uvicorn from fastapi.middleware.cors import CORSMiddleware from supabase import create_client, Client load_dotenv() logging.basicConfig(level=logging.INFO) SUPABASE_URL: str = os.environ.get('SUPABASE_URL') SUPABASE_KEY: str = os.environ.get('SUPABASE_KEY') supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY) def download_blob(browser, blob_url, filename='output.mp3'): # 使用 Selenium 獲取 Blob 內容 js_code = f""" fetch('{blob_url}') .then(response => response.blob()) .then(blob => {{ const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = '{filename}'; document.body.appendChild(a); a.click(); document.body.removeChild(a); }}) .catch(error => console.error('Error downloading file:', error)); """ browser.execute_script(js_code) def tts_downloadfile(text): start_time = time.time() print(f'text長度: {len(text)}') num = random.randint(3,5) url = 'http://tts001.iptcloud.net:8804/' default_download_folder = os.path.join(os.path.expanduser('~'), 'Downloads') download_folder = '/var/www/html/innolux/tts_folder' file_name = datetime.now().strftime(f"%Y%m%d%H%M%S_{''.join(random.sample(string.ascii_lowercase, 3))}.mp3") # 替換為你希望的文件夾路徑 os.makedirs(download_folder, exist_ok=True) options = uc.ChromeOptions() options.add_argument('--ignore-certificate-errors') prefs = { "download.default_directory": download_folder, # 設定預設下載文件夾 "download.prompt_for_download": False, "safebrowsing.enabled": True, # 確保安全瀏覽 } options.add_experimental_option("prefs", prefs) # options.add_argument('--incognito') options.add_argument('--headless') # 如果不想顯示瀏覽器可以啟用這行 options.add_argument("--disable-gpu") # 禁用 GPU 加速 # 設置自定義 headers with uc.Chrome(options=options, version_main=129) as browser: try: browser.get(url) time.sleep(num) # 轉中文成台語拼音 browser.find_element(By.XPATH, '//*[@id="js-input"]').send_keys(text) time.sleep(0.1) browser.find_element(By.XPATH, '//*[@id="js-translate"]').click() time.sleep(0.1 + len(text)*0.01) browser.execute_script('window.scrollBy(0, 200);') # 轉語音 browser.find_element(By.XPATH, '//*[@id="button1"]').click() # time.sleep(len(text)*0.6) # audio_element = browser.find_element(By.XPATH, '//*[@id="audio1"]') # time.sleep(0.2) # # 取得