Skip to content

Instantly share code, notes, and snippets.

@masotime
Last active June 14, 2020 21:58
Show Gist options
  • Save masotime/27f2a4b2a05aa52d9de6308cf16d9119 to your computer and use it in GitHub Desktop.
Save masotime/27f2a4b2a05aa52d9de6308cf16d9119 to your computer and use it in GitHub Desktop.
Download stuff from Automatic Dashboard API
#!/bin/bash
START_TIME=1420099200000 # Unix timestamp
END_TIME=1592164819833 # Unix timestamp
INDEX=0
PREFIX='drives' # name is up to you
BEARER_TOKEN="<fill-this-with-your-token>"
URL="https://api.automatic.com/trip/?started_at__gte=${START_TIME}&started_at__lte=${END_TIME}&limit=250" # limit cannot be > 250 I think
set_current_file () {
CURRENT="${PREFIX}_${INDEX}.json"
}
increment () {
((++INDEX))
}
scrape () {
set_current_file
echo Going to write to $CURRENT using $URL
# Fetch Data
curl "$URL" \
--compressed \
-XGET \
-H 'Accept: */*' \
-H 'Origin: https://dashboard.automatic.com' \
-H 'Referer: https://dashboard.automatic.com/' \
-H 'Accept-Language: en-us' \
-H 'Host: api.automatic.com' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15' \
-H "Authorization: bearer $BEARER_TOKEN" \
-H 'Accept-Encoding: gzip, deflate, br' \
-H 'Connection: keep-alive' | jq '.' > $CURRENT
# Grab Metadata
COUNT=$(cat $CURRENT | jq '._metadata.count')
PREVIOUS=$(cat $CURRENT | jq -r '._metadata.previous')
NEXT=$(cat $CURRENT | jq -r '._metadata.next')
if [ $PREVIOUS == "null" ]; then
echo $COUNT drives found.
fi
echo $URL fetched. Continuing with $NEXT.
URL=$NEXT
increment
}
scrape
while [ $NEXT != "null" ]; do
scrape
done
echo Next URL $NEXT, terminated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment