
    Mg(                     t    S SK r S SKrS SKrS SKJr  S SKJrJr  \" SS9\" S5       " S S5      5       5       rg)	    N)Fore)pluginrequireT)networknewsc                   f    \ rS rSrS rS r\S 5       rS rS r	S r
S rS	 rS
 rS rS rS rSrg)News
   c                     / SQU l         0 U l        U R                    H6  nXR                  [        U R                   R                  U5      S-   5      '   M8     g )N)	bloombergzfinancial-timescnbcreuterszal-jazeera-englishzthe-wall-street-journalzthe-huffington-postzbusiness-insiderzthe-new-york-timeszabc-newszfox-newscnnzgoogle-newswired   )sourcessource_dictstrindex)selfsources     :/Users/admin/workspace/ai/Jarvis/jarviscli/plugins/news.py__init__News.__init__   sJ     llFDJS!3!3F!;a!?@A #    c                 t   US:X  a  UR                  S5      nUR                  5       S:X  d  UR                  5       S:X  a!  UR                  S[        R                  5        g U R                  X5        UR                  S[        R                  5        g US:X  a  U R                  U5        g US:X  a  U R                  U5        g US	:X  a  UR                  S
5        UR                  S5        UR                  S
5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        UR                  S5        g US:X  a  U R                  U5      nU(       d!  UR                  S[        R                  5        g 0 nU H"  nXe[        UR                  U5      S-   5      '   M$     [        UR                  5        Vs/ s H  n[        U5      PM     sn5       H/  nUR                  [        U5      S-   U[        U5         -   5        M1     g U R                  U5      ca  UR                  S[        R                  5        UR                  S[        R                  5        UR                  S[        R                  5        g US:X  d  US:X  a"  U R!                  U R#                  U5      U5        g UR%                  S5      n	SU	;   a  U	R'                  S5        SU	;   a  U	R'                  S5        U R!                  U R)                  X5      U5        g s  snf )N	updatekeyz5Please enter your NEWS API key (q or Enter go back): q z#Could not update the NEWS API key! z#NEWS API key successfully updated! 	configureremovehelpz%-------------------------------------zCommand		 | Descriptionznews		 : Finds top headlinesz6news updatekey	 : Updates the news API key of the userz9news configure	 : Configures the news channel of the userz0news sources	 : List the configured news sourceszAnews remove	 : Removes a source from the news channel of the userz2news [word]	 : Finds articles related to that wordr   z;No sources configured. Use 'news configure' to add sources.r    : zMissing API keyz)Visit https://newsapi.org/ to get the keyz*Use 'news updatekey' command to add a key
 )inputlowersayr   REDupdate_api_keyGREENr    remove_sourceget_news_sourcesr   r   sortedkeysintget_api_keyparse_articlesget_headlinessplitr!   get_news)
r   jarvisskeyr   dicr   xr   
searchlists
             r   __call__News.__call__#   s   ,,GICyy{c!SYY[B%6

@$((K##F0

@$**M+NN6"(]v&&[JJ>?JJ23JJ>?JJ78JJIKJJLNJJJKJJTVJJLM)^++F3G

QHH %F:@GMM&1A567 & $SXXZ$@ZSVZ$@AEJJs5zE1CE
OCD Bf%-JJ($((3JJBDHHMJJ? "WS 2 26 :FCJZ!!"%j !!#&f A6J! %As   #L5c                 $    U R                  S5      $ )z
will return either the news_api key of the user, already stored in the memory.json
file or None in case the user does not have his own api
news-settingsget_data)r5   s    r   r0   News.get_api_keyZ   s     //r   c                 H    UR                  SU5        U R                  U5      $ )zc
the user might have a news api key and they might want to add to memory.json or update an old one
r>   )update_datar0   )r   r5   api_keys      r   r)   News.update_api_keyb   s%     	?G4''r   c                 2    UR                  S5      nUc  / nU$ )zM
returns a list of all the new sources added to the news channel of the user
news-sourcesr?   )r   r5   r   s      r   r,   News.get_news_sourcesi   s!     //.1?Gr   c                 $   U R                  U5      nX#;  aG  UR                  U5        UR                  SU5        UR                  US-   [        R
                  5        O#UR                  US-   [        R
                  5        U R                  U5      $ )zJ
adds a new source (if it does not exist) to the news channel of the user
rG   z2 has been successfully been added to your sources!z& was already included in your sources!)r,   appendrC   r'   r   r*   )r   r5   news_sourcer   s       r   
add_sourceNews.add_sourcer   s     ''/%NN;'~w7JJFG


 JJ:;

 $$V,,r   c                 ,   U R                  U5      n0 nU H"  nXC[        UR                  U5      S-   5      '   M$     [        UR	                  5        Vs/ s H  n[        U5      PM     sn5       H/  nUR                  [        U5      S-   U[        U5         -   5        M1     UR                  S5      nUR                  S5      nSU;   a  UR                  S5        SU;   a  UR                  S5        U H  n[        U5      U;   aV  U[        U5         nUR                  U5        UR                  SU5        UR                  US-   [        R                  5        Mh  UR                  S[        R                  5        M     U R                  U5      $ s  snf )	z8
removes a new source from the news channel of the user
r   r#   z_Type the indexes of the sources you would like to remove from your channel separated by space: r$   r   rG   z6 has been successfully removed from your news channel!zIndex not found!)r,   r   r   r-   r.   r/   r'   r%   r3   r!   rC   r   r*   r(   )r   r5   r   r8   r   r9   r   
index_lists           r   r+   News.remove_source   sf    ''/F28GMM&)A-./  SXXZ8ZSVZ89EJJs5zE)CE
O;< :\\
  %%c*
*c"b!E5zS SZv&"">7;

NOJJ 
 

-txx8   $$V,,+ 9s   Fc           	         [        U R                  R                  5        Vs/ s H  n[        U5      PM     sn5       HE  nUR	                  [        U5      S-   U R                  R                  [        U5      5      -   5        MG     UR                  S5      nUR                  S5      nSU;   a  UR                  S5        SU;   a  UR                  S5        U Ho  nX0R                  R                  5       ;   a,  U R                  XR                  R                  X35      5        ML  UR	                  US-   [        R                  5        Mq     gs  snf )z)
configures the news channel of the user
: zZType the indexes of the sources you would like to add to your channel separated by space: r$   r    is not a valid indexN)r-   r   r.   r/   r'   r   getr%   r3   r!   rL   r   r(   )r   r5   r9   r   rO   s        r   r    News.configure   s    T-=-=-B-B-DE-DSV-DEFEJJs5zD(4+;+;+?+?E
+KKL G\\
  %%c*
*c"b!E((--//(8(8(<(<U(JK

5#::DHHE	   Fs   Ec                     U R                  U5      n[        U5      S:X  a5  UR                  S[        R                  5        SU R                  U5      -   nO)SnU H
  nX4S-   -  nM     USU R                  U5      -   -  nU R                  X5      $ )zo
gets top headlines for a quick lookup of the world news, based on the news channel of the user (if it exists)
r   z:You have not configured any source. Getting top headlines
z7https://newsapi.org/v2/top-headlines?country=us&apiKey=z-https://newsapi.org/v2/top-headlines?sources=,&apiKey=)r,   lenr'   r   r*   r0   _get)r   r5   r   urlr   s        r   r2   News.get_headlines   s     ''/w<1JJM

 L  ()C BC!|# ": 0 0 888Cyy%%r   c                     U R                  U5      nSnU H
  nXES-   -  nM     [        U5      S:w  a  US-  nU H
  nXFS-   -  nM     USU R                  U5      -   -  nU R                  X5      $ )zh
gets top news based on a particular search list , based on the news channel of the user (if it exists)
z$https://newsapi.org/v2/everything?q=z%20r   z	&sources=rW   rX   )r,   rY   r0   rZ   )r   r5   r:   r   r[   ir   s          r   r4   News.get_news   s     ''/4Au9C w<1;C!|# "zD,,V444yy%%r   c                    [         R                  " U5      nUR                  [         R                  R                  :X  a"  [
        R                  " UR                  5      nU$ UR                  S:X  a!  UR                  S[        R                  5        gUR                  SUR                  5       -   [        R                  5        g)zfetch a webpagei  zAPI key not validzAn error occured: Error code: N)requestsrT   status_codecodesokjsonloadstextr'   r   r(   raise_for_status)r   r5   r[   responsedatas        r   rZ   	News._get   s    <<$8>>#4#44::hmm,DK##s*

.9  

;%6689:>((Dr   c                    0 nSnUc!  UR                  S[        R                  5        g US   (       d!  UR                  S[        R                  5        g US    H/  nUR                  [        U5      S-   US   -   5        XSU'   US-  nM1     UR                  S5        UR                  S5        UR	                  5       nUR                  5       S	:X  a  [        R                  " S
5        g  [        U5        [        U5      US-
  :  a-  UR                  [        U5      S-   [        R                  5        g [        U5      S:X  a  g  UR                  U[        U5         S   5        UR                  S5        UR	                  5       nUR                  5       S:X  d  UR                  5       S:X  a%  [        R                  " U[        U5         S   5        g ! [         a#    UR                  S[        R                  5         g f = f)Nr   zNo Articlesarticlesz No Articles matching the word(s)rR   titlez0
Powered by News API. Type NewsAPI to learn morez9
Type index to expand news, 0 to return to jarvis prompt
newsapizhttps://newsapi.org/rS   r   zNot a valid indexdescriptionz$Do you want to read more? (yes/no): yesyr[   )
r'   r   r(   r   r%   r&   
webbrowseropenr/   BaseException)r   rj   r5   article_listr   articleidxr^   s           r   r1   News.parse_articles   s   <JJ}dhh/JJJ9488DJ'GJJs5zD(77+;;<")QJE ( 	

FG

PQ lln99;)#OO23		H3x519%

3s8&==txxHSQ  	

<C)-89

9:LLN779c!1OOLS259:  	JJ*DHH5	s   *A	G 4G *H H)r   r   N)__name__
__module____qualname____firstlineno__r   r;   staticmethodr0   r)   r,   rL   r+   r    r2   r4   rZ   r1   __static_attributes__ r   r   r	   r	   
   sR    K*5Kn 0 0(-&-BF(&&&"-r   r	   )re   ra   rs   coloramar   r   r   r	   r   r   r   <module>r      sC        " 	M M  Mr   