
    Mg                         S r SSKJr  SSKJrJrJr  SSKrSSKr\" S5      \" SS9\" S5       " S	 S
5      5       5       5       rg)a4  Twitter Trends plugin.

This plugin prints a list of countries with their unique id and returns
the top 10 trends in Twitter for this specific country.

Typical usage example:

>twitter trends
>number_of_country

@Author: Leonidha Mara @leonidhaMara & Emmanouil Manousakis @manousakis01
@Date: 7th May, 2022
    )Fore)pluginrequirealiasN	trendtwitT)networkztwitter trendsc                   ~    \ rS rSrS rS rS rS rS\S\	4S jr
S	 rS
\4S jrS rS rS rS\4S jrS rS rSrg)TwitterTrends   c                 t    Xl         U R                  c  U R                  5       U l        U R                  5         g )N)jarvis	countriesavailable_countriesmain)selfr   ss      D/Users/admin/workspace/ai/Jarvis/jarviscli/plugins/twitter_trends.py__call__TwitterTrends.__call__   s*    >>!!557DN		    c                      S U l         SU l        g )Nexit)r   exit_msg)r   s    r   __init__TwitterTrends.__init__   s    r   c                 6   U R                  5         U R                  R                  S[        R                  S9   U R                  5       nU R                  U5      (       a  g U R                  U5      nU R                  U5        U R                  5       (       a  g Ma  )NzTo exit enter word 'exit'color)
display_countriesr   sayr   YELLOWget_countryis_exit_inputget_trends_fromdisplay_trendsis_end)r   country_coderetrivied_trendss      r   r   TwitterTrends.main#   s     34;;G++-L!!,//#33LA 01{{}} r   c                 ~    U R                   R                  S[        R                  S9nUR	                  5       S:X  a  gg )NzDo you want to continue? (Y/N) r   nT)r   inputr   REDlower)r   inps     r   r&   TwitterTrends.is_end0   s:    kk-TXX   ?99;# r   xreturnc                 P    [        U R                  5      S-
  nX:  d  US:  a  S$ S$ )N   r   TF)lenr   )r   r1   upper_bounds      r   is_out_of_rangeTwitterTrends.is_out_of_range6   s*    $..)A-AE>>r   c                     SnU R                  U5      nUS   n[        5       n[        U5       H  u  pVUS   US   /XE'   M     SS/U[        U5      '   U$ )zs
Return a dictionary of the supported countries.
Key: number of country,
Value: [country name, country url suffix]
z1https://twitter-trends-api.azharimm.site/locationdatalocation_pathlocation_url	worldwidez/trends?location=)get_jsondict	enumerater5   )r   URL	json_textcountries_responser   keycountrys          r   r   !TwitterTrends.available_countries:   ss     BMM#&	&v.F	%&89LC%o6%n57IN : /:;N-O	#()*r   rA   c                      [         R                  " U5      n[        R
                  " WR                  5      $ !   U R                  R                  S5         N?= f)z+
Return data from given url in json format
z!Can not reach URL for the moment.)requestsgetr   r    jsonloadstext)r   rA   responses      r   r>   TwitterTrends.get_jsonL   sD    	5||C(H zz(--((	5KKOO35s	   8 Ac                    SnU(       dX   U R                   R                  S[        R                  S9nU R	                  [        U5      5      (       a  [        e U(       d  MX  [        U5      $ ! [         a_    U R                  U5      (       a   gU R                   R                  S[        U R                  5      S-
   S3[        R                  S9  Sn N|f = f)zW
Get user input and validate it.
Input must be a number that corresponds to a country.
NzChoose country: r   r   zPlease select a number (0 - r4   ))r   r,   r   GREENis_valid_inputint
ValueErrorr#   r    r5   r   r!   )r   r'   s     r   r"   TwitterTrends.get_countryW   s    
 
$#{{00&djj  1  :&&s<'899$$ :	 , <    $%%l33!23t~~3F3J2K1MUYU`U`   b#$s   AA- - CACCc                 V    [        U5      [        :X  a  UR                  5       S:X  a  gg g )Nr   T)typestrr.   r   r,   s     r   r#   TwitterTrends.is_exit_inputl   s&    K35;;=F#: $;r   c                 Z    [        U5      [        :X  a  U R                  U5      (       a  gg g )NT)rW   rS   r7   rY   s     r   rR   TwitterTrends.is_valid_inputp   s)    ;#$"6"6u"="= #>r   rE   c                     SU R                   U   S    3nU R                  U5      n[        5       nUS   S   S   S   nUSS  H+  nUS   nS	nUS
   (       a  US
   nUR                  Xx/5        M-     U$ )z5
Get the top 10 Twitter trends of the given country.
z(https://twitter-trends-api.azharimm.siter4   r:   trendsr   N
   name-tweet_count)r   r>   listappend)	r   rE   rA   rB   r^   trends_infotrendr`   rb   s	            r   r$   TwitterTrends.get_trends_fromt   s     99PQR9S8TUMM#&	'1!4V< "%E=DK]##M2MM4-. & r   c                     SnU R                    HF  nU R                  R                  US  SS  U R                   U   U   R                  5        35        MH     g)z*
Display available countries to the user.
r      .N)r   r   r    upper)r   country_name_positioncountry_keys      r   r   TwitterTrends.display_countries   s[    
 !">>KKKOOs#C9T^^K-HI^-_-e-e-g,hik *r   c                     U R                   R                  SSS  SS  35        [        U5       H9  u  p#U R                   R                  US-   S  SS	  US
   S  US   S  35        M;     g )Nz     Trend   Tweets   r4   ri   rj      r      )r   r    r@   )r   r^   ivs       r   r%   TwitterTrends.display_trends   ss    %~ht_=>f%DAKKOOq1uaSk#qc1Q4+adB4[IJ &r   )r   r   r   N)__name__
__module____qualname____firstlineno__r   r   r   r&   rS   boolr7   r   rX   r>   r"   r#   rR   r$   r   r%   __static_attributes__ r   r   r
   r
      sa    ? ? ?$	)C 	)!*s "kKr   r
   )	__doc__coloramar   r   r   r   rH   rJ   r
   r   r   r   <module>r      sZ     ) )   {zK zK   zKr   