
    KJi\                     V   d 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m	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlm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mZmZ  ej        d            ej         dej!                  Z"e"j#        Z$d Z% e%d          Z& ej         d          Z' ej         d          Z(d Z)dddddd dfdZ*d6dZ+de	z   dz   Z,de
z   dz   Z-dez   dz   Z. e+e,e-e.          Z/ e+e,e-e.d          Z0d  Z1d7d!Z2 ej         d"          Z3d8d#Z4d$ Z5d% Z6d& Z7eeeed'Z8 e9d(  e: ej;                              D                       Z<e<=                    e8           d) Z>d* Z?d+Z@d,ZAd-ZBd9d.ZCd/ ZD ej         d0          ZE ej         d1          ZFd2 ZGd:d3ZHd7d4ZId;d5ZJdS )<zq
This module provides functions not used directly by the imdb package,
but useful for Cinemagoer-based programs.
    N)escape)IMDbimdbURL_character_baseimdbURL_movie_baseimdbURL_person_base)	IMDbError)	Character)Company)COUNTRY_LANG)_)Movie)Person)TAGS_TO_MODIFY_tagAttrre_characterRef
re_nameRefre_titleRefimdbpyz(http://.+?)(?=\s|$)c                       fd}|S )z4Make a function to pretty-print strings for the web.c                     dz   t          | d          } t          | t                    r|                     d          } | S )Nz|Encode the given string using the %s encoding, and replace
        chars outside the given charset with XML char references.   )quotexmlcharrefreplace)r   
isinstancestrencode)sencodings    L/var/www/html/auto_sub_bot/venv/lib/python3.11/site-packages/imdb/helpers.pycgiPrintz&makeCgiPrintEncoding.<locals>.cgiPrint@   sU    	EGO	P 	P1Aa 	8#677A     )r   r    s   ` r   makeCgiPrintEncodingr#   >   s#         Or!   utf8z%\((.+?)\)sz!<if\s+(.+?)\s*>(.+?)</if\s+\1\s*>c                       fd}|S )a  Create a function useful to handle text[::optional_note] values.
    replaceTxtNotes is a format string, which can include the following
    values: %(text)s and %(notes)s.
    Portions of the text can be conditionally excluded, if one of the
    values is absent. E.g.: <if notes>[%(notes)s]</if notes> will be replaced
    with '[notes]' if notes exists, or by an empty string otherwise.
    The returned function is suitable be passed as applyToValues argument
    of the makeObject2Txt function.c                    }t          | t                    s| S |                     dd          }|d         }i |rdd<   |                    d|          }t	          |          dk    r"dd<   |                    d	|d                   }n|                    d	d
          }fd}t
                              |          r5t
                              ||          }t
                              |          5|S )N::r   r   Ttextz%(text)s   notesz	%(notes)s c                 `    |                      d          v r|                      d          S dS Nr   r)   r+   )group)matchobjkeysDicts    r   _excludeFalseConditionalszCmakeTextNotes.<locals>._replacer.<locals>._excludeFalseConditionalsm   s1    ~~a  H,,~~a(((2r!   )r   r   splitreplacelenre_conditionalsearchsub)r   outSssplitr(   r1   r0   replaceTxtNotess        @r   	_replacerz makeTextNotes.<locals>._replacer\   s   !S!! 	Hq!!ay 	$#HV||J--v;;! $HW<<VAY77DD<<R00D	 	 	 	 	 ##D)) 	G!%%&?FFD ##D)) 	Gr!   r"   )r:   r;   s   ` r   makeTextNotesr<   S   s$        4 r!   z / c                     | S Nr"   )xs    r   <lambda>r@   {   s    1 r!   Tc           	      P      d dddd fd	S )a  "Return a function useful to pretty-print Movie, Person,
    Character and Company instances.

    *movieTxt* -- how to format a Movie object.
    *personTxt* -- how to format a Person object.
    *characterTxt* -- how to format a Character object.
    *companyTxt* -- how to format a Company object.
    *joiner* -- string used to join a list of objects.
    *applyToValues* -- function to apply to values.
    *_recurse* -- if True (default) manage only the given object.
    Nz%(long imdb title)sz%(long imdb name)sc                     dndk    rdS dz  t           t          t          f          r"                    fd D                       S t           t                    rA                    fdt                                                     D                       S i }t           t                    r j        |d<   }nht           t                    r j	        |d	<   }nFt           t                    r j        |d
<   	}n$t           t                    r j        |d<   
}n S  fd}t                              |          r5t                              ||          }t                              |          5t"                              |          D ]}                     |          pt)           |d          }t          |t*                    s#s|rt+          |          }|r |          }n|r t+          |                    }|sd}n$t          |t*                    st+          |          }|                    d|z   dz   |          }|S )zPretty-print objects.Nr      r+   r   c                 *    g | ]} |           S )_limitRecursionr"   ).0orF   
object2txts     r   
<listcomp>z6makeObject2Txt.<locals>.object2txt.<locals>.<listcomp>   s8      .  .  .$% !+
1o N N N  .  .  .r!   c                 P    g | ]"\  }} |           d |           #S )rE   r'   r"   )rG   kvrF   rI   s      r   rJ   z6makeObject2Txt.<locals>.object2txt.<locals>.<listcomp>   s]     0 0 0Q (Z?KKKKK'Z?KKKKM 0 0 0r!   movieIDpersonIDcharacterID	companyIDc                     |                      d          }                    |          pt          |d           }|r|                      d          S dS r-   )r.   getgetattr)r/   	conditionproceedobjs      r   r1   zEmakeObject2Txt.<locals>.object2txt.<locals>._excludeFalseConditionals   sU     q))Iggi((IGCD,I,IG ~~a(((rr!   rE   z%(z)s)r   listtuplejoindictitemsr   rN   r   rO   r	   rP   r
   rQ   r5   r6   r7   re_substfindallrS   rT   r   r3   )rW   rF   objDataoutsr1   keyvalue_recurseapplyToValuescharacterTxt
companyTxtjoinermovieTxtrI   	personTxts   ``     r   rI   z"makeObject2Txt.<locals>.object2txt   s    "OOq  21cD%=)) 		;;  .  .  .  .  .), .  .  . / / /T"" 	;;0 0 0 0 0!#))++..0 0 0  
 c5!! 	!$GIDDV$$ 
	"%,GJDDY'' 	%(_GM"DDW%% 	#&=GK DDJ	 	 	 	 	 ##D)) 	G!%%&?FFD ##D)) 	G##D)) 	: 	:CGGCLL;GCd$;$;EeS)) 2 + + #E

 O&JuoNNNE 2%c%jj11 #s++ #E

<<s
T 1599DDr!   r>   r"   )rh   ri   re   rf   rg   rd   rc   rI   s   ```````@r   makeObject2Txtrj   y   s     ((	+)
; ; ; ; ; ; ; ; ; ; ; ; ;x r!   c                 l     t                    d fd	} |_        |_        |_        |S )ad  Make a function used to pretty-print movies and persons refereces;
    movieTxt and personTxt are the strings used for the substitutions.
    movieTxt must contains %(movieID)s and %(title)s, while personTxt
    must contains %(personID)s and %(name)s and characterTxt %(characterID)s
    and %(name)s; characterTxt is optional, for backward compatibility.Nc                 8   i 	
fd}	fd}	fd}|                      dd                               dd          } t          d	|           } t          j        ||           } t	          j        ||           } t          j        ||           } | S )
z)Substitute movies and persons references.Nc                     |                      d          }                    |          }|r'|j        }|t           |          d          dz  }|S )Nr   r   )rN   title)r.   rS   rN   r   )match
to_replaceitemrN   	_cgiPrintr   rh   
titlesRefss       r   _replaceMoviez;makeModCGILinks.<locals>.modCGILinks.<locals>._replaceMovie   sk    QJ>>*--D ,%& :!6!6BUVV) ) 
 r!   c                     |                      d          }                    |          }|r'|j        }|t           |          d          dz  }|S )Nr   r   )rO   name)r.   rS   rO   r   )ro   rp   rq   rO   rr   r   	namesRefsri   s       r   _replacePersonz<makeModCGILinks.<locals>.modCGILinks.<locals>._replacePerson   sk    QJ==,,D =& (		* 5 5xATUU* * 
 r!   c                     |                      d          }|S                     |          }|r+|j        }||S |t           |          d          dz  }|S )Nr   r   )rP   rv   )r.   rS   rP   r   )ro   rp   rq   rP   rr   characterRefsre   r   s       r   _replaceCharacterz?makeModCGILinks.<locals>.modCGILinks.<locals>._replaceCharacter   s    QJ#!! $$Z00D ".&%%)#.		* 5 5xATUU- - 
 r!   <z&lt;>z&gt;z<a href="\1">\1</a>)r3   _re_hrefsubr   r7   r   r   )r   rs   rw   rz   rt   rx   r{   rr   re   r   rh   ri   s    ```   r   modCGILinksz$makeModCGILinks.<locals>.modCGILinks   s     M		 		 		 		 		 		 		 				 		 		 		 		 		 		 			 	 	 	 	 	 	 	 IIc6""**377.22OM1--N>1-- 1155r!   r>   )r#   rh   ri   re   )rh   ri   re   r   r   rr   s   ```` @r   makeModCGILinksr      si     %X..I/ / / / / / / / / /` $K%K+Kr!   z	<a href="ztt%(movieID)s">%(title)s</a>znm%(personID)s">%(name)s</a>zch%(characterID)s">%(name)s</a>)rh   ri   re   ascii)rh   ri   re   r   c                     t          |                     di                                                     }|                                 |S )z4Return a sorted list of seasons of the given series.episodes)rX   rS   keyssort)mseasonss     r   sortedSeasonsr     s;    155R((--//00GLLNNNNr!   c                    g }|}|t          |           }nt          |t          t          f          s|g}|D ]}t          |                     di                               |i                                                     }|                                 |D ])}|                    | d         |         |                    *|S )z{Return a sorted list of episodes of the given series,
    considering only the specified season(s) (every season, if None).Nr   )r   r   rY   rX   rS   r   r   append)r   seasonr   r   r   eps_indxes          r   sortedEpisodesr   !  s     HG~""&5$-00 	hG 1 1j"--11!R88==??@@ 	1 	1AOOAjM!,Q/0000	1Or!   z\b(nm|tt|ch|co)([0-9]{7,8})\bc                    |g }|i }t          |i |}t                              |           }|sdS |                    d          }|                    d          }|dk    r|                    ||          S |dk    r|                    ||          S |dk    r|                    ||          S |dk    r|                    ||          S dS )	aJ  Return a Movie, Person, Character or Company object for the given URL;
    info is the info set to retrieve, args and kwds are respectively a list
    and a dictionary or arguments to initialize the data access system.
    Returns None if unable to correctly parse the url; can raise
    exceptions if unable to retrieve the data.Nr   r)   tt)infonmchco)r   _re_imdbIDurlr6   r.   	get_movie
get_personget_characterget_company)urlr   argskwdsiaro   imdbtypeimdbIDs           r   	get_byURLr   7  s     ||	t	t		B  %%E t{{1~~H[[^^F4||F|...	T		}}V$}///	T		T222	T		~~f4~0004r!   c                 *    |                                  S )aG  Given an URL string or a Movie, Person or Character instance,
    returns an URL to the full-size version of the cover/headshot,
    or None otherwise.  This function is obsolete: the same information
    are available as keys: 'full-size cover url' and 'full-size headshot',
    respectively for movies and persons/characters.)get_fullsizeURL)rW   s    r   fullSizeCoverURLr   U  s        r!   c                 .    t          | d          d         S )zReturn a key (the ones used to access information in Movie and
    other classes instances) converted to the style of the XML output.r+   r   )r   ra   s    r   keyToXMLr   ^  s     CQr!   c                 :    t          t          |                     S )zTranslate a given key.)r   r   r   s    r   translateKeyr   d  s    Xc]]r!   )personmovie	charactercompanyc                 "    g | ]}|d          dfS )r   Nr"   rG   r?   s     r   rJ   rJ   r  s     JJJqqtTlJJJr!   c                     |                      d          }|r*|                      d          dk    rt          |          }|S | j        S )zOReturn the name of the tag, taking it from the 'key' attribute,
    if present.ra   keytypeint)rS   r   tag)r   keyAttrs     r   tagToKeyr   v  sI     ggennG 779&&'llG7Nr!   c                     |                      d          }|dk    rt          |          }n|dk    rt          |          }|S )z0Return tagValue, handling some type conversions.typer   float)rS   r   r   )r   tagValuetagTypes      r   _valueWithTyper     sD    ggfooG%x==	G		??Or!   )	imdbindexkindyear)r   )r   countryc                 l	   d|i }|i }t          |           |                                 pdgd         }| j        pd                                }|sJdk    rD|                                 }|r.|d         j        r!|d         j        pd                                }|                     d          }|r.||<   |                    |g                                          | j        t          v rt                               |                     d          }	|	r|s|	}n|}	|	_	        g }
|                     d          }dk    rU|_
        t          }
|                     d	          }|/                    |j                   |                     |           nŉd
k    r;|_        t           }
|                     d          }|s|                     d          }nSdk    r$|_        t           }
|                     d          }n)dk    r#|_        t&          }
|                     d          }|/                    |j                   |                     |           |
D ]M}|j        v r|                     |          }|*t-          ||j                  j        t          |          <   N|                     d          }|!|j        _        |                     |           |                     d          }|1t1          |d|||          j        d<   |                     |           |                     d          }|0t1          |d|||          }|_        |                     |           |sS fd}n|rO|                     d          }|&|j        pd                                }|r|d|z  z  }nt-          | |          }|S |(t          |          }|t4          v rg fd}ni fd}ni fd}|                                 D ]X}t          |          }| j        t          v r|                                v r6t1          |d|||          }|r |||           Y|r=t          v r4|_        |_        t=          |                                          _         S )z!Recursively parse a tree of tags.Nr   r+   rq   infosetzaccess-systemidr   rn   r   zlong imdb canonical namerv   r   r   r*   z
episode-ofF)	_topLevel_as_infoset2keys_key2infosetz
episode ofzcurrent-rolec                 <    j                             | |i          S r>   )dataupdatera   rb   rq   s     r   r@   zparseTags.<locals>.<lambda>  s    DI$4$4c5\$B$B r!   z::%sc                 .                         |          S r>   )r   r   s     r   r@   zparseTags.<locals>.<lambda>  s    E(:(: r!   c                 2                         | |i          S r>   r   r   s     r   r@   zparseTags.<locals>.<lambda>  s    S%L(A(A r!   c                 2                         |i          S r>   r   )ra   rb   rq   rv   s     r   r@   zparseTags.<locals>.<lambda>  s    DKKu$>$> r!   )!r   getchildrenr(   striprS   
setdefaultr   r   _MAP_TOP_OBJaccessSystemrN   
_titleTagsfind	set_titleremoverO   	_nameTagsrP   rQ   _companyTagsset_namer   r   r*   	parseTagscurrentRole_TAGS_TO_LIST_additional_keysinfoset2keyskey2infosetrX   r   current_info)r   r   r   r   r   
firstChildtagStr
tagContentr   itemAs	tagsToGettheIDttitletheNametdataTagnotesTag	episodeOfcRolecr_addertagNotesr*   firstChildNamesubTag	subTagKeysubItemrq   rv   s                              @@r   r   r     sK    DC==D//##-vq1Jhn"##%%F 8dfnn__&&
 	8*Q-, 	8 m(.B5577Fggi  G ;$T  "--44T:::
w,D!##)) 	 F"	7?? DL"IXXg&&F!v{+++

6"""x %%	((#=>> /!hhv..G$$#( %	((6**""!&(	((6**"gl+++

7### 	U 	UADI~~hhqkkG"/=gw|/T/T	(7++,88G$$!DJJJx   HH\**	 &/	U47}=I'K 'K 'KDIl# JJy!!!((5Es)6\S S SB!DJJu  	KBBBB	 ?88G$$](b//11E )&5.(#C00F		!*--]**D::::FFDAAAAFF>>>>>//## ' 'V$$	7l""yD4I4I4K4K'K'KFe*7lT T T 	'F9g&&& 7T\)))' !3!3!5!566Kr!   c                 X    ddl }t          |j                            |                     S )ziParse a XML string, returning an appropriate object (usually an
    instance of a subclass of _Container.r   N)
lxml.etreer   etree
fromstring)xmllxmls     r   parseXMLr     s.     TZ**3//000r!   z (?:[(])([a-zA-Z]+?)(?: title[)])z\(.*?\)c                    g }t          |                     d          pg |                     d          pg z             }|D ]}t          j        d|                              dd          }t          |          dk    rt                              |d                   }|r|                                d         }ne|d                             d          d         }t          
                    d|                                          }t          j        |          }nd	}|                    ||d         f           |S )
zhGiven a movie, return a list of tuples in (lang, AKA) format;
    lang can be None, if unable to detect.akaszakas from release infoz^(.*) \((.*?)\)r   r)   r   ,r+   N)setrS   rer6   r.   r4   _re_akas_langgroupsr2   _re_akas_countryr7   r   r   r   )r   lang_and_akar   akalanguager   s         r   akasLanguagesr    s.    L		&!!'REII6N,O,O,USUVWWD 0 0i*C0066q!<<s88q==$++CF33H 5#??,,Q/ a&,,s++A.*..r7;;AACC'+G44HXs1v.////r!   c                 N   |                                  }| d                                         }|                                }g }t          j        d||                                          }|                    || d         df           t          |           D ]f\  }}	|	                                }t          j        d||                                          }|r||k    r|dz  }|                    ||	|f           g|                    d           |rd |D             S |S )a@  Return a list of movie AKAs, sorted by their similarity to
    the given title.
    If _titlesOnly is not True, similarity information are returned.
    If _preferredLang is specified, AKAs in the given language will get
    a higher score.
    The return is a list of title, or a list of tuples if _titlesOnly is False.rn   Nr   Treversec                     g | ]
}|d          S r   r"   r   s     r   rJ   z(sortAKAsBySimilarity.<locals>.<listcomp>O      %%%!%%%r!   )guessLanguagelowerdifflibSequenceMatcherratior   r  r   )
r   rn   _titlesOnly_preferredLangr  m_titlel_titlescoresscorer  s
             r   sortAKAsBySimilarityr  6  s-    ""$$HGn""$$GkkmmGF#D'7;;AACCE
MM5%.$/000&u-- . .#))++'gw??EEGG 	n88QJEuc8,----
KKK &%%f%%%%Mr!   c           	      \   g }t          |           D ] \  }}||k    r|                    |           !|rwg }|D ]P}|                    t          j        d|                                |                                          |           Q|                    d           d |D             }|S )zReturn a list of AKAs of a movie, in the specified language.
    If _searchedTitle is given, the AKAs are sorted by their similarity
    to it.NTr	  c                     g | ]
}|d          S r  r"   r   s     r   rJ   z%getAKAsInLanguage.<locals>.<listcomp>a  r  r!   )r  r   r  r  r  r   )r   lang_searchedTitler   r  r  r  s          r   getAKAsInLanguager  S  s     D&u--  #8KK & 	P 	PCMM'1$		2@2F2F2H2HJ JKNP P P PD!!!%%f%%%Kr!   c                 ,   |r|nd}	 t          j        ||           d         }n # t          $ r t          d| z            w xY wd| v r|dz  }d| vr|dz  }n|dz  }|r|d|z  z  }|r|d	|z  z  }|rd
                    |          }|d|z  z  }|dz  }|S )z%Return resized and cropped image url.z(https://m.media-amazon.com/images/\w/\w+r   z)Image url not matched. Original url: "%s"z@@@z._V1_z@._V1_zSX%s_zSY%s_r   zCR%s_z.jpg)r   r^   
IndexErrorr   rZ   )imagewidthheightcropcustom_regexregexStringresultImagecropValss           r   resizeImager*  e  s     #/_,,4_KOje44Q7 O O OCuMNNNO u}}s
%wx 'w& (w'' *88D>>w))6Ks	   $ A)Nr$   r>   )NNN)TNNN)TN)NNNN)K__doc__r  gettextr   htmlr   imdbr   r   r   r   imdb._exceptionsr   imdb.Characterr	   imdb.Companyr
   imdb.linguisticsr   imdb.localer   
imdb.Movier   imdb.Personr   
imdb.utilsr   r   r   r   r   
textdomaincompileI_re_hrefr7   r~   r#   r    r]   r5   r<   rj   r   	_movieTxt
_personTxt_characterTxtmodHtmlLinksmodHtmlLinksASCIIr   r   r   r   r   r   r   r   r[   rX   valuesr   r   r   r   r   r   r   r   r   r  r  r  r  r  r*  r"   r!   r   <module>rA     s0  $    				                  ' & & & & & $ $ $ $ $ $             ) ) ) ) ) )                                 8    2:-rt44l	 	 	  '' 2:n%%@AA# # #L !Dt"5!,tT T T Tn; ; ; ;~ ,,/MM	..1OO
4412	Z,9; ; ;#OY*1>-46 6 6 
     & 
;<<   <! ! !        	  JJDD1F1F1H1H,I,IJJJKK   \ " " "     +
	'z z z zz1 1 1 
=>>2:j)) 
  6   :   $     r!   