
    }MRjkY                     H   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m	Z	  ed      Z
 ed      Z ed      ZdZdZd	Zd
ZdZdZdZd Zd Zd Zd Zd Zd%dZ	 	 	 	 d&dZd Zd Z	 	 d'dZd Zd Z	 	 d'dZ d Z!d Z"d Z#d Z$	 	 d'dZ%d  Z&d! Z'd" Z(d# Z)e*d$k(  r e)        yy)(    N)Path)ThreadPoolExecutoras_completedz&/var/www/html/Extract/series_full.jsonz%/var/www/html/Extract/series_list.m3uz&/var/www/html/Extract/series_list.jsonz"http://127.0.0.1:9393/extract?url=z,https://hrrejhp.com/scripttestfasel.php?api=        
   zoMozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36c                 f    t        j                  ddt        | xs d            j                         S )Nz\s+  )resubstrstripvalues    $/var/www/html/Extract/Movies_m3u8.pycleanr      s'    66&#s5;B/06688    c                 @    t        | xs d      j                  dd      S )Nr   "')r   replacer   s    r   escr   !   s    u{##C--r   c                     	 t         j                  j                  |       }|j                  r'|j                  r|j                   d|j                   S y# t
        $ r Y yw xY w)Nz://r   )urllibparseurlparseschemenetloc	Exception)refererparseds     r   make_originr$   %   s]    &&w/==V]]mm_C77
   s   AA 	A! A!c                 h   t        |       j                         j                         }|sy	 t        j                  j                  |      }|j                  j                         }d|v xs |j                  d      xs d|v S # t        $ r& |j                  dd      d   j                         }Y Kw xY w)NF?   r   z.m3u8z.txtz
master.txt)
r   r   lowerr   r   r   pathr!   splitendswith)urlr   r#   	path_onlys       r   is_m3u8r.   2   s    #J$$&E3&&u-KK%%'	
 	9 	%f%	%9$  3KKQ'*002	3s   9B ,B10B1c                 8    dt        |       j                         v S )Nz.mp4)r   r(   )r,   s    r   is_mp4r0   D   s    U3Z%%'''r   c                 `   t        | t              rut        | j                  d      xs; | j                  d      xs( | j                  d      xs | j                  d      xs |      }t        | j                  d      xs |      }| d| j	                         t        fddD              S )	Nlinkr,   embedm3u8serverr   c              3   &   K   | ]  }|v  
 y wN ).0r   texts     r   	<genexpr>z"is_fasel_source.<locals>.<genexpr>Y   s      
 	
s   )zfasel-hd.comfaselfaselhdfaisalfaislzvip fast)
isinstancedictr   getr(   any)videor2   r5   r:   s      @r   is_fasel_sourcerE   H   s    %IIf yyyy! yy  
 IIh 

 XQtf##%D 


 
 
r   c                    |xs t         |xs dddd}|r||d<   t        |      }|r||d<   t        j                  j	                  | |      }t        j                  j                  ||      5 }|j                  d	      }	|j                  |	j                  d
d      |j                         fcd d d        S # 1 sw Y   y xY w)Nz]application/vnd.apple.mpegurl,application/x-mpegURL,application/json,text/plain,text/html,*/*zen-US,en;q=0.9,ar;q=0.8close)
User-AgentAcceptzAccept-Language
ConnectionRefererOriginheaderstimeouti  utf-8ignore)
UAr$   r   requestRequesturlopenreadstatusdecodegeturl)
r,   rP   r"   	useragentacceptrN   originrT   responsebodys
             r   
fetch_textr`   f   s      o2 
 5G $	W% &GHnn$$ % G
 
		 
  
 

 
}}V$ OOKK*OO


 

 

s   />B77C c                     t        j                  d| t         j                        }|sy	 t        |j	                  d            S # t
        $ r Y yw xY w)NzRESOLUTION\s*=\s*\d+x(\d+)r   r'   r   searchIintgroupr!   stream_infomatchs     r   
get_heightrj      sO    II%
E 5;;q>""    A 	AAc                     t        j                  d| t         j                        }|sy	 t        |j	                  d            S # t
        $ r Y yw xY w)Nz"(?:AVERAGE-)?BANDWIDTH\s*=\s*(\d+)r   r'   rb   rg   s     r   get_bandwidthrm      sO    II-
E 5;;q>"" rk   c                    t        |       } | syt        |       ryt        |       sy	 t        | d||d      \  }}}|dvryd|j	                         vry|j                         }g }t        |      D ]  \  }}	|	j                         }
|
j	                         j                  d      s6|dz   }|t        |      k  sJ||   j                         }|s|dz  }*|j                  d	      r|dz  }At        j                  j                  ||      }t        |      rt        |      s|j                  t        |
      t        |
      |d
        |st        |      r|S dS |D cg c]  }|d   t         k\  r| }}|xs |}|j#                  d d       t        |d   d         }t        |      ryt        |      r|S dS c c}w # t$        $ r Y yw xY w)Nr      zVapplication/vnd.apple.mpegurl,application/x-mpegURL,application/mpegurl,text/plain,*/*rP   r"   r[   r\         #EXTM3Uz#EXT-X-STREAM-INFr'   #)height	bandwidthr,   rv   c                     | d   | d   fS )Nrv   rw   r8   items    r   <lambda>z&resolve_best_quality.<locals>.<lambda>  s    X[! r   Tkeyreverser   r,   )r   r0   r.   r`   upper
splitlines	enumerater   
startswithlenr   r   urljoinappendrj   rm   
MIN_HEIGHTsortr!   )
master_urlr"   r[   rX   r:   	final_urllinesvariantsindexlinerh   
next_index
media_line	media_urlrz   high_variantschoicesselecteds                     r   resolve_best_qualityr      s%   
 z"Jj:[",#
i #DJJL(!$U+ &	KE4**,K$$&11# Js5z)":.446
!!OJ((-!OJ"LL00	
 )$y)(5!.{!;$!  M&	P  '	 29:: !
H~+ 
 
  +8  	 	
 E*+("8,x4"4+
.  sO   G G A&G ;B!G G G #G 88G 1G >G  G 	GGc                 |   t        |       j                         } t        j                  d| t        j                        }|rR	 t        |j                  d            }|dk\  rd|z   S |dk\  rd|z   S |dk\  rd|z   S |d	k\  rd
|z   S |dk\  rd|z   S d|z   S d| v sd| v ryd| v sd| v ryd| v sd| v ryy# t        $ r Y 'w xY w)Nz(\d{3,4})\s*p?r'   ip  ip  i8  i  r   i  i  i  ih  i  i  4kuhdiX  fullhdzfull hdi  automasteri  r   )r   r(   r   rc   rd   re   rf   r!   )labelheight_matchrv   s      r   fasel_label_scorer   #  s    %L E99
L 	++A./F~f}$~f}$}f}$}f}$}f}$&= 
 u}5I.(e+  		s0   #B/ &	B/ 0	B/ :	B/ 	B/ B/ /	B;:B;c           	        
 g }g d}|D ]  }t        j                  || t         j                  t         j                  z        D ]a  \  
}t	        
j                  dd            
t	        |      }t        
      s8t        
      rD|j                  
|t        |      d       c  t        j                  d| t         j                        D ]  
t	        
j                  dd            
t        
      s*t        
      r6t        
fd|D              rKd}
j                         }d|v rd	}nd
|v rd}nd|v rd}nd|v rd}nd|v rd}|j                  
|t        |      d        g }t               }|D ]<  }|d   j                         }	|	|v r|j                  |	       |j                  |       > |j                  d d       |S )N)zEfile\s*:\s*["\']([^"\']+?)["\']\s*,\s*label\s*:\s*["\']([^"\']*)["\']zY["\']file["\']\s*:\s*["\']([^"\']+?)["\']\s*,\s*["\']label["\']\s*:\s*["\']([^"\']*)["\']zGfile\s*=\s*["\']([^"\']+?)["\'].{0,150}?label\s*=\s*["\']([^"\']*)["\']z\//)r,   r   scorez1https?://[^\s"\'<>\\]+?\.m3u8(?:\?[^\s"\'<>\\]*)?c              3   .   K   | ]  }|d    k(    yw)r,   Nr8   )r9   rz   r,   s     r   r;   z'parse_fasel_response.<locals>.<genexpr>  s"      
 K3
s   r   10801080p720720p480480p360360pr   r   r,   c                     | d   S )Nr   r8   ry   s    r   r{   z&parse_fasel_response.<locals>.<lambda>  s
    g r   Tr|   )r   findallrd   Sr   r   r.   r0   r   r   rC   r(   setr   addr   )r_   resultspatternspatternr   url_lowuniqueseenrz   
normalizedr,   s             @r   parse_fasel_responser   P  s    GH   **DD244K
 	JC
 E3'C %LE3<c{NN*51 #	0 zz	 
	 ) KKs#
 s|#; 

 
 ))+WEgEgEgE E&u-
 	K)V F5D %[&&(
d KK&  
 Mr   c                 L   t        |       } | sy	 t        t        j                  j	                  | d      z   }|xs d}t        |t        ||xs t        d      \  }}}|dvryt        |      }|D ]#  }	|	d   }
t        |
||xs t              }|s!|c S  y# t        $ r Y yw xY w)	Nr   safehttps://fasel-hd.com/z)application/json,text/plain,text/html,*/*rp   rq   r,   r"   r[   )r   	FASEL_APIr   r   quoter`   TIMEOUTrS   r   r   r!   )page_urlr"   r[   api_urlfasel_refererrX   r_   _
candidates	candidater,   finals               r   extract_faselr     s    
 XH/ll   !  	  '& 	
 %!o2
a #)$/
# 
	IE"C(%#/rE 
	  s$   AB #-B B B 	B#"B#c                    t        | j                  d      xs d      j                         }t        | j                  d      xs; | j                  d      xs( | j                  d      xs | j                  d      xs d      j                         }| d| }t        |       ry	d
|v ryd|v sd|v sd|v sd|v sd|v sd|v sd|v sd|v ryt	        |      ryt        |      ryy)Nr5   r   r2   r,   r3   r4   r   rD   r   uqloadr'   
egybestvidvidtubevidspeedvidoba
streamwish	filelionsearnvidsupdown      d      )r   rB   r(   rE   r.   r0   )rD   r5   r2   r:   s       r   source_priorityr     s   		( 	 eg 
 		& 	99U	99W	 99V	  eg 	 XQtfD 4 	t4$tt}d|r   c                 (   g }t               }t        | j                  d      xs g       D ]  \  }}t        |t              st        |j                  d      xs; |j                  d      xs( |j                  d      xs |j                  d      xs d      }|sqt        |      r}|j                         j                         }||v r|j                  |       t	        |      }||d<   |j                  |        |j                  d 	       |S )
Nvideosr2   r,   r3   r4   r   _original_indexc                 <    t        |       | j                  dd      fS )Nr   r   )r   rB   r   s    r   r{   z#get_unique_videos.<locals>.<lambda>I  s     E"II'+
 r   r}   )r   r   rB   r@   rA   r   r0   r   r(   r   r   r   )rz   r   r   original_indexrD   r2   r   copieds           r   get_unique_videosr   #  s   F5D!* b"  %&IIf yyyy! yy  
  $<ZZ\'')
e$2 !f?B KK
   Mr   c                 r    t        |      }|sy t        |      ry t        |      sy | j                  |       y r7   )r   r0   r.   r   )urlsr   s     r   add_m3u8_urlr   R  s1    %LEe}5>KKr   c                 h   g }t        | t              sg S | j                  d      }t        |t              rYt        ||j                  d      xs; |j                  d      xs( |j                  d      xs |j                  d      xs d       nt        |t              rt        ||       dD ]  }| j                  |      xs g }t        |t
              s)|D ]  }t        |t              rYt        ||j                  d      xs; |j                  d      xs( |j                  d      xs |j                  d      xs d       lt        |t              s}t        ||         g }t               }|D ]<  }|j                         }	|	s|	|v r|j                  |	       |j                  |	       > |S )Nbestr,   r2   srcfiler   )r   r   sourcesmedialinks)
r@   rA   rB   r   r   listr   r   r   r   )
datar   r   r}   valuesr   outputr   r,   r   s
             r   get_extractor_urlsr   a  s   DdD!	88FD$HHUO xxxx xx 	
 
D#		

  #$"&$' 	E%&IIe$ yy(yy' yy(  E3'	: F5D 
"YY[
j!
" Mr   c                    t        |       } | syt        |       ry	 t        t        j                  j                  | d      z   }|xs t        dd}|r||d<   t        |      }|r||d<   t        j                  j                  ||      }t        j                  j                  |t              5 }|j                  d	      j                  d
d      }d d d        t        j                        }	t!        |	t"              syt%        |	      }
|
D ]  }t'        |||      }|s|c S  	 y# 1 sw Y   WxY w# t(        $ r Y yw xY w)Nr   r   zapplication/json,*/*)rH   rI   rK   rL   rM   rO   i@B rQ   rR   r   )r   r0   EXTRACTr   r   r   rS   r$   rT   rU   rV   r   rW   rY   jsonloadsr@   rA   r   r   r!   )r3   r"   r[   r   rN   r]   rT   r^   rawr   r   r,   r   s                r   extract_first_workingr     sn   
 %LEe}8ll   !  	 $/r,

 !(GI )F$*!..(( ) 

 ^^## $ 
 		 --f 			 zz#$%!$' 	C(#E 	 ?		 		8  s<   BD4 0"D(-D4   D4 !D4 %D4 (D1-D4 4	E ?E c                 <   | j                  dd      }t        | j                  d      xs( | j                  d      xs | j                  d      xs d      }t        | j                  d      xs( | j                  d      xs | j                  d      xs d      }t        | j                  d	      xs | j                  d
      xs d      }t        | j                  d      xs | j                  d      xs d      }t        |       }d}|D ]  }t        |j                  d      xs; |j                  d      xs( |j                  d      xs |j                  d      xs d      }	t        |j                  d      xs( |j                  d      xs |j                  d      xs d      }
t        |j                  d      xs |j                  d      xs d      }t        |j                  d      xs d      }|	st        |	      r|dz  }t	        ||	|      r-t        |	|
xs d|xs t              }|rd|||||f||dd c S =t        |	      r!t        |	|
|      }|rd|||||f||d!d c S it        |	|
|      }|s{d|||||f||d"d c S  d#|||d$S )%Nidr   nametitlezNo Namelogoimageposter_pathrf   categoryMoviessubtitlequalityr   r2   r,   r3   r4   headerr"   referrerr[   
user_agentr5   Unknownr'   )rD   r2   r5   r   r   Tz	fasel-api)okrowtestedr5   source_typez
direct-hlszlocal-extractorF)r  r   r   r  )
rB   r   r   r0   rE   r   rS   r.   r   r   )rz   movie_idr   r   rf   r   r   r  rD   r2   r"   r[   r5   r   s                 r   processr    s<   xxb!H 	88G	88D>	 	D  	88G	88M"	 	D  	88J	E  	88I	H t$FF nIIf yyyy! yy  
 IIh yy#yy$ 	
 IIk" yy&
	 IIh 

 $<! 

 " +*#/r	E   %$#.   4=(#E   %$#/   &
  ! 0 Enb 	 r   c                     t        | d       S )Nc                 (    | d   j                         S )Nr   )r(   )r  s    r   r{   zsort_rows.<locals>.<lambda>  s    A r   r   )sorted)rowss    r   	sort_rowsr    s    & r   c                    t        |       }dg}g }|D ]  \  }}}}}t        |      st        |      r!|j                  dt	        |       dt	        |       dt	        |       d|        |j                  |       |j                  ||||||||||||d|igd        t
        j                  t
        j                  dz         }	t        j                  t        j                  dz         }
|	j                  d	j                  |      d	z   d
       |
j                  t        j                  |dd      d
       |	j                  t
               |
j                  t               y )Nrt   z#EXTINF:-1 tvg-name="z" tvg-logo="z" group-title="z",playUrl)r   r   r   r   r   imgr   r   rf   r2   r,   alturlobjz.tmp
rQ   encodingFr   )ensure_asciiindent)r  r.   r0   r   r   OUT	with_namer   JSON_OUT
write_textjoinr   dumpsr   )r  ordered_rows	m3u_lines
json_itemsr   r   rf   r2   r   m3u_tmpjson_tmps              r   write_filesr#    s   T?LIJ 
) 	t}$<T $T $J<rf		
 	  t
 	/)V mm6G !!H 		)t#  
 

	

    OOCXr   c                  V   t        j                  t        j                  d            } t	        | t
              st        d      | D cg c]  }t	        |t              r| }}g }d}d}t        d |D              }t        dt        |      d       t        d	|d       t        j                  d
d       t        j                  dd       t        t              5 }|D cg c]  }|j!                  t"        |       }}t%        |      D ]  }		 |	j'                         }
|
r|
j-                  d      sS|dz  }t        d|
r|
j-                  dd      nd|
r|
j-                  dd      ndd|
r|
j-                  dd      ndd       {|dz  }|j/                  |
d          t        d||
d   d   d|
j-                  dd      d|
j-                  dd      d|
j-                  dd      d
       |t0        z  dk(  st3        |       t        d|dd|d       
 	 d d d        t3        |       t        d|d |d       t        d!t        d       t        d"t        d       y c c}w c c}w # t(        $ r'}|dz  }t        dt+        |      d       Y d }~d }~ww xY w# 1 sw Y   xY w)#NrQ   r  z%series_full.json must be a JSON arrayr   c              3   D   K   | ]  }t        t        |              y wr7   )r   r   )r9   rz   s     r   r;   zmain.<locals>.<genexpr>  s#       	d#$s    zTOTAL MOVIES:T)flushzTOTAL UNIQUE NON-MP4 SOURCES:z#EXTM3U
z[])max_workersr'   zFAIL EXCEPTION:r  FAILr   r   r   z	| tested:r  r  OKz	| server:r5   z| type:r  zSAVED:moviesz| FAIL:zDONE OK:zFAIL:zM3U:zJSON:)r   r   SRC	read_textr@   r   	TypeErrorrA   sumprintr   r  r  r  r   MAX_WORKERSsubmitr  r   resultr!   reprrB   r   
SAVE_EVERYr#  )itemsrz   valid_itemsr  r  failtotal_sourcesexecutorfuturesfuturer2  errors               r   mainr=    s   JJ 	 	
E eT"3
 	
 dD! 	K  D	
BD  M
 
K 
' NN  
   
 

 E	 $

 	 OO
 
 #7+ :	F D!1	 JJtR(#% JJw+#% JJx+#$
 !GBKKu&ua 

8R(

="-

8Q' J!#D!g:	EN 	
 
 
yP
  		%K 	E EsU   	I"J$I'JI,#CJ:J'J,	J5JJJJJ(__main__)Nr   r   )r	   r   r   r   )r   r   )+r   r   urllib.parser   urllib.requestpathlibr   concurrent.futuresr   r   r+  r  r  r   r   r0  r   r   r4  rS   r   r   r$   r.   r0   rE   r`   rj   rm   r   r   r   r   r   r   r   r   r   r  r  r#  r=  __name__r8   r   r   <module>rD     s    	    ? 34
2389
.:	


% 9.
$(@ ,
^ $ k\*ZgX 9x*Z,^DR GTVrHVL^ zF r   