Created
December 16, 2023 00:22
-
-
Save CodeAlDente/d78bba302824dc62b85f6d50ae13f76d to your computer and use it in GitHub Desktop.
Fetch JSON-data from a BuyMeACoffee profile page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script Description: | |
# This Bash script fetches JSON data associated with a BuyMeACoffee profile | |
# for a specified username and decodes specific HTML entities in the retrieved data. | |
# Function to decode HTML entities in a string | |
html_entity_decode() { | |
# Use sed to replace HTML entity " with a double quote (") | |
echo "$1" | sed -e 's/"/"/g' | |
} | |
# Initialize username variable | |
username="" | |
# Parse command-line arguments | |
while [[ "$#" -gt 0 ]]; do | |
case $1 in | |
--username) | |
username="$2" | |
shift 2 | |
;; | |
*) | |
echo "Unknown parameter: $1" | |
exit 1 | |
;; | |
esac | |
done | |
# Check if a username is provided | |
if [ -z "$username" ]; then | |
echo "Usage: $0 --username <username>" | |
exit 1 | |
fi | |
# Use the provided username to construct the BuyMeACoffee profile URL | |
profile_url="https://www.buymeacoffee.com/$username" | |
# Download HTML content of the specified URL | |
html_content=$(curl -s "$profile_url") | |
# Extract JSON information from the specific div-container | |
# Find the div with id="app" and attribute data-page="...", then extract the value of data-page | |
json_data=$(echo "$html_content" | grep -o "<div id=\"app\"[^>]*data-page=\"[^\"]*\"" | grep -o "data-page=\"[^\"]*\"" | cut -d'"' -f2) | |
# Output JSON content after decoding certain HTML entities | |
echo $(html_entity_decode "$json_data") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple way to get information about a user:
bash fetch_buymeacoffee_profile_json_data.bash --username codealdente | jq .props.creator_data
Example output: