
    Si/W                     h    d dl mZ ddlZd dlmZmZmZmZ ddlZdZ	dZ
dZdZd	Zd
ZdZ G d d      Zy)   )PublicationParser    N)AuthorAuthorSourcePublicationSourcePublicAccesszuser=([\w-]*)zhttps://scholar.google.com{0}d   zVerified email at z/citations?hl=en&user={0}z1/citations?view_op=list_colleagues&hl=en&user={0}zE/citations?hl=en&tzom=300&user={0}&view_op=list_mandates&pagesize={1}c                       e Zd ZdZd ZdefdZd Zd Zd Z	d Z
d	 ZddedefdZd Zd Zd Zg dd
fdedefdZd Zy)AuthorParserz%Returns an object for a single authorc                 $    || _         g d| _        y )N)basicsindicescounts	coauthorspublicationspublic_access)nav	_sections)selfr   s     O/home/agent/.friday_env/lib/python3.12/site-packages/scholarly/author_parser.py__init__zAuthorParser.__init__   s    +    returnc                    ddi}g |d<   t        |t              r||d<   t        j                  |d<   |S t        j                  |d<   t        j                  t         |d      d   d         d   |d<   d	j                  |d         }t        j                  |      |d
<   | j                  |dd      }|j                  d|      j                  |d<   | j                  |dd      }|j                  d|      }|r|j                  |d<   | j                  |dd      }|j                  d|      }|r(t        j                  t        d|j                        |d<   | j                  |dd      }	|	r?|j                  d|	      }
|
D cg c]  }|j                  j!                          c}|d<   ng |d<   | j                  |dd      }|j                  d|      }|r*|j                  dk7  rt#        |j                  dd       |d<   |S c c}w )z7 Fills the information for an author container
        container_typer   filled
scholar_idsourcear   hrefz'/citations?view_op=medium_photo&user={}url_pictureh3nameclass_divaffaffiliationeml@email_domainone_int	interestscby 	   Ncitedby)
isinstancestrr   AUTHOR_PROFILE_PAGESEARCH_AUTHOR_SNIPPETSrefindall_CITATIONAUTHREformat_HOST_find_tag_class_namefindtextsub_EMAILAUTHORREfind_allstripint)r   _AuthorParser__dataauthorpic
name_class	aff_classr(   email_classemail	int_classr-   icitedby_classr1   s                 r   
get_authorzAuthorParser.get_author   s    +H5xfc"#)F< +??F8B ?  ,BBF8#%::ovc{1~f?U#VWX#YF< ;BB6,CWXC$)LL$5F=!2264HJ#[[j[AFFF6N11&%GI ++eI+>K(3(8(8}%33FE5IKKKkK:E)+ejj)Q~&11&#yII"OOC	OB	?H&I!qvv||~&I{#&({# 55feUKMkk%k>G7<<2-$'QR(8$9y! 'Js   !Hc                     |j                  |      }|D ]9  }d|j                  v s||j                  d   d   v s'|j                  d   d   c S  y )Nclassr   )r@   attrs)r   rC   tagr=   elementselements         r   r;   z!AuthorParser._find_tag_class_nameF   sT    ??3' 	1G'--'DGMM'4J14M,M}}W-a00	1r   c                    |j                  dd      j                  |d<   |d   t        j                  k(  r%|j                  dd      }|fd|d	   vr_|d	   |d
<   nV|d   t        j                  k(  r@|j                  dd      j                  d	      }d|v rt        j                  |      }||d
<   |j                  dd      }|j                  |d<   |j                  d      }|r/t        |j                  d      j                  d      d         |d<   |j                  dd      D cg c]  }|j                  j                          c}|d<   |j                  ddd      }|d   t        j                  k(  r3|j                  dk7  r$d|j                  j                  d      d   z   |d<   |j                  dd      }	|	r|	j                  d      |d<   |j                  dd      }
|
rt        |
d    j                        |d!<   y y c c}w )"Nr&   
gsc_prf_inidr#   r   imgzgsc_prf_pup-imgavatar_scholarsrcr!   
gsc_prf_ilr$   r(   r   r    zorg=organizationgsc_prf_intar-   gsc_prf_ivh)rW   r%   zNo verified emailr*       r+   gsc_prf_ilahomepagetdgsc_rsb_stdr   r1   )r<   r=   r   r4   CO_AUTHORS_LISTgetr:   r9   rB   splitr@   rA   )r   souprD   respicturer(   affiliation_linkrK   rI   rc   indexs              r   _fill_basicszAuthorParser._fill_basicsL   s   5\:??v(|???))E&7)8C#3u:5,/JF=)H!=!==ii*;i<@@GG7*,,w/$+F=!iili; + 0 0}&++C0%()9)=)=f)E)K)KF)STV)W%XF>"--N-CE!qvv||~ E{		%M,	G(|???zz00),UZZ-=-=c-B1-E)E~&::c-:8!)f!5F:d=9 #E!HMM 2F9 Es   +!H
c                    |j                  dd      }|rt        |d   j                        |d<   t        |d   j                        |d<   t        |d   j                        |d	<   t        |d
   j                        |d<   t        |d   j                        |d<   t        |d   j                        |d<   y d|d	<   d|d<   d|d<   d|d<   y )Nrd   re   r$   r   r1   r   	citedby5y   hindexra   hindex5y   i10index   
i10index5y)r@   rB   r=   )r   ri   rD   rm   s       r   _fill_indiceszAuthorParser._fill_indicesl   s    d=9 #E!HMM 2F9"%eAhmm"4F;"58==1F8!$U1X]]!3F:!$U1X]]!3F:#&uQx}}#5F<  F8!"F:!"F:#$F< r   c                    |j                  dd      D cg c]  }t        |j                         }}dgt        |      z  }|j                  dd      D ]L  }t        |d   j	                  d      d	         }t        |j                  dd
      j                        || <   N t        t        ||            |d<   y c c}w )Nspangsc_g_tr$   r   r   gsc_g_astyle:r\   gsc_g_alcites_per_year)r@   rB   r=   lenrh   r<   dictzip)r   ri   rD   yyearscitescrK   s           r   _fill_countszAuthorParser._fill_counts{   s    --y-AC QVV C C CJs95 	DAAgJ$$S)"-.AAFF6*F=BBCE1"I	D $(E5(9#: Cs   B>c           	      :   |j                  dd      }|j                  dd      }d\  }}|r<t        t        j                  dd|j                  j                  d      d	               }|r<t        t        j                  dd|j                  j                  d      d	               }t        ||
      |d<   d|d   vry |d   D ci c]  }|d   |
 }}| j                  j                  t        j                  |d   t                    }	 |j                  dd      }	|	r>|	d	   j                  dd      D ]&  }
t        j                  d|
d         d	   }d||   d<   ( |j                  dd      }	|	r>|	d	   j                  dd      D ]&  }
t        j                  d|
d         d	   }d||   d<   ( |j                  d      }|rPd|j                  vrB|d   dd } t        j                   d      |      d	   }| j                  j                  |      }ny c c}w )Nr&   gsc_rsb_m_ar$   gsc_rsb_m_na)r   r   z[.,]r/   r`   r   )	availablenot_availabler   r   r   author_pub_idr   Tgsc_mnd_sec_nar   z(gsc_mnd_art_rvw gs_nph gsc_mnd_link_fontzcitation_for_view=([\w:-]*)z	data-hrefFgsc_mnd_sec_avlgs_btnPRdisabledonclick   r\   unicode_escape)r<   rB   r6   r>   r=   rh   r   r   	_get_soup	_MANDATESr9   	_PAGESIZEr@   r7   rP   codecs
getdecoder)r   ri   rD   r   r   n_availablen_not_availablepubr   rowsrowr   next_buttonurls                 r   _fill_public_accessz AuthorParser._fill_public_access   sJ   IIeMI:			%	?'+$_bffVR1E1Ec1J11MNOK!"&&]5G5G5M5Mc5RST5U"VWO".=L#N !11 =C><RSSO,S0SSxx!!)"2"26,3G"ST==(89D7++C1[\ IC$&JJ/M/2;/?%AAB%DMCHL/@I
 ==(9:D7++C1[\ HC$&JJ/M/2;/?%AAB%DMCGL/@H
 )):)6Kz1B1BB!),R39f''(89#>qAxx))#.+  Ts   Hr   publication_limit
sortby_strc                    t               |d<   d}t        j                  |d         }||z  }t        | j                        }d}	 |j                  dd      D ]N  }	|j                  |	t        j                        }
|d   j                  |
       |s:t        |d         |k\  sLd} n d	|j                  d
d      j                  vr>|s<|t        z  }dj                  ||t              }| j                  j                  |      }ny )Nr   r   r   FTtrgsc_a_trr$   r   buttongsc_bpf_morerV   z{0}&cstart={1}&pagesize={2})list_CITATIONAUTHr9   r   r   r@   get_publicationr   AUTHOR_PUBLICATION_ENTRYappendr   r<   rP   r   r   )r   ri   rD   r   r   pubstarturl_citations
pub_parserflagr   new_pubr   s               r   _fill_publicationszAuthorParser._fill_publications   s
   !%~%,,VL-AB#&txx0
}}T*}= $44S:K:d:de~&--g6%C~0F,GK\,\D 8!G!M!MMVZI%3::!8Y8xx))#. r   c           	      x   |j                  dd      }|D cg c]7  }t        j                  t         |d      d   j	                  d            d   9 }}|D cg c]  }|j                  d      j                    }}|D cg c]  }|j                  d	      j                    }}|||fS c c}w c c}w c c}w )
aA  Get the short list of coauthors from the profile page.

        This method fetches the list of coauthors visible from an author's
        prilfe page alone. This may or may not be the complete list of
        coauthors.

        Note:
        -----
        This method is to be called by _fill_coauthors method.
        rz   gsc_rsb_a_descr$   r   r   r    z-1)tabindexgsc_rsb_a_ext)r@   r6   r7   r8   rg   r<   r=   )r   ri   r   coauthcoauthor_idscoauthor_namescoauthor_affilss          r   _get_coauthors_shortz!AuthorParser._get_coauthors_short   s     MM&1AMB	 '01" 

?sA**624457 1 1
 )23$ !++t+499 3 3 *34% ";;o;>CC 4 4 ^_<<134s   <B-#B2#B7c           	         | j                   j                  t        j                  |d               }|j	                  dd      }|D cg c]7  }t        j                  t         |d      d   j                  d            d   9 }}|D cg c]  }|j                  d      j                    }}|D cg c]  }|j                  d	      j                    }}|||fS c c}w c c}w c c}w )
a  Get the long (>20) list of coauthors.

        This method fetches the complete list of coauthors bu opening a new
        page filled with the complete coauthor list.

        Note:
        -----
        This method is to be called by _fill_coauthors method.
        r   r&   zgs_ai gs_sclr   r   r    
gs_ai_namer$   	gs_ai_aff)r   r   _COAUTHr9   r@   r6   r7   r8   rg   r<   r=   )r   rD   ri   r   r   r   r   r   s           r   _get_coauthors_longz AuthorParser._get_coauthors_long   s     xx!!'..1E"FGMM%8	 '01" 

?sA**624457 1 1 OXXF&++\+:??XX)24% ";;k;:?? 4 4 ^_<<1 Y4s   <C
#C"3#C'c                    |j                  dd      s| j                  |      }n	 | j                  |      }g |d<   t        | D ]H  \  }}}| j                  |      }||d<   ||d<   t        j                  |d<   |d   j                  |       J y # t        $ re}| j                  |      }| j                  j
                  j                  |       | j                  j
                  j                  d       Y d }~d }~ww xY w)	Nr   gsc_coauth_opnrV   z"Fetching only the top 20 coauthorsr   r#   r(   r   )r@   r   r   	Exceptionr   loggerwarningr   rM   r   rf   r   )	r   ri   rD   coauthor_infoerr	coauth_idcoauth_namecoauth_affilnew_coauthors	            r   _fill_coauthorszAuthorParser._fill_coauthors   s    }}X*:}; 55d;MN $ 8 8 @ !{474G 	50I{L??95L#.L *6L'%1%A%AL";&&|4	5  N $ 9 9$ ?'',''(LMMNs   B 	C?AC::C?r1   sectionsc                    	 |D cg c]  }|j                          }}|j                  d       d}|dk(  rd}n|dk7  rt        d      t        j	                  |d         }||z  }d	j	                  |t
              }| j                  j                  |      }	t        j                  t        |	j                  d
d      j                  dd            d   }
|
|d   k7  r0| j                  j                  j                  d|d   |
|
       |
|d<   |g k(  rc| j                  D ]Q  }||d   vs|dk7  r t!        | d|       |	|      n t!        | d|       |	|||       |d   j#                  |       S 	 |S |D ]`  }|| j                  v s||d   vs|dk7  r t!        | d|       |	|      n t!        | d|       |	|||       |d   j#                  |       b 	 |S c c}w # t        $ r}|d}~ww xY w)al"  Populate the Author with information from their profile

        The `sections` argument allows for finer granularity of the profile
        information to be pulled.

        :param sections: Sections of author profile to be filled, defaults to ``[]``.

            * ``basics``: fills name, affiliation, and interests;
            * ``citations``: fills h-index, i10-index, and 5-year analogues;
            * ``counts``: fills number of citations per year;
            * ``public_access``: fills number of articles with public access mandates;
            * ``coauthors``: fills co-authors;
            * ``publications``: fills publications;
            * ``[]``: fills all of the above
        :type sections: ['basics','citations','counts','public_access','coauthors','publications',[]] list, optional
        :param sortby: Select the order of the citations in the author page. Either by 'citedby' or 'year'. Defaults to 'citedby'.
        :type sortby: string
        :param publication_limit: Select the max number of publications you want you want to fill for the author. Defaults to no limit.
        :type publication_limit: int
        :returns: The filled object if fill was successfull, False otherwise.
        :rtype: Author or bool

        :Example::

        .. testcode::

            search_query = scholarly.search_author('Steven A Cholewiak')
            author = next(search_query)
            author = scholarly.fill(author, sections=['basics', 'citations', 'coauthors'])
            scholarly.pprint(author)

        :Output::

        .. testoutput::

            {'affiliation': 'Vision Scientist',
             'citedby': 304,
             'citedby5y': 226,
             'coauthors': [{'affiliation': 'Kurt Koffka Professor of Experimental '
                                           'Psychology, University of Giessen',
                            'filled': False,
                            'name': 'Roland Fleming',
                            'scholar_id': 'ruUKktgAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Professor of Vision Science, UC Berkeley',
                            'filled': False,
                            'name': 'Martin Banks',
                            'scholar_id': 'Smr99uEAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Durham University, Computer Science & Physics',
                            'filled': False,
                            'name': 'Gordon D. Love',
                            'scholar_id': '3xJXtlwAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Professor of ECE, Purdue University',
                            'filled': False,
                            'name': 'Hong Z Tan',
                            'scholar_id': 'OiVOAHMAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Deepmind',
                            'filled': False,
                            'name': 'Ari Weinstein',
                            'scholar_id': 'MnUboHYAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': "Brigham and Women's Hospital/Harvard Medical "
                                           'School',
                            'filled': False,
                            'name': 'Chia-Chien Wu',
                            'scholar_id': 'dqokykoAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Professor of Psychology and Cognitive Science, '
                                           'Rutgers University',
                            'filled': False,
                            'name': 'Jacob Feldman',
                            'scholar_id': 'KoJrMIAAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Research Scientist at Google Research, PhD '
                                           'Student at UC Berkeley',
                            'filled': False,
                            'name': 'Pratul Srinivasan',
                            'scholar_id': 'aYyDsZ0AAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Formerly: Indiana University, Rutgers '
                                           'University, University of Pennsylvania',
                            'filled': False,
                            'name': 'Peter C. Pantelis',
                            'scholar_id': 'FoVvIK0AAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Professor in Computer Science, University of '
                                           'California, Berkeley',
                            'filled': False,
                            'name': 'Ren Ng',
                            'scholar_id': '6H0mhLUAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Yale University',
                            'filled': False,
                            'name': 'Steven W Zucker',
                            'scholar_id': 'rNTIQXYAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Brown University',
                            'filled': False,
                            'name': 'Ben Kunsberg',
                            'scholar_id': 'JPZWLKQAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Rutgers University, New Brunswick, NJ',
                            'filled': False,
                            'name': 'Manish Singh',
                            'scholar_id': '9XRvM88AAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Silicon Valley Professor of ECE, Purdue '
                                           'University',
                            'filled': False,
                            'name': 'David S. Ebert',
                            'scholar_id': 'fD3JviYAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Clinical Director, Neurolens Inc.,',
                            'filled': False,
                            'name': 'Vivek Labhishetty',
                            'scholar_id': 'tD7OGTQAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'MIT',
                            'filled': False,
                            'name': 'Joshua B. Tenenbaum',
                            'scholar_id': 'rRJ9wTJMUB8C',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Chief Scientist, isee AI',
                            'filled': False,
                            'name': 'Chris Baker',
                            'scholar_id': 'bTdT7hAAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Professor of Psychology, Ewha Womans '
                                           'University',
                            'filled': False,
                            'name': 'Sung-Ho Kim',
                            'scholar_id': 'KXQb7CAAAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Assistant Professor, Boston University',
                            'filled': False,
                            'name': 'Melissa M. Kibbe',
                            'scholar_id': 'NN4GKo8AAAAJ',
                            'source': 'CO_AUTHORS_LIST'},
                           {'affiliation': 'Nvidia Corporation',
                            'filled': False,
                            'name': 'Peter Shirley',
                            'scholar_id': 'nHx9IgYAAAAJ',
                            'source': 'CO_AUTHORS_LIST'}],
             'email_domain': '@berkeley.edu',
             'homepage': 'http://steven.cholewiak.com/',
             'filled': False,
             'hindex': 9,
             'hindex5y': 9,
             'i10index': 8,
             'i10index5y': 7,
             'interests': ['Depth Cues',
                           '3D Shape',
                           'Shape from Texture & Shading',
                           'Naive Physics',
                           'Haptics'],
             'name': 'Steven A. Cholewiak, PhD',
             'scholar_id': '4bahYMkAAAAJ',
             'source': 'SEARCH_AUTHOR_SNIPPETS',
             'url_picture': 'https://scholar.google.com/citations?view_op=medium_photo&user=4bahYMkAAAAJ'}
        T)reverser/   yearz"&view_op=list_works&sortby=pubdater1   zAPlease enter a valid sortby parameter. Options: 'year', 'citedby'r   z{0}&pagesize={1}link	canonical)relr    r   zpChanging the scholar_id following redirect from %s to %s. To avoid this warning, use %s to look up this scholar.r   r   _fill_N)lowersortr   r   r9   r   r   r   r6   r7   r8   r<   rg   r   r   r   getattrr   )r   rD   r   sortbyr   sectionr   r   r   ri   r   rK   es                r   fillzAuthorParser.fill
  sd   H 	7?@G@H@MM$M'JA
9$ cdd)001EFMZ'M$++M9EC88%%c*D OTYYv;Y5W5[5[\bdf5ghijkJVL11'' )a(.|(<j*V (2|$2~ 3Ax 00FG>FY4s|4T6B_z_fgkouvwuxmy_z{  BH  J[  ]g  `hx(//23  " 3ADNN*qx8H/HFG>FY4s|4T6B_z_fgkouvwuxmy_z{  BH  J[  ]g  `hx(//23 C A<  	H	sB   G$ GDG$ +A	G$ 7G$ G$ A	G$ G$ $	G4-G//G4c                 "    | j                         S )N)__str__)r   s    r   __repr__zAuthorParser.__repr__  s    ||~r   N)r   r/   )__name__
__module____qualname____doc__r   r   rM   r;   rn   rx   r   r   rB   r3   r   r   r   r   r   r   r    r   r   r   r      s|    /+(& (V13@%	;'T# WZ .=.=,5, -/y[\ FT FUX FRr   r   )publication_parserr   r6   
data_typesr   r   r   r   r   r8   r:   r   r?   r   r   r   r   r   r   r   <module>r      sC    1 	 M M "'	&+
=S	E Er   