Skip to content

Commit

Permalink
Ran shells through shellcheck:
Browse files Browse the repository at this point in the history
https://github.com/koalaman/shellcheck

and fixed some issues.

Updated status_notification for bash to delay check after status
changes.
  • Loading branch information
mikechambers committed Dec 15, 2020
1 parent 0c72c46 commit 8d4ee8f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
8 changes: 4 additions & 4 deletions examples/mail_report
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ EMAIL_ADDRESS=$EMAIL
# (here it pulls from environment but you can also hardcode)
MEMBER_ID=$MEMBER_ID

PLATFORM=xbox
PLATFORM=$PLATFORM

# character_id to retrieve stats for
# can retrieve character_ids from dclic
Expand All @@ -33,7 +33,7 @@ CHARACTER_ID=$CHARACTER_ID
# and trialsofosiris
declare -a MODES=("all_pvp" "quickplay" "iron_banner" "trials_of_osiris" "rumble" "pvp_competitive" )

REPORT_START=$(date -v-7d +"%A, %B %-d, %Y")
REPORT_START=$(date +"%A, %B %-d, %Y")
REPORT_END=$(date +"%A, %B %-d, %Y")

SUBJECT="Destiny Crucible Report for week of ${REPORT_START}"
Expand All @@ -45,7 +45,7 @@ OUTPUT+="\n"
echo "Gathering data for report"

# Iterate the string array using for loop
for mode in ${MODES[@]}; do
for mode in "${MODES[@]}"; do

echo "Getting data for $mode"

Expand Down Expand Up @@ -74,7 +74,7 @@ for mode in ${MODES[@]}; do
OUTPUT+="************************************************************************************************************\n"

#2>&1 this redirects stderr to stdout so we can capture
TMP_OUTPUT=$(dclics --member-id $MEMBER_ID --character-id $CHARACTER_ID --platform $PLATFORM --mode $mode --moment week 2>&1)
TMP_OUTPUT=$(dclics --member-id "$MEMBER_ID" --character-id "$CHARACTER_ID" --platform "$PLATFORM" --mode "$mode" --moment week 2>&1)

if [ $? -eq 1 ]
then
Expand Down
4 changes: 2 additions & 2 deletions examples/session
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ CHECK_INTERVAL=30
MODE="all_pvp"

#to moment to start pull stats from.
MOMENT="day"
MOMENT="now"
#tip to track trials for the weekend MODE=trials_of_osiris and MOMENT=weekend

#lets get the start time string
Expand All @@ -51,7 +51,7 @@ while :
do

#this redirects stderr put to /dev/null
ACTIVITY_HISTORY=$(dcliah --member-id ${MEMBER_ID} --character-id ${CHARACTER_ID} --platform ${PLATFORM} --manifest-path "${MANIFEST_PATH}" --mode ${MODE} --moment custom --custom-time "${SESSION_START}" 2> /dev/null)
ACTIVITY_HISTORY=$(dcliah --member-id "${MEMBER_ID}" --character-id "${CHARACTER_ID}" --platform "${PLATFORM}" --manifest-path "${MANIFEST_PATH}" --mode "${MODE}" --moment custom --custom-time "${SESSION_START}" 2> /dev/null)

#check and see if an error occured.
if [ $? -eq 1 ]
Expand Down
Empty file modified examples/session.ps1
100644 → 100755
Empty file.
26 changes: 16 additions & 10 deletions examples/stats_report
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# function for rounding floating point values
# https://unix.stackexchange.com/a/265202


#you can get your member id and platform from dclim
# pull member_id from environment variable but you can just hardcode here
MEMBER_ID=$MEMBER_ID
Expand All @@ -43,8 +42,15 @@ round() {
}

#capture output of command into a variable / we could also pipe this in
OUTPUT=$(dclics --member-id ${MEMBER_ID} --platform ${PLATFORM} --mode all_pvp \
--character-id ${CHARACTER_ID} --moment month --output-format tsv)
OUTPUT=$(dclics --member-id "${MEMBER_ID}" --platform "${PLATFORM}" --mode all_pvp \
--character-id "${CHARACTER_ID}" --moment month --output-format tsv)


if [ $? -eq 1 ]
then
echo -e "$OUTPUT"
exit 1
fi

# replace tabs with '='. We could probably skip this step, but I couldnt get it
# working below
Expand All @@ -55,7 +61,7 @@ OUTPUT=${OUTPUT//$'\t'/=}
declare -A data

#read through each line (split by \n)
while read line
while read -r line
do
#extract the key
key=${line%%=*}
Expand All @@ -74,30 +80,30 @@ done < <(echo "$OUTPUT")
#this will work with any of the dcli apps that output name / value pairs (tsv)

#round numbers. Might be a better way in bash
KD=$(round 3 ${data['kills_deaths_ratio']})
EFFICIENCY=$(round 3 ${data['efficiency']})
KD=$(round 3 "${data['kills_deaths_ratio']}")
EFFICIENCY=$(round 3 "${data['efficiency']}")

#figure out win percent
WIN_PERCENT=$(awk "BEGIN {print ${data['activities_won']}/${data['activities_entered']}}")
WIN_PERCENT=$(awk "BEGIN {print $WIN_PERCENT * 100}")
WIN_PERCENT=$(round 4 ${WIN_PERCENT})
WIN_PERCENT=$(round 4 "${WIN_PERCENT}")

DIALOG="Lets look at your crucible stats for ${data['mode']} ${data['moment_human']}. \
Over this period you played for a total of ${data['human_time_played']}. \
You played ${data['activities_entered']} matches with ${data['activities_won']} \
wins and ${data['activities_lost']} losses for a ${WIN_PERCENT} percent win rate. \
Your overall K D ratio is ${KD} with an efficiency of ${EFFICIENCY}."

echo $DIALOG
echo "$DIALOG"

#check that say command exists
if command -v say &> /dev/null
then
#say is a command on OS X to read the input (-v to change voices)
say $DIALOG
say "$DIALOG"
elif command -v espeak &> /dev/null
then
echo $DIALOG | espeak -s 125
echo "$DIALOG" | espeak -s 125
fi


Expand Down
25 changes: 14 additions & 11 deletions examples/status_notification
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,41 @@
# More info at:
# https://github.com/mikechambers/dcli/

#https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -u

#pull variables from environment variables. Otherwise, you can
#just manually set them below

#you can get your member id and platform from dclim
# pull member_id from environment variable but you can just hardcode here
MEMBER_ID=$MEMBER_ID
PLATFORM=xbox
PLATFORM=$PLATFORM

#run dclim --manifest-path ~/tmp/
#to sync manifest before running this script
$MANIFEST_PATH="~/tmp/manifest.sqlite3"
##can get from dclim
MANIFEST_PATH=$MANIFEST_PATH

CHECK_INTERVAL_SECONDS=15
DEFAULT_CHECK_INTERVAL_SECONDS=15

echo "CTRL-C to end program"

OLD_ACTIVITY=""
while :
do
CHECK_INTERVAL_SECONDS=$DEFAULT_CHECK_INTERVAL_SECONDS
echo "Checking Status"
ACTIVITY=$(dclia --manifest-path $MANIFEST_PATH --member-id $MEMBER_ID --platform $PLATFORM)
ACTIVITY=$(dclia --manifest-path "$MANIFEST_PATH" --member-id "$MEMBER_ID" --platform "$PLATFORM")
#note should do some error checking here in case command fails (can capture exit code)

#we could have the command above output name / value pairs via --output-format tsv
#and then filter based on type of activity (i.e. crucible, strikes, etc...)
if [ "$OLD_ACTIVITY" != "$ACTIVITY" ]; then
echo "Status has changed"
echo $ACTIVITY
echo "$ACTIVITY"
#note, you could get this running on linux using notify-send command
osascript -e "display notification \"${ACTIVITY}\" with title \"Destiny 2 Activity Changed\""
OLD_ACTIVITY=$ACTIVITY


#TODO: when we find a new activity, lets delay the next check for a bit, in case the server api returns old data
# for a short period
CHECK_INTERVAL_SECONDS=60
fi

sleep $CHECK_INTERVAL_SECONDS
Expand Down
Empty file modified examples/status_notification.ps1
100644 → 100755
Empty file.

0 comments on commit 8d4ee8f

Please sign in to comment.