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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#!/usr/bin/env python
#Copyright (C) 2024 Michael Kantor
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <https://www.gnu.org/licenses/>.
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)
|