Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds offline caching functionality #85

Merged
merged 1 commit into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ <h3>Additional Options</h3>
<label for="groupcheckbox">Group crypto elements under one button</label>
</div>

<div id="cacheToggle">
<input id="cachecheckbox" type="checkbox">
<label for="cachecheckbox">Cache results for offline viewing</label>
</div>

</form>

<br>
Expand Down
3 changes: 2 additions & 1 deletion js/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ function generateJSON(template, cb) {
percent: userData.userPercentageModifer,
output_type: userData.formatSelector,
apiSelector: userData.apiSelector.dataset.apitype,
extraOptions: extraOptions
extraOptions: extraOptions,
offline_cache: userData.cacheBool
};

coin.BTTTriggerConfig.BTTTouchBarAppleScriptString = Mustache.render(template, data);
Expand Down
3 changes: 2 additions & 1 deletion js/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ function getSelectedValues() {
formatSelector: document.querySelector('input[name="variance-type"]:checked').dataset.variance,
dateTimeSelector: document.getElementById('flatpicker-output'),
dateTimeSelectorString: document.getElementById('flatpicker-output-string'),
userPercentageModifer: parseFloat(document.getElementById('user-percentage').value / 100)
userPercentageModifer: parseFloat(document.getElementById('user-percentage').value / 100),
cacheBool: document.getElementById('cachecheckbox').checked
}
}

Expand Down
1 change: 1 addition & 0 deletions templates/BTTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function widget() {
"BTTTouchBarButtonColor": "243.776725, 130.266312, 8.181293, 255.000000",
"BTTTouchBarAlwaysShowButton": "0",
"BTTTouchBarAppleScriptString": "**RESPONSE****OUTPUT****ERROR**",
"BTTTouchBarColorRegex" : "CACHED",
"BTTTouchBarAlternateBackgroundColor": "109.650002, 109.650002, 109.650002, 255.000000",
"BTTTouchBarScriptUpdateInterval": 60
}
Expand Down
42 changes: 30 additions & 12 deletions templates/crypto_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
output_type = "{{output_type}}" if "{{output_type}}"[0] != "{" else "no"
api_type = "{{apiSelector}}" if "{{apiSelector}}"[0] != "{" else "live"
extraOptions = "{{{extraOptions}}}" if "{{{extraOptions}}}"[0] != "{" else "&limit=1&aggregate=1&toTs=1514376000"
offline_cache = "{{offline_cache}}" if "{{offline_cache}}"[0] != "{" else "false"

try:
if (api_type == 'live'):
if (api_type == "live"):

url = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms={}&tsyms={}".format(coin_ticker, fiat_ticker)

Expand Down Expand Up @@ -40,23 +41,30 @@
trend = "▼"

if (output_type is "no"):
print(fiat_symbol + current)
output = fiat_symbol + current
elif (output_type is "simple"):
print(fiat_symbol + current + " " + trend)
output = fiat_symbol + current + " " + trend
elif (output_type is "mktcap"):
print(fiat_symbol + current + " (" + fiat_symbol + mktcap + ")")
output = fiat_symbol + current + " (" + fiat_symbol + mktcap + ")"
elif (output_type is "absolute"):
print(fiat_symbol + current + " (L: " + fiat_symbol + low + " H: " + fiat_symbol + high + ")")
output = fiat_symbol + current + " (L: " + fiat_symbol + low + " H: " + fiat_symbol + high + ")"
elif (output_type is "relative"):
print(fiat_symbol + current + " (L: -" + fiat_symbol + str(raw_current - raw_low) + " H: +" + fiat_symbol + str(raw_high - raw_current) + ")")
output = fiat_symbol + current + " (L: -" + fiat_symbol + str(raw_current - raw_low) + " H: +" + fiat_symbol + str(raw_high - raw_current) + ")"
elif (output_type is "current-percentage"):
print(fiat_symbol + current + " (" + str(round (((raw_current - raw_opening) / raw_current) * 100)) + "%)")
output = fiat_symbol + current + " (" + str(round (((raw_current - raw_opening) / raw_current) * 100)) + "%)"
elif (output_type is "range-percentage"):
print(fiat_symbol + current + " (L: -" + str(round (((raw_current - raw_low) / raw_current) * 100)) + "% H: +" + str(round (((raw_high - raw_current) / raw_current) * 100)) + "%)")
output = fiat_symbol + current + " (L: -" + str(round (((raw_current - raw_low) / raw_current) * 100)) + "% H: +" + str(round (((raw_high - raw_current) / raw_current) * 100)) + "%)"
elif (output_type is "user-percentage"):
print(fiat_symbol + current + " (L: " + fiat_symbol + str(raw_current - (raw_current * mod_percent)) + " H: " + fiat_symbol + str(raw_current + (raw_current * mod_percent)) + ")")
output = fiat_symbol + current + " (L: " + fiat_symbol + str(raw_current - (raw_current * mod_percent)) + " H: " + fiat_symbol + str(raw_current + (raw_current * mod_percent)) + ")"

elif (api_type == 'historical'):
if (offline_cache is "true"):
tmp_file = open("/tmp/"+coin_ticker+"-"+fiat_ticker+"-"+output_type+".txt", "w")
tmp_file.write(output)
tmp_file.close()

print(output)

elif (api_type == "historical"):
url = "https://min-api.cryptocompare.com/data/histohour?fsym={}&tsym={}" + extraOptions
url = url.format(coin_ticker, fiat_ticker)

Expand All @@ -66,10 +74,20 @@
raw_high = float(obj["Data"][1]["high"])
high = num_format.format(raw_high)

print(fiat_symbol + high)
output = fiat_symbol + high

if (offline_cache is "true"):
tmp_file = open("/tmp/"+coin_ticker+"-"+fiat_ticker+"-"+output_type+".txt", "w")
tmp_file.write(output)
tmp_file.close()

print(output)
except urllib2.URLError, e:
print('Unable to get data from API: %s' % e)
try:
tmp_file = open("/tmp/"+coin_ticker+"-"+fiat_ticker+"-"+output_type+".txt", "r")
print 'CACHED ' + tmp_file.read()
except IOError, e:
print('Unable to get data from API & no cache available')
except ValueError, e:
print('There was an error formatting the output: %s' % e)
# Please submit any issues https://github.com/chrislennon/Crypto-Touchbar-App/issues with the above script