def read_stock(self, company): try: url = self.STOCK_API.format(company.stockExchangeCode, company.code) response = requests.get(url) if 'dataset' not in response.json(): print response.json() self.sleep_time = 2 time.sleep(2) self.read_stock(company) data = response.json()['dataset'] if (company.refreshedAt - helper.str_to_datetime(data['refreshed_at']).replace( tzinfo=None)) == timedelta(0): print company.stockExchangeCode, company.code, company.name, "Continuing" else: print company.stockExchangeCode, company.code, company.name, "Updating" company.refreshedAt = helper.str_to_datetime( data['refreshed_at']) company.oldestAvailableDate = helper.str_to_datetime( data['oldest_available_date']) company.newAvailableDate = helper.str_to_datetime( data['newest_available_date']) company.history = helper.str_to_datetime(data) company.save() except Exception as e: print "Error (" + company.code + "): " + e.__str__() pass self.lock.acquire() self.count += 1 self.lock.release()
def _extract_company_data(self, data): history = self._extract_history(data) return { "name": data['name'], "code": data['dataset_code'], #"quandlCode": data['dataset_code'], "description": data['description'], "stockExchangeCode": data['database_code'], "type": data['type'], "frequency": data['frequency'], "refreshedAt": helper.str_to_datetime(data['refreshed_at']), "oldestAvailableDate": helper.str_to_datetime(data['oldest_available_date']), "newAvailableDate": helper.str_to_datetime(data['newest_available_date']), "history": history, "historyCount": len(history), }
def timesince(date, default="Just Now"): """ Returns string representing "time since" e.g. 3 days ago, 5 hours ago """ now = helper.now() if not isinstance(date, datetime): date = helper.str_to_datetime(date) diff = now - date periods = ( (diff.days / 365, "year", "years"), (diff.days / 30, "month", "months"), (diff.days / 7, "week", "weeks"), (diff.days, "day", "days"), (diff.seconds / 3600, "hour", "hours"), (diff.seconds / 60, "minute", "minutes"), (diff.seconds, "second", "seconds"), ) for period, singular, plural in periods: if period: return "%d %s ago" % (period, singular if period == 1 else plural) return default
def transform_quandl_stock(data): """ Will not be used. Using in stockapis.BSE""" stock = None if len(data['data']): # send by stockExchangeCode stock = { "date": data['data'][0][0], "open": data['data'][0][1], "high": data['data'][0][2], "low": data['data'][0][3], "last": data['data'][0][4], "close": data['data'][0][5], "totalTradeQuantity": data['data'][0][6], "turnover": data['data'][0][7], } stock['refreshedAt'] = helper.str_to_datetime(data['refreshed_at']) return stock
def transform_quandl(self, data): stock = None if 'data' not in data: return self.transform(data) if len(data['data']): # send by stockExchangeCode stock = { "date": data['data'][0][0], "open": data['data'][0][1], "high": data['data'][0][2], "low": data['data'][0][3], "last": data['data'][0][4], "close": data['data'][0][5], "totalTradeQuantity": data['data'][0][6], "turnover": data['data'][0][7], } stock['refreshedAt'] = helper.str_to_datetime(data['refreshed_at']) return stock