Format: application/x-python-code
⬇ Download File (22.9 KB)
   1  o
   2  
   3  F'h�f�@sTdZddlmZddlZddlZddlZddlZddlZddlZddl	Z	ddl
   4  Z
   5  ddlZddlm
   6  Z
   7  mZddlmZddlmZmZmZddlmZddlmZdd	lmZmZmZmZe�e�Zej Z!Gd
   8  d�de"�Z#Gdd
   9  �d
  10  e�Z$Gdd�de�Z%Gdd�de"�Z&Gdd�de"�Z'Gdd�de(�Z)Gdd�de)�Z*Gdd�de)�Z+dd�Z,dS)z.. include:: ../README.md�)�annotationsN)�	urlencode�urlparse)�urlretrieve)�datetime�	timedelta�timezone)�timegm)�Enum)�Dict�	Generator�List�Optionalc@sreZdZUdZded<	ded<	ded<	ded<	ded	<	ded
  11  <	ded<	ded
  12  <	ded<	ded<	ded<	ded<	ded<	ded<	eedgdddddggdfdEdd�ZdFdd�ZdGdd �ZdGd!d"�Z	dHd$d%�Z
  13  dGd&d'�ZdIdJd*d+�Z	,		-dKdLd1d2�Z
  14  	,		-dKdLd3d4�ZdMd5d6�ZdNd9d:�ZdOd=d>�ZGd?d@�d@e�ZGdAdB�dBe�ZGdCdD�dDe�ZdS)P�Resultz�
  15      An entry in an arXiv query results feed.
  16  
  17      See [the arXiv API User's Manual: Details of Atom Results
  18      Returned](https://arxiv.org/help/api/user-manual#_details_of_atom_results_returned).
  19      �str�entry_idr�updated�	published�title�List[Author]�authors�summary�
  20  Optional[str]�comment�journal_ref�doi�primary_category�	List[str]�
  21  categories�
  22  List[Link]�links�pdf_url�feedparser.FeedParserDict�_raw�NcCs^||_||_||_||_||_||_||_||_|	|_|
  23  |_	||_
  24  ||_t�
  25  |�|_|
  26  |_dS)z�
  27          Constructs an arXiv search result item.
  28  
  29          In most cases, prefer using `Result._from_feed_entry` to parsing and
  30          constructing `Result`s yourself.
  31          N)rrrrrrrrrrrr r�_get_pdf_urlr!r#)�selfrrrrrrrrrrrr r#�r'�DC:\Users\shubh\Videos\agent\nova\Lib\site-packages\arxiv/__init__.py�__init__Js
  32  zResult.__init__�entry�returncCs�t|d�s
  33  t�d��d}t|d�r|j}nt�d|j�t|jt�|j�t�|j	�t
  34  �dd|�dd�|jD�|j
  35  |�d	�|�d
  36  �|�d�|j�d�d
  37  d�|jD�dd�|jD�|d�
  38  S)zk
  39          Converts a feedparser entry for an arXiv search result feed into a
  40          Result object.
  41          �id�0rz7Result %s is missing title attribute; defaulting to '0'z\s+� cS�g|]}tj�|��qSr')r�Author�_from_feed_author)�.0�ar'r'r(�
  42  <listcomp>��z+Result._from_feed_entry.<locals>.<listcomp>Z
  43  arxiv_commentZarxiv_journal_refZ	arxiv_doi�termcSsg|]}|�d��qS)r6)�get)r2�tagr'r'r(r4�scSr/r')r�Link�_from_feed_link�r2�linkr'r'r(r4�r5)
  44  rrrrrrrrrrrr r#)�hasattrr�MissingFieldErrorr�logger�warningr,�_to_datetimeZupdated_parsedZpublished_parsed�re�subrrr7Zarxiv_primary_category�tagsr )r*rr'r'r(�_from_feed_entryqs*
  45  
  46  
  47  
  48  
  49  
  50  �zResult._from_feed_entrycC�|jS�N)r�r&r'r'r(�__str__��zResult.__str__cCsnd�t|�t|j�t|j�t|j�t|j�t|j�t|j�t|j	�t|j
  51  �t|j�t|j�t|j
  52  �t|j��
  53  S)Nz�{}(entry_id={}, updated={}, published={}, title={}, authors={}, summary={}, comment={}, journal_ref={}, doi={}, primary_category={}, categories={}, links={}))�format�
  54  _classname�reprrrrrrrrrrrrr rHr'r'r(�__repr__�s"��zResult.__repr__�boolcCst|t�r|j|jkSdS�NF)�
  55  isinstancerr�r&�otherr'r'r(�__eq__�s
  56  z
  57  Result.__eq__cCs|j�d�dS)a;
  58          Returns the short ID for this result.
  59  
  60          + If the result URL is `"https://arxiv.org/abs/2107.05580v1"`,
  61          `result.get_short_id()` returns `2107.05580v1`.
  62  
  63          + If the result URL is `"https://arxiv.org/abs/quant-ph/0201082v1"`,
  64          `result.get_short_id()` returns `"quant-ph/0201082v1"` (the pre-March
  65          2007 arXiv identifier format).
  66  
  67          For an explanation of the difference between arXiv's legacy and current
  68          identifiers, see [Understanding the arXiv
  69          identifier](https://arxiv.org/help/arxiv_identifier).
  70          zarxiv.org/abs/�����)r�splitrHr'r'r(�get_short_id�szResult.get_short_id�pdf�	extensioncCs6|jr|jnd}d�|���dd�t�dd|�|g�S)zK
  71          A default `to_filename` function for the extension given.
  72          ZUNTITLED�.�/�_z[^\w])r�joinrW�replacerBrC)r&rYZnonempty_titler'r'r(�_get_default_filename�s��zResult._get_default_filename�./�export.arxiv.org�dirpath�filename�download_domaincCs:|s|��}tj�||�}t�|j|�}t||�\}}|S)z�
  73          Downloads the PDF for this result to the specified directory.
  74  
  75          The filename is generated by calling `to_filename(self)`.
  76          )r_�os�pathr]r�_substitute_domainr!r)r&rbrcrdrfr!�written_pathr\r'r'r(�download_pdf�szResult.download_pdfc	CsH|s|�d�}tj�||�}t�|j|�}|�dd�}t||�\}}|S)z�
  77          Downloads the source tarfile for this result to the specified
  78          directory.
  79  
  80          The filename is generated by calling `to_filename(self)`.
  81          ztar.gzz/pdf/z/src/)	r_rerfr]rrgr!r^r)	r&rbrcrdrfr!Zsrc_urlrhr\r'r'r(�download_source�s
  82  zResult.download_sourcecCsBdd�|D�}t|�dkrdSt|�dkrt�d|d�|dS)z�
  83          Finds the PDF link among a result's links and returns its URL.
  84  
  85          Should only be called once for a given `Result`, in its constructor.
  86          After construction, the URL should be available in `Result.pdf_url`.
  87          cSsg|]
  88  }|jdkr|j�qS�rX)r�hrefr;r'r'r(r4�sz'Result._get_pdf_url.<locals>.<listcomp>rN�z'Result has multiple PDF links; using %s)�lenr?r@)r Zpdf_urlsr'r'r(r%�szResult._get_pdf_url�ts�time.struct_timecCstjt|�tjd�S)z�
  89          Converts a UTC time.struct_time into a time-zone-aware datetime.
  90  
  91          This will be replaced with feedparser functionality [when it becomes
  92          available](https://github.com/kurtmckee/feedparser/issues/212).
  93          )�tz)r�
  94  fromtimestampr	r�utc)ror'r'r(rAszResult._to_datetime�url�domaincCst|�}|j|d���S)z
  95          Replaces the domain of the given URL with the specified domain.
  96  
  97          This is useful for testing purposes.
  98          )�netloc)r�_replace�geturl)rtru�
  99  parsed_urlr'r'r(rg
 100  szResult._substitute_domainc@sNeZdZUdZded<	ddd�Zdd	d
 101  �Zddd�Zdd
 102  d�Zddd�Z	dS)�
 103  Result.AuthorzJ
 104          A light inner class for representing a result's authors.
 105          r�namecCs
 106  ||_dS)z�
 107              Constructs an `Author` with the specified name.
 108  
 109              In most cases, prefer using `Author._from_feed_author` to parsing
 110              and constructing `Author`s yourself.
 111              N�r{)r&r{r'r'r(r)s
 112  zResult.Author.__init__�feed_authorr"r+cCst�|j�S)z�
 113              Constructs an `Author` with the name specified in an author object
 114              from a feed entry.
 115  
 116              See usage in `Result._from_feed_entry`.
 117              )rr0r{)r}r'r'r(r1$szResult.Author._from_feed_authorcCrFrGr|rHr'r'r(rI-rJzResult.Author.__str__cC�d�t|�t|j��S�Nz{}({}))rKrLrMr{rHr'r'r(rN0�zResult.Author.__repr__rOcC�t|tj�r|j|jkSdSrP)rQrr0r{rRr'r'r(rT3�zResult.Author.__eq__N)r{r)r}r"r+rz�r+r�r+rO)
 118  �__name__�
 119  __module__�__qualname__�__doc__�__annotations__r)r1rIrNrTr'r'r'r(r0s
 120  
 121  
 122  	
 123  	
 124  r0c@steZdZUdZded<	ded<	ded<	ded<				ddd	d
 125  �Zddd�Zddd�Zddd�Zddd�Z	dS)�Result.LinkzH
 126          A light inner class for representing a result's links.
 127          rrlrr�rel�content_typeNcCs||_||_||_||_dS)z�
 128              Constructs a `Link` with the specified link metadata.
 129  
 130              In most cases, prefer using `Link._from_feed_link` to parsing and
 131              constructing `Link`s yourself.
 132              N�rlrr�r�)r&rlrr�r�r'r'r(r)Fs
 133  
 134  zResult.Link.__init__�	feed_linkr"r+cCs&tj|j|�d�|�d�|�d�d�S)z�
 135              Constructs a `Link` with link metadata specified in a link object
 136              from a feed entry.
 137  
 138              See usage in `Result._from_feed_entry`.
 139              rr�r�r�)rr9rlr7)r�r'r'r(r:Xs�zResult.Link._from_feed_linkcCrFrG)rlrHr'r'r(rIfrJzResult.Link.__str__cCs.d�t|�t|j�t|j�t|j�t|j��S)Nz){}({}, title={}, rel={}, content_type={}))rKrLrMrlrr�r�rHr'r'r(rNis�zResult.Link.__repr__rOcCr�rP)rQrr9rlrRr'r'r(rTrr�zResult.Link.__eq__)NNN)rlrrrr�rr�r)r�r"r+r�r�r�)
 140  r�r�r�r�r�r)r:rIrNrTr'r'r'r(r98s$
 141  �
 142  
 143  
 144  	r9c@s8eZdZUdZded<	ded<	dd�Zddd	�Zd
 145  S)zResult.MissingFieldErrorzg
 146          An error indicating an entry is unparseable because it lacks required
 147          fields.
 148          r�
 149  missing_field�messagecCs||_d|_dS)Nz&Entry from arXiv missing required info)r�r�)r&r�r'r'r(r)�s
 150  z!Result.MissingFieldError.__init__r+cCr~r)rKrLrMr�rHr'r'r(rN�r�z!Result.MissingFieldError.__repr__Nr�)r�r�r�r�r�r)rNr'r'r'r(r>ws
 151  r>)rrrrrrrrrrrrrrrrrrrrrrr rr#r")r*r"r+rr�r�rk)rYrr+r)r`r$ra)rbrrcrrdrr+r)r rr+r)rorpr+r)rtrrurr+r)r�r�r�r�r��
 152  _DEFAULT_TIMEr)rErIrNrTrWr_rirjr%rArg�objectr0r9�	Exceptionr>r'r'r'r(rs~
 153  �
 154  '
 155   
 156  
 157  
 158  ��
 159  
 160  
 161  		%?rc@seZdZdZdZdZdZdS)�
 162  SortCriterionz�
 163      A SortCriterion identifies a property by which search results can be
 164      sorted.
 165  
 166      See [the arXiv API User's Manual: sort order for return
 167      results](https://arxiv.org/help/api/user-manual#sort).
 168      Z	relevanceZlastUpdatedDateZ
 169  submittedDateN)r�r�r�r��	RelevanceZLastUpdatedDateZ
 170  SubmittedDater'r'r'r(r��s
 171  r�c@seZdZdZdZdZdS)�	SortOrderz�
 172      A SortOrder indicates order in which search results are sorted according
 173      to the specified arxiv.SortCriterion.
 174  
 175      See [the arXiv API User's Manual: sort order for return
 176      results](https://arxiv.org/help/api/user-manual#sort).
 177      Z	ascendingZ
 178  descendingN)r�r�r�r�Z	Ascending�
 179  Descendingr'r'r'r(r��sr�c@s�eZdZUdZded<	ded<	ded<	ded	<	d
 180  ed<	dgd
 181  ejejfddd�Z	ddd�Z
 182  ddd�Zd dd�Zd!d"dd�Z
 183  d
 184  S)#�Searchz�
 185      A specification for a search of arXiv's database.
 186  
 187      To run a search, use `Search.run` to use a default client or `Client.run`
 188      with a specific client.
 189      r�queryr�id_list�
 190  int | None�max_resultsr��sort_byr��
 191  sort_orderr$NcCs0||_||_|tjkr
 192  dn||_||_||_dS)zM
 193          Constructs an arXiv API search with the specified criteria.
 194          N)r�r��math�infr�r�r�)r&r�r�r�r�r�r'r'r(r)�s
 195  
 196  zSearch.__init__r+cC�t|�SrG�rMrHr'r'r(rI��zSearch.__str__c	Cs6d�t|�t|j�t|j�t|j�t|j�t|j��S)NzC{}(query={}, id_list={}, max_results={}, sort_by={}, sort_order={}))rKrLrMr�r�r�r�r�rHr'r'r(rN�s�zSearch.__repr__�Dict[str, str]cCs |jd�|j�|jj|jjd�S)zx
 197          Returns a dict of search parameters that should be included in an API
 198          request for this search.
 199          �,)Zsearch_queryr�ZsortByZ	sortOrder)r�r]r�r��valuer�rHr'r'r(�	_url_args�s
 200  
 201  �zSearch._url_argsr�offset�int�Generator[Result, None, None]cCs tjdtdd�t�j||d�S)z�
 202          Executes the specified search using a default arXiv API client. For info
 203          on default behavior, see `Client.__init__` and `Client.results`.
 204  
 205          **Deprecated** after 2.0.0; use `Client.results`.
 206          zGThe 'Search.results' method is deprecated, use 'Client.results' instead�)�
 207  stacklevel)r�)�warnings�warn�DeprecationWarning�Client�results)r&r�r'r'r(r��s�zSearch.results)
 208  r�rr�rr�r�r�r�r�r�r�)r+r��r)r�r�r+r�)r�r�r�r�r�r�r�r�r�r)rIrNr�r�r'r'r'r(r��s,
 209  	�
 210  
 211  
 212  
 213  r�c@s�eZdZUdZdZ	ded<	ded<	ded<	ded	<d
 214  ed<d/d0dd�Zd1dd�Zd1dd�Zd2d3dd�Z	d2d3dd�Z
 215  d4d!d"�Z	d5d6d)d*�Zd7d,d-�Z
 216  d.S)8r�z�
 217      Specifies a strategy for fetching results from arXiv's API.
 218  
 219      This class obscures pagination and retry logic, and exposes
 220      `Client.results`.
 221      z%https://export.arxiv.org/api/query?{}r��	page_size�float�
 222  delay_seconds�num_retriesr�_last_request_dtzrequests.Session�_session�d�@�cCs&||_||_||_d|_t��|_dS)ag
 223          Constructs an arXiv API client with the specified options.
 224  
 225          Note: the default parameters should provide a robust request strategy
 226          for most use cases. Extreme page sizes, delays, or retries risk
 227          violating the arXiv [API Terms of Use](https://arxiv.org/help/api/tou),
 228          brittle behavior, and inconsistent results.
 229          N)r�r�r�r��requests�Sessionr�)r&r�r�r�r'r'r(r)(s
 230  	zClient.__init__r+rcCr�rGr�rHr'r'r(rI7r�zClient.__str__cC�&d�t|�t|j�t|j�t|j��S)Nz2{}(page_size={}, delay_seconds={}, num_retries={}))rKrLrMr�r�r�rHr'r'r(rN;s�zClient.__repr__r�searchr�r�r�cCs<|jr|j|nd}|r|dkrtd�St�|�||�|�S)ab
 231          Uses this client configuration to fetch one page of the search results
 232          at a time, yielding the parsed `Result`s, until `max_results` results
 233          have been yielded or there are no more search results.
 234  
 235          If all tries fail, raises an `UnexpectedEmptyPageError` or `HTTPError`.
 236  
 237          Setting a nonzero `offset` discards leading records in the result set.
 238          When `offset` is greater than or equal to `search.max_results`, the full
 239          result set is discarded.
 240  
 241          For more on using generators, see
 242          [Generators](https://wiki.python.org/moin/Generators).
 243          Nrr')r��iter�	itertools�islice�_results)r&r�r��limitr'r'r(r�CszClient.resultsccs��|�|||j�}|j|dd�}|jst�d�dSt|jj�}t�dt	|j�|�|jru|jD]#}zt
 244  �|�VWq0t
 245  jyS}zt�
 246  d|�WYd}~q0d}~ww|t	|j�7}||kradS|�|||j�}|j|dd�}|js-dSdS)NT)�
 247  first_pagez)Got empty first page; stopping generationz&Got first page: %d of %d total resultszSkipping partial result: %sF)�_format_urlr��_parse_feed�entriesr?�infor��feedZopensearch_totalresultsrnrrEr>r@)r&r�r��page_urlr��
 248  total_resultsr*�er'r'r(r�Ws4�
 249  �
 250  ���zClient._results�startcCs(|��}|�||d��|j�t|��S)z�
 251          Construct a request API for search that returns up to `page_size`
 252          results starting with the result at index `start`.
 253          )r�r�)r��update�query_url_formatrKr)r&r�r�r�Zurl_argsr'r'r(r�ps��zClient._format_urlTrtr�rO�
 254  _try_indexr"c
 255  Cs�z	|j|||d�WStttjjfy?}z%||jkr2t�d||�|j	|||dd�WYd}~St�d||�|�d}~ww)z�
 256          Fetches the specified URL and parses it with feedparser.
 257  
 258          If a request fails or is unexpectedly empty, retries the request up to
 259          `self.num_retries` times.
 260          )r��	try_indexzGot error (try %d): %srm)r�r�NzGiving up (try %d): %s)
 261  �_Client__try_parse_feed�	HTTPError�UnexpectedEmptyPageErrorr��
 262  exceptions�ConnectionErrorr�r?�debugr�)r&rtr�r��errr'r'r(r�~s	�
 263   ��zClient._parse_feedr�c	Cs�|jdur't|jd�}t��|j}||kr'||��}t�d|�t�	|�t�d|||�|j
 264  j|ddid�}t��|_|jt
 265  jjkrLt|||j��t�|j�}t|j�dkra|sat|||��|jrqt�d	d
 266  |vrn|jnd�|S)z�
 267          Recursive helper for _parse_feed. Enforces `self.delay_seconds`: if that
 268          number of seconds has not passed since `_parse_feed` was last called,
 269          sleeps until delay_seconds seconds have passed.
 270          N)�secondszSleeping: %f secondsz(Requesting page (first: %r, try: %d): %sz
 271  user-agentzarxiv.py/2.2.0)�headersrz Bozo feed; consider handling: %s�bozo_exception)r�rr�r�now�
 272  total_secondsr?r��time�sleepr�r7�status_coder��codes�OKr��
 273  feedparser�parse�contentrnr�r�Zbozor@r�)	r&rtr�r��requiredZsince_last_requestZto_sleep�respr�r'r'r(Z__try_parse_feed�s*
 274  
 275  
 276  �zClient.__try_parse_feedN)r�r�r�)r�r�r�r�r�r�r�r�)r�r�r�r�r+r�)r�r�r�r�r�r�r+r)Tr)rtrr�rOr�r�r+r")rtrr�rOr�r�r+r")r�r�r�r�r�r�r)rIrNr�r�r�r�r�r'r'r'r(r�s*
 277  
 278  
 279  
 280  �r�csLeZdZUdZded<	ded<	ded<	d�fdd�Zd
 281  d
 282  d�Z�ZS)�
 283  ArxivErrorz$This package's base Exception class.rrtr��retryr�cs$||_||_||_t��|j�dS)zZ
 284          Constructs an `ArxivError` encountered while fetching the specified URL.
 285          N)rtr�r��superr))r&rtr�r���	__class__r'r(r)�szArxivError.__init__r+cCsd�|j|j�S)Nz{} ({}))rKr�rtrHr'r'r(rI�szArxivError.__str__)rtrr�r�r�rr�)r�r�r�r�r�r)rI�
 286  __classcell__r'r'r�r(r��s
 287  	r�cs8eZdZUdZded<	d
 288  �fdd	�Zddd�Z�ZS)r�a
 289      An error raised when a page of results that should be non-empty is empty.
 290  
 291      This should never happen in theory, but happens sporadically due to
 292      brittleness in the underlying arXiv API; usually resolved by retries.
 293  
 294      See `Client.results` for usage.
 295      r"�raw_feedrtrr�r�cs ||_||_t��||d�dS)z}
 296          Constructs an `UnexpectedEmptyPageError` encountered for the specified
 297          API URL after `retry` tries.
 298          z&Page of results was unexpectedly emptyN)rtr�r�r))r&rtr�r�r�r'r(r)�sz!UnexpectedEmptyPageError.__init__r+cCr��Nz{}({}, {}, {}))rKrLrMrtr�r�rHr'r'r(rN���z!UnexpectedEmptyPageError.__repr__)rtrr�r�r�r"r��r�r�r�r�r�r)rNr�r'r'r�r(r��s
 299  		r�cs8eZdZUdZded<	d�fdd�Zd
 300  d
 301  d�Z�ZS)r�zm
 302      A non-200 status encountered while fetching a page of results.
 303  
 304      See `Client.results` for usage.
 305      r��statusrtrr�cs(||_||_t��||d�|j��dS)z�
 306          Constructs an `HTTPError` for the specified status code, encountered for
 307          the specified API URL after `retry` tries.
 308          z Page request resulted in HTTP {}N)rtr�r�r)rK)r&rtr�r�r�r'r(r)�s
 309  �zHTTPError.__init__r+cCr�r�)rKrLrMrtr�r�rHr'r'r(rNr�zHTTPError.__repr__)rtrr�r�r�r�r�r�r'r'r�r(r��s
 310  
 311  r�cCsd�|jj�S)zAA helper function for use in __repr__ methods: arxiv.Result.Link.zarxiv.{})rKr�r�)�or'r'r(rLsrL)-r��
 312  __future__r�loggingr�r�r�rer�rBr�r��urllib.parserr�urllib.requestrrrr�calendarr	�enumr
 313  �typingrrr
 314  r�	getLoggerr�r?�minr�r�rr�r�r�r�r�r�r�r�rLr'r'r'r(�<module>s>
 315  p
 316  a7