Skip to content

Instantly share code, notes, and snippets.

@thislooksfun
Last active February 13, 2022 20:36
Show Gist options
  • Save thislooksfun/0c4519ffceb81c400ea8801522d2aba5 to your computer and use it in GitHub Desktop.
Save thislooksfun/0c4519ffceb81c400ea8801522d2aba5 to your computer and use it in GitHub Desktop.
#!/bin/sh
# START CONFIG
USER_AGENT=''
CLIENT_ID=''
CLIENT_SECRET=''
USERNAME=''
PASSWORD=''
# END CONFIG
# DO NOT EDIT BELOW THIS LINE
#region dns
t_dns() {
ip="$(LC_ALL=C nslookup $1 2>/dev/null | sed -nr '/Name/,+1s|Address(es)?: *||p')"
echo "$ip"
if [ "$ip" == "0.0.0.0" ] || [ "$ip" == "127.0.0.1" ] ; then
return 1
else
return 0
fi
}
#endregion dns
#region curl
t_curl() {
curl -sf "$1"
}
h_curl_grant() {
curl -sfX POST -H "Content-Type: application/x-www-form-urlencoded" \
-u "${CLIENT_ID}:${CLIENT_SECRET}" \
--data-ascii "api_type=json&$1" \
'https://www.reddit.com/api/v1/access_token'
}
t_curl_grant_cc() {
h_curl_grant "grant_type=client_credentials"
}
t_curl_grant_pw() {
# URL encode the username and password before using
SAFE_USERNAME=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$USERNAME")
SAFE_PASSWORD=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$PASSWORD")
h_curl_grant "grant_type=password&username=${SAFE_USERNAME}&password=${SAFE_PASSWORD}"
}
#endregion curl
#region npm
h_npm() {
tmpdir="$(mktemp -d)"
cd "$tmpdir"
npm init -y
npm i $1
echo "$2" > run.js
node run.js
code=$?
rm -rf "$tmpdir"
return $code
}
#region got
h_got() {
h_npm "got@11" "const got = require(\"got\");$($@)"
}
t_got_get() {
h_got echo "got.get(\"$1\");"
}
h_got_grant() {
h_got cat <<EOF
got.post("https://www.reddit.com/api/v1/access_token", {
headers: { "user-agent": "${USER_AGENT}" },
form: {
api_type: "json",
$1
},
username: "${CLIENT_ID}",
password: "${CLIENT_SECRET}",
}).then(console.log);
EOF
}
t_got_grant_cc() {
h_got_grant 'grant_type: "client_credentials"'
}
t_got_grant_pw() {
h_got_grant "grant_type: 'password', username: '${USERNAME}', password: '${PASSWORD}'"
}
#endregion got
#region snoots
t_snoots() {
contents=$(cat <<EOF
const {Client} = require("snoots");
const client = new Client({
userAgent: "${USER_AGENT}",
creds: {
clientId: "${CLIENT_ID}",
clientSecret: "${CLIENT_SECRET}",
},
auth: {
username: "${USERNAME}",
password: "${PASSWORD}",
},
});
void (async () => {
const sub = await client.subreddits.fetch("funny");
const post = await sub.getRandomPost();
console.log(post.title, post.author, post.score, post.id);
})();
EOF
)
h_npm "snoots" "$contents"
}
#endregion snoots
#endregion npm
# Run a test and pretty-print the output
test() {
MSG="$1"
shift
printf "\033[34m[TEST]\033[0m $MSG"
output="$(2>&1 $@)"
if [ "$?" -eq "0" ] ; then
printf "\r\033[32m[ OK ]\033[0m $MSG\n"
else
printf "\r\033[31m[FAIL]\033[0m $MSG\n"
echo "$output"
echo
fi
}
# Actually run tests
# Test dns resolution
test 'dns: reddit.com' t_dns 'reddit.com'
test 'dns: www.reddit.com' t_dns 'www.reddit.com'
# Test URLs (via cURL)
test 'curl: https://reddit.com' t_curl 'https://reddit.com'
test 'curl: https://www.reddit.com' t_curl 'https://www.reddit.com'
# Test grants (via cURL)
test 'curl: client credentials grant' t_curl_grant_cc
test 'curl: password grant' t_curl_grant_pw
# Test URLs (via got)
test 'got: https://reddit.com' t_got_get 'https://reddit.com'
test 'got: https://www.reddit.com' t_got_get 'https://www.reddit.com'
# Test grants (via got)
test 'curl: client credentials grant' t_got_grant_cc
test 'curl: password grant' t_got_grant_pw
# Test snoots
test 'snoots' t_snoots
#!/bin/sh
# START CONFIG
USER_AGENT=''
CLIENT_ID=''
CLIENT_SECRET=''
USERNAME=''
PASSWORD=''
# END CONFIG
# DO NOT EDIT BELOW THIS LINE
#region curl
h_curl_grant() {
curl -sSX POST -H "Content-Type: application/x-www-form-urlencoded" \
-u "${CLIENT_ID}:${CLIENT_SECRET}" \
--data-ascii "api_type=json&$1" \
'https://www.reddit.com/api/v1/access_token'
}
t_curl_grant_cc() {
h_curl_grant "grant_type=client_credentials"
}
t_curl_grant_pw() {
# URL encode the username and password before using
SAFE_USERNAME=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$USERNAME")
SAFE_PASSWORD=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$PASSWORD")
h_curl_grant "grant_type=password&username=${SAFE_USERNAME}&password=${SAFE_PASSWORD}"
}
#endregion curl
echo 'creds grant:'
t_curl_grant_cc
echo 'password grant:'
t_curl_grant_pw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment