-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalma_flip_ident.pl
executable file
·171 lines (147 loc) · 5.09 KB
/
alma_flip_ident.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/perl
#
# alma_flip_ident.pl - iterate through file of Alma primary IDs and brute force flip
# Barcode ident to internal
#
# Format of input file:
# user_primary_id1
# user_primary_id2
# user_primary_idN...
#
# last updated: 6/26/16, np, OHSU Library
#----------------------------------------------------------------------------------------#
# DECLARATIONS
#----------------------------------------------------------------------------------------#
use strict;
use warnings;
use utf8;
use Cwd qw();
use File::Slurp;
use IO::Socket::SSL qw( SSL_VERIFY_NONE );
use LWP::UserAgent;
use Time::Piece;
use XML::Twig;
#----------------------------------------------------------------------------------------#
# GLOBALS
#----------------------------------------------------------------------------------------#
# Alma API login/url:
my $API_USER = 'APIUSER';
my $API_PASS = 'APIPASS';
my $API_BURL = 'APIURL';
#----------------------------------------------------------------------------------------#
# MAIN
#----------------------------------------------------------------------------------------#
# exit and warn user if no data file is given:
if ($#ARGV == -1) {
print "Please give me a file of Alma user primary_ids to process!\n";
print "Script usage: <" . $0 . "> <data_file.txt> <pre_change.xml> <post_change.xml>\n";
exit;
}
# get our current working dir:
my $path = Cwd::cwd() . '/';
# timestamp:
my $ts = localtime->strftime('%Y%m%d_%H%M%S');
# setup our file handler locations:
my $data_file = $ARGV[0];
my ($pre_change, $post_change, $fh_pre, $fh_post);
if (exists $ARGV[1]) {
$pre_change = $ARGV[1];
print "Sending pre_change data to file: " . $pre_change . "\n";
}
else {
$pre_change = $path . $ts . '_pre_change.xml';
print "No pre_change file path given, sending pre_change to file: " . $pre_change . "\n";
}
if (exists $ARGV[2]) {
$post_change = $ARGV[2];
print "Sending post_change data to file: " . $post_change . "\n";
}
else {
$post_change = $path . $ts . '_post_change.xml';
print "No post_change file path given, sending post_change to file: " . $post_change . "\n";
}
#open and prep FHs:
open $fh_pre, '>', $pre_change or do {
warn "$0: open $pre_change: $!";
return;
};
open $fh_post, '>', $post_change or do {
warn "$0: open $post_change: $!";
return;
};
print $fh_pre '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n";
print $fh_post '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n";
# create request obj and set content_type to XML:
my $ua = LWP::UserAgent->new(keep_alive => 0,
timeout => 45,
sl_opts => {SSL_verify_mode =>
IO::Socket::SSL::SSL_VERIFY_NONE,
verify_hostname => 0});
# iterate through user list:
my @lines = read_file($data_file);
print "Begin processing file: " . $data_file . "\n";
foreach my $line (@lines){
# our primary ID:
my $api_pri_id = $line;
chomp($api_pri_id);
# make sure to get full record view (for barcodes and primary_id):
my $req = new HTTP::Request(GET => ($API_BURL . '/' . $api_pri_id . '?view=full'));
$req->content_type('application/xml');
$req->authorization_basic($API_USER, $API_PASS);
my $response = $ua->request($req);
# user is a match, process it:
if ($response->is_success) {
# format and flush pre modified XML:
my $twig_pre = XML::Twig->new(
pretty_print => 'indented_a',
keep_atts_order => 1,
no_prolog => 1
);
$twig_pre->parse( $response->content );
$twig_pre->flush( $fh_pre );
# create post modified XML parser w/2 handlers:
# 1. - Set UIs with id_type BARCODE to an 'Internal' segment_type
# 2. - 'NULL' out any blank phone number fields that may have been loaded via the initial EXL loads
my $twig_post = XML::Twig->new(
pretty_print => 'indented_a',
keep_atts_order => 1,
no_prolog => 1,
twig_handlers => {
q(user/user_identifiers/user_identifier[string(id_type)="BARCODE"]) => sub { $_->set_att(segment_type => 'Internal') },
q(user/contact_info/phones/phone/phone_number[string()=""]) => sub { $_->set_text('NULL') },
}
);
$twig_post->parse( $response->content );
my $mod_xml = $twig_post->sprint;
# attempt to PUT new XML:
my $put = new HTTP::Request(GET =>($API_BURL . '/' . $api_pri_id . '?view=full'));
use bytes;
my $length = length($mod_xml);
#no bytes;
use utf8;
#debug
#print $mod_xml . "\n";
$put->method('PUT');
$put->content($mod_xml);
$put->content_type('application/xml;charset=UTF-8');
$put->content_length($length);
$put->authorization_basic($API_USER, $API_PASS);
my $put_res = $ua->request($put);
if ($put_res->is_success) {
# update client:
print "Barcode set to INTERNAL for user " . $api_pri_id . "\n";
}
else {
print "Error setting INTERNAL barcode for user " . $api_pri_id . ": " . $put_res->status_line() . "\n";
}
# dump post XML data:
$twig_post->flush( $fh_post );
}
else {
print "No user found for primary_id = " . $api_pri_id . "\n";
}
}
# cleanup:
close $fh_pre or warn "$0: close $fh_pre: $!";
close $fh_post or warn "$0: close $fh_post: $!";
print "Finished!\n";