clean up, make more vars local

This commit is contained in:
Connor Olding 2013-07-09 17:54:29 -07:00
parent d833f2e810
commit 1f28bc635d
2 changed files with 26 additions and 24 deletions

48
meow.sh
View File

@ -1,12 +1,17 @@
#!/usr/bin/env bash #!/usr/bin/env bash
SEP=$'\1' SEP=$'\1'
# all timestamps are given in seconds since the epoch
declare -A groupinsane # unsanitized group names
declare -A groupregex
declare -A grouptime # last seen release
die() { die() {
echo -E "$@" 1>&2 echo -E "$@" >&2
exit 1 exit 1
} }
nullcheck() { nullcheck() { # {group name}
[[ -n "$1" ]] || die "Null group name"; [[ -n "$1" ]] || die "Null group name";
} }
@ -14,32 +19,28 @@ sanitize() {
sed -e 's/[^0-9a-zA-Z_]/_/g' sed -e 's/[^0-9a-zA-Z_]/_/g'
} }
splittags() { splittags() { # {tag}
awk -v tag="$1" -f "$SRCDIR/splittags.awk" awk -v tag="$1" -f "$SRCDIR/splittags.awk"
} }
scrape() { scrape() { # {group name} {timestamp}
TZ=UTC0 awk -v g="$1" -v timestamp="${2:-0}" -v sep="$SEP" -f "$SRCDIR/scrape.awk" TZ=UTC0 awk -v g="$1" -v ts="${2:-0}" -v sep="$SEP" -f "$SRCDIR/scrape.awk"
} }
declare -A groupinsane # unsanitized group names
declare -A groupshows # regexes
watch() { # {group name} [regex...] watch() { # {group name} [regex...]
nullcheck "$1" nullcheck "$1"
local gs="$(sanitize<<<"$1")" local gs="$(sanitize<<<"$1")" regex=
groupinsane[$gs]="$1" groupinsane[$gs]="$1"
shift shift
while (( "$#" )); do for regex; do
groupshows[$gs]+="|($1)" groupregex[$gs]+="|($regex)"
shift
done done
} }
declare -A grouptimes # last times timestamp touchgroup() { # {group name} {timestamp}
touchgroup() { # {group name} {unix time}
nullcheck "$1" nullcheck "$1"
local gs="$(sanitize<<<"$1")" local gs="$(sanitize<<<"$1")"
grouptimes[$gs]="$2" grouptime[$gs]="$2"
} }
groupreleases() { # groupname [timestamp] groupreleases() { # groupname [timestamp]
@ -52,14 +53,15 @@ groupreleases() { # groupname [timestamp]
groupfilter() { # groupname regex [timestamp] groupfilter() { # groupname regex [timestamp]
groupreleases "$1" "${3:-}" | while IFS=$SEP read -r title etc; do groupreleases "$1" "${3:-}" | while IFS=$SEP read -r title etc; do
grep -P "$2" <<< "$title" 1>/dev/null && echo -E "$title$SEP$etc" grep -P "$2" <<< "$title" >/dev/null && echo -E "$title$SEP$etc"
done done
[ ${PIPESTATUS[0]} = 0 ] || exit 1 [ ${PIPESTATUS[0]} = 0 ] || exit 1
} }
cleanup() { cleanup() {
for gs in "${!grouptimes[@]}"; do local gs= v=
local v="${grouptimes[$gs]}" for gs in "${!grouptime[@]}"; do
v="${grouptime[$gs]}"
echo -E "touchgroup $gs $v" >> times.sh echo -E "touchgroup $gs $v" >> times.sh
[ -e "$gs.xml" ] && rm "$gs.xml" [ -e "$gs.xml" ] && rm "$gs.xml"
done done
@ -67,15 +69,15 @@ cleanup() {
} }
rungroup() { rungroup() {
local insane regex timestamp res recent local insane= regex= timestamp= res= _= recent=
insane="${groupinsane[$1]}" insane="${groupinsane[$1]}"
regex="${groupshows[$1]:1}" regex="${groupregex[$1]:1}"
timestamp="${grouptimes[$1]}" timestamp="${grouptime[$1]}"
res="$(groupfilter "$insane" "$regex" "$timestamp")" res="$(groupfilter "$insane" "$regex" "$timestamp")"
[ $? = 0 ] || return $? [ $? = 0 ] || return $?
IFS=$SEP read -r _ _ recent <<< "$res" IFS=$SEP read -r _ _ recent <<< "$res"
[ -n "$recent" ] && { [ -n "$recent" ] && {
grouptimes[$1]="$recent" grouptime[$1]="$recent"
echo -E "$res" echo -E "$res"
} }
return 0 return 0
@ -83,7 +85,7 @@ rungroup() {
runall() { runall() {
trap cleanup INT trap cleanup INT
ret=0 local ret=0 gs=
for gs in "${!groupshows[@]}"; do rungroup "$gs" || ret=1; done for gs in "${!groupregex[@]}"; do rungroup "$gs" || ret=1; done
cleanup $ret cleanup $ret
} }

View File

@ -31,7 +31,7 @@ function run(str) {
pub=unescape(opentag($0, "pubDate")) pub=unescape(opentag($0, "pubDate"))
# "date -d \""pub "\" +%s" | getline pubunix # "date -d \""pub "\" +%s" | getline pubunix
pubunix=hotdate(pub) pubunix=hotdate(pub)
if (pubunix <= timestamp) return if (pubunix <= ts) return
torrent=unescape(opentag(str, "link")) torrent=unescape(opentag(str, "link"))
print title sep torrent sep pubunix print title sep torrent sep pubunix
} }