Home > other >  How to split Python GET request by parameter into different bitcoin offers
How to split Python GET request by parameter into different bitcoin offers

Time:02-19

I'd like to use the BISQ API to extract BTC buy offers, and list them separately in a new line. However, the return I'm getting has too many levels for me to extract using key-value pairs (although I'm very new to Python so am probably missing something!)

Currently, I've called the API with the following response:

import requests
import json
response_USD = requests.get('https://bisq.markets/api/offers?market=BTC_USD')
print(response_USD.json()['btc_usd']['buys'])

Which returns (cut due to length):

[{'offer_id': 'aMDMHXt-9222c082-5f60-4274-9bbf-5d58d986d8b3-182', 'offer_date': 1645195105176, 'direction': 'BUY', 'min_amount': '0.01000000', 'amount': '0.02000000', 'price': '45000.00000000', 'volume': '900.00000000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': 'xf6xttjw-a21b0c58-4f46-487f-8f71-c33f20f0f61d-182', 'offer_date': 1644872308763, 'direction': 'BUY', 'min_amount': '0.00900000', 'amount': '0.01700000', 'price': '43255.79460000', 'volume': '735.34850000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': '82198-b1ab6e83-8a07-4a7c-8a25-00f91202c67f-180', 'offer_date': 1644131836719, 'direction': 'BUY', 'min_amount': '0.01130000', 'amount': '0.01130000', 'price': '42851.53480000', 'volume': '484.22230000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': '9y8ezr-0f9cb55a-0030-4610-b5af-72e7f19775ae-182', 'offer_date': 1644932335080, 'direction': 'BUY', 'min_amount': '0.01000000', 'amount': '0.02000000', 'price': '42851.53480000', 'volume': '857.03060000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': '856233-e943bc25-9df4-47e2-977f-6a65425fe45b-182', 'offer_date': 1644469262319, 'direction': 'BUY', 'min_amount': '0.00250000', 'amount': '0.00250000', 'price': '42447.27500000', 'volume': '106.11810000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': '318317-fb6d2475-f2dd-4b74-886e-7da6ff7b349c-182', 'offer_date': 1644122335075, 'direction': 'BUY', 'min_amount': '0.00310000', 'amount': '0.00310000', 'price': '42447.27500000', 'volume': '131.58650000', 'payment_method': 'AMAZON_GIFT_CARD', 'offer_fee_txid': None}, {'offer_id': '46MoyVB-daf83cf0-a064-4d32-8fd9-73593599bbb5-182', 'offer_date': 1645120327323, 'direction': 'BUY', 'min_amount': '0.01000000', 'amount': '0.01000000', 'price': '42043.01530000', 'volume': '420.43010000', 'payment_method': 'CLEAR_X_CHANGE', 'offer_fee_txid': None}...

I'd like to extract every offer (from 'offer_id' to 'offer_fee_txid') as a new line, and then call within that particular part (e.g. call 'price').

Is there an easy way to do that?

CodePudding user response:

I recomment iterating the data and copying out the data points you require as follows:

import requests
import json

#request the raw dataset
response_USD = requests.get('https://bisq.markets/api/offers?market=BTC_USD')

#list of buys, and keys to extract
buys = []
extract = ["offer_id", "price"]

#iterate raw data, copying required datapoints
for buy_data in response_USD.json()['btc_usd']['buys']:
    buy = {}
    for key in extract:
        buy[key] = buy_data[key]
    buys.append(buy)

#print the collected data for each buy
for buy in buys:
    print(buy)

CodePudding user response:

Not sure exactly what you need but this should get you started:

import requests

(r := requests.get('https://bisq.markets/api/offers?market=BTC_USD')).raise_for_status()
for buy in r.json()['btc_usd']['buys']:
    print(f"Offer id {buy['offer_id']} -> Price {buy['price']}")

Output (extract):

...
Offer id 8wqgbau-b2edf3d0-2f2b-4738-a956-e92227c906a1-182 -> Price 39277.97460000
Offer id ZEHFTYH-49190056-2aa8-4ea6-895f-39e1a1b22d89-182 -> Price 38877.17900000
Offer id XLKeI-0e6bbd7f-a947-4da1-8028-b8b9ef0a7308-175 -> Price 38877.17900000
Offer id 28444200-0b2719ca-544b-4c10-a6f3-0b811075e885-182 -> Price 38709.18190000
...
  • Related