1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| import base64 import hmac import json import urllib.parse from datetime import datetime, timezone from typing import Dict
import requests
class OkApiService: key = '' secret = ""
def pre_hash(self, timestamp, method, request_path, params=None): query_string = '' if method == 'GET' and params: query_string = '?' + urllib.parse.urlencode(params).replace("%2520", "%20") if method == 'POST' and params: query_string = json.dumps(params) return f"{timestamp}{method}{request_path}{query_string}"
def create_signature(self, method, request_path, params=None): timestamp = datetime.now(timezone.utc).isoformat()[:-9] + 'Z' message = self.pre_hash(timestamp, method, request_path, params) signature = hmac.new(bytes(self.secret, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256') signature = base64.b64encode(signature.digest()) return signature, timestamp
def get_market_list(self, token_info: Dict) -> None:
url = f"https://www.okx.com/api/v5/mktplace/nft/markets/listings?chain={token_info.get('chain')}&collectionAddress={token_info.get('collectionAddress')}&limit={100}&sort=price_asc"
params = { "chain": token_info.get('chain'), "collectionAddress": token_info.get('collectionAddress'), "limit": 100, "sort": "price_asc" } signature, timestamp = self.create_signature("GET", "/api/v5/mktplace/nft/markets/listings", params) headers = { "Content-Type": 'application/json', 'OK-ACCESS-KEY': self.key, 'OK-ACCESS-SIGN': signature, 'OK-ACCESS-TIMESTAMP': timestamp, 'OK-ACCESS-PASSPHRASE': '', }
response = requests.request("GET", url, headers=headers) print(len(response.json().get("data").get("data"))) for d in response.json().get("data").get("data"): print(f"{d.get('tokenId')}")
if __name__ == '__main__': params = { "chain": "Lumi%20Layer3", "collectionAddress": "0x3fa10a9d43113687f2fd143fca7445d8ef8e334a" } OkApiService().get_market_list(params) params = { "chain": "eth", "collectionAddress": "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d" } OkApiService().get_market_list(params)
|