Skip to content

Instantly share code, notes, and snippets.

@hkurokawa
Last active March 10, 2016 06:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hkurokawa/1b8c2f4321397b9b26cd to your computer and use it in GitHub Desktop.
Save hkurokawa/1b8c2f4321397b9b26cd to your computer and use it in GitHub Desktop.
Post Google Play App. Reviews to HipChat
verifyCmd() {
for cmd in ${@}; do
if [ -z "$(which $cmd)" ]; then
echo "$cmd not found in PATH: $PATH"
return 1
fi
done
}
: ${GOOGLE_BUCKET_ID?"Need to set GOOGLE_BUCKET_ID"}
: ${APP_PACKAGE?"Need to set APP_PACKAGE"}
: ${HIPCHAT_TOKEN?"Need to set HIPCHAT_TOKEN"}
: ${ROOM_ID?"Need to set ROOM_ID"}
verifyCmd "gsutil" "awk" "curl" "jq" "nkf" "mktemp" "comm" || exit 1
backupdir=_back
mkdir ${backupdir}
if [ $? -ne 0 ]; then
echo "[$(date)] Failed to create a backup directory: ${backupdir}. It might indicate the previous execution failed."
exit 1
fi
if [ ! -z $(ls -1 *.csv | head -n 1) ]; then
cp *.csv ${backupdir} || exit 1
fi
echo "...Retrieving reviews."
gsutil -m cp -r "gs://${GOOGLE_BUCKET_ID}/reviews/reviews_${APP_PACKAGE}_*" .
if [ $? -ne 0 ]; then
echo "[$(date)] Failed to download the latest reviews."
exit 1
fi
echo "...Looking for and posting new reviews."
i=0
printNewReviews.sh ${backupdir} . | awk -F"," '{print $7 " Star " $9 " " $10 " " $11 " " $15}' | while read msg; do
encoded=$(echo "\"${msg}\"" | jq -a '.')
curl -s -H 'Content-type: application/json' -d "{\"message\": ${encoded}}" "https://api.hipchat.com/v2/room/${ROOM_ID}/notification?auth_token=${HIPCHAT_TOKEN}"
if [ $? -ne 0 ]; then
echo "[$(date)] Failed to post to the HipChat room. msg: ${msg}"
exit 1
else
printf "Num Posted: %d\r" $i
fi
i=$(expr $i + 1)
done
if [ $? -ne 0 ]; then
echo "[$(date)] Failed to diff old and new reviews."
else
echo "...Done!"
fi
rm -rf ${backupdir}
exit $?
#! /bin/sh
verifyFile() {
if [ ! -f "$1" ]; then
echo "The specified file does not exist: $1"
return 1
fi
}
verifyDir() {
if [ ! -d "$1" ]; then
echo "The specified directory does not exist: $1"
return 1
fi
}
diffFiles() {
file1=$1
file2=$2
verifyFile "${file1}" || return 1
verifyFile "${file2}" || return 1
if [ -z "$(which nkf)" ]; then
echo "nkf not found in PATH: $PATH"
exit 1
fi
# If the files do not differ from each other, just exit successfully
if [ -z "$(diff ${file1} ${file2})" ]; then
return 0
fi
tmp1=$(mktemp -t _csv_) || return 1
tmp2=$(mktemp -t _csv_) || return 1
nkf -w ${file1} | sort > ${tmp1} || return 1
nkf -w ${file2} | sort > ${tmp2} || return 1
comm -13 ${tmp1} ${tmp2} || return 1
return 0
}
if [ $# -ne 2 ]; then
echo "Usage: $0 old_review_csv_dir new_review_csv_dir"
exit 1
fi
olddir=$1
newdir=$2
verifyDir "${olddir}"
verifyDir "${newdir}"
base=$(basename $0)
TMPDIR=$(mktemp -d -t ${base}) || return 1
trap "rm -rf ${TMPDIR}" EXIT
for file in ${newdir}/*.csv; do
base=$(basename ${file})
cmpt="${olddir}/$(basename ${file})"
if [ -f "${cmpt}" ]; then
diffFiles ${cmpt} ${file}
else
# Display all the lines except for the header
tmp=$(mktemp -t _csv_) || exit 1
nkf -w ${file} > ${tmp} || exit 1
tail -n +2 ${tmp}
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment