I wanted grab some data from an API result and make the grouping to make if readable. The url I need to grab is https://api.testnet.minepi.com/accounts/GCMWJ5FHNS6VFMIY34IL55YCQEJCGWYWUBKDRAEA3KS7DHRIPRA37P7D#payments
. There are 55 total incoming and outgoing transactions made.
I also do not understand this part of the url {?cursor,limit,order}
https://api.testnet.minepi.com/accounts/GCMWJ5FHNS6VFMIY34IL55YCQEJCGWYWUBKDRAEA3KS7DHRIPRA37P7D/transactions{?cursor,limit,order}
Initial Code:
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:104.0) Gecko/20100101 Firefox/104.0',}
url = f"https://api.testnet.minepi.com/accounts/GCMWJ5FHNS6VFMIY34IL55YCQEJCGWYWUBKDRAEA3KS7DHRIPRA37P7D/payments?0,300,asc"
response = requests.get(url, headers=headers,timeout=timeoutReq).json()
print (response)
Needed Output Sample: #-- All transactions
Outgoing Transactions:
amount: 5.0000000 -> to: GA6DJZLKRPWATZVGKZEKBGEX4CHJYBSRNPDBIQ3RUCC3RW3XVL2EZ6XF
amount: 3.0000000 -> to: GCMWJ5FHNS6VFMIY34IL55YCQEJCGWYWUBKDRAEA3KS7DHRIPRA37P7D
Total Outgoing: 2
Total Amount: 8.0000000
Incoming Transactions:
amount: 5.1000000 -> from: 'GD3TTNZ47C4CTJM6A6O7AU7YVQOJN6YEFBYWDKNLCJFOFSXEEPK5WYMN'
amount: 15.0000000 -> from: 'GD3TTNZ47C4CTJM6A6O7AU7YVQOJN6YEFBYWDKNLCJFOFSXEEPK5WYMN'
amount: 25.2000000 -> from: 'GD3TTNZ47C4CTJM6A6O7AU7YVQOJN6YEFBYWDKNLCJFOFSXEEPK5WYMN'
amount: 35.0000000 -> from: 'GD3TTNZ47C4CTJM6A6O7AU7YVQOJN6YEFBYWDKNLCJFOFSXEEPK5WYMN'
amount: 45.5000000 -> from: 'GD3TTNZ47C4CTJM6A6O7AU7YVQOJN6YEFBYWDKNLCJFOFSXEEPK5WYMN'
Total Incoming: 5
Total Amount: 125.8000000
Current Output:
{'_links': {'self': {'href': 'https://api.testnet.minepi.com/accounts/GCMWJ5FHNS6VFMIY34IL55YCQEJCGWYWUBKDRAEA3KS7DHRIPRA37P7D/payments?0,300,asc=&cursor=&limit=10&order=asc'}, 'next': {'href': 'https://api.testnet.minepi.com/accounts/GCMWJ5FHNS6VFMIY34IL55YCQEJCGWYWUBKDRAEA3KS7DHRIPRA37P7D/payments?0,300,asc=&cursor=17860118989250561&limit=10&order=asc'}, 'prev': {'href': 'https://api.testnet.minepi.com/accounts/GCMWJ5FHNS6VFMIY34IL55YCQEJCGWYWUBKDRAEA3KS7DHRIPRA37P7D/payments?0,300,asc=&cursor=15670488827207681&limit=10&order=desc'}},
#-- more lines below
CodePudding user response:
If you have the account of interest, which in this case is the one you are using in the URL: GCMWJ5FHNS6VFMIY34IL55YCQEJCGWYWUBKDRAEA3KS7DHRIPRA37P7D
, you can use it to compare with the data in each record you get from the API and group them as you want.
In your case, you want to group them by outgoing
and incoming
, so you can compare with the properties from
and to
, in the response data. If the account of interest is in the from
property, it means that it should be grouped in the outgoing
group. If it is in the to
property, it should be grouped in the incoming
group.
There are many different approaches to grouping the records. A simple one would be to create 2 lists: payments_received
and payments_sent
. It should be something like this:
import requests
account = "GCMWJ5FHNS6VFMIY34IL55YCQEJCGWYWUBKDRAEA3KS7DHRIPRA37P7D"
timeoutReq = 5000
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:104.0) Gecko/20100101 Firefox/104.0'
}
url = f"https://api.testnet.minepi.com/accounts/{account}/payments?0,300,asc"
response = requests.get(url, headers=headers,timeout=timeoutReq).json()
# create lists to store the transactions we want
payments_sent = []
payments_received = []
# iterate over the records in the response
for record in response["_embedded"]["records"]:
# we are interested in the "payment" types
if record["type"] == "payment":
if record["from"] == account:
payments_sent.append(record)
elif record["to"] == account:
payments_received.append(record)
print(f"Number of payments received: {len(payments_received)}")
print("---")
print(f"Number of payments sent: {len(payments_sent)}")
You can see it in action here: https://codehs.com/sandbox/id/python-3-nN1xfb
Once you have this data grouped, you can iterate over each list and present the data as you want it.
The {?cursor,limit,order}
in the URL are parameters that can be used to paginate and sort the data you get from the API. You can see that the values for ?cursor,limit,order
have been defined to 0,300,asc
in the URL you make a request to: https://api.testnet.minepi.com/accounts/{account}/payments?0,300,asc
I also think that the {}
inform that they are optional. But I am not sure.
UPDATE: how to iterate over the grouped records
To iterate over the records list and print the data you want, you can use a simple for
loop, like:
print(f"Number of payments received: {len(payments_received)}")
for payment in payments_received:
print(f"amount: {payment['amount']} -> from: {payment['from']}")
print("---")
print(f"Number of payments sent: {len(payments_sent)}")
for payment in payments_sent:
print(f"amount: {payment['amount']} -> to: {payment['to']}")