Skip to main content
ExLibris
  • Subscribe by RSS
  • Ex Libris Knowledge Center

    Python script to scan in items enroute to a department, as determined by an Alma Analytics report

    Created By: Lisa Hamlett
    Created on: 1/17/2020



    #!/opt/rh/python27/root/usr/bin/python
    """
       This script scans in items enroute to Woodruff Technical Services from Preservation, as determined by an Alma Analytics report
    """
    __author__= 'Lisa Hamlett'
    __date__ = 'January 2020'

    import sys
    import requests
    import xml.etree.ElementTree as ET
    import time

    # DEFINE SECONDARY FUNCTIONS FOR USE BY MAIN FUNCTION

    ## GET ANALYTICS REPORT
    def get_analytics_report(analytics_url,path,analytics_apikey):
      payload = {'path':path, 'apikey':analytics_apikey}
      r = requests.get(analytics_url,params=payload)
      response = r.content
      url = r.url
      return response,url

    ## GET BARCODES FROM ANALYTICS REPORT
    def get_barcodes(xml):
      xml = xml.replace("\n", "")
      xml = xml.replace(' xmlns="urn:schemas-microsoft-com:xml-analysis:rowset"', "")
      tree = ET.fromstring(xml)

      barcode = ""
      parsed_xml = []
      rowset = tree.find("QueryResult/ResultXml/rowset")
      rows = rowset.findall("Row")
      if rows != None:
        for row in rows:
          barcode = row.find("Column1")
          barcode = str(barcode.text)
          parsed_xml.append(barcode)
      return parsed_xml

    ## GET ITEM INFO FROM ALMA
    def get_item_info(line,api_key):
      barcode = line
      payload = { 'item_barcode' : barcode, 'apikey' : api_key }
      r = requests.get('https://api-na.hosted.exlibrisgroup....lmaws/v1/items?', params=payload)
      return r

    ## SCAN IN ITEMS
    def scan_item(r,api_key):

      ### RETRIEVE MMS_ID, HOLDING_ID, and ITEM_PID
      root = ET.fromstring(r.text)

      ### FEED INTO PAYLOAD
      mmsid = root[0][0].text
      holdingid = root[1][0].text
      pid = root[2][0].text

      ### GET ITEM STATUS INFORMATION
      item_data = root.find("item_data")
      base_status = item_data.find("base_status")
      base_status_desc = base_status.get("desc")
      process_type = item_data.find("process_type")
      process_type_desc = process_type.get("desc")

      ### SEND PAYLOAD TO SCAN-IN URL
      payload2 = {'op' : 'scan', 'library' : 'UNIV', 'department' : 'AcqDeptUNIV', 'register_in_house_use' : 'false', 'apikey' : api_key}
      r2 = requests.post('https://api-na.hosted.exlibrisgroup....lmaws/v1/bibs/' + mmsid + '/holdings/' + holdingid + '/items/' + pid + '?', params=payload2)
      root2 = ET.fromstring(r2.text)
      return r2.status_code

    # DEFINE MAIN FUNCTION
    def main():

    ## OPEN CONFIGURATION FILE AND FOR EACH LINE IN THE CONFIGURATION FILE, STRIP OUT NEW LINE VALUES, SPLIT LINES AT EQUAL SIGN, AND ASSIGN VALUES FROM ARRAY TO VARIABLE
      configurations = ("job_keys_GenLibstoTechSer.cfg")
      configfile = open(configurations, "r")

      for line in configfile:
        line = line.rstrip("\n")
        value = line.split("=")
        if value[0] == "analytics_url":
          analytics_url = value[1]
        elif value[0] == "path":
          path = value[1]
        elif value[0] == "analytics_apikey":
          analytics_apikey = value[1]
        elif value[0] == "api_key":
          api_key = value[1]
        # elif value[0] == "sandbox_api_key":
          # api_key = value[1]

    ## CALL SECONDARY FUNCTION TO NEW VARIABLES
      response,url = get_analytics_report(analytics_url,path,analytics_apikey)
      barcode = get_barcodes(response)

    ## CREATE LIST OF BARCODES AND RETURN ITEM INFO
      barcode_list = ""
      for line in barcode:
        barcode_list += line + '\n'
        # print line
        r = get_item_info(line,api_key)
        if r.status_code == 200:
          # print(r.text)
          status = scan_item(r,api_key)
          # print(status)
        else:
          sys.stderr.write("Barcode " + line + " does not exist in Alma" + "\n")
          continue

    ## CLOSE FILES
      configfile.close()

    # EXIT SCRIPT
    if __name__ == "__main__":
      sys.exit(main())




    //Feedback