
    i              	       0   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZmZ d dlmZmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZ ddlmZ  e       Z e       Zej>                  jA                  dd      Z!dede"fdZ#d Z$ddede"de"ddfdZ%y)    N)BeautifulSoup)Console)Prompt)
os_managerstart_messageconfig_manager)create_clientget_headers)site_constants	MediaItem)map_episode_title)process_season_selectionprocess_episode_download)HLS_Downloader)VideoSource   )GetSerieInfoM3U8_CONVERSION	extensionselect_titlereturnc                    t                t        j                  dt        j                   d| j
                   d       	 t        t                     j                  | j                        }|j                          t        |j                  d      }|j                  d      }|d   d   }d}	 t        t                     j                  |      }|j                          t        |j                  d      }d}t        j                   ||j                        }d|j#                  d      z   }t%        |      }	|	j'                         }
t)        j*                  | j
                  | j,                         dt.         }t0        j2                  j5                  t        j6                  |j9                  dt.         d            }t;        |
t0        j2                  j5                  ||            j=                         S # t        $ r4}t        j                  d	t        j                   d
| d       Y d}~yd}~ww xY w# t        $ rI}t        j                  d	t        j                   d
| d       t        j                  d       Y d}~yd}~ww xY w)zD
    Downloads a film using the provided MediaItem information.
    
[yellow]Download: [red]    → [cyan]z 
)headerszhtml.parseriframer   srcz[red]Site: z, request error: z, get mostraguardaNz&//supervideo\.[^/]+/[a-z]/[a-zA-Z0-9]+zhttps:z, get supervideo URLz,[yellow]This content will be available soon!. m3u8_urloutput_path)r   consoleprintr   	SITE_NAMEnamer	   r
   geturlraise_for_statusr   textfind_all	Exceptionresearchgroupr   get_playlistr   get_sanitize_fileyearextension_outputospathjoinMOVIE_FOLDERreplacer   start)r   responsesoupiframesmostraguardaesupervideo_urlpatternsupervideo_matchvideo_sourcemaster_playlist
title_namemp4_paths                k/home/nidran/Documenti/script/streaming-community/StreamingCommunity/services/altadefinizione/downloader.pydownload_filmrG   '   s#    OMM-n.F.F-G{S_SdSdReehij
 7;;L<L<LM!!#X]]M:--)qz%( N 7;;LI!!#X]]M:;99Whmm<!$4$:$:1$== ~.L"//1O 001B1BLDUDUVWWXYiXjkJww||N779K9KaP`OaLbdf9ghH  GGLL:6 eg;  N$<$<#==NqcQcde  N$<$<#==NqcQefgDEs2    A,G: /A?H: :	H7*H22H7:	J?JJc                 d   t                t        j                  dt        j                   d|j
                   d| j                   d| d| d       t        |j
                  ||| j                         dt         }t        j                  j                  t        j                  |j
                  d|       }t        | j                        }|j                  | j                         |j!                         }t#        |t        j                  j                  ||      	      j%                         S )
zA
    Downloads a specific episode from the specified season.
    r   r   z [white]\ [magenta]z	 ([cyan]SEz) 
r   Sr    )r   r#   r$   r   r%   series_namer&   r   r3   r4   r5   r6   SERIES_FOLDERr   r(   make_requestr0   r   r9   )obj_episodeindex_season_selectedindex_episode_selectedscrape_seriemp4_namerE   rB   rC   s           rF   download_episoderS   Z   sT    OMM-n.F.F-G{S_SkSkRl  mA  BM  BR  BR  AS  S\  ]r  \s  st  uK  tL  LP  Q  R $L$<$<>SUkmxm}m}~  @A  BR  AS  THww||N88,:R:RVWXmWnTopH {/Lkoo."//1O  GGLL84 eg    select_seasonseason_selectionepisode_selectionc                     t                t        | j                        j                         }ddt        dt
        dt        ffd}t        ||||       y)aK  
    Handle downloading a complete series.

    Parameters:
        - select_season (MediaItem): Series metadata from search
        - season_selection (str, optional): Pre-defined season selection that bypasses manual input
        - episode_selection (str, optional): Pre-defined episode selection that bypasses manual input
    Nseason_numberdownload_allrW   c                 0    fd}t        | |||       y)z:Callback to handle episode downloads for a specific seasonc                      t        | ||      S N)rS   )rN   
season_idxepisode_idxrQ   s      rF   download_video_callbackzSdownload_series.<locals>.download_episode_callback.<locals>.download_video_callback   s    #K[,WWrT   )rO   rQ   r`   rZ   rW   N)r   )rY   rZ   rW   r`   rQ   s       rF   download_episode_callbackz2download_series.<locals>.download_episode_callback~   s"    	X 	!"/%$;%/	
rT   )rQ   seasons_countrV   rW   ra   r]   )r   r   r(   getNumberSeasonintboolstrr   )rU   rV   rW   rb   ra   rQ   s        @rF   download_seriesrg   p   s\     O 1 12L 002M
 
D 
]` 
" !#)+";rT   )NN)&r4   r-   bs4r   rich.consoler   rich.promptr   StreamingCommunity.utilsr   r   r   $StreamingCommunity.utils.http_clientr	   r
   !StreamingCommunity.services._baser   r   4StreamingCommunity.services._base.tv_display_managerr   5StreamingCommunity.services._base.tv_download_managerr   r   "StreamingCommunity.core.downloaderr   $StreamingCommunity.player.supervideor   scrapperr   r#   msgconfigr'   r3   rf   rG   rS   rg    rT   rF   <module>rv      s    
 	     O N K G R t > = # )h!((,,->L 0	 0c 0f,%9 % %_b %nr %rT   