
    i                      L    d dl Z d dlZd dlZd dlmZ d dlmZmZ  G d d      Zy)    N)BeautifulSoup)create_client_curlget_headersc                   J    e Zd ZdeddfdZdedefdZd Zd Zd Zdefd	Z	y)
VideoSourceurlreturnNc                 0    t               | _        || _        y)z
        Initializes the VideoSource object with default values.

        Attributes:
            - url (str): The URL of the video source.
        N)r   headersr   )selfr   s     Y/home/nidran/Documenti/script/streaming-community/StreamingCommunity/player/supervideo.py__init__zVideoSource.__init__   s     #}    c                 ,   	 t        | j                        j                  |      }|j                  dk\  r&t	        j
                  d|j                   d|        y|j                  S # t        $ r"}t	        j
                  d|        Y d}~yd}~ww xY w)z
        Make an HTTP GET request to the provided URL.

        Parameters:
            - url (str): The URL to make the request to.

        Returns:
            - str: The response content if successful, None otherwise.
        )r   i  z!Request failed with status code: z
, to url: NzRequest failed: )r   r   getstatus_codeloggingerrortext	Exception)r   r   responsees       r   make_requestzVideoSource.make_request   s    
	)$,,?CCCHH##s* A(BVBVAWWabeafgh==  	MM,QC01	s   AA( A( (	B1BBc                     |j                  d      }|r8t        |      dkD  r*|d   j                  d      xs |d   j                  d      S y)a2  
        Extracts the source URL of the second iframe in the provided BeautifulSoup object.

        Parameters:
            - soup (BeautifulSoup): A BeautifulSoup object representing the parsed HTML.

        Returns:
            - str: The source URL of the second iframe, or None if not found.
        iframe   r   srczdata-srcN)find_alllenr   )r   soupiframess      r   
get_iframezVideoSource.get_iframe1   sH     --)s7|a'1:>>%(FGAJNN:,FFr   c                 B    | j                  |      }|rt        |d      S y)a*  
        Makes a request to the specified URL and parses the HTML content.

        Parameters:
            - url (str): The URL to fetch content from.

        Returns:
            - BeautifulSoup: A BeautifulSoup object representing the parsed HTML content, or None if the request fails.
        html.parserN)r   r   )r   r   contents      r   find_contentzVideoSource.find_contentA   s'     ##C( -88r   c                     |j                  d      D ]0  }dt        |      v st        j                  |j                        c S  y)ah  
        Prepares and runs a Node.js script from the provided BeautifulSoup object to retrieve the video URL.

        Parameters:
            - soup (BeautifulSoup): A BeautifulSoup object representing the parsed HTML content.

        Returns:
            - str: The output from the Node.js script, or None if the script cannot be found or executed.
        scriptevalN)r   strjsbeautifierbeautifyr   )r   r    r(   s      r   get_result_node_jszVideoSource.get_result_node_jsQ   sB     mmH- 	:FV$#,,V[[99	: r   c                    	 | j                  | j                        }|st        j                  d       y| j	                  t        |d            }|?t        j                  d|      }|r|j                  d      S t        j                  d       y| j                  t        |d            }|st        j                  d       y| j                  |      }|st        j                  d       yd	}t        j                  |t        |            }|st        j                  d
       yd|j                  d      z   }| j                  |      }|st        j                  d       y| j	                  |      }t        j                  d|      }|r|j                  d      S t        j                  d       y# t        $ r"}	t        j                  d|	        Y d}	~	yd}	~	ww xY w)z
        Download a video from the provided URL.

        Returns:
            str: The URL of the downloaded video if successful, None otherwise.
        zFailed to fetch HTML content.Nr$   z#sources:\s*\[\{\s*file:\s*"([^"]+)"r   z'Failed to find M3U8 URL: No match foundzNo iframe found.z"Failed to fetch down page content.zdata-link="(//supervideo[^"]+)"z!No player available for download.zhttps:z#Failed to fetch supervideo content.zAn error occurred: )r   r   r   r   r-   r   researchgroupr"   r&   r*   r   )
r   html_contentdata_jsmatch
iframe_srcdown_page_souppatternsupervideo_urlsupervideo_soupr   s
             r   get_playlistzVideoSource.get_playlista   s   7	,,TXX6L=> --mL-.XYG"		"H'R ;;q>)MM"KLH A "__]<-WX
!MM"45!%!2!2:!>%MM"FG<		'3~+>?MM"EF!)EKKN!:"&"3"3N"C&MM"GH 11/B		"H'R ;;q>)MM"KL 	MM/s34	sG   2F3 AF3 ;F3 2F3 (F3 -8F3 &<F3 #9F3 F3 3	G<GG)
__name__
__module____qualname__r*   r   r   r"   r&   r-   r:    r   r   r   r      sC    C D   ,   >c >r   r   )	r/   r   r+   bs4r   $StreamingCommunity.utils.http_clientr   r   r   r>   r   r   <module>rA      s'    
    QO Or   