U
    VhI                     @   s   d dl 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	 d dl
mZmZ d dlmZ d dlmZmZmZmZmZ d dlmZ e Zejdd	d
dd Zejdd	d
ejdddZejdd	d
ejdddZi ZejedddZdS )    N)web)BadStatusLine)multi_clients
work_loads)FIleNotFound)Varutils	StartTime__version__	StreamBot)render_page/T)
allow_headc                    sT   t dtt t dtj tt	t
dd ttt dd ddD td	S )
Nrunning@c                 s   s*   | ]"\}\}}d t |d  |fV  qdS )bot   N)str).0c_l r   ?/opt/bot/vods_filmes/script/WebStreamer/server/stream_routes.py	<genexpr>   s   
z%root_route_handler.<locals>.<genexpr>c                 S   s   | d S )Nr   r   )xr   r   r   <lambda>       z$root_route_handler.<locals>.<lambda>T)keyreverse)Zserver_statusZuptimeZtelegram_botZconnected_botsloadsversion)r   json_responser   get_readable_timetimer	   r   usernamelenr   dict	enumeratesortedr   itemsr
   )r   r   r   r   root_route_handler   s    r+   z/watch/{path:\d+})requestc              
      s   z,| j d }t|}tjt|I d H ddW S  tk
r\ } ztj|jdW 5 d }~X Y nZ tt	t
fk
rt   Y nB tk
r } z$t|d  tjt|dW 5 d }~X Y nX d S )Npathz	text/html)textcontent_typer.   )
match_infointr   Responser   r   HTTPNotFoundmessageAttributeErrorr   ConnectionResetError	Exceptionloggingcriticalwith_tracebackHTTPInternalServerErrorr   r,   r-   
message_ider   r   r   stream_handler"   s    
r@   z/{path:\d+}c              
      s   z$| j d }t|}t| |I d H W S  tk
rT } ztj|jdW 5 d }~X Y nZ ttt	fk
rl   Y nB t
k
r } z$t|d  tjt|dW 5 d }~X Y nX d S )Nr-   r0   )r1   r2   media_streamerr   r   r4   r5   r6   r   r7   r8   r9   r:   r;   r<   r   r=   r   r   r   r@   0   s    
)r,   r>   c                    sB  z| j dd}tttjd}t| }tjrFtd| d| j	  t
|}|sft|}|t
|< ||I d H }|j}|rz|ddd}t|dkrtd	|d
 rt|d
 nd
}	|d rt|d n|d }
|	d
k rtd|
|kr|d }
|	|
kr.|	|
d kr&|	|kr&|
}	ntdW n^ tk
r } z>td| dt|  tjddt| dd W Y W S d }~X Y nX nd
}	|d }
|
|	 d }ztt|I d H d}W n> tk
r } ztdt| d d}W 5 d }~X Y nX z>tt|	|I d H d
}|	| }|
| d }t|| }W nL tk
r } z,tdt|  tjddd W Y W S d }~X Y nX td|j pd d| d|	 d|
 d| d| d |  z|!|||||||}W nL tk
r2 } z,td!t|  tjdd"d W Y W S d }~X Y nX |j"pVt#$|j pJdd
 pVd#}|j pt%&d$ d%d&|kr|d&d' nd( }|d)|	 d|
 d&| d*| d+d,d-d.}|rtjd/||d0W S t||d1< tjd2||d0W S W nN tk
r< } z.tj'd3t| d4d5 tjdd6d W Y S d }~X Y nX d S )7NZRange )r   zClient z is now serving zbytes=-   u   Formato de range inválidor   r   u(   Posição inicial não pode ser negativau+   Posição inicial maior que posição finalu   Range header inválido: z - i  z$. Formato esperado: bytes=inicio-fim)statusr.   i   u    Erro no cálculo do chunk_size: u    - Usando padrão 1MBi   u!   Erro nos cálculos de streaming: i  z(Erro interno no processamento do arquivozStreaming: Zunnamedz	 | Size: z
 | Range: z | Req: z bytes | Chunk: z
 | Parts: zFalha ao gerar stream: u'   Falha ao acessar o conteúdo do arquivozapplication/octet-stream   .r   binzbytes zinline; filename=""byteszpublic, max-age=3600)zContent-TypezContent-RangezContent-DispositionzAccept-RangeszCache-Control   )rE   bodyheaderszContent-Length   u!   Erro crítico no media_streamer: T)exc_infoz%Erro interno no servidor de streaming)(rN   getminr   r   r   MULTI_CLIENTr9   inforemoteclass_cacher   ByteStreamerZget_file_properties	file_sizereplacesplitr&   
ValueErrorr2   warningr   r   r3   max
chunk_sizer8   error
offset_fixmathceildebug	file_nameZ
yield_file	mime_type	mimetypes
guess_typesecretsZ	token_hexr:   )r,   r>   Zrange_headerindexZfaster_clientZ
tg_connectfile_idrX   Z
range_data
from_bytesZuntil_bytesr?   Z
req_lengthZnew_chunk_sizeoffsetZfirst_part_cutZlast_part_cut
part_countrM   re   rd   rN   r   r   r   rA   @   s    


 4      $4
	rA   ) rer$   ra   r9   rh   rf   aiohttpr   Zaiohttp.http_exceptionsr   ZWebStreamer.botr   r   WebStreamer.server.exceptionsr   WebStreamerr   r   r	   r
   r   Z!WebStreamer.utils.render_templater   RouteTableDefroutesrQ   r+   Requestr@   rV   r2   rA   r   r   r   r   <module>   s(   
