#! /usr/bin/perl

$baseURL = "https://okapi-drew.folio.ebsco.com";
$jsonHeader = "Content-type: application/json";
$xOkapiToken = `cat okapi-token.txt`;

use JSON;

@allBannerInfo = `cat allBannerInfo.txt`;

$groupsJSON = `curl -s -X GET -G -H '$jsonHeader' -H '$xOkapiToken' -d 'limit=100' $baseURL/groups?query=group="*"`;
$groupsHash = decode_json $groupsJSON;
for ( @{$groupsHash->{usergroups}} ) {
    $id = $_->{'id'};
    $group = $_->{'group'};
    $userGroup{$id} = $group;
}

foreach $row (@allBannerInfo) {
    $countRows++;
    @fields = split(/\|/,$row);
    if ($fields[20] eq "Y") {
        $countUsers++;
        $barcode = $fields[0];
        $userJSON = `curl -s -w '\n' -X GET -G -H '$jsonHeader' -H '$xOkapiToken' $baseURL/users?query=barcode=="$barcode"`;
        if ($userJSON =~ /"totalRecords": 0/) {
            $do = "nothing";
        } else {
            $countUpdates++;
            $do = "put";
            $userHash = decode_json $userJSON;
            for ( @{$userHash->{users}} ) {
                $groupId = $_->{'patronGroup'};
                $patronGroup = $userGroup{$groupId};
                if ($patronGroup eq "Faculty" || $patronGroup eq "LibFacStaff" || $patronGroup eq "Staff") {
                    $userId = $_->{'id'};
                    $userJSON =~ s/{\n  "users": \[\n//;
                    ($data,$moreDreck) = split(/\],\n  "totalRecords"/,$userJSON);
                    $data =~ s/expirationDate":"\d\d\d\d-\d\d-\d\d/expirationDate":"2026-03-15/;
                    $data =~ s/'/'\\''/g;
                    $put = `curl -s -w '\n' -X PUT -H '$jsonHeader' -H '$xOkapiToken' -d '$data' $baseURL/users/$userId`;
                    print "$countRows $countUsers $countUpdates\n";
                }
            }
        }
    }
}

  • No labels
Write a comment…