Script 'schedule': simplify code a bit.

This commit is contained in:
vonavi 2014-09-03 00:00:56 +04:00
parent 41b13ed5eb
commit 2a6f4fdbe9

View File

@ -53,24 +53,26 @@ function setup_repo {
# If no tournament given, set it to the last one # If no tournament given, set it to the last one
if [[ -z $TOURNAMENT ]]; then if [[ -z $TOURNAMENT ]]; then
if [[ $REPO_DIR =~ ^https://github\.com/(.*) ]]; then if [[ $REPO_DIR =~ ^https://github\.com/(.*) ]]; then
REPO_NAME=${BASH_REMATCH[1]} REPO_API="https://api.github.com/repos/${BASH_REMATCH[1]}/contents"
local year=$(curl $CURL_OPTS \ local year=$(curl $CURL_OPTS $REPO_API \
https://api.github.com/repos/${REPO_NAME}/contents \
| sed -En "/\"path\":/h;/\"type\": \"dir\"/{g;p;}" \ | sed -En "/\"path\":/h;/\"type\": \"dir\"/{g;p;}" \
| sed -En "s|.*\"([0-9]{4})\".*|\1|p" | tail -1) | sed -En "s|.*\"([0-9]{4})\".*|\1|p" | tail -1)
TOURNAMENT=$(curl $CURL_OPTS \ TOURNAMENT=$(curl $CURL_OPTS ${REPO_API}/${year} \
https://api.github.com/repos/${REPO_NAME}/contents/${year} \
| sed -En "/\"path\":/h;/\"type\": \"dir\"/{g;p;}" \ | sed -En "/\"path\":/h;/\"type\": \"dir\"/{g;p;}" \
| sed -En "s|.*\"(${year}/[0-9]-.*)\".*|\1|p" | tail -1) | sed -En "s|.*\"(${year}/[0-9]-.*)\".*|\1|p" | tail -1)
else
# Convert REPO_DIR to an absolute path
[[ $REPO_DIR =~ ^/ ]] || REPO_DIR=$(cd ${REPO_DIR}; pwd)
REPO_URL="${REPO_DIR}/raw/master"
else
local year_dir=$(ls -1 -d ${REPO_DIR}/[0-9][0-9][0-9][0-9]/ | tail -1) local year_dir=$(ls -1 -d ${REPO_DIR}/[0-9][0-9][0-9][0-9]/ | tail -1)
TOURNAMENT=$(ls -1 -d ${year_dir}[0-9]-*/ | tail -1 \ TOURNAMENT=$(ls -1 -d ${year_dir}[0-9]-*/ | tail -1 \
| sed -E "s|${REPO_DIR}/(.*)|\1|") | sed -E "s|${REPO_DIR}/(.*)|\1|")
# Remove the trailing slash # Remove the trailing slash
TOURNAMENT=${TOURNAMENT%/} TOURNAMENT=${TOURNAMENT%/}
# Convert REPO_DIR to an absolute path
[[ $REPO_DIR =~ ^/ ]] || REPO_DIR=$(cd $REPO_DIR; pwd)
REPO_URL="file://${REPO_DIR}"
fi fi
fi fi
} }
@ -81,8 +83,7 @@ function show_tour_sequence {
# If no tour sequence given, set it to all tours # If no tour sequence given, set it to all tours
if [[ -z $tour_seq ]]; then if [[ -z $tour_seq ]]; then
if [[ $REPO_DIR =~ ^https://github\.com/ ]]; then if [[ $REPO_DIR =~ ^https://github\.com/ ]]; then
tour_seq=$(curl $CURL_OPTS \ tour_seq=$(curl $CURL_OPTS ${REPO_API}/${TOURNAMENT}/tours \
https://api.github.com/repos/${REPO_NAME}/contents/${TOURNAMENT}/tours \
| sed -En "/\"path\":/h;/\"type\": \"dir\"/{g;p;}" \ | sed -En "/\"path\":/h;/\"type\": \"dir\"/{g;p;}" \
| sed -En "s|.*\"${TOURNAMENT}/tours/([0-9]{2})\".*|\1|p") | sed -En "s|.*\"${TOURNAMENT}/tours/([0-9]{2})\".*|\1|p")
else else
@ -96,24 +97,17 @@ function show_tour_sequence {
tour=$(printf "%02g" $tour) tour=$(printf "%02g" $tour)
# *NOTE* The incorporation of a newline at the end of # *NOTE* The incorporation of a newline at the end of
# 'tour_info' is important and allows one to read the last # 'tour_info' (--write-out '\n') is important and allows one
# line with no trailing '\n' # to read the last line with no trailing '\n'
if [[ $REPO_DIR =~ ^https://github\.com/ ]]; then local tour_info=$(curl $CURL_OPTS --write-out '\n' \
local tour_info=$(curl $CURL_OPTS --write-out '\n' \ ${REPO_URL}/${TOURNAMENT}/tours/${tour}/tour_info)
${REPO_DIR}/raw/master/${TOURNAMENT}/tours/${tour}/tour_info) [[ -n $tour_info ]] && output_tour_info
[[ -n $tour_info ]] && output_tour_info
else
local info_file=${REPO_DIR}/${TOURNAMENT}/tours/${tour}/tour_info
if [[ -f $info_file ]]; then
local tour_info=$(cat $info_file; echo)
output_tour_info
fi
fi
done done
} }
function output_tour_info { function output_tour_info {
local date_re="([0-9?]{2})\.([0-9?]{2})\.([0-9?]{4})" local date_re="([0-9?]{2})\.([0-9?]{2})\.([0-9?]{4})"
local res_re="(1-0|1/2|0-1)"
local white= black= result= game_date= local white= black= result= game_date=
# Lines with game info # Lines with game info
local output_lines= local output_lines=
@ -147,15 +141,13 @@ function output_tour_info {
fi fi
done <<< "$tour_info" done <<< "$tour_info"
[[ -n $output_lines ]] && eval "info_output_$FORMAT \"${output_lines}\"" [[ -n $output_lines ]] && eval "info_output_$FORMAT"
} }
function keep_game_info { function keep_game_info {
local res_re="(1|1/2|0)-(1|1/2|0)"
local keep=1 local keep=1
if [[ $line =~ ^$date_re\ +([^\ ]+)\ +-\ +([^\ ]+)(\ +$res_re)? ]]; then if [[ $line =~ ^$date_re\ +([^\ ]+)\ +-\ +([^\ ]+)(\ +$res_re)? ]]; then
white=${BASH_REMATCH[4]} black=${BASH_REMATCH[5]} result=${BASH_REMATCH[6]// /} white=${BASH_REMATCH[4]} black=${BASH_REMATCH[5]} result=${BASH_REMATCH[7]}
game_date=${BASH_REMATCH[3]}-${BASH_REMATCH[2]}-${BASH_REMATCH[1]} game_date=${BASH_REMATCH[3]}-${BASH_REMATCH[2]}-${BASH_REMATCH[1]}
# If the player was passed as an argument, check if it is his game or not # If the player was passed as an argument, check if it is his game or not
@ -168,22 +160,15 @@ function keep_game_info {
fi fi
fi fi
fi fi
return $keep return $keep
} }
function append_game_url { function append_game_url {
local tour=$(printf "%02g" "$tour_num") local tour=$(printf "%02g" "$tour_num")
local game_dir=${game_date}-${white}-vs-${black} local game_dir=${game_date}-${white}-vs-${black}
local game_url=$(curl $CURL_OPTS \
if [[ $REPO_DIR =~ ^https://github\.com/ ]]; then ${REPO_URL}/${TOURNAMENT}/tours/${tour}/${game_dir}/1.pgn \
local pgn_text=$(curl $CURL_OPTS \ | grep -Eo "\[Site \"[a-z]+:[^\"]+\"\]" \
${REPO_DIR}/raw/master/${TOURNAMENT}/tours/${tour}/${game_dir}/1.pgn)
else
local pgn_text=$(cat ${REPO_DIR}/${TOURNAMENT}/tours/${tour}/${game_dir}/1.pgn)
fi
local game_url=$(echo "$pgn_text" | grep -Eo "\[Site \"[a-z]+:[^\"]+\"\]" \
| sed -E "s/\[Site \"([a-z]+:[^\"]+)\"\]/\1/") | sed -E "s/\[Site \"([a-z]+:[^\"]+)\"\]/\1/")
output_lines+=" $game_url" output_lines+=" $game_url"
} }
@ -192,20 +177,20 @@ function info_output_term {
echo -n "$(tput setaf 2)${tour_num} тур " echo -n "$(tput setaf 2)${tour_num} тур "
echo "$(tput setaf 6)(${date_beg} - ${date_end})$(tput sgr0)" echo "$(tput setaf 6)(${date_beg} - ${date_end})$(tput sgr0)"
echo -en "$1" | column -t | while IFS= read line; do echo -en "$output_lines" | while IFS= read line; do
# Highlight player's name # Highlight player's name
if [[ -n $PLAYER ]]; then if [[ -n $PLAYER ]]; then
players=$(grep -o "[^ ]*${PLAYER}[^ ]*" <<< "$line") players=$(grep -o "[^ ]*${PLAYER}[^ ]*" <<< "$line")
for name in $players; do for name in $players; do
# PLAYER is a part of 'game_url' or 'result' # PLAYER is a part of 'game_url' or 'result'
[[ $name =~ ^http:// || $name =~ ^(1-0|1/2-1/2|0-1|)$ ]] && continue [[ $name =~ ^http:// || $name =~ ^${res_re}$ ]] && continue
line=$(sed -E "s/(${name})/$(tput setaf 1)\1$(tput sgr0)/g" <<< "$line") line=$(sed -E "s/(${name})/$(tput setaf 1)\1$(tput sgr0)/g" <<< "$line")
done done
fi fi
# Highlight result # Highlight result
line=$(sed -E "s/ (1-0|1\/2-1\/2|0-1)( |$)/ $(tput setaf 6)\1$(tput sgr0)\2/g" <<< "$line") line=$(sed -E "s~ ${res_re}( |$)~ $(tput setaf 6)\1$(tput sgr0)\2~g" <<< "$line")
echo "$line" echo "$line"
done done
@ -217,7 +202,7 @@ function info_output_lor {
echo "[b]${tour_num} тур (${date_beg} - ${date_end})[/b]" echo "[b]${tour_num} тур (${date_beg} - ${date_end})[/b]"
echo "[list]" echo "[list]"
echo -en "$1" | while read white hyphen black result url; do echo -en "$output_lines" | while read white hyphen black result url; do
[[ -n $url ]] && result="[url=${url}]${result}[/url]" [[ -n $url ]] && result="[url=${url}]${result}[/url]"
echo " [*] [user]${white}[/user] - [user]${black}[/user] ${result}" echo " [*] [user]${white}[/user] - [user]${black}[/user] ${result}"
@ -236,7 +221,8 @@ function info_output_html {
echo " </caption>" echo " </caption>"
echo " <tbody>" echo " <tbody>"
echo -en "$1" | while read white hyphen black result url; do echo -en "$output_lines" | while read white hyphen black result url; do
[[ $result == "1/2" ]] && result="½-½"
[[ -n $url ]] && result="<a href=\"${url}\">${result}</a>" [[ -n $url ]] && result="<a href=\"${url}\">${result}</a>"
echo " <tr>" echo " <tr>"