11import requests ,time
2- from datetime import datetime
32from newsdataapi import constants
43from typing import Optional ,Union
4+ from datetime import datetime ,timezone
55from urllib .parse import urlencode , quote
66from newsdataapi .newsdataapi_exception import NewsdataException
77
@@ -40,7 +40,7 @@ def __validate_parms(self,param:str,value:Union[list,int,str,bool])->dict:
4040 int_params = {'size' }
4141 string_params = {
4242 'q' ,'qInTitle' ,'country' ,'category' ,'language' ,'domain' ,'domainurl' ,'excludedomain' ,'timezone' ,'page' ,
43- 'from_date' ,'to_date' ,'apikey' ,'qInMeta' ,'prioritydomain' ,'timeframe' ,'tag' ,'sentiment' ,'region'
43+ 'from_date' ,'to_date' ,'apikey' ,'qInMeta' ,'prioritydomain' ,'timeframe' ,'tag' ,'sentiment' ,'region' , 'coin'
4444 }
4545
4646 if param in string_params :
@@ -61,28 +61,28 @@ def __validate_parms(self,param:str,value:Union[list,int,str,bool])->dict:
6161 def __get_feeds (self ,url :str )-> dict :
6262 try :
6363 if self .recursive_retry <= 0 :
64- raise NewsdataException ('maximum retry limit reached.' )
64+ raise NewsdataException ('Maximum retry limit reached. For more information use debug parameter while initializing NewsDataApiClient .' )
6565 response = self .request_method .get (url = url ,proxies = self .proxies ,timeout = self .request_timeout )
6666 if self .is_debug == True :
6767 headers = response .headers
68- print (f'Debug | { datetime .utcnow ( ).replace (microsecond = 0 )} | x_rate_limit_remaining: { headers .get ("x_rate_limit_remaining" )} | x_api_limit_remaining: { headers .get ("x_api_limit_remaining" )} ' )
68+ print (f'Debug | { datetime .now ( tz = timezone . utc ).replace (microsecond = 0 )} | x_rate_limit_remaining: { headers .get ("x_rate_limit_remaining" )} | x_api_limit_remaining: { headers .get ("x_api_limit_remaining" )} ' )
6969 feeds_data :dict = response .json ()
7070 if response .status_code != 200 :
7171 if response .status_code == 500 :
7272 if self .is_debug == True :
73- print (f"Debug | { datetime .utcnow ( ).replace (microsecond = 0 )} | Encountered 'ServerError' going to sleep for: { self .retry_delay } seconds." )
73+ print (f"Debug | { datetime .now ( tz = timezone . utc ).replace (microsecond = 0 )} | Encountered 'ServerError' going to sleep for: { self .retry_delay } seconds." )
7474 time .sleep (self .retry_delay )
7575 self .recursive_retry -= 1
7676 return self .__get_feeds (url = url )
7777 elif feeds_data .get ('results' ,{}).get ('code' ) == 'TooManyRequests' :
7878 if self .is_debug == True :
79- print (f"Debug | { datetime .utcnow ( ).replace (microsecond = 0 )} | Encountered 'TooManyRequests' going to sleep for: { constants .DEFAULT_RETRY_DELAY_TooManyRequests } seconds." )
79+ print (f"Debug | { datetime .now ( tz = timezone . utc ).replace (microsecond = 0 )} | Encountered 'TooManyRequests' going to sleep for: { constants .DEFAULT_RETRY_DELAY_TooManyRequests } seconds." )
8080 time .sleep (constants .DEFAULT_RETRY_DELAY_TooManyRequests )
8181 self .recursive_retry -= 1
8282 return self .__get_feeds (url = url )
8383 elif feeds_data .get ('results' ,{}).get ('code' ) == 'RateLimitExceeded' :
8484 if self .is_debug == True :
85- print (f"Debug | { datetime .utcnow ( ).replace (microsecond = 0 )} | Encountered 'RateLimitExceeded' going to sleep for: { constants .DEFAULT_RETRY_DELAY_RateLimitExceeded } seconds." )
85+ print (f"Debug | { datetime .now ( tz = timezone . utc ).replace (microsecond = 0 )} | Encountered 'RateLimitExceeded' going to sleep for: { constants .DEFAULT_RETRY_DELAY_RateLimitExceeded } seconds." )
8686 time .sleep (constants .DEFAULT_RETRY_DELAY_RateLimitExceeded )
8787 self .recursive_retry -= 1
8888 return self .__get_feeds (url = url )
@@ -195,21 +195,21 @@ def sources_api( self, country:Optional[str]= None, category:Optional[str]= None
195195 return self .__get_feeds (url = f'{ constants .SOURCES_URL } ?{ URL_parameters_encoded } ' )
196196
197197 def crypto_api (
198- self , q :Optional [str ]= None , qInTitle :Optional [str ]= None , country :Optional [Union [str , list ]]= None , category :Optional [Union [str , list ]]= None ,
199- language :Optional [Union [str , list ]]= None , domain :Optional [Union [str , list ]]= None , timeframe :Optional [Union [int , str ]] = None , size : Optional [ int ]= None ,
200- domainurl :Optional [Union [ str , list ]] = None , excludedomain :Optional [Union [ str , list ]] = None , timezone :Optional [str ]= None , full_content :Optional [bool ]= None ,
201- image :Optional [bool ]= None , video :Optional [bool ]= None , prioritydomain :Optional [str ]= None , page :Optional [str ]= None , scroll :Optional [bool ] = False ,
202- max_result :Optional [int ] = None , qInMeta : Optional [ str ]= None ,tag :Optional [Union [str ,list ]]= None , sentiment : Optional [ str ] = None ,
198+ self , q :Optional [str ]= None , qInTitle :Optional [str ]= None ,language :Optional [Union [str , list ]]= None , domain :Optional [Union [str , list ]]= None ,
199+ timeframe :Optional [Union [int , str ]]= None , size :Optional [int ] = None , domainurl : Optional [ Union [str , list ]]= None , excludedomain :Optional [Union [str , list ] ]= None ,
200+ timezone :Optional [str ] = None , full_content :Optional [bool ] = None , image : Optional [ bool ] = None , video :Optional [bool ]= None , prioritydomain :Optional [str ]= None ,
201+ page :Optional [str ]= None , scroll :Optional [bool ]= False , max_result :Optional [int ]= None , qInMeta :Optional [str ]= None ,tag :Optional [Union [ str , list ]] = None ,
202+ sentiment :Optional [str ]= None ,coin :Optional [Union [str , list ]]= None
203203 )-> dict :
204204 """
205205 Sending GET request to the crypto api
206206 For more information about parameters and input, Please visit our documentation page: https://newsdata.io/documentation
207207 """
208208
209209 params = {
210- 'apikey' :self .apikey ,'q' :q ,'qInTitle' :qInTitle ,'country' : country , 'category' : category , ' language' :language ,'domain' :domain ,'size' :size ,'domainurl' :domainurl ,
210+ 'apikey' :self .apikey ,'q' :q ,'qInTitle' :qInTitle ,'language' :language ,'domain' :domain ,'size' :size ,'domainurl' :domainurl ,
211211 'excludedomain' :excludedomain ,'timezone' :timezone ,'full_content' :full_content ,'image' :image ,'video' :video ,'prioritydomain' :prioritydomain ,'page' :page ,
212- 'timeframe' :str (timeframe ) if timeframe else timeframe ,'qInMeta' :qInMeta ,'tag' :tag , 'sentiment' :sentiment
212+ 'timeframe' :str (timeframe ) if timeframe else timeframe ,'qInMeta' :qInMeta ,'tag' :tag , 'sentiment' :sentiment , 'coin' : coin
213213 }
214214
215215 URL_parameters = {}
0 commit comments