def fetch_quotes(ticker_id_dict): global logger global logger_alert # timestamp start_time = datetime.datetime.now() fetch_time = chop_microseconds(start_time) count = -1 try: # build query url for api baseurl = "https://query.yahooapis.com/v1/public/yql?" yql_query = "select * from yahoo.finance.quote where symbol in ('" ticker_url = "','".join(ticker_id_dict.keys()) yql_query = yql_query + ticker_url + "')" yql_url = baseurl + urllib.urlencode({'q': yql_query}) + \ "&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=" # get data for i in range(5): try: result = urllib2.urlopen(yql_url).read() log_string = '(QUERY:' + yql_url + '),SUCCESS' logger.info(log_string) break except: log_string = '(QUERY:' + yql_url + '),FAIL' logger.info(log_string) if (i == 4): return data = json.loads(result) quote_data = data['query']['results']['quote'] # create objects quotes = [Entry.Quote(id=ticker_id_dict[q['symbol']], price=unicode2int(q['LastTradePriceOnly']), volume=q['Volume'], time=fetch_time) for q in quote_data] count = len(quotes) # write results into db Entry.Quote.add(quotes) result = 'True' except Exception as e: result = 'False' logger_alert.exception('fetch_error@' + str(fetch_time)) finish_time = datetime.datetime.now() log_string = "[({0}),({1}),{2},{3}]".format(start_time, finish_time, result, count) logger.info(log_string)
def update_company_info(): stocks = Stock.search() tickers = [x['ticker'] for x in stocks] # get company info and stores in db # build query url for api baseurl = "https://query.yahooapis.com/v1/public/yql?" yql_query = "select * from yahoo.finance.quote where symbol in ('" ticker_url = "','".join(tickers) yql_query = yql_query + ticker_url + "')" yql_url = baseurl + urllib.urlencode({'q': yql_query}) + \ "&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback=" print yql_url # get data result = urllib2.urlopen(yql_url).read() data = json.loads(result) quote_data = data['query']['results']['quote'] for q in quote_data: stock = Stock.search(ticker=q['symbol'])[0] stock['name'] = q['Name'][:20] stock['exchange'] = q['StockExchange'] stock['pv_close'] = unicode2int(q['LastTradePriceOnly']) stock['pv_volume'] = q['Volume'] stock.save()