-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcourses-addbarcodes
More file actions
executable file
·79 lines (57 loc) · 2.38 KB
/
Copy pathcourses-addbarcodes
File metadata and controls
executable file
·79 lines (57 loc) · 2.38 KB
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
#!/usr/bin/bash
auth 2>/dev/null || authn 2>/dev/null
#
# This program expect a list of course codes which it converts to course listing UUIDs
# and then adds the barcode in the second colum
#
# e.g. ENG 101 340523520352
#
tenant=$(cat tenant)
okapi_url=$(cat okapi.url)
okapi_token=$(cat okapi.token)
infile=course_barcode
logfile=course_addbarcodes.log
dos2unix "${infile}"
sort -u "${infile}" > tmp_courses_addbarcodes
mv tmp_courses_addbarcodes "${infile}"
if [[ ${infile} == "course_barcode" ]];then idType="barcode";fi
declare -A dict_courses
numrecs=$(wc -l ${infile} |cut -d " " -f1)
# get list of course departments and populate array using name as key with uuid as value
eval "$(curl -s -w '\n' -X GET -D -H "Accept: application/json" -H "X-Okapi-Tenant: ${tenant}" -H "x-okapi-token: ${okapi_token}" "${okapi_url}/coursereserves/courses?limit=100000" | jq -r '.courses[] | @sh "dict_courses[\(.courseNumber|tostring|ascii_downcase |gsub("[^0-9a-z]"; ""))]=\(.courseListingId|tostring)"')"
counter=0
found=0
notfound=0
while IFS=$'\t' read -r courseNumber barcode;do
courseNumber=$(tr -dc "[0-9a-z]" <<< ${courseNumber,,})
listingId=${dict_courses[${courseNumber}]}
if [[ $SECONDS -gt 500 ]]; then
auth 2>/dev/null || authn 2>/dev/null
okapi_token=$(cat okapi.token)
SECONDS=1
OFFSET=$(($OFFSET + 1))
TIMER=$(($OFFSET * 500 + $SECONDS))
fi
counter=$(($counter + 1))
courseListingId=${dict_courses["${courseNumber}"]}
id=$(uuidgen)
IFS='' read -r -d '' item << EndOfItem
{
"courseListingId": "${courseListingId}",
"copiedItem": {"barcode": "${barcode}"},
"id": "${id}"
}
EndOfItem
apicall=$(curl --http1.1 -s -w '\n' -X POST -H "Content-type: application/json" -H "X-Okapi-Tenant: ${tenant}" -H "x-okapi-token: ${okapi_token}" -d "${item}" "${okapi_url}/coursereserves/courselistings/${courseListingId}/reserves")
echo ${apicall} |tr "\n" " " |sed 's/$/\n/' >> ${logfile}
if [[ ${apicall} =~ courseListingId ]];then
found=$(($found + 1))
else
notfound=$(($notfound + 1))
fi
TIME=$TIMER;msg="$TIMER seconds"
if [[ $TIMER -gt 60 ]];then TIME=$(($TIMER/60));msg="$TIME minutes";fi
if [[ $TIMER -gt 3600 ]];then TIME=$(($TIMER/3600));msg="$TIME hours";fi
echo -en "Processing record $counter of $numrecs in $msg. Barcodes added: $found -- Failures: $notfound\r"
done < ${infile}
echo "Processed $numrecs barcodes. Results logged in ${logfile}. Barcodes added: $found -- Failures: $notfound"