
    i                     X    d dl Z d dlZd dlmZ d dlmZmZ d dlmZm	Z	m
Z
  G d d      Zy)    N)BeautifulSoup)create_clientget_headers)SeasonManagerEpisodeSeasonc            	       n    e Zd ZddedededefdZddZd	eddfd
ZdefdZdede	fdZ
dededefdZy)GetSerieInfoNmedia_idseries_nameyearprovider_languagec                     d| _         t               | _        || _        || _        || _        t               | _        || _        |d| _         || _	        yy)a/  
        Initialize the GetSerieInfo class for scraping TV series information.
        
        Args:
            - url (str): The URL of the streaming site.
            - media_id (int, optional): Unique identifier for the media
            - series_name (str, optional): Name of the TV series
        FNT)
	is_seriesr   headersurlr   r   r   seasons_managerr   r   )selfr   r   r   r   r   s         l/home/nidran/Documenti/script/streaming-community/StreamingCommunity/services/streamingcommunity/scrapper.py__init__zGetSerieInfo.__init__   sU     "} 	,!2"!DN*D #    returnc                    	 t        | j                        j                  | j                   d| j                   d| j
                         }|j                          t        |j                  d      }t        j                  |j                  dddi      j                  d            }|d	   | _        |j                  d
i       j                  di       }|| _        |j                  dg       }|D ]i  }| j                  j                  t!        |j                  d      |j                  d      d|j                  d       |j                  d                   k y# t"        $ r}t%        j&                  d|         d}~ww xY w)z
        Retrieve general information about the TV series from the streaming site.
        
        Raises:
            Exception: If there's an error fetching series information
        r   /titles/-zhtml.parserdividappz	data-pageversionpropstitleseasonsnumberSeason slug)r   r$   namer&   zError collecting series info: N)r   r   getr   r   r   raise_for_statusr   textjsonloadsfindr    
title_infor   addr   	Exceptionloggingerror)r   responsesoupjson_response
title_dataseasons_dataseason_dataes           r   collect_info_titlezGetSerieInfo.collect_info_title&   s_   	$T\\:>>$((8TXTaTaSbbcdhdtdtcu?vwH%%' !>D JJtyyu'F'J'J;'WXM(3DL '**7B7;;GRHJ )DO &>>)R8L+ $$(("t,&??84";??8#<"=>$0	*   	MM:1#>?	s   EE 	F&E??Fnumber_seasonc                 <   	 | j                   j                  |      }|st        j                  d| d       y| j                  j                         }|j                  d| j                  d       t        |      j                  | j                   d| j                   d| j                   d	|       }|j                         j                  d
i       j                  di       j                  dg       }|D ]v  }|j                  j                  t!        |j                  d      |j                  d      |j                  d      |j                  d      |j                  d                   x y# t"        $ r!}t        j                  d| d|         d}~ww xY w)a  
        Retrieve episode information for a specific season.
        
        Args:
            number_season (int): Season number to fetch episodes for
        
        Raises:
            Exception: If there's an error fetching episode information
        r%   
 not foundNtrue)z	x-inertiazx-inertia-versionr   r   r   z/season-r!   loadedSeasonepisodesr   r$   r'   duration)r   video_idr$   r'   rA   z%Error collecting episodes for season z: )r   get_season_by_numberr1   r2   r   copyupdater    r   r(   r   r   r   r+   r@   r/   r   r0   )r   r;   seasoncustom_headersr3   r5   epr9   s           r   collect_info_seasonz GetSerieInfo.collect_info_seasonJ   s   	))>>}MFjAB!\\..0N!!#%)\\#  %^<@@DHH:XVZVcVcUddefjfvfveww  AN  @O  BP  QH %MMO//<@@QSTXXYceghM $ ##Gvvd|VVD\66(+VVJ/%   	MMA-PRSTRUVW	s   6E1 D7E1 1	F:FFc                     | j                   j                  s| j                          t        | j                   j                        S )zK
        Get the total number of seasons available for the series.
        )r   r#   r:   len)r   s    r   getNumberSeasonzGetSerieInfo.getNumberSeasonu   s6     ##++##%4''//00r   season_numberc                     | j                   j                  |      }|st        j                  d| d       g S |j                  j                  s| j                  |       |j                  j                  S )z9
        Get all episodes for a specific season.
        r%   r=   )r   rC   r1   r2   r@   rI   )r   rM   rF   s      r   getEpisodeSeasonszGetSerieInfo.getEpisodeSeasons~   sb     %%::=IMMGM?*=>I''$$]3'''r   episode_indexc                     | j                  |      }|r|dk  s|t        |      k\  rt        j                  d| d|        y||   S )zN
        Get information for a specific episode in a specific season.
        r   zEpisode index z is out of range for season N)rO   rK   r1   r2   )r   rM   rP   r@   s       r   selectEpisodezGetSerieInfo.selectEpisode   sS     ))-8=1,X0NMMN=/9UVcUdef&&r   )NNNit)r   N)__name__
__module____qualname__intstrr   r:   rI   rL   listrO   r   rR    r   r   r
   r
      sx    +c +s +QT +ps +*"H' ' 'V1 1(s (t (	'3 	's 	'w 	'r   r
   )r+   r1   bs4r   $StreamingCommunity.utils.http_clientr   r   (StreamingCommunity.services._base.objectr   r   r   r
   rZ   r   r   <module>r^      s*       L S SF' F'r   