irccex- fantasy cryptocurrency exchange for irc |
git clone git://git.acid.vegas/irccex.git |
Log | Files | Refs | Archive | README | LICENSE |
functions.py (5230B)
1 #!/usr/bin/env python 2 # IRC Cryptocurrency Exchange (IRCCEX) - Developed by acidvegas in Python (https://git.acid.vegas/irccex) 3 # functions.py 4 5 import calendar 6 import datetime 7 import random 8 import time 9 10 import constants 11 12 def check_pair(from_symbol, to_symbol): 13 if from_symbol == 'USD': 14 return True if to_symbol in ('BTC','ETH','LTC') else False 15 elif to_symbol == 'USD': 16 return True if from_symbol in ('BTC','ETH','LTC') else False 17 elif from_symbol == to_symbol: 18 return False 19 elif from_symbol in ('BTC','ETH') or to_symbol in ('BTC','ETH'): 20 return True 21 else: 22 return False 23 24 def clean_float(value): 25 if value > 24.99: 26 return int(value) 27 elif 'e-' in str(value): 28 return '{0:.8f}'.format(float(value)) 29 else: 30 return '{0:.8g}'.format(float(value)) 31 32 def clean_value(value): 33 value = float(value) 34 if value < 0.01: 35 return '${0:,.8f}'.format(value) 36 elif value < 24.99: 37 return '${0:,.2f}'.format(value) 38 else: 39 return '${:,}'.format(int(value)) 40 41 def coin_info(data): 42 sep = color('|', constants.grey) 43 sep2 = color('/', constants.grey) 44 rank = color(data['rank'], constants.pink) 45 name = '{0} ({1})'.format(color(data['name'], constants.white), data['symbol']) 46 value = clean_value(data['price']) 47 perc_1h = color('{:,.2f}%'.format(data['percent']['1h']), percent_color(data['percent']['1h'])) 48 perc_24h = color('{:,.2f}%'.format(data['percent']['24h']), percent_color(data['percent']['24h'])) 49 perc_7d = color('{:,.2f}%'.format(data['percent']['7d']), percent_color(data['percent']['7d'])) 50 percent = sep2.join((perc_1h,perc_24h,perc_7d)) 51 volume = '{0} {1}'.format(color('Volume:', constants.white), '${:,}'.format(data['volume'])) 52 cap = '{0} {1}'.format(color('Market Cap:', constants.white), '${:,}'.format(data['market_cap'])) 53 return f'[{rank}] {name} {sep} {value} ({percent}) {sep} {volume} {sep} {cap}' 54 55 def coin_matrix(data): # very retarded way of calculating the longest strings per-column 56 results = {'symbol':list(),'value':list(),'perc_1h':list(),'perc_24h':list(),'perc_7d':list(),'volume':list(),'cap':list()} 57 for item in data: 58 results['symbol'].append(item['symbol']) 59 results['value'].append(clean_value(item['price'])) 60 for perc in ('1h','24h','7d'): 61 results['perc_' + perc].append('{:,.2f}%'.format(item['percent'][perc])) 62 results['volume'].append('${:,}'.format(item['volume'])) 63 results['cap'].append('${:,}'.format(item['market_cap'])) 64 for item in results: 65 results[item] = len(max(results[item], key=len)) 66 if results['symbol'] < len('Symbol'): 67 results['symbol'] = len('Symbol') 68 if results['value'] < len('Value'): 69 results['value'] = len('Value') 70 if results['volume'] < len('Volume'): 71 results['volume'] = len('Volume') 72 if results['cap'] < len('Market Cap'): 73 results['cap'] = len('Market Cap') 74 return results 75 76 def coin_table(data): 77 matrix = coin_matrix(data) 78 header = color(' {0} {1} {2} {3} {4} {5} {6} '.format('Symbol'.center(matrix['symbol']), 'Value'.center(matrix['value']), '1H'.center(matrix['perc_1h']), '24H'.center(matrix['perc_24h']), '7D'.center(matrix['perc_7d']), 'Volume'.center(matrix['volume']), 'Market Cap'.center(matrix['cap'])), constants.black, constants.light_grey) 79 lines = [header,] 80 for item in data: 81 symbol = item['symbol'].ljust(matrix['symbol']) 82 value = clean_value(item['price']).rjust(matrix['value']) 83 perc_1h = color('{:,.2f}%'.format(item['percent']['1h']).rjust(matrix['perc_1h']), percent_color(item['percent']['1h'])) 84 perc_24h = color('{:,.2f}%'.format(item['percent']['24h']).rjust(matrix['perc_24h']), percent_color(item['percent']['24h'])) 85 perc_7d = color('{:,.2f}%'.format(item['percent']['7d']).rjust(matrix['perc_7d']), percent_color(item['percent']['7d'])) 86 volume = '${:,}'.format(item['volume']).rjust(matrix['volume']) 87 cap = '${:,}'.format(item['market_cap']).rjust(matrix['cap']) 88 lines.append(' {0} | {1} | {2} {3} {4} | {5} | {6} '.format(symbol,value,perc_1h,perc_24h,perc_7d,volume,cap)) 89 return lines 90 91 def color(msg, foreground, background=None): 92 if background: 93 return f'\x03{foreground},{background}{msg}{constants.reset}' 94 else: 95 return f'\x03{foreground}{msg}{constants.reset}' 96 97 def days(date_obj): 98 return (date_obj-datetime.date.today()).days 99 100 def fee(amount, percent): 101 return amount-(amount*percent) 102 103 def is_amount(amount, star=True): 104 if amount[:1] == '$': 105 amount = amount[1:] 106 if amount.isdigit(): 107 return True if int(amount) > 0.0 else False 108 else: 109 try: 110 float(amount) 111 return True if float(amount) > 0.0 else False 112 except ValueError: 113 return True if star and amount == '*' else False 114 115 def month_days(): 116 now = datetime.datetime.now() 117 return calendar.monthrange(now.year, now.month)[1] 118 119 def percent_color(percent): 120 percent = float(percent) 121 if percent == 0.0: 122 return constants.grey 123 elif percent < 0.0: 124 return constants.brown if percent > -10.0 else constants.red 125 else: 126 return constants.green if percent < 10.0 else constants.light_green 127 128 def random_int(min, max): 129 return random.randint(min, max) 130 131 def uptime(start): 132 uptime = datetime.datetime(1,1,1) + datetime.timedelta(seconds=time.time() - start) 133 return f'{uptime.day-1} Days, {uptime.hour} Hours, {uptime.minute} Minutes, {uptime.second} Seconds'