aboutsummaryrefslogtreecommitdiff
path: root/cusmodules/cuswidgets/ticker_gecko.py
diff options
context:
space:
mode:
authorMichael Kantor <michael@mikekantor.xyz>2024-01-09 22:02:20 -0500
committerMichael Kantor <michael@mikekantor.xyz>2024-01-09 22:02:20 -0500
commit270f3e6d77cc720b1b3f3c529b1b6aa5c63578a2 (patch)
treec65da331bc5306f820918f3a54ef51ac2ab746d9 /cusmodules/cuswidgets/ticker_gecko.py
Recreated git repo as screenshots were too large.
Diffstat (limited to 'cusmodules/cuswidgets/ticker_gecko.py')
-rwxr-xr-xcusmodules/cuswidgets/ticker_gecko.py126
1 files changed, 126 insertions, 0 deletions
diff --git a/cusmodules/cuswidgets/ticker_gecko.py b/cusmodules/cuswidgets/ticker_gecko.py
new file mode 100755
index 0000000..1b7d1ee
--- /dev/null
+++ b/cusmodules/cuswidgets/ticker_gecko.py
@@ -0,0 +1,126 @@
+#!/usr/bin/env python
+import os
+import sys
+import json
+import time
+import requests
+import subprocess
+from datetime import datetime
+from threading import Thread
+
+def match(currency):
+ currencies = {
+ "BTC": "bitcoin",
+ "ETH": "ethereum",
+ "XRP": "xrp",
+ "LTC": "litecoin",
+ "BCH": "bitcoin-cash",
+ "XMR": "monero",
+ "DOGE": "dogecoin"
+ }
+
+ return currencies[currency]
+
+def Gecko(currs):
+ #prices = ''
+ prices = {}
+
+ for i in currs:
+ curr = match(i)
+
+ source = requests.get(f'https://api.coingecko.com/api/v3/coins/{curr}/').text
+
+ data = json.loads(source)
+
+ price = str(data['market_data']['current_price']['usd'])
+
+ try:
+ if len(price[:price.index('.')]) > 3:
+ price = price[:2] + ',' + price[2:]
+
+ except:
+ price = price[:2] + ',' + price[2:]
+
+ price = '$' + price
+
+ price = {i: price}
+
+ prices.update(price)
+
+ with open('/tmp/crprices.json', 'w') as f:
+ f.write(str(prices).replace("'", '"'))
+
+def main():
+ global crpos
+ home = os.path.expanduser('~')
+ currs = []
+ priced = ''
+
+ try:
+ crpos += 1
+
+ except:
+ crpos = 0
+
+ try:
+ with open('/tmp/crprices.json', 'r') as f:
+ prices_str = f.read()
+ prices = json.loads(prices_str)
+
+ for i in prices:
+ currs += i
+
+ for i in prices:
+ if currs[len(currs) - 1] == i:
+ price = f'{i}: {prices[i]} |'
+
+ else:
+ price = f'{i}: {prices[i]} | '
+
+ priced += price
+
+ leng = len(priced)
+
+ #print(crpos >= leng)
+ if crpos >= leng:
+ crpos = 0
+
+ crprices = f"{priced[crpos:]} {priced[:crpos]}"
+
+ #print(priced)
+ #print(crpos)
+ #print(leng)
+ #print(crprices)
+ #print(crprices[:15], '\n')
+ return crprices[:15]
+
+ except Exception as e:
+ with open(f'{home}/.local/share/ticker_gecko/ticker_gecko.log', 'w') as f:
+ f.write(e)
+
+ return f'ERROR! Check "{home}/.local/share/ticker_gecko/ticker_gecko.log".'
+
+if __name__ == '__main__':
+ if sys.argv[1] == '--help' or sys.argv[1] == '-help' or sys.argv[1] == '--h' or sys.argv[1] == '-h':
+ print('Usage:\n\tStart server by running command: "/path/to/ticker_gecko.py".\n\tPut abbreviations of crypto in arguments: "/path/to/ticker_gecko.py BTC LTC" (will get price for bitcoin and litecoin)\n\tAdd main function to GenPollText widget in config.py: "\n\t\timport ticker_gecko\n\t\twidget.GenPollText(\n\t\t\tfunc=ticker_gecko.main,\n\t\t\tupdate_interval= 0.5),"')
+ sys.exit(0)
+
+ currs = []
+ argc = len(sys.argv)
+ for i in range(argc):
+ if i == 0:
+ pass
+
+ else:
+ currs.append(sys.argv[i])
+
+ while True:
+ Gecko(currs)
+
+ with open('/tmp/crprices.json', 'r') as f:
+ leng = len(f.read())
+
+ repeat_time = leng - ((len(currs) - 1) * 5)
+
+ time.sleep(repeat_time)
+