The code works by the user inputting on Discord "$CryptoUpdate bitcoin" I want the program to output the following without the speech marks and curly brackets:
{"id":"bitcoin",
"symbol":"btc",
"name":"Bitcoin","image":"https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
"current_price":36953,
"market_cap":698702581464,
"market_cap_rank":1,"fully_diluted_valuation":776008266281,"total_volume":16490236396,
"high_24h":37609,
"low_24h":36620,
"price_change_24h":58.65,
"price_change_percentage_24h":0.15896,
"market_cap_change_24h":362040201,
"market_cap_change_percentage_24h":0.05184,
"circulating_supply":18907987.0,
"total_supply":21000000.0,
"max_supply":21000000.0,
"ath":51032,
"ath_change_percentage":-27.54985,
"ath_date":"2021-11-10T14:24:11.849Z",
"atl":43.9,
"atl_change_percentage":84116.23351,
"atl_date":"2013-07-05T00:00:00.000Z",
"roi":"null",
"last_updated":"2021-12-22T17:31:34.189Z"}
Click here for JSON response output on Discord
CODE:
import discord
import requests
from replit import db
def getCryptoUpdate(crypto):
URL ='https://api.coingecko.com/api/v3/coins/markets?vs_currency=gbp'
r = requests.get(url=URL)
data = r.json()
for i in range(len(data)):
db[data[i]['id']] = data[i]
if crypto in db.keys():
return db[crypto]
else:
return None
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("$CryptoUpdate "):
cryptoToBeChecked = message.content.split('$CryptoUpdate ',1)[1].lower()
await message.channel.send(getCryptoUpdate(cryptoToBeChecked))
CodePudding user response:
I would suggest having a function like format_message
, and pass in the parsed JSON as a python dict. From then on, you can tweak how the message you send looks, and which attributes from the response to show.
E.g.
def format_message(crypto):
return f'Name: {crypto.name}. Current price: {crypto.current_price}'
If you really need all to show the data, removing the curly brace and quotes would make a negligible difference in terms of readability, in my opinion.