Skip to content

Commit

Permalink
Simplify twitpull, contruct full URL in function
Browse files Browse the repository at this point in the history
  • Loading branch information
decklin committed Jun 6, 2015
1 parent 0e0a7e6 commit ecfddcf
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions contrib/twitpull
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,32 @@ while getopts 'f:j:npv' opt; do
esac
done; shift $(($OPTIND-1))

curlicue_req() {
local url="$1"; shift
request() {
local resource="$1"; shift
local params="$1"; shift
test -n "$verbose" && echo "requesting: $method $url" ${params:+"with params $params"} 1>&2
test -n "$verbose" && echo "requesting: $method $resource" ${params:+"with params $params"} 1>&2
local url="$api_root/$resource.json"
case "$method" in
GET) curlicue -f "$creds" -- -s "$url${params:+?$params}";;
POST) curlicue -f "$creds" -- -s -d "$params" "$url";;
esac
}

get_resource() {
local url="$1"; shift
local params="$1"; shift
if test -n "$no_cursor"; then
curlicue_req "$url" "$params" | jq -r "$filter"
else
temp="$(mktemp -t twitpull.XXXXXX)"; trap "rm -f '$temp'" EXIT
cursor='-1'
while test -n "$cursor" -a "$cursor" != 0; do
curlicue_req "$url" "cursor=$cursor${params:+&$params}" > "$temp" || break
jq -r "$filter" < "$temp"
cursor=$(jq -r '.next_cursor_str' < "$temp")
if test "$cursor" = 'null'; then
echo "error: could not find cursor in response: $(cat "$temp")" 1>&2
break
fi
done
fi
}

resource="$1"; shift
get_resource "$api_root/$resource.json" "$(curl-encode "$@")"
params="$(curl-encode "$@")"

if test -n "$no_cursor"; then
request "$resource" "$params" | jq -r "$filter"
else
temp="$(mktemp -t twitpull.XXXXXX)"; trap "rm -f '$temp'" EXIT
cursor='-1'
while test -n "$cursor" -a "$cursor" != 0; do
request "$resource" "cursor=$cursor${params:+&$params}" > "$temp" || break
jq -r "$filter" < "$temp"
cursor=$(jq -r '.next_cursor_str' < "$temp")
if test "$cursor" = 'null'; then
echo "error: could not find cursor in response: $(cat "$temp")" 1>&2
break
fi
done
fi

0 comments on commit ecfddcf

Please sign in to comment.