Last active
December 18, 2015 11:08
-
-
Save s-zeid/5773027 to your computer and use it in GitHub Desktop.
count-uploads - The script I use to count my camera uploads saved in Google Drive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (c) 2013 Scott Zeid. Released under the X11 License. | |
# | |
# This is the script I use to count my camera uploads saved in Google Drive. | |
# This script is located at the root of my Camera Uploads folder in Google Drive, | |
# and the directory structure relative to the location of this script is | |
# YYYY/MM. | |
# | |
# With no arguments, the script will count all years (and if -v/--verbose is given, | |
# all months as well). With one argument, the script will count that year, and with | |
# two arguments, the script will count the given year (argument 1) and month (argument | |
# 2). The special month value "??" (without quotes) will count all photos/videos for | |
# a given year which have not been sorted into a month. | |
UPLOADS_LABEL="Photos / Videos" | |
function main() { | |
FLAG="" | |
if [ "${1:0:1}" = "-" ]; then | |
FLAG=$1 | |
shift | |
fi | |
DATE="$1" | |
if [ -n "$2" ]; then | |
DATE="$DATE $2" | |
fi | |
DATE=$(sed -r -e 'y@/ @--@' -e 's/^([0-9]{4}-[0-9]{2}).*$/\1/g' <<< "$DATE") | |
YEAR=$(cut -d'-' -f1 <<< "$DATE") | |
MONTH=$(cut -d'-' -f2 -s <<< "$DATE") | |
if [ -n "$MONTH" ]; then | |
count_month -v "$YEAR" "$MONTH" | |
return $? | |
elif [ -n "$YEAR" ]; then | |
count_year -v "$YEAR" | |
return $? | |
else | |
count_all "$FLAG" | |
return $? | |
fi | |
} | |
function print_row() { | |
if [ "$1" = "-l" ]; then | |
ALIGN=left | |
shift | |
elif [ "$1" = "-r" ]; then | |
ALIGN=right | |
shift | |
else | |
ALIGN=right | |
fi | |
LABEL=$1 | |
TOTAL=$2 | |
if [ ${#LABEL} -lt 7 ]; then | |
if [ "$ALIGN" = "left" ]; then | |
LABEL=$(head -c 7 <<< "$LABEL ") | |
elif [ "$ALIGN" = "right" ]; then | |
LABEL=$(tail -c 8 <<< " $LABEL") | |
fi | |
fi | |
echo "$LABEL $TOTAL" | |
} | |
function print_count() { | |
YEAR=$1 | |
MONTH=$2 | |
TOTAL=$3 | |
print_row "$YEAR-$MONTH" "$TOTAL" | |
} | |
function count_month() { | |
VERBOSE=0 | |
if [ "$1" = "-v" -o "$1" = "--verbose" ]; then | |
VERBOSE=1 | |
shift | |
if [ ! -d "$(dirname "$0")/$YEAR/$MONTH" -a "$MONTH" != "??" ]; then | |
echo "error: $(dirname "$0")/$YEAR/$MONTH is not a directory" | |
return 1 | |
else | |
print_row -l "Month" "$UPLOADS_LABEL" | |
fi | |
fi | |
YEAR=$1 | |
MONTH=$2 | |
total=0 | |
if [ -n "$MONTH" -a "$MONTH" != "??" ]; then | |
if [ -d "$(dirname "$0")/$YEAR/$MONTH" ]; then | |
total=$(ls "$(dirname "$0")/$YEAR/$MONTH" | wc -l) | |
elif [ $VERBOSE -ne 0 ]; then | |
echo "error: $(dirname "$0")/$YEAR/$MONTH is not a directory" | |
return 1 | |
fi | |
else | |
for f in "$(dirname "$0")/$YEAR"/*; do | |
if [ -e "$f" -a ! -d "$f" ]; then | |
total=$(($total + 1)) | |
fi | |
done | |
fi | |
if [ $VERBOSE -ne 0 ]; then | |
print_row "$YEAR-$MONTH" $total | |
else | |
echo $total | |
fi | |
} | |
function count_year() { | |
VERBOSE=0 | |
if [ "$1" = "-v" -o "$1" = "--verbose" ]; then | |
VERBOSE=1 | |
shift | |
if [ ! -d "$(dirname "$0")/$YEAR" ]; then | |
echo "error: $(dirname "$0")/$YEAR is not a directory" | |
return 1 | |
else | |
print_row -l "Month" "$UPLOADS_LABEL" | |
fi | |
fi | |
YEAR=$1 | |
total=0 | |
for m in 01 02 03 04 05 06 07 08 09 10 11 12 '??'; do | |
count=$(count_month "$YEAR" "$m") | |
total=$(($total + $count)) | |
if [ $VERBOSE -ne 0 ]; then | |
[ "$m" != "??" -o $count -gt 0 ] && print_row "$YEAR-$m" "$count" | |
fi | |
done | |
if [ $VERBOSE -ne 0 ]; then | |
print_row "Total" "$total" | |
else | |
echo $total | |
fi | |
} | |
function count_all() { | |
VERBOSE=0 | |
if [ "$1" = "-v" -o "$1" = "--verbose" ]; then | |
VERBOSE=1 | |
shift | |
print_row -l "Month" "$UPLOADS_LABEL" | |
else | |
print_row -l "Year" "$UPLOADS_LABEL" | |
fi | |
total=0 | |
for y in "$(dirname "$0")"/*; do | |
if [ -d "$y" ]; then | |
y=$(basename "$y") | |
if [ -n "$(egrep -o '^[0-9]{4}$' <<< "$y")" ]; then | |
count=$(count_year "$y") | |
total=$(($total + $count)) | |
if [ $VERBOSE -ne 0 ]; then | |
count_year -v "$y" | head -n -1 | tail -n +2 | |
print_row "$y" "$count" | |
echo | |
else | |
print_row -l "$y" "$count" | |
fi | |
fi | |
fi | |
done | |
if [ $VERBOSE -ne 0 ]; then | |
print_row "Total" "$total" | |
else | |
print_row -l "Total" "$total" | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment