123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- 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/downloads'
- file_name = datetime.now().strftime(f"%Y%m%d%H%M%S_{''.join(random.sample(string.ascii_lowercase, 3))}.wav")
- # 替換為你希望的文件夾路徑
- 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.5)
- audio_element = browser.find_element(By.XPATH, '//*[@id="audio1"]')
- time.sleep(0.2)
- # # 取得 <audio> 標籤的屬性(例如 src)
- audio_src = audio_element.get_attribute('src')
- print("音頻來源:", audio_src)
- # 下載音檔
- download_blob(browser, audio_src, file_name)
- # check 是否下載完成
- file_path = os.path.normpath(os.path.join(download_folder, file_name))
- while not os.path.exists(file_path):
- print('...')
- time.sleep(0.01)
- print(file_path)
- # destination_path = os.path.join(download_folder, datetime.now().strftime(f"%Y%m%d%H%M%S_{''.join(random.sample(string.ascii_lowercase, 3))}.wav"))
- # shutil.move(file_path, destination_path)
- print(f"下載完成: {file_path}")
- print(time.time() - start_time)
- return file_path
- except Exception as e:
- print(f'Error: {e}')
- return e
-
-
- tts_downloadfile('沒問題,可以試著從包包自己拿出來放桌上,也可以請老師協助你唷')
|