Add support + Tests for using News API

This commit is contained in:
Cameron Yick
2017-09-17 15:17:03 -04:00
parent 87246485d5
commit 14d3bb6bd3
2 changed files with 52 additions and 31 deletions

View File

@@ -3,7 +3,7 @@
"""Tests for `tiingo` package.""" """Tests for `tiingo` package."""
import csv import csv
import pytest
from unittest import TestCase from unittest import TestCase
from tiingo import TiingoClient from tiingo import TiingoClient
from tiingo.restclient import RestClientError from tiingo.restclient import RestClientError
@@ -12,7 +12,7 @@ from tiingo.restclient import RestClientError
# TODO # TODO
# Add tests for # Add tests for
# Invalid API key # Invalid API key
# Invalid ticker, etc # Invalid ticker
# Use unittest asserts rather than regular asserts # Use unittest asserts rather than regular asserts
# Wrap server errors with client side descriptive errors # Wrap server errors with client side descriptive errors
# Coerce startDate/endDate to string if they are passed in as datetime # Coerce startDate/endDate to string if they are passed in as datetime
@@ -70,21 +70,42 @@ class TestTickerPrices(TestCase):
response = self._client.list_tickers() response = self._client.list_tickers()
assert not response assert not response
# News Feed
# tiingo/news # tiingo/news
class TestNews(TestCase): class TestNews(TestCase):
def setUp(self): def setUp(self):
self._client = TiingoClient() self._client = TiingoClient()
self.article_keys = [ # Properties every article should have.
'description',
'title',
'url',
'publishedDate',
'tags',
'source',
'tickers',
'crawlDate',
'id'
]
def test_get_news_articles(self): def test_get_news_articles(self):
"""Rewrite when method is implemented """Confirm that news article work"""
""" search_params = {
with self.assertRaises(NotImplementedError): "tickers": ["aapl", "googl"],
self._client.get_news() "tags": ["Technology", "Bitcoin"],
"startDate": "2016-01-01",
"endDate": "2017-08-31",
"sources": ['washingtonpost.com', 'altcointoday.com'],
"limit": 10
}
articles = self._client.get_news(**search_params)
for article in articles:
assert all(key in article for key in self.article_keys)
def test_get_news_bulk(self): def test_get_news_bulk(self):
"""Will fail because this API key lacks institutional license""" """Fails because this API key lacks institutional license"""
with self.assertRaises(RestClientError): with self.assertRaises(RestClientError):
value = self._client.get_bulk_news(file_id="1") value = self._client.get_bulk_news(file_id="1")
assert value assert value

View File

@@ -94,7 +94,7 @@ class TiingoClient(RestClient):
"""Return list of news articles matching given search terms """Return list of news articles matching given search terms
https://api.tiingo.com/docs/tiingo/news https://api.tiingo.com/docs/tiingo/news
# If no tickers provided, just searches general tickers # Dates are in YYYY-MM-DD Format.
Args: Args:
tickers [string] : List of unique Stock Tickers to search tickers [string] : List of unique Stock Tickers to search
@@ -105,28 +105,28 @@ class TiingoClient(RestClient):
offset (int): Search results offset, used for paginating offset (int): Search results offset, used for paginating
sortBy (string): "publishedDate" OR (#TODO: UPDATE THIS) sortBy (string): "publishedDate" OR (#TODO: UPDATE THIS)
""" """
# url = "tiingo/news" url = "tiingo/news"
# params = { params = {
# 'limit': limit, 'limit': limit,
# 'offset': offset, 'offset': offset,
# 'sortBy': sortBy 'sortBy': sortBy
# } }
# # TBD: whether these commas are necessary if just pass list instead
# if tickers:
# params['tickers'] = ",".join(tickers)
# if tags:
# params['tags'] = ",".join(tags)
# if sources:
# params['sources'] = ",".join(sources)
# if startDate: # TBD: whether these commas are necessary if just pass list instead
# params['startDate'] = startDate if tickers:
# if endDate: params['tickers'] = ",".join(tickers)
# params['endDate'] = endDate if tags:
params['tags'] = ",".join(tags)
if sources:
params['sources'] = ",".join(sources)
# response = self._request('GET', url, params=params) if startDate:
# return response.json() params['startDate'] = startDate
raise NotImplementedError if endDate:
params['endDate'] = endDate
response = self._request('GET', url, params=params)
return response.json()
def get_bulk_news(self, file_id=None): def get_bulk_news(self, file_id=None):
"""Only available to institutional clients. """Only available to institutional clients.
@@ -135,9 +135,9 @@ class TiingoClient(RestClient):
file, as well as some metadata about that file. file, as well as some metadata about that file.
""" """
if file_id: if file_id:
url = "tiingo/news/bulk_download" url = "tiingo/news/bulk_download/{}".format(file_id)
else: else:
url = "tiingo/news/bulk_download{}".format(file_id) url = "tiingo/news/bulk_download"
response = self._request('GET', url) response = self._request('GET', url)
return response.json() return response.json()