|
| 1 | +#!/usr/bin/env python3 |
| 2 | +from unittest import result |
| 3 | +import requests |
| 4 | +import json |
| 5 | +import sys |
| 6 | +import time |
| 7 | + |
| 8 | +CONSTANTS = { |
| 9 | + "GATE_IO": { |
| 10 | + "SYMBOLS": { |
| 11 | + "NOM":"nom", |
| 12 | + }, |
| 13 | + "URL": "https://api.gateio.ws/api/v4/spot/tickers" |
| 14 | + }, |
| 15 | + |
| 16 | + "MEXC" : { |
| 17 | + "SYMBOLS": { |
| 18 | + "NOM":"nom", |
| 19 | + }, |
| 20 | + "URL": "https://api.mexc.com/api/v3/ticker/price" |
| 21 | + |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +def get_gate_price(symbols): |
| 28 | + if not symbols: |
| 29 | + return [] |
| 30 | + |
| 31 | + # Set a header for api |
| 32 | + HEADER = { |
| 33 | + "Accept": "application/json", |
| 34 | + } |
| 35 | + |
| 36 | + PARAMETERS = { |
| 37 | + "currency_pair": "NOM_USDT", |
| 38 | + } |
| 39 | + |
| 40 | + # URL to retrieve the price of all tokens |
| 41 | + url = CONSTANTS["GATE_IO"]["URL"] |
| 42 | + result = [] |
| 43 | + # Set request parameters |
| 44 | + |
| 45 | + |
| 46 | + try: |
| 47 | + # Make api call |
| 48 | + response = requests.get(url, params=PARAMETERS, headers=HEADER, timeout=3) |
| 49 | + # Retrieve prices from response |
| 50 | + for idx, symbol in enumerate(symbols): |
| 51 | + if response.status_code == 200: |
| 52 | + result.append(float(response.json()[idx]["last"])) |
| 53 | + else: |
| 54 | + result.append(0) |
| 55 | + # Return prices |
| 56 | + return result |
| 57 | + except Exception as e: |
| 58 | + return [0 for i in range(len(symbols))] |
| 59 | + |
| 60 | +def get_price_mecx(symbols): |
| 61 | + if not symbols: |
| 62 | + return [] |
| 63 | + |
| 64 | + # Set a header for api |
| 65 | + HEADER = { |
| 66 | + "Accept": "application/json", |
| 67 | + } |
| 68 | + |
| 69 | + # URL to retrieve the price of all tokens |
| 70 | + url = CONSTANTS["MEXC"]["URL"] |
| 71 | + |
| 72 | + result = [] |
| 73 | + # Set request parameters |
| 74 | + PARAMETERS = { |
| 75 | + "symbol": "NOMUSDT", |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + try: |
| 80 | + # Make api call |
| 81 | + response = requests.get(url, params=PARAMETERS, headers=HEADER, timeout=3) |
| 82 | + |
| 83 | + # Retrieve prices from response |
| 84 | + for idx, symbol in enumerate(symbols): |
| 85 | + if response.status_code == 200: |
| 86 | + result.append(float(response.json()["price"]) ) |
| 87 | + else: |
| 88 | + result.append(0) |
| 89 | + # Return prices |
| 90 | + return result |
| 91 | + except Exception as e: |
| 92 | + return [0 for i in range(len(symbols))] |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +def main(symbols): |
| 98 | + if len(symbols) == 0: |
| 99 | + return "" |
| 100 | + try: |
| 101 | + # Get price from bingx |
| 102 | + result_mecx = get_price_mecx(symbols) |
| 103 | + # Get price from gate |
| 104 | + result_gate = get_gate_price(symbols) |
| 105 | + |
| 106 | + # if lenghth of the results from all the sources is not same, then return 0 |
| 107 | + if not len(result_gate) == len(result_mecx): |
| 108 | + return ",".join("0" for i in range(len(symbols))) |
| 109 | + |
| 110 | + result = [] |
| 111 | + for item in zip(result_gate, result_mecx): |
| 112 | + different_sources_price = list(item) |
| 113 | + non_zero_sources = [price for price in different_sources_price if price != 0] |
| 114 | + if len(non_zero_sources) != 0: |
| 115 | + mean = sum(non_zero_sources) / len(non_zero_sources) |
| 116 | + result.append(mean) |
| 117 | + else: |
| 118 | + result.append(0) |
| 119 | + |
| 120 | + return ",".join(str(price) for price in result) |
| 121 | + except Exception as e: |
| 122 | + return ",".join("0" for i in range(len(symbols))) |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + try: |
| 126 | + print(main(sys.argv[1:])) |
| 127 | + except Exception as e: |
| 128 | + print(e, file=sys.stderr) |
| 129 | + sys.exit(1) |
0 commit comments