# Exercise 5.1 import base64 import requests import json from password import apiSecret ## Test item record: i1001890 (Five smooth stones) # API key/secret key = 'F/bQEdfLuNdzjNnNIUmG+okZP36/' rest_uri = 'https://sierratrain.iii.com:443/iii/sierra-api/v6/' # Assign item record to be updated. item_number = str(1001890) # Set up variables for API URIs. auth_uri = rest_uri + 'token' item_uri = rest_uri + 'items/' + item_number + '?fields=fixedFields%2CbibIds' item_var_uri = rest_uri + 'items/' + item_number + '?fields=varFields' bib_uri = rest_uri + 'bibs/1100871?fields=title' item_update_uri = rest_uri + 'items/' + item_number # Construct merged key and secret. auth_str = key + ':' + apiSecret auth_bytes = auth_str.encode('ascii') auth_b64 = base64.b64encode(auth_bytes) b64_str = auth_b64.decode('ascii') # Function to get fixed-length data and bib record number from item. def get_item_fix_data(item_uri, token_value): get_fix_item = requests.get(item_uri, headers={"Authorization": "Bearer " + token_value}) get_fix_item_string = get_fix_item.content.decode("UTF-8") gfi = get_fix_item_string[51:-1] gfi_json = json.loads(gfi) bib_ID = get_fix_item_string[27:34] return(gfi_json, bib_ID) # Function to get variable-length data from item. def get_item_var_data(item_var_uri, token_value): get_var_item = requests.get(item_var_uri, headers={"Authorization": "Bearer " + token_value}) get_var_item_string = get_var_item.content.decode("UTF-8") gvi = get_var_item_string[28:-1] gvi_json = json.loads(gvi) return(gvi_json) # Function to get title from bib record. def get_item_title(bib_uri, token_value): get_bib_title = requests.get(bib_uri, headers={"Authorization": "Bearer " + token_value}) get_bib_title_string = get_bib_title.content.decode("UTF-8") title = get_bib_title_string[25:-2] return(title) # Function to output the item data. def output_item (item_number, title, price, gvi_json): print('\ni{} ({})\nprice: {}'.format(item_number, title, price)) print('Variable fields: ') for num in range (len(gvi_json)): print('{}: '.format(gvi_json[num]['fieldTag']), end=' ') if 'subfields' in gvi_json[num]: for num2 in range (len(gvi_json[num]['subfields'])): print('|{}{}'.format(gvi_json[num]['subfields'][num2]['tag'], gvi_json[num]['subfields'][num2]['content']), end=' ') print('\n') else: print(gvi_json[num]['content']) return() # Function to update the item record to add price to the variable-length # note field. def update_item (item_update_uri, token_value, price): body = '{ "internalNotes": [ "' + price + '" ] }' up_item = requests.put(item_update_uri, headers={"Authorization": "Bearer " + token_value, 'content-type':'application/json'}, data = body) return() # Generate token token = requests.post(auth_uri, headers={"Authorization": "Basic " + b64_str}) token_string = token.content.decode("UTF-8") token_json = json.loads(token_string) token_value = token_json["access_token"] # Acquire fixed-length data and bib record number. gfi_json, bib_ID = get_item_fix_data(item_uri, token_value) # Acquire variable-length data. gvi_json = get_item_var_data(item_var_uri, token_value) # Assign the price field price = str(float(gfi_json['62']['value'])/100) # Use item data to get the title from the linked bib record. bib_uri = rest_uri + 'bibs/' + bib_ID + '?fields=title' title = get_item_title(bib_uri, token_value) # Output item data before update. print('\nItem before update: ') output_item (item_number, title, price, gvi_json) # Update item data update_item (item_update_uri, token_value, price) # Refresh variable-length data and output item data after update. print('\nItem after update: ') gvi_json = get_item_var_data(item_var_uri, token_value) output_item (item_number, title, price, gvi_json) ################################## # Exercise 5.2 import base64 import requests import json from password import apiSecret # API key/secret key = 'F/bQEdfLuNdzjNnNIUmG+okZP36/' rest_uri = 'https://sierratrain.iii.com:443/iii/sierra-api/v6/' # Assign series title s_title = 'The magic school bus : a science chapter book' # Create the body to be used to find bib records matching the series. body = '{"target": {"record": {"type": "bib"}, "field": {"marcTag": "490", "subfields": "a"}},"expr": {"op": "has","operands": ["' + s_title +'", ""] } }' # Set up API URIs auth_uri = rest_uri + 'token' bib_uri = rest_uri + 'bibs/query?offset=0&limit=100' # Construct merged key and secret. auth_str = key + ':' + apiSecret auth_bytes = auth_str.encode('ascii') auth_b64 = base64.b64encode(auth_bytes) b64_str = auth_b64.decode('ascii') # Generate token token = requests.post(auth_uri, headers={"Authorization": "Basic " + b64_str}) token_string = token.content.decode("UTF-8") token_json = json.loads(token_string) token_value = token_json["access_token"] # Launch search for bib records matching series. series_search = requests.post(bib_uri, headers={"Authorization": "Bearer " + token_value, 'content-type':'application/json'}, data = body) string_ss = series_search.content.decode("UTF-8") string_ss_2 = string_ss[string_ss.find('['):][:-1] json_ss = json.loads(string_ss_2) # Identify the MARC 245 (title) and 490 (series) for each bib record retrieved. series = [{'title': None, 'series_num': None}] for num in range (len(json_ss)): ss_bib = requests.get(json_ss[num]['link'] + '?fields=varFields,', headers={"Authorization": "Bearer " + token_value}) info_ss_bib = ss_bib.content.decode("UTF-8") info_ss_bib_2 = info_ss_bib.replace('"id":"{}","varFields":'.format(json_ss[num]['link'].replace('https://sierratrain.iii.com/iii/sierra-api/v6/bibs/', '')), '')[1:][:-1] info_json_ss = json.loads(info_ss_bib_2) for num in range(len(info_json_ss)): if info_json_ss[num]['fieldTag'] == 't': if info_json_ss[num]['marcTag'] == '245': for num2 in range(len(info_json_ss[num]['subfields'])): if info_json_ss[num]['subfields'][num2]['tag'] == 'a': st = info_json_ss[num]['subfields'][num2]['content'][:-1] for num in range(len(info_json_ss)): if info_json_ss[num]['fieldTag'] == 's': if info_json_ss[num]['marcTag'] == '490': for num2 in range(len(info_json_ss[num]['subfields'])): if info_json_ss[num]['subfields'][num2]['tag'] == 'v': sn = info_json_ss[num]['subfields'][num2]['content'] series.append({'title': st, 'series_num': sn}) # Get the highest numbered volume within series. del series[0] series_sorted = sorted(series, key=lambda s: (s['series_num'])) top_vol = int(series_sorted[len(series) - 1]['series_num'][1:-1]) # Output each title of series in order, compare to a full numbered list # from 1 to the highest volume number. If comparison results in a missing # volume, report that as missing. Otherwise display title with the volume # number. print('\nSeries information for {}\n'.format(s_title)) for num in range(top_vol): success = 0 for num2 in range(len(series_sorted)): if '#{}.'.format(str(num + 1).zfill(2)) == series_sorted[num2]['series_num']: print(' {} ({})'.format(series_sorted[num2]['title'], series_sorted[num2]['series_num'])) success = 1 if success == 0: print('Missing: #{}.'.format(str(num + 1).zfill(2)))