From 83e804e5072ec2d78397463f7303b03aa13d7354 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Tue, 7 Jun 2022 20:10:46 -0300 Subject: [PATCH] chore: thing for dzi --- .gitignore | 4 +- 2022-06-07/paxful/paxful_api.py | 96 +++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 2022-06-07/paxful/paxful_api.py diff --git a/.gitignore b/.gitignore index 2e244f9..8534f69 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ **/.vscode **/bin **/rom -**/.gitignore \ No newline at end of file +**/.gitignore + +**/venv \ No newline at end of file diff --git a/2022-06-07/paxful/paxful_api.py b/2022-06-07/paxful/paxful_api.py new file mode 100644 index 0000000..c882cf1 --- /dev/null +++ b/2022-06-07/paxful/paxful_api.py @@ -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()