Created
January 20, 2021 09:47
-
-
Save 3dprogramin/26c232cb03661a39c12f57330363bcbe to your computer and use it in GitHub Desktop.
Script for gathering currency values every X minutes, and saving them to text files for conky usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import requests as req | |
from lxml import html | |
from time import gmtime, strftime | |
import json | |
OUTPUT_FILE_USD = '/home/icebox/.config/conky/exchange/usd.txt' | |
OUTPUT_FILE_EURO = '/home/icebox/.config/conky/exchange/euro.txt' | |
OUTPUT_FILE_BTC = '/home/icebox/.config/conky/exchange/btc.txt' | |
OUTPUT_FILE_ETH = '/home/icebox/.config/conky/exchange/eth.txt' | |
UPDATE_EVERY = 60 # x minutes | |
def update(): | |
print ('updating ...') | |
# euro / usd | |
r = req.get('http://www.cursbnr.ro/', headers={'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0'}) | |
tree = html.fromstring(r.text) | |
euro = tree.xpath('//div[@class="currency-value"]/div[@class="value"]')[0].text | |
usd = tree.xpath('//div[@class="currency-value"]/div[@class="value"]')[1].text | |
# btc | |
r = req.get('https://blockchain.info/ticker') | |
js = json.loads(r.text) | |
btc = '1 BTC = {} USD'.format(int(js['USD']['last'])) | |
# eth | |
r = req.get('https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD') | |
js = json.loads(r.text) | |
eth = '1 ETH = {} USD'.format(int(js['USD'])) | |
# persist | |
with open(OUTPUT_FILE_USD, 'w') as f: | |
f.write(usd) | |
with open(OUTPUT_FILE_EURO, 'w') as f: | |
f.write(euro) | |
with open(OUTPUT_FILE_BTC, 'w') as f: | |
f.write(btc) | |
with open(OUTPUT_FILE_ETH, 'w') as f: | |
f.write(eth) | |
# print in UI | |
print (strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())) | |
print (usd) | |
print (euro) | |
print (btc) | |
print (eth) | |
print ('---------------------------') | |
def main(): | |
from time import sleep | |
while True: | |
update() | |
sleep(UPDATE_EVERY * 60) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment