def get_popularity(self, stock=''): """Get the number of robinhood users who own the given stock Args: stock (str): stock ticker Returns: (int): number of users who own the stock """ stock_instrument = self.get_url(self.quote_data(stock)["instrument"])["id"] return self.get_url(endpoints.instruments(stock_instrument, "popularity"))["num_open_positions"]
def instrument(self, id): """Fetch instrument info Args: id (str): instrument id Returns: (:obj:`dict`): JSON dict of instrument """ url = str(endpoints.instruments()) + "?symbol=" + str(id) try: req = requests.get(url, headers=self.headers, timeout=15) req.raise_for_status() data = req.json() except requests.exceptions.HTTPError: raise RH_exception.InvalidInstrumentId() return data['results']
def instrument(self, id): """Fetch instrument info Args: id (str): instrument id Returns: (:obj:`dict`): JSON dict of instrument """ url = str(endpoints.instruments()) + str(id) + "/" try: req = requests.get(url, timeout=300) req.raise_for_status() data = req.json() except requests.exceptions.HTTPError: raise RH_exception.InvalidInstrumentId() return data
def instruments(self, stock): """Fetch instruments endpoint Args: stock (str): stock ticker Returns: (:obj:`dict`): JSON contents from `instruments` endpoint """ res = self.session.get(endpoints.instruments(), params={'query': stock.upper()}, timeout=15) res.raise_for_status() res = res.json() # if requesting all, return entire object so may paginate with ['next'] if (stock == ""): return res return res['results']