Page MenuHomec4science

check-scipers
No OneTemporary

File Metadata

Created
Fri, Jun 27, 18:39

check-scipers

#!/bin/bash
# Set SNAME, SDIR and SPATH
if [ -h $0 ]; then SNAME=$(readlink $0); else SNAME=$0; fi
SDIR=$(dirname $SNAME)
if [[ "$SDIR" =~ ^/ ]]; then SPATH=$SDIR; else SPATH=$(realpath $(pwd)/$SDIR); fi
#=================================
# Set DIR_LIB and source libs
DIR_LIB=$(realpath $SPATH/../../lib/bash)
source $DIR_LIB/tools.sh
source $DIR_LIB/io.sh
source $DIR_LIB/cmdline_parser.sh
source $DIR_LIB/debugging.sh
# Set DIR_CACHE
DIR_CACHE=$(realpath $SPATH/cache/)
DEFAULT_ACTIONS="help"
DEFAULT_PARAMS="name=1,sciper=2"
DEFAULT_ITEMS="$(find . -type f -name '*\.csv' | sort)"
function usage() {
echo "USAGE: $(basename $SNAME) --only scipers_1.csv[,scipers_2.csv] --params name=1,sciper=2,[refetch] [help|fetch|csv]";
}
function cb_help() {
usage
return 0
}
function cb_fetch() {
local csv
local id sciper email section name status found l
for csv in $(echo $@); do
check_file_exists $csv
if [ $? -ne 0 ]; then usage; exit 1; fi
perma "[FETCH] Processing file $(basename $csv)"
# Process file
id=0
local old_IFS=$IFS
local IFS=$'\n'
for l in $(cat $csv); do
((id++))
sciper=$(echo $l | cut -d ',' -f $(get_parameter 'sciper'))
fetch_data $sciper
found=$(get_field 'found')
if [ "$found" = "1" ]; then
verbose "$id: found"
else
verbose "$id: not found"
fi
done
IFS=$old_IFS
done
return 0
}
function cb_csv() {
local csv
local id sciper email section name status found l
for csv in $(echo $@); do
check_file_exists $csv
if [ $? -ne 0 ]; then usage; exit 1; fi
# Process file
id=0
echo 'ID,SCIPER,NAME,SECTION,EMAIL,STATUS'
local old_IFS=$IFS
local IFS=$'\n'
for l in $(cat $csv); do
((id++))
sciper=$(echo $l | cut -d ',' -f $(get_parameter 'sciper'))
fetch_data $sciper
found=$(get_field 'found')
if [ "$found" = "1" ]; then
status="OK";
email=$(get_field 'email')
section=$(get_field 'section')
name=$(get_field 'cname_usual_reversed')
else
status="NOT FOUND"
email='unknown@epfl.ch'
section='XXX'
name=$(echo $l | cut -d ',' -f $(get_parameter 'name'))
fi
echo "$id,$sciper,$name,$section,$email,$status"
continue
done
IFS=$old_IFS
done
return 0
}
function fetch_data() {
local sciper=$1
local fetch=0
local cache_file="$DIR_CACHE/${sciper}.txt"
is_set 'refetch'
if [ $? -eq 1 ]; then
fetch=1
else
check_file_exists $cache_file 2> /dev/null
if [ $? -eq 1 ]; then
fetch=1
else
if [ $(cat $cache_file | wc -l) -eq 0 ]; then
fetch=1
fi
fi
fi
if [ $fetch -eq 1 ]; then
search-epfl -s $sciper > $cache_file
DATA=$(search-epfl -s $sciper | tr '\n' '~');
else
DATA=$(cat $cache_file | tr '\n' '~')
fi
}
function get_field() {
local field=$1
echo $DATA | tr '~' '\n' | grep "^$field:" | cut -d ':' -f 2-
}
run $@
# RIP
exit 0

Event Timeline