def save_json(cls, data, tickers, _from, _to): with open( './json/{}-{}-{}.json'.format(tickers.replace(',', '_'), _from, _to), 'w') as outfile: json.dump(data, outfile) logger.info('{}-{}-{} - saved'.format(tickers.replace(',', '_'), _from, _to))
async def handle(request): """Handle incoming requests.""" pool = request.app['pool'] _code = request.rel_url.query.get('codes') _start_date = request.rel_url.query.get('start_date') _end_date = request.rel_url.query.get('end_date') codes = [] for code in _code.split(','): codes.append(code) # Take a connection from the pool. async with pool.acquire() as connection: await connection.set_type_codec('json', encoder=json.dumps, decoder=json.loads, schema='pg_catalog') # Open a transaction. async with connection.transaction(): # Run the query passing the request argument. results = await connection.fetch( '''SELECT json_agg(json_build_object( 'code', t.code, 'd_change', t.d_change )) results FROM stock_returns_by_given_dates_v3('{%s}', '%s', '%s') t;''' % (','.join(codes), _start_date, _end_date)) logger.info(results[0].get('results')) return web.json_response({ 'code': _code, 'start_date': _start_date, 'end_date': _end_date, 'result': results[0].get('results') })
def load_data_from_db(self): logger.info('loading data from db') try: self.db_connection = sqlite3.connect() cur = self.db_connection.corsor() cur.execute("SELECT * FROM holidays") rows = cur.fetchall() return rows except Exception as e: logger.error(traceback.format_tb(e.__traceback__))
def init(cls): logger.info('initializing util class....') # cls.load_data_from_db() if not cls._unique_instance: with cls._lock: if not cls._unique_instance: cls._unique_instance = cls.__internal_new__() return cls._unique_instance
def run(self, num_of_requests): request_lists = [] for ticker in self.tickers: for value in self.values: logger.info('requesting {0} for {1}-{2}'.format( ticker, value[0], value[1])) request_lists.append({ 'tickers': ticker, '_from': value[0], '_to': value[1] }) start = self.count_existing_json_file() logger.info('start from {}th element'.format(start)) for request in request_lists[start:start + int(num_of_requests)]: logger.info('sending a request for {} from {} to {}'.format( request['tickers'], request['_from'], request['_to'])) d = self.send_request(request['tickers'], request['_from'], request['_to']) if d.get("message") == "Limit Exceeded": logger.error('request limit exceeded') sys.exit() self.save_json(d, request['tickers'], request['_from'], request['_to']) time.sleep(0.5)