Skip to content

Instantly share code, notes, and snippets.

@CodeAlDente
Created December 16, 2023 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeAlDente/d78bba302824dc62b85f6d50ae13f76d to your computer and use it in GitHub Desktop.
Save CodeAlDente/d78bba302824dc62b85f6d50ae13f76d to your computer and use it in GitHub Desktop.
Fetch JSON-data from a BuyMeACoffee profile page
#!/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")
@CodeAlDente
Copy link
Author

Simple way to get information about a user:

bash fetch_buymeacoffee_profile_json_data.bash --username codealdente | jq .props.creator_data

Example output:

{
  "data": {
    "user_id": 2076937,
    "project_id": 2076702,
    "links": [
      "https://github.com/CodeAlDente"
    ],
    "default_widget": 1,
    "description": "&lt;h1&gt;Hi there! I&#39;m @CodeAlDente.&lt;/h1&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;You most probably come from my profile on GitHub. I&#39;m trying to help others there by providing services and data for free. Tho I&#39;ve expenses to run services in background. If you want and if you can, your support is higly appreciated. All donations will be used to pay the bills. Thank you very much!&lt;/p&gt;",
    "featured_image": "",
    "featured_video": {
      "url": "",
      "thumbnail": ""
    },
    "wishlist_count": 0,
    "feature_wishlist": false,
    "name": "CodeAlDente",
    "dp": "https://cdn.buymeacoffee.com/uploads/profile_pictures/2022/03/kMCkQ2DSBFiL7imD.jpg",
    "cover_image": "https://cdn.buymeacoffee.com/uploads/cover_images/2022/04/9478bb9103fa1462978a7d2b6dd6d72c.jpg",
    "work": "helping others by contributing on GitHub!",
    "slug": "codealdente",
    "public_supporter_count": 1,
    "supporter_count": false,
    "tag_alternative": "coffee",
    "twitter_handle": "",
    "tag_emoji": "",
    "coffee_price": "1.0000",
    "page_color": "#FF5F5F",
    "currency": "EUR",
    "payment_method": "0",
    "payout_connected": false,
    "membership_enabled": true,
    "own_page": false,
    "following": false,
    "subscribed": false,
    "supporter": false,
    "can_message": false,
    "banned": 0,
    "active": 1,
    "deleted": false,
    "payment_disabled": false,
    "charity": [],
    "has_nsfw": 0,
    "auth_user_blocked": false,
    "featured_content": 1,
    "project_og_image_style": 2,
    "google_analytics": ""
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment