Skip to content

Instantly share code, notes, and snippets.

@robin-a-meade
Last active January 7, 2022 02:58
Show Gist options
  • Save robin-a-meade/34eb36415bebc100ec80747ac49f52d8 to your computer and use it in GitHub Desktop.
Save robin-a-meade/34eb36415bebc100ec80747ac49f52d8 to your computer and use it in GitHub Desktop.
Bash script to lookup the creation date of a reddit post or comment

reddit_created — A bash script to lookup the creation date of a reddit post or comment

Example

[robin@laptop ~] $ reddit_created https://www.reddit.com/r/redditisfun/comments/48dk3e/see_actual_date_of_comment_instead_of_2_months/
Mon Feb 29 02:41:34 PM HST 2016
Tue Mar  1 12:41:34 AM GMT 2016

It shows both local time and GMT time.

#!/bin/bash
set -o pipefail
# Check that jq is installed
if ! type -P jq >/dev/null; then
echo "error: couldn't find jq on path."
exit 1
fi
url=$1
# Chop off / from the end
url=${url%/}
# Add .json to the end
url=${url}.json
max_attempts=3
success=
for (( i=0; i<max_attempts; i++ )); do
if created=$(curl -fs "$url" | jq '.[0].data.children.[0].data.created'); then
success=Y
break
fi
# Probably got the "Too Many Requests" response
# Sleep for a period of time before making next attempt
# Increase the duration for each iteration: 5, 10, 15 seconds
sleep "$((5 + i*5))"
done
if [[ $success ]]; then
echo "$(date -d @"$created")"
echo "$(TZ=GMT date -d @"$created")"
else
# Must have reached max_attempts
echo "Sorry, tried $max_attempts times without success. Probably \"Too Many requests.\""
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment