Last active
March 20, 2023 07:17
-
-
Save cscottyb/d7a630a707c3471bcc5d3d1404ad7388 to your computer and use it in GitHub Desktop.
Plugin for xbar app for MacOS to show cryptocurrency prices and 24 hour changes for an array of tokens using Coin Gecko API
This file contains hidden or 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
| #!/usr/bin/env python3 | |
| # <xbar.title>CoinGecko Price and 24 Hour Percent Changes</xbar.title> | |
| # <xbar.version>v1.2</xbar.version> | |
| # <xbar.author>Scotty B</xbar.author> | |
| # <xbar.author.github>cscottyb</xbar.author.github> | |
| # <xbar.desc>Using CoinGecko API to monitor a coin</xbar.desc> | |
| # <xbar.image>http://www.hosted-somewhere/pluginimage</xbar.image> | |
| # <xbar.dependencies>python,JetBrainsMono</xbar.dependencies> | |
| # <xbar.abouturl>https://github.com/cscottyb</xbar.abouturl> | |
| import urllib.request | |
| import json | |
| def get_coin_price(coin_id, coin_currency): | |
| url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies={coin_currency}&include_24hr_change=true" | |
| with urllib.request.urlopen(url) as response: | |
| if response.status != 200: | |
| print("❌ Error") | |
| else: | |
| data = json.loads(response.read().decode()) | |
| coin_price = data[coin_id][coin_currency] | |
| coin_24h_change = data[coin_id]["usd_24h_change"] | |
| return coin_price, coin_24h_change | |
| def print_coin_prices(coin_details): | |
| coin_prices = [] | |
| max_price_len = 0 | |
| price_length = 9 # length of the price string | |
| for coin in coin_details: | |
| coin_id = coin["coin_id"] | |
| coin_symbol = coin["coin_symbol"] | |
| coin_currency = coin["coin_currency"] | |
| coin_price, coin_24h_change = get_coin_price(coin_id, coin_currency) | |
| if coin_price is not None: | |
| coin_price_formatted = '{:.{pl}f}'.format( | |
| coin_price, pl=price_length).rstrip("0").rstrip(".") | |
| coin_24h_change_formatted = "{:.2f}".format( | |
| abs(coin_24h_change)) | |
| if len(coin_price_formatted) > max_price_len: | |
| max_price_len = len(coin_price_formatted) | |
| if coin_24h_change > 0: | |
| coin_prices.append( | |
| f'{coin_symbol} {coin_price_formatted.rjust(max_price_len).replace(" ", "․")} \033[38;2;75;211;75m↑\033[0m {coin_24h_change_formatted}% | font=JetBrainsMono-Regular size=14') | |
| else: | |
| coin_prices.append( | |
| f'{coin_symbol} {coin_price_formatted.rjust(max_price_len).replace(" ", "․")} \033[38;2;255;50;75m↓\033[0m {coin_24h_change_formatted}% | font=JetBrainsMono-Regular size=14') | |
| return coin_prices | |
| def main(): | |
| # ***** Change these values for your coin ******** | |
| coin_details = [ | |
| {"coin_id": "ash-token", "coin_symbol": "ASH", "coin_currency": "usd"}, | |
| {"coin_id": "immutable-x", "coin_symbol": "IMX", "coin_currency": "usd"}, | |
| {"coin_id": "binancecoin", "coin_symbol": "BNB", "coin_currency": "usd"}, | |
| {"coin_id": "bitcoin", "coin_symbol": "BTC", "coin_currency": "usd"} | |
| ] | |
| coin_prices = print_coin_prices(coin_details) | |
| for coin_price in coin_prices: | |
| print(coin_price) | |
| if __name__ == "__main__": | |
| main() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This Python script uses the CoinGecko API to fetch the current price and 24-hour percentage change of a set of cryptocurrencies, specified in the coin_details list.
The script first defines two functions. The
get_coin_pricefunction constructs a URL using the CoinGecko API, retrieves the data using theurllibmodule, and returns the current price and 24-hour percentage change of the specified coin. Theprint_coin_pricesfunction iterates through the coin_details list and callsget_coin_pricefor each coin. It formats the output using f-strings and adds the resulting strings to a list, which it then returns.The main function sets up the
coin_detailslist and calls theprint_coin_pricesfunction, printing the resulting list of formatted coin data to the console.The script is designed to be used with the xbar (previously known as BitBar) macOS application, which allows users to create custom menu bar items that can display dynamic information, such as stock prices, weather forecasts, or cryptocurrency data. The xbar-specific comments at the top of the script specify the title, version, author, and other metadata for the xbar item.
The script requires the
urllibandjsonmodules to be installed, as well as theJetBrainsMonofont.This documentation was written by ChatGPT