mattlisiv / newsapi-python

A Python Client for News API
MIT License
403 stars 139 forks source link

newsapi.newsapi_exception.NewsAPIException #59

Open reshu-b7 opened 5 years ago

reshu-b7 commented 5 years ago

While trying to run the following code snippet , which is actually a part of documentation :

all_articles = newsapi.get_everything(q='bitcoin',
                                      sources='bbc-news,the-verge',
                                      domains='bbc.co.uk,techcrunch.com',
                                      from_param='2017-12-01',
                                      to='2017-12-12',
                                      language='en',
                                      sort_by='relevancy',
                                      page=2)

I am getting this error again and again :

Traceback (most recent call last):
  File "news.py", line 16, in <module>
    page=2)
  File "C:\Python27\lib\site-packages\newsapi\newsapi_client.py", line 261, in get_everything
    raise NewsAPIException(r.json())
newsapi.newsapi_exception.NewsAPIException

Any suggestions on why this might be happening ?

danielmichaels commented 5 years ago

I've tried to replicate this in Python 2.7.17 and 3.7.4 with the following results.

python3:

I started with Python 3 because I am more familiar with it. The snippet you've supplied works as expected with a caveat; are you using a free plan? If so, you will get the following exception:

Traceback (most recent call last):
  File "/home/daniel/Code/junkyard/newsapi_test.py", line 12, in <module>
    page=2)
  File "/home/daniel/.virtualenvs/scripts/lib/python3.7/site-packages/newsapi/newsapi_client.py", line 261, in get_everything
    raise NewsAPIException(r.json())
newsapi.newsapi_exception.NewsAPIException: {'status': 'error', 'code': 'parameterInvalid', 'message': 'You are trying to request results too far in the past. Your plan permits you to request articles as far back as 2019-09-26, but you have requested 2018-10-01. To extend this please upgrade to a paid plan.'}

Adjusting the from_param and to within the allowable date range returns as expected.

python2:

Running the same all_articles command returns the exception as detailed by @reshu-b7

Traceback (most recent call last):
  File "/home/daniel/Code/junkyard/newsapi_test.py", line 12, in <module>
    page=2)
  File "/home/daniel/.virtualenvs/py2/lib/python2.7/site-packages/newsapi/newsapi_client.py", line 261, in get_everything
    raise NewsAPIException(r.json())
newsapi.newsapi_exception.NewsAPIException

Again, adjusting the from_param and to parameters works as excepted.

For clarity, the snippet I used to get it working:

all_articles = newsapi.get_everything(q='bitcoin',
                       sources='bbc-news,the-verge',
                       domains='bbc.co.uk,techcrunch.com',
                       from_param='2019-10-01',
                       to='2019-10-12',
                       language='en',
                       sort_by='relevancy',
                       page=2)

Without doing much more investigation than that, I would suggest it is a case of the exception not raising as expected in Python 2 and leaving the user guessing as to the problem.

Hope that helps, or gets us looking at it.