def get_latest_records(auth_key, zip3_loc_data, use_grades=['A', 'B', 'C', 'D', 'E', 'F']): header = {'Authorization': auth_key, 'Content-Type': 'application/json'} apiVersion = 'v1' loanListURL = 'https://api.lendingclub.com/api/investor/' + apiVersion + \ '/loans/listing' payload = {'showAll': 'true'} resp = requests.get(loanListURL, headers=header, params=payload) loans = resp.json()['loans'] '''Make a list of tuples specifying the name of each data column to pull, and a function to use to grab that piece of data from the raw loan data''' record_map = ( ('acc_now_delinq', lambda x: x['accNowDelinq']), ('annual_inc', lambda x: x['annualInc']), ('collections_12_mths_ex_med', lambda x: x['collections12MthsExMed']), ('cr_line_dur', lambda x: LCL.get_cr_line_dur(x['earliestCrLine'])), ('delinq_2yrs', lambda x: x['delinq2Yrs']), ('desc_length', lambda x: LCL.get_desc_length(x['desc'])), ('dti', lambda x: x['dti']), ('emp_length', lambda x: LCL.get_emp_length(x['empLength']) ), #convert to years from months ('id', lambda x: x['id']), # ('funded_amnt', lambda x: x['loanAmount']), #use amount requested rather than funded amnt! ('loan_amnt', lambda x: x['loanAmount'] ), #use amount requested rather than funded amnt! ('inq_last_6mths', lambda x: x['inqLast6Mths']), ('int_rate', lambda x: x['intRate']), ('mths_since_last_delinq', lambda x: LCL.get_mnths_since(x['mthsSinceLastDelinq'])), ('mths_since_last_major_derog', lambda x: LCL.get_mnths_since(x['mthsSinceLastMajorDerog'])), ('mths_since_last_record', lambda x: LCL.get_mnths_since(x['mthsSinceLastRecord'])), ('num_add_desc', lambda x: LCL.get_num_descs(x['desc'])), ('open_acc', lambda x: x['openAcc']), ('pub_rec', lambda x: x['pubRec']), ('revol_bal', lambda x: x['revolBal']), ('revol_util', lambda x: x['revolUtil']), ('term', lambda x: x['term']), ('total_acc', lambda x: x['totalAcc']), ('tot_cur_bal', lambda x: x['totCurBal']), ('addr_state', lambda x: x['addrState']), ('home_ownership', lambda x: x['homeOwnership']), ('grade', lambda x: x['grade']), ('purpose', lambda x: x['purpose']), ('latitude', lambda x: LCL.get_zip_loc(x['addrZip'], zip3_loc_data, 'latitude')), ('longitude', lambda x: LCL.get_zip_loc(x['addrZip'], zip3_loc_data, 'longitude'))) records = [make_record(loan, record_map) for loan in loans] records = [record for record in records if record['grade'] in use_grades] return records
def get_latest_records(auth_key, zip3_loc_data, use_grades = ['A','B','C','D','E','F']): header = {'Authorization' : auth_key, 'Content-Type': 'application/json'} apiVersion = 'v1' loanListURL = 'https://api.lendingclub.com/api/investor/' + apiVersion + \ '/loans/listing' payload = {'showAll' : 'true'} resp = requests.get(loanListURL, headers=header, params=payload) loans = resp.json()['loans'] '''Make a list of tuples specifying the name of each data column to pull, and a function to use to grab that piece of data from the raw loan data''' record_map = (('acc_now_delinq', lambda x: x['accNowDelinq']), ('annual_inc', lambda x: x['annualInc']), ('collections_12_mths_ex_med', lambda x: x['collections12MthsExMed']), ('cr_line_dur', lambda x: LCL.get_cr_line_dur(x['earliestCrLine'])), ('delinq_2yrs', lambda x: x['delinq2Yrs']), ('desc_length', lambda x: LCL.get_desc_length(x['desc'])), ('dti', lambda x: x['dti']), ('emp_length', lambda x: LCL.get_emp_length(x['empLength'])), #convert to years from months ('id', lambda x: x['id']), # ('funded_amnt', lambda x: x['loanAmount']), #use amount requested rather than funded amnt! ('loan_amnt', lambda x: x['loanAmount']), #use amount requested rather than funded amnt! ('inq_last_6mths', lambda x: x['inqLast6Mths']), ('int_rate', lambda x: x['intRate']), ('mths_since_last_delinq', lambda x: LCL.get_mnths_since(x['mthsSinceLastDelinq'])), ('mths_since_last_major_derog', lambda x: LCL.get_mnths_since(x['mthsSinceLastMajorDerog'])), ('mths_since_last_record', lambda x: LCL.get_mnths_since(x['mthsSinceLastRecord'])), ('num_add_desc', lambda x: LCL.get_num_descs(x['desc'])), ('open_acc', lambda x: x['openAcc']), ('pub_rec', lambda x: x['pubRec']), ('revol_bal', lambda x: x['revolBal']), ('revol_util', lambda x: x['revolUtil']), ('term', lambda x: x['term']), ('total_acc', lambda x: x['totalAcc']), ('tot_cur_bal', lambda x: x['totCurBal']), ('addr_state', lambda x: x['addrState']), ('home_ownership', lambda x: x['homeOwnership']), ('grade', lambda x: x['grade']), ('purpose', lambda x: x['purpose']), ('latitude', lambda x: LCL.get_zip_loc(x['addrZip'], zip3_loc_data, 'latitude')), ('longitude', lambda x: LCL.get_zip_loc(x['addrZip'], zip3_loc_data, 'longitude'))) records = [make_record(loan, record_map) for loan in loans] records = [record for record in records if record['grade'] in use_grades] return records