
    "ig?                     b    d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ  G d de      Zy)	    )unicode_literalsN)const)NewsApiAuth)NewsAPIException)is_valid_stringstringify_date_paramc                   H    e Zd ZdZddZ	 ddZ	 	 	 	 	 	 	 	 	 	 	 d	dZd
dZy)NewsApiClienta  The core client object used to fetch data from News API endpoints.

    :param api_key: Your API key, a length-32 UUID string provided for your News API account.
        You must `register <https://newsapi.org/register>`_ for a News API key.
    :type api_key: str

    :param session: An optional :class:`requests.Session` instance from which to execute requests.
        **Note**: If you provide a ``session`` instance, :class:`NewsApiClient` will *not* close the session
        for you.  Remember to call ``session.close()``, or use the session as a context manager, to close
        the socket and free up resources.
    :type session: `requests.Session <https://2.python-requests.org/en/master/user/advanced/#session-objects>`_ or None
    Nc                 P    t        |      | _        |t        | _        y || _        y )N)api_key)r   authrequestsrequest_method)selfr   sessions      S/var/www/html/Investors/venv/lib/python3.12/site-packages/newsapi/newsapi_client.py__init__zNewsApiClient.__init__   s#    0	?"*D")D    c	                 F   i }	|t        |      r||	d<   nt        d      |t        |      r||	d<   nt        d      |||t        d      |t        |      r||	d<   nt        d      |9t        |      r#|t        j                  v r||	d<   nt        d	      t        d
      |9t        |      r#|t        j
                  v r||	d<   nt        d      t        d      |9t        |      r#|t        j                  v r||	d<   nt        d      t        d      |Ft        |      t        k(  r)d|cxk  rdk  rn t        d      ||	d<   nt        d      t        d      |3t        |      t        k(  r|dkD  r||	d<   nt        d      t        d      | j                  j                  t        j                  | j                  d|	      }
|
j                  t        j                  j                   k7  rt#        |
j%                               |
j%                         S )a  Call the `/top-headlines` endpoint.

        Fetch live top and breaking headlines.

        This endpoint provides live top and breaking headlines for a country, specific category in a country,
        single source, or multiple sources. You can also search with keywords.  Articles are sorted by the earliest
        date published first.

        :param q: Keywords or a phrase to search for in the article title and body.  See the official News API
            `documentation <https://newsapi.org/docs/endpoints/everything>`_ for search syntax and examples.
        :type q: str or None

        :param qintitle: Keywords or a phrase to search for in the article title and body.  See the official News API
            `documentation <https://newsapi.org/docs/endpoints/everything>`_ for search syntax and examples.
        :type q: str or None

        :param sources: A comma-seperated string of identifiers for the news sources or blogs you want headlines from.
            Use :meth:`NewsApiClient.get_sources` to locate these programmatically, or look at the
            `sources index <https://newsapi.org/sources>`_.  **Note**: you can't mix this param with the
            ``country`` or ``category`` params.
        :type sources: str or None

        :param language: The 2-letter ISO-639-1 code of the language you want to get headlines for.
            See :data:`newsapi.const.languages` for the set of allowed values.
            The default for this method is ``"en"`` (English).  **Note**: this parameter is not mentioned in the
            `/top-headlines documentation <https://newsapi.org/docs/endpoints/top-headlines>`_ as of Sep. 2019,
            but *is* supported by the API.
        :type language: str or None

        :param country: The 2-letter ISO 3166-1 code of the country you want to get headlines for.
            See :data:`newsapi.const.countries` for the set of allowed values.
            **Note**: you can't mix this parameter with the ``sources`` param.
        :type country: str or None

        :param category: The category you want to get headlines for.
            See :data:`newsapi.const.categories` for the set of allowed values.
            **Note**: you can't mix this parameter with the ``sources`` param.
        :type category: str or None

        :param page_size: Use this to page through the results if the total results found is
            greater than the page size.
        :type page_size: int or None

        :param page: The number of results to return per page (request).
            20 is the default, 100 is the maximum.
        :type page: int or None

        :return: JSON response as nested Python dictionary.
        :rtype: dict
        :raises NewsAPIException: If the ``"status"`` value of the response is ``"error"`` rather than ``"ok"``.
        q,keyword/phrase q param should be of type strqintitle3keyword/phrase qintitle param should be of type strz5cannot mix country/category param with sources param.sources#sources param should be of type strlanguageinvalid language$language param should be of type strcountryinvalid country#country param should be of type strcategoryinvalid category$category param should be of type strr   d   pageSize2page_size param should be an int between 1 and 100 page_size param should be an intpage*page param should be an int greater than 0page param should be an int   r   timeoutparams)r   	TypeError
ValueErrorr   	languages	countries
categoriestypeintr   getTOP_HEADLINES_URLr   status_coder   codesokr   json)r   r   r   r   r   r   r"   	page_sizer)   payloadrs              r   get_top_headlineszNewsApiClient.get_top_headlines    sD   n  =q!  NOO x(&.
# UVV w':@TTUU w'%,	" EFF x(u.*2GJ'$%788 FGG w'eoo-)0GI&$%677 EFF x(u///*2GJ'$%788 FGG  I#%	(S( %%YZZ +4GJ'$%YZZ BCC DzS !8&*GFO$%QRR =>> ##E$;$;$))UW`g#h ==HNN---"1668,,vvxr   c                 p   i }|t        |      r||d<   nt        d      |t        |      r||d<   nt        d      |t        |      r||d<   nt        d      |t        |      r||d<   nt        d      |!t        |t              r||d	<   nt        d
      |t	        |      |d<   |t	        |      |d<   |9t        |      r#|t
        j                  vrt        d      ||d<   nt        d      |	9t        |	      r#|	t
        j                  v r|	|d<   nt        d      t        d      |Ft        |      t        k(  r)d|cxk  rdk  rn t        d      ||d<   nt        d      t        d      |
3t        |
      t        k(  r|
dkD  r|
|d<   nt        d      t        d      | j                  j                  t
        j                  | j                  d|      }|j                  t         j"                  j$                  k7  rt'        |j)                               |j)                         S )a|  Call the `/everything` endpoint.

        Search through millions of articles from over 30,000 large and small news sources and blogs.

        :param q: Keywords or a phrase to search for in the article title and body.  See the official News API
            `documentation <https://newsapi.org/docs/endpoints/everything>`_ for search syntax and examples.
        :type q: str or None

        :param qintitle: Keywords or a phrase to search for in the article title and body.  See the official News API
            `documentation <https://newsapi.org/docs/endpoints/everything>`_ for search syntax and examples.
        :type q: str or None

        :param sources: A comma-seperated string of identifiers for the news sources or blogs you want headlines from.
            Use :meth:`NewsApiClient.get_sources` to locate these programmatically, or look at the
            `sources index <https://newsapi.org/sources>`_.
        :type sources: str or None

        :param domains:  A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com)
            to restrict the search to.
        :type domains: str or None

        :param exclude_domains:  A comma-seperated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com)
            to remove from the results.
        :type exclude_domains: str or None

        :param from_param: A date and optional time for the oldest article allowed.
            If a str, the format must conform to ISO-8601 specifically as one of either
            ``%Y-%m-%d`` (e.g. *2019-09-07*) or ``%Y-%m-%dT%H:%M:%S`` (e.g. *2019-09-07T13:04:15*).
            An int or float is assumed to represent a Unix timestamp.  All datetime inputs are assumed to be UTC.
        :type from_param: str or datetime.datetime or datetime.date or int or float or None

        :param to: A date and optional time for the newest article allowed.
            If a str, the format must conform to ISO-8601 specifically as one of either
            ``%Y-%m-%d`` (e.g. *2019-09-07*) or ``%Y-%m-%dT%H:%M:%S`` (e.g. *2019-09-07T13:04:15*).
            An int or float is assumed to represent a Unix timestamp.  All datetime inputs are assumed to be UTC.
        :type to: str or datetime.datetime or datetime.date or int or float or None

        :param language: The 2-letter ISO-639-1 code of the language you want to get headlines for.
            See :data:`newsapi.const.languages` for the set of allowed values.
        :type language: str or None

        :param sort_by: The order to sort articles in.
            See :data:`newsapi.const.sort_method` for the set of allowed values.
        :type sort_by: str or None

        :param page: The number of results to return per page (request).
            20 is the default, 100 is the maximum.
        :type page: int or None

        :param page_size: Use this to page through the results if the total results found is
            greater than the page size.
        :type page_size: int or None

        :return: JSON response as nested Python dictionary.
        :rtype: dict
        :raises NewsAPIException: If the ``"status"`` value of the response is ``"error"`` rather than ``"ok"``.
        r   r   r   r   r   r   domainsz#domains param should be of type strexcludeDomainsz+exclude_domains param should be of type strfromtor   r   r   sortByzinvalid sortz#sort_by param should be of type strr   r%   r&   r'   r(   r)   r*   r+   r,   r-   )r   r0   
isinstancestrr   r   r2   r1   sort_methodr5   r6   r   r7   EVERYTHING_URLr   r9   r   r:   r;   r   r<   )r   r   r   r   rB   exclude_domains
from_paramrE   r   sort_byr)   r=   r>   r?   s                 r   get_everythingzNewsApiClient.get_everything   s_   P  =q!  NOO x(&.
# UVV w'%,	" EFF w'%,	" EFF&/3/,;() MNN !2:>GFO >04GDM x(5??2$%788*2GJ' FGG w'e///(/GH%$^44 EFF  I#%	(S( %%YZZ +4GJ'$%YZZ BCC DzS !8&*GFO$%QRR =>> ##E$8$8tyyRT]d#e ==HNN---"1668,,vvxr   c                 v   i }|9t        |      r#|t        j                  v r||d<   nt        d      t	        d      |9t        |      r#|t        j
                  v r||d<   nt        d      t	        d      |9t        |      r#|t        j                  v r||d<   nt        d      t	        d	      | j                  j                  t        j                  | j                  d
|      }|j                  t        j                  j                  k7  rt        |j!                               |j!                         S )a  Call the `/sources` endpoint.

        Fetch the subset of news publishers that /top-headlines are available from.

        :param category: Find sources that display news of this category.
            See :data:`newsapi.const.categories` for the set of allowed values.
        :type category: str or None

        :param language: Find sources that display news in a specific language.
            See :data:`newsapi.const.languages` for the set of allowed values.
        :type language: str or None

        :param country: Find sources that display news in a specific country.
            See :data:`newsapi.const.countries` for the set of allowed values.
        :type country: str or None

        :return: JSON response as nested Python dictionary.
        :rtype: dict
        :raises NewsAPIException: If the ``"status"`` value of the response is ``"error"`` rather than ``"ok"``.
        r   r   r   r   r    r!   r"   r#   r$   r,   r-   )r   r   r2   r1   r0   r3   r4   r   r7   SOURCES_URLr   r9   r   r:   r;   r   r<   )r   r"   r   r   r>   r?   s         r   get_sourceszNewsApiClient.get_sourcesR  s%   ,  x(u.*2GJ'$%788 FGG w'eoo-)0GI&$%677 EFF x(u///*2GJ'$%788 FGG ##E$5$5DIIrZa#b ==HNN---"1668,,vvxr   )N)NNNenNNNN)NNNNNNNNNNN)NNN)__name__
__module____qualname____doc__r   r@   rN   rQ    r   r   r
   r
      sI    * uyK^ cJ=r   r
   )
__future__r   r   newsapir   newsapi.newsapi_authr   newsapi.newsapi_exceptionr   newsapi.utilsr   r   objectr
   rW   r   r   <module>r^      s%    '   , 6 ?DF Dr   