Last active
October 18, 2016 05:52
-
-
Save lordloh/e7333122c96b7f8f22ab67b95e86cf7d to your computer and use it in GitHub Desktop.
A snippet that helps cache the instrument -> stock symbol API request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code by Bharath Bhushan lohray | |
# https://bharath.lohray.com/ | |
# github: lordloh | |
# bitbuckte: lordloh | |
def dict_factory(cursor, row): | |
d = {} | |
for idx, col in enumerate(cursor.description): | |
d[col[0]] = row[idx] | |
return d | |
def resolve_instrument(R,c,instrument_url): | |
instrument_id = get_instrument_from_url(instrument_url) | |
c.execute('SELECT symbol FROM instruments where id=?', (instrument_id,)) | |
sym_record=c.fetchone() | |
if (sym_record==None): | |
i_obj = R.get_url(instrument_url) | |
c.execute('INSERT INTO instruments VALUES (?,?,?,?,?,?,?)',\ | |
(str(i_obj['id']),\ | |
str(i_obj['name']),\ | |
str(i_obj['symbol']),\ | |
str(i_obj['market']),\ | |
str(i_obj['fundamentals']),\ | |
str(i_obj['quote']),\ | |
str(i_obj['splits']))) | |
symbol=str(i_obj['symbol']); | |
else: | |
symbol=str(sym_record[0]); | |
return symbol | |
def get_instrument_from_url(instrument_url): | |
instrument_id_part = str(instrument_url).split('/') | |
instrument_id = instrument_id_part[-2] | |
return instrument_id | |
robinhood_db = sqlite3.connect('robinhood.db') | |
robinhood_db.row_factory = dict_factory | |
c = robinhood_db.cursor() | |
c.execute('''CREATE TABLE IF NOT EXISTS instruments(id text PRIMARY KEY, name text, symbol text, market text, fundamentals text, quote text, splits text)''') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment