o
    i                     @   s8  d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ ddlmZ deee	e f fdd	Zd
eddfddZd
ede
e de
e dedeee
e f f
ddZdede
e de	e ddfddZd
edede
e ddfddZde
e de
e deddfddZdddZedkre  dS dS )z.Main entrypoint for the edge-playback package.    N)which)ListOptionalTuple   )pr_errreturnc                  C   sD   t jdddd} | jdddd |  \}}tjd	kp|j}||fS )
Nzedge-playbackz;Speak text using Microsoft Edge's online text-to-speech APIz'See `edge-tts` for additional arguments)progdescriptionepilogz--mpv
store_truezSUse mpv to play audio. By default, false on Windows and true on all other platforms)actionhelpwin32)argparseArgumentParseradd_argumentparse_known_argssysplatformmpv)parserargstts_argsuse_mpv r   P/home/kim/smarthome/.venv/lib/python3.10/site-packages/edge_playback/__main__.py_parse_args   s   r   r   c                 C   sZ   d}dg}| r| d |D ]}t|st| d d}q|r+td td d S d S )NFedge-ttsr   z is not installed.Tz(Please install the missing dependencies.r   )appendr   r   r   exit)r   Zdepcheck_faileddepsdepr   r   r   _check_deps   s   
r#   	mp3_fname	srt_fnamedebugc                 C   s~   d  }}|st jddd}|  |j}|rtd|  |s/| r/t jddd}|  |j}|r;|r;td| d ||fS )Nz.mp3F)suffixdeletezMedia file: z.srtzSubtitle file: 
)tempfileNamedTemporaryFileclosenameprint)r   r$   r%   r&   Zmediasubtitler   r   r   _create_temp_files.   s   r0   r   c                 C   sb   dd|  g}|r| d|  || }t|}|  W d    d S 1 s*w   Y  d S )Nr   z--write-media=z--write-subtitles=)r   
subprocessPopencommunicate)r$   r%   r   Zedge_tts_cmdprocessr   r   r   _run_edge_ttsD   s   
"r5   c                 C   s   t jdkr| sddlm} || d S ddg}|r!|d|  || t|}|  W d    d S 1 s;w   Y  d S )Nr   r   )play_mp3_win32r   z'--msg-level=all=error,statusline=statusz--sub-file=)r   r   Zwin32_playbackr6   r   r1   r2   r3   )r   r$   r%   r6   Zmpv_cmdr4   r   r   r   _play_mediaO   s   

"r7   keepc                 C   s   |r| d urt d|  dd |rt d| dd t   d S | d ur.tj| r.t|  |d ur?tj|rAt| d S d S d S )Nz
Keeping temporary files:  )endz and )r.   ospathexistsunlink)r$   r%   r8   r   r   r   _cleanupb   s   
r?   c               
   C   s   t  \} }t|  tjdd u}tjdd u}tjd}tjd}zt| |||\}}t||| t| || W t||| d S t||| w )NZEDGE_PLAYBACK_DEBUGZEDGE_PLAYBACK_KEEP_TEMPZEDGE_PLAYBACK_MP3_FILEZEDGE_PLAYBACK_SRT_FILE)	r   r#   r;   environgetr0   r5   r7   r?   )r   r   r&   r8   r$   r%   r   r   r   _mainp   s   
rB   __main__)r   N)__doc__r   r;   r1   r   r*   shutilr   typingr   r   r   utilr   boolstrr   r#   r0   r5   r7   r?   rB   __name__r   r   r   r   <module>   sJ    

"

