chore: thing for dzi

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-06-07 20:10:46 -03:00
parent f1b634b20b
commit 83e804e507
2 changed files with 99 additions and 1 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@
**/bin **/bin
**/rom **/rom
**/.gitignore **/.gitignore
**/venv

View File

@ -0,0 +1,96 @@
import json
import requests
import time
import signal
import sys
# Paxful API Base URL
api_url = 'https://paxful.com/data/trades'
# `start_date` and `end_date` are time-date strings in the format
# 'YYY:MM:DD HH:mm:ss'. These represent the range of transactions we want to
# collect from the API
start_date = '2022-01-01 00:00:00'
end_date = '2022-01-01 1:00:00'
# This is the list of known (and UNIQUE) transactions
known_txs = {}
def main():
# You can use CTRL-C from the terminal in order to end the program
# early and the will save all the transactions it's collected thus far to disk
signal.signal(signal.SIGINT, write_and_exit)
# We will not store nor query for transactions past this point in time
needle_limit = get_unix_timestamp(end_date)
# The current time we're querying the Paxful API for
needle = get_unix_timestamp(start_date)
# This loop iterates over all points of time between the start and the end date
# and checks to see if there are any transactions that we don't already know about
# that might have occurred during this time
while needle < needle_limit:
# Build Proper URL for API Request
req_url = api_url + '?sincetype=date&since=' + str(needle)
req = requests.get(req_url)
txs = req.json() # Get Array of transaction JSON objects
# The number of transactions received from the API. Should be ~100, doesn't have to be
txs_len = len(txs)
# This keeps track of how many new (and within-range) transactions we found
valid_count = 0
# For every Transaction in the ~100 transactions we received from Paxful
for tx in txs:
# get the Paxful-defined ID of the transaction. This is guaranteed (by paxful) to be unique
# so we can use it to keep track of unique transactions `known_txs` above
id = tx['id']
if id not in known_txs:
tx_timestamp = int(tx['date'])
# Before we get batches in 100, sometimes we can get transactions that
# fall outside our range. We filter those out here
if (tx_timestamp <= needle_limit):
valid_count += 1
known_txs[id] = tx
# For Debugging: Number of transactions received from Paxful and number of transactions we care about
print("Batch: " + str(txs_len) + ' Valid: ' + str(valid_count))
# Paxful Transactions are sorted by date, and so instead of incrementing the timestamp by a
# second and performing another HTTP request, we can set the needle to the timestamp of the last
# transaction we found. This also reduces the amount of repeated transactions we find when querying
# Paxful's API
needle = int(txs[txs_len - 1]['date'])
# Once we've found all transactions within the range, set by `start_date` and `end_date`
# Write to file and exit the program
write_and_exit(None, None)
# Converts a date-string to a Unix timestamp, which is the format Paxful uses
# to keep track of time
def get_unix_timestamp(date: str):
return int(time.mktime(time.strptime(date, '%Y-%m-%d %H:%M:%S')))
def write_and_exit(sig, frame):
# Write all found transactions to file (JSON format)
known_txs_len = len(known_txs)
out_file = open('tx.json', 'w')
print("Total In-Range Transactions: " + str(known_txs_len))
out_file.write('[')
for i, tx in enumerate(known_txs):
suffix = ',' if (i < known_txs_len - 1) else ''
out_file.write(json.dumps(known_txs[tx]) + suffix)
out_file.write(']')
sys.exit(0)
if __name__ == '__main__':
main()