<filename>ally/News/Search.py
# MIT License
#
# Copyright (c) 2020 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from ..Api		import AuthenticatedEndpoint, RequestType






class SearchNews ( AuthenticatedEndpoint ):
	_type		= RequestType.Info
	_resource	= 'market/news/search.json'



	def req_body ( self, **kwargs ):
		"""Return get params together with post body data
		"""
		params = {
			'symbols':kwargs.get('symbols'),
			'maxhits':kwargs.get('limit',10)
		}
		return params, None




	def extract ( self, response ):
		"""Extract certain fields from response
		"""
		k = response.json().get('response')['articles']['article']

		return k




	@staticmethod
	def DataFrame ( raw ):
		import pandas as pd

		# Create dataframe from our dataset
		df = pd.DataFrame( raw ).replace({'na':None}).set_index('id')

		return df







def searchNews ( self, symbols, limit=None, dataframe = True, block: bool = True ):
	"""Searches for news on a set of symbols.

	Calls the 'market/news/search.json' endpoint to search for
	news articles related to some set of symbols.

	Args:
		symbols: Specify the stock symbols for which to search

		limit: (int) maximum number of hits (10 default)

		dataframe: whether to return results as dataframe

		block: Specify whether to block thread if request exceeds rate limit


	Returns:
		Dataframe, or list

	Raises:
		RateLimitException: If block=False, rate limit problems will be raised

	Example:
		.. code-block:: python

		   df = a.searchNews('spy')

		   df.columns
		   # Index(['date', 'headline', 'story'], dtype='object')

		   df.index
		   # Index([...], dtype='object', name='id')

	"""
	result = SearchNews (
		auth		= self.auth,
		account_nbr	= self.account_nbr,
		block		= block,
		limit		= limit,
		symbols		= symbols
	).request()

	if dataframe:
		try:
			result = SearchNews.DataFrame ( result )
		except:
			raise

	return result

<filename>ally/News/Search.py
# MIT License
#
# Copyright (c) 2020 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from ..Api		import AuthenticatedEndpoint, RequestType






class SearchNews ( AuthenticatedEndpoint ):
	_type		= RequestType.Info
	_resource	= 'market/news/search.json'



	def req_body ( self, **kwargs ):
		"""Return get params together with post body data
		"""
		params = {
			'symbols':kwargs.get('symbols'),
			'maxhits':kwargs.get('limit',10)
		}
		return params, None




	def extract ( self, response ):
		"""Extract certain fields from response
		"""
		k = response.json().get('response')['articles']['article']

		return k




	@staticmethod
	def DataFrame ( raw ):
		import pandas as pd

		# Create dataframe from our dataset
		df = pd.DataFrame( raw ).replace({'na':None}).set_index('id')

		return df







def searchNews ( self, symbols, limit=None, dataframe = True, block: bool = True ):
	"""Searches for news on a set of symbols.

	Calls the 'market/news/search.json' endpoint to search for
	news articles related to some set of symbols.

	Args:
		symbols: Specify the stock symbols for which to search

		limit: (int) maximum number of hits (10 default)

		dataframe: whether to return results as dataframe

		block: Specify whether to block thread if request exceeds rate limit


	Returns:
		Dataframe, or list

	Raises:
		RateLimitException: If block=False, rate limit problems will be raised

	Example:
		.. code-block:: python

		   df = a.searchNews('spy')

		   df.columns
		   # Index(['date', 'headline', 'story'], dtype='object')

		   df.index
		   # Index([...], dtype='object', name='id')

	"""
	result = SearchNews (
		auth		= self.auth,
		account_nbr	= self.account_nbr,
		block		= block,
		limit		= limit,
		symbols		= symbols
	).request()

	if dataframe:
		try:
			result = SearchNews.DataFrame ( result )
		except:
			raise

	return result

# MIT License # # Copyright (c) 2020 <NAME> # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. Return get params together with post body data Extract certain fields from response # Create dataframe from our dataset Searches for news on a set of symbols.  	Calls the 'market/news/search.json' endpoint to search for 	news articles related to some set of symbols.  	Args: 		symbols: Specify the stock symbols for which to search  		limit: (int) maximum number of hits (10 default)  		dataframe: whether to return results as dataframe  		block: Specify whether to block thread if request exceeds rate limit   	Returns: 		Dataframe, or list  	Raises: 		RateLimitException: If block=False, rate limit problems will be raised  	Example: 		.. code-block:: python  		   df = a.searchNews('spy')  		   df.columns 		   # Index(['date', 'headline', 'story'], dtype='object')  		   df.index 		   # Index([...], dtype='object', name='id')