
    4j%                    t    d Z ddlmZ ddlZddlZddlZddlZdZdZ	dZ
dZddZdd	Z G d
 de      ZdddZy)a  Deepgram, in about forty lines of stdlib.

The foreman is standing in the wind holding a phone. Typing WEATHER and ISSUES into a form at ten
past four is exactly the chore this whole app exists to delete, so those boxes get a mic instead.

One call, one transcript, one row in Diary-Notes.csv. No streaming, no websocket, no SDK, no new
dependency in requirements.txt - urllib posts the bytes the browser recorded and reads the JSON
back. If the key is missing the page says so in words and the typed box still works; if the call
fails the foreman is told to try again and nothing is written.

Never log the key. transcribe() takes it as an argument so it never has to reach for os.environ in
a code path that also builds an error message.
    )annotationsNzPhttps://api.deepgram.com/v1/listen?model=nova-2&smart_format=true&punctuate=true   u    couldn't hear that — try againu.   voice unavailable — no transcription key setc                 P    t        j                  d      xs dj                         S )NDEEPGRAM_API_KEY )osgetenvstrip     /opt/lfcs-signon/app/voice.pykeyr      s     II()/R6688r   c                 (    t        t                     S N)boolr   r   r   r   	availabler   "   s    ;r   c                  &     e Zd ZdZdd fdZ xZS )
VoiceErrorzHCarries the words for the screen; the caller logs the detail separately.c                H    t         |   |xs |       || _        || _        y r   )super__init__saydetail)selfr   r   	__class__s      r   r   zVoiceError.__init__)   s"    3'r   r   )r   strr   r   )__name__
__module____qualname____doc__r   __classcell__)r   s   @r   r   r   &   s    R r   r   c                   |xs
 t               j                         }|st        t        d      | st        t        d      t
        j                  j                  t        | dd|z   |xs dd      }	 t
        j                  j                  |t              5 }t        j                  |j                         j                  d	            }d
d
d
       	 d   d   d   d   d   }|j3                  d      xs dj                         }	|	st        t        d      |	S # 1 sw Y   RxY w# t
        j                  j                   $ r}d}	 |j                         j                  d	d      d
d }n# t"        $ r Y nw xY w|j$                  dv rt        dd|j$                  d|      t        t        d|j$                  d|      d
}~wt"        $ r-}t        t        t'        |      j(                  d|      d
}~ww xY w# t*        t,        t.        f$ r* t        t        dt        j0                        d
d z         w xY w)zMBytes in, words out. Raises VoiceError with something sayable on any failure.zDEEPGRAM_API_KEY not setzempty recordingPOSTzToken z
audio/webm)AuthorizationzContent-Type)datamethodheaders)timeoutzutf-8Nr   replacei,  )i  i  u7   voice unavailable — the transcription key was refusedzHTTP  z: resultschannelsr   alternativeszunexpected response shape: 
transcriptztranscript came back empty)r   r
   r   NO_KEY	CANT_HEARurllibrequestRequestAPIurlopenTIMEOUTjsonloadsreaddecodeerror	HTTPError	Exceptioncodetyper   KeyError
IndexError	TypeErrordumpsget)
audiocontent_typeapi_keyreqrespbodyer   altsaids
             r   
transcriberO   /   s   #%&&(G!;<<$566
..
 
 5!G+$4J
  CF^^##C#9 	;T::diik009:D	; \9oj)!,^<Q? GGL!'R..0D$@AAK/	; 	;<<!! 
E	VVX__Wi8#>F 		66ZV-.VVV<> >AFFF$CDD FQ0@0@!$DEEF
 j), \$ADJJtDTUYVYDZ$Z[[\sm   3%D( 3DD( H D%!D( (G>G#E,+G,	E85G7E88AGG>(G99G>>H?)returnr   )rP   r   r   )rF   bytesrG   r   rH   r   rP   r   )r!   
__future__r   r8   r   urllib.errorr2   urllib.requestr5   r7   r1   r0   r   r   r>   r   rO   r   r   r   <module>rU      sL    #  	  X
 /		99 $r   