Skip to content

Instantly share code, notes, and snippets.

@mlagerberg
Last active September 10, 2017 22:56
Show Gist options
  • Save mlagerberg/ce0b28ef337aec01d1ea to your computer and use it in GitHub Desktop.
Save mlagerberg/ce0b28ef337aec01d1ea to your computer and use it in GitHub Desktop.
[Bitcoin sparkline in the terminal]
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# btcspark.py
#
# Shows the current value of Bitcoin in Euro, as given by bitcoinaverage.com,
# along with a sparkline of the last 24 hours.
#
# Requires
from __future__ import print_function, unicode_literals
import requests, csv, StringIO, os
# TODO edit this line to point to the Spark script:
SPARK = './spark'
SPARK_LENGTH = 32
HISTORY_URL = 'https://api.bitcoinaverage.com/history/{0}/per_minute_24h_global_average_sliding_window.csv'
# Supports 'USD', 'EUR' and 'both'.
# The latter will give you a single (USD) sparkline, but both averages.
def spark(currency = 'USD'):
r = requests.get(HISTORY_URL.format(currency if currency != 'both' else 'USD'))
f = StringIO.StringIO(r.text)
currindex = 5 if currency == 'EUR' else 2
try:
reader = csv.reader(f)
args = ""
rows = list(reader)
totalrows = len(rows)
chunk = 1 if totalrows < SPARK_LENGTH else totalrows / SPARK_LENGTH
for i, row in enumerate(rows):
# using != 1 instead of 0 to skip the first row (which contains column headers)
if i % chunk != 1:
continue
# Get value
val = row[currindex]
# Append to arguments
args = args + " " + val
os.system(SPARK + args)
usd = float(rows[totalrows - 1][2])
eur = float(rows[totalrows - 1][5])
output = ''
if currency == 'USD' or currency == 'both':
output = output + "{0:.2f} (USD) ".format(usd)
if currency == 'EUR' or currency == 'both':
output = output + "{0:.2f} (EUR) ".format(eur)
print(output)
finally:
f.close()
spark('both')
@mlagerberg
Copy link
Author

Shows the current value of Bitcoin in EUR or USD, as given by bitcoinaverage.com, along with a sparkline of the last 24 hours.

Requires this Spark script by @holman

Sample output:

    ██▆▆▅▅▄▃▃▅▅▆█▆▆▄▅▆▅▅▅▆▄▅▃▃▁▃▃▄▄▅
    585.02 (USD) 438.21 (EUR)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment