This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

DANGEROUS - coinmarket.tcl - another crypto tcl script

Support & discussion of released scripts, and announcements of new releases.
Post Reply
n
nobody
Voice
Posts: 31
Joined: Fri Apr 03, 2020 10:01 pm

DANGEROUS - coinmarket.tcl - another crypto tcl script

Post by nobody »

Hi,

again, this is another crypto tcl script that fetches live prices from coinmarketcap.com.

Cheers!

Code: Select all

#ₙₒₜₕᵢₙg ᵢₛ ₕₐᵣₘ ₑₓcₑₚₜ yₒᵤᵣ ₚᵣᵢdₑ ☢ [ ᴇxᴘʟᴏɪᴛᴇʀ ] ☢
#https://www.blackbug.org
# Load necessary packages
package require json

# Define API credentials
set api_url "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest"
set api_key "370cdba7-79dc-4180-94e8-50afacc9d44f"

# List of top 50 cryptocurrency symbols
set coin_list {
    BTC ETH USDT XRP BNB SOL USDC DOGE ADA TRX HBAR LINK LTC XLM LEO SUI AVAX TON SHIB OM DOT HYPE BCH USDe DAI BGB UNI XMR NEAR APT ONDO PEPE ICP AAVE ETC TAO OKB TRUMP MNT POL VET TIA S FIL ALGO FDUSD KAS JUP RENDER CRO GT ARB ATOM OP FET DEXE MKR KCS IP INJ XDC ENA IMX STX WLD SEI THETA LDO QNT GRT BONK MOVE FLR BERA JASMY EOS XTZ SAND FLOKI ENS PYUSD NEXO FLOW IOTA PYTH GALA JTO BTT KAIA XAUt BSV VIRTUAL NEO RAY GRASS PAXG EGLD WIF ZEC RON AXS CAKE HNT STRK MANA CRV PENGU CFX FTT AR MATIC KAITO DYDX TUSD XCN SPX AERO CORE PENDLE CHZ MELANIA APE KAVA MORPHO XEC COMP RUNE AMP TWT NFT BNX AIOZ BEAM DEEP RSR AKT AI16Z W AXL GNO MINA EIGEN BRETT LUNC ZK GLM 1INCH JST CTC KSM SFP DASH TFUEL SNX CKB MX BLUR ASTR ZRO NOT SUPER ATH ROSE SATS QTUM ACH ZIL SAFE VANA FARTCOIN WEMIX VTHO MOG LPT BAT POPCAT ZRX HOT ID ORDI BabyDoge ZKJ MOCA TURBO XCH CVX MASK PNUT GAS CELO STPT ETHFI ZETA OSMO MEW SUSHI
}

# Function to fetch cryptocurrency data
proc fetch_coin_data {symbol} {
    global api_url api_key

    set symbol [string toupper $symbol] ;# Convert symbol to uppercase
    set request_url "$api_url?symbol=$symbol&convert=USD"

    # Fetch data using curl
    set response [exec curl -s -H "X-CMC_PRO_API_KEY: $api_key" $request_url]

    if {[string length $response] == 0} {
        return "Error: No data received from API."
    }

    # Parse JSON response
    set data [json::json2dict $response]

    if {[dict exists $data "data"] && [dict exists [dict get $data "data"] $symbol]} {
        set coin_data [dict get [dict get $data "data"] $symbol]
        set quote_data [dict get $coin_data "quote"]
        set usd_data [dict get $quote_data "USD"]

        if {
            [dict exists $usd_data "price"] &&
            [dict exists $usd_data "percent_change_1h"] &&
            [dict exists $usd_data "percent_change_24h"] &&
            [dict exists $usd_data "percent_change_7d"] &&
            [dict exists $usd_data "percent_change_30d"] &&
            [dict exists $coin_data "name"] &&
            [dict exists $usd_data "last_updated"]
        } {
            # Extract values
            set coin_name [dict get $coin_data "name"]
set price [format "%.2f" [dict get $usd_data "price"]]
set price_1h [format "%.2f" [dict get $usd_data "percent_change_1h"]]
set price_1d [format "%.2f" [dict get $usd_data "percent_change_24h"]]
set price_1w [format "%.2f" [dict get $usd_data "percent_change_7d"]]
set price_1m [format "%.2f" [dict get $usd_data "percent_change_30d"]]
            set last_updated [dict get $usd_data "last_updated"]

            # Convert timestamp to readable format
            set timestamp [clock scan $last_updated -format "%Y-%m-%dT%H:%M:%S.000Z"]
            set formatted_date [clock format $timestamp -format "%a, %d-%b-%y %H:%M:%S %Z"]

            # Colors
            set color_green "\00303"  ;# Green
            set color_red "\00304"    ;# Red
            set color_reset "\00301"  ;# Reset color

            # Determine color and arrow direction
            set color_1h $color_red; set arrow_1h "▼"
            if {$price_1h >= 0} { set color_1h $color_green; set arrow_1h "▲" }

            set color_1d $color_red; set arrow_1d "▼"
            if {$price_1d >= 0} { set color_1d $color_green; set arrow_1d "▲" }

            set color_1w $color_red; set arrow_1w "▼"
            if {$price_1w >= 0} { set color_1w $color_green; set arrow_1w "▲" }

            set color_1m $color_red; set arrow_1m "▼"
            if {$price_1m >= 0} { set color_1m $color_green; set arrow_1m "▲" }

            # Construct output message (Arrow first, then percentage)
            set output "\00305 ««\[ $coin_name \00302($symbol) \]»»  \00301Price:\00304 \$${price} USD \00301☢ 1h: ${color_1h}${arrow_1h} ${price_1h}% \00301☢ 1d: ${color_1d}${arrow_1d} ${price_1d}% \00301☢ 1w: ${color_1w}${arrow_1w} ${price_1w}% \00301☢ 1m: ${color_1m}${arrow_1m} ${price_1m}% \00301☢ Last update: \00302${formatted_date} \00309☢ \[ ᴇxᴘʟᴏɪᴛᴇʀ \] ☢"

            # Debugging
            puts "Output message: $output"

            return $output
        } else {
            return "Error: Missing necessary data in API response."
        }
    } else {
        return "Error: Coin symbol not found."
    }
}

# Function to fetch and post a random coin price every hour
proc random_coin_price {channel} {
    global coin_list

    set random_coin [lindex $coin_list [expr {int(rand() * [llength $coin_list])}]]
    set data [fetch_coin_data $random_coin]
    putserv "PRIVMSG $channel :$data"

    # Schedule next update in 1 hour (3600000ms)
    after 3600000 [list random_coin_price $channel]
}

# Start auto-updating prices
proc start_price_updates {channel} {
    after 10000 [list random_coin_price $channel]  ;# Start after 10 seconds
}

# Bind !coin command for user requests
bind pub - !coin fetch_coin_command

# Function to handle the !coin command
proc fetch_coin_command {nick host handle channel arg} {
    if {[string length $arg] == 0} {
        putserv "PRIVMSG $channel :Usage: !coin <symbol>"
        return
    }

    set data [fetch_coin_data $arg]
    putserv "PRIVMSG $channel :$data"
}

# Start price updates in the designated channel
start_price_updates "#blockchain"

putlog "coinswithtimer.tcl loaded with auto-updates enabled - Janroe"


User avatar
CrazyCat
Revered One
Posts: 1340
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Re: coinmarket.tcl - another crypto tcl script

Post by CrazyCat »

WARNING: usage of exec in script, please adapt it to use http package.
Post Reply