
    Mg                         S SK JrJrJr  S SKrS SKJr  S SKJr  S SK	r	S SK
J
r
JrJr   " S S\5      r " S S	\5      r\" S
S9\" S5      \
" S5       " S S5      5       5       5       rg)    )ListTupleSetN)BeautifulSoup)Fore)pluginaliasrequirec                       \ rS rSrSrSrg)HackerNewsRequestException   z)Custom exception class for request errors N__name__
__module____qualname____firstlineno____doc____static_attributes__r       @/Users/admin/workspace/ai/Jarvis/jarviscli/plugins/hackernews.pyr   r          3r   r   c                       \ rS rSrSrSrg)HackerNewsInvalidInputException   z)Custom exception class for invalid inputsr   Nr   r   r   r   r   r      r   r   r   T)networkreadhn
hackernewsc                       \ rS rSrSrSrSSS\SS4S	 jrSSS\\	\\4      4S
 jr
SSS\\   4S jrS\\   SS4S jrSrg)
HackerNews   zlList titles from https://news.ycombinator.com/

Selected titles are opened in the browser as separate tabs.
zhttps://news.ycombinator.com/jarvis	JarvisAPIsreturnNc                      U R                  U5      U l        U R                  U5      nU R                  U5        g! [        [
        4 a1  nUR                  [        U5      [        R                  S9   SnAgSnAff = f)zList titles, get selected titles from user and open them in the browser

:type jarvis: JarvisAPI
:param jarvis: JarvisAPI instance
:type s: str
:param s: Text to say
)colorN)
_list_titlestitles_get_selected_titles_open_selected_titlesr   r   saystrr   RED)selfr"   r$   selected_titlesexps        r   __call__HackerNews.__call__   se    	1++F3DK"77?O&&7*,KL 	1JJs3xtxxJ0	1s   8; A<'A77A<c                 N   / n [         R                  " U R                  5      nUR                  5         [	        UR
                  S5      nUR                  S5      n[        USS9 HW  u  pgUR                  SR                  XgR                  5       5      5        UR                  UR                  5       US   45        MY     U$ ! [         R                  R                  [         R                  R                  [         R                  R                  4 a  n[!        U5      UeSnAff = f)zList titles

If any errors are encountered during the request,
HackerNewsRequestException will be raised.

:type jarvis: JarvisAPI
:param jarvis: JarvisAPI instance
:rtype: list
:returns: List of titles as (title, link) tuples
zhtml.parserza.titlelink   )startz
[{:>2}] {}hrefN)requestsgetURLraise_for_statusr   contentselect	enumerater,   formatgetTextappend
exceptionsConnectionErrorTimeout	HTTPErrorr   )	r/   r"   r)   responseparsed_pagetitle_elementsindexelementr1   s	            r   r(   HackerNews._list_titles/   s     	D||DHH-H%%' ((8(8-HK(//>N"+N!"D

<..uoo6GHIw0'&/BC #E  //''))
 	;
 -S1s:	;s   0C AD$DD$c                 F   UR                  S[        R                  S9nUR                  5        Vs/ s H$  o3R	                  5       (       d  M  [        U5      PM&     nnU(       d  [        S5      eU H  nSUs=::  a  S::  a  M  O  [        S5      e   [        U5      $ s  snf )zGet selected indexes from user

If there is no index selected or indexes are in the invalid range,
HackerNewsInvalidInputException will be raised.

:type jarvis: JarvisAPI
:param jarvis: JarvisAPI instance
:rtype: set
:returns: Set of selected indexes
z 
Enter requested title indexes: )promptr'   zNo index selected!r5      z'Invalid index! Should be in range 1..30)inputr   YELLOWsplitisdigitintr   set)r/   r"   indexesrI   selected_indexess        r   r*   HackerNews._get_selected_titlesQ   s     ,,6dkk  
 5<MMOWO5}}JCJOW12FGG%E##5=  & #$$ Xs   BBr0   c                     U HC  nU R                   US-
     S   nSU;   a  U R                  U-   n[        R                  " U5        ME     g)zsOpen selected titles in the browser as new tabs

:type selected_titles: set
:param selected_titles: Set of indexes
r5   zitem?N)r)   r:   
webbrowseropen_new_tab)r/   r0   rI   links       r   r+    HackerNews._open_selected_titlesn   sI     %E;;uqy)!,D $xx$##D) %r   )r)   )r   r   r   r   r   r:   r-   r2   r   r   r(   r   rS   r*   r+   r   r   r   r   r    r       sw    
 *C1{ 1s 1t 1" ;  4c3h3H  D%; %3s8 %:*SX *$ *r   r    )typingr   r   r   rY   bs4r   coloramar   r8   r   r	   r
   	Exceptionr   r   r    r   r   r   <module>ra      so    # #     ) )4 44i 4 	xf* f*   f*r   