question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Script return nothing w/ direct import and not displayed properly w/ JSON import

See original GitHub issue

I’m on MacOS High Sierra V10.13.6. / BTT v2.627

When I do a direct import to BTT, no price is displayed in the BTC indicator on the touch bar. If i run the script imported from BTT, it returns “Error/Nothing was returned”.

When I do JSON import, raw script code is displayed in the BTC indicator. If I run the script imported from BTT, I get the right price returned.

It happens no matter the advanced options selected.

Script from direct import :


#!_usr_bin_python
# -*- coding: utf-8 -*-
import urllib2,json,sys

coin_ticker = "BTC" if "BTC"[0] != "{" else "BTC"
fiat_ticker = "USD" if "USD"[0] != "{" else "USD"
fiat_symbol = "$" if "$"[0] != "{" else  "$"
num_format = "{}" if "{}"[2:8] != "format" else  "{}"
mod_percent = float("0") if "0"[0] != "{" else float(0)
output_type = "no" if "no"[0] != "{" else  "mktcap"
api_type = "live" if "live"[0] != "{" else  "live"
extraOptions = "False" if "False"[0] != "{" else  "&limit=1&aggregate=1&toTs=1514376000"
offline_cache = "false" if "false"[0] != "{" else "false"
percentageRound = int("0") if "0"[0] != "{" else int(0)
literalRound = int("0") if "0"[0] != "{" else int(0)

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

        url = "https:__min-api.cryptocompare.com_data_pricemultifull?fsyms={}&tsyms={}".format(coin_ticker, fiat_ticker)

        data = urllib2.urlopen(url)
        obj=json.load(data)

        raw_current = float(obj["RAW"][coin_ticker][fiat_ticker]["PRICE"])
        raw_opening = float(obj["RAW"][coin_ticker][fiat_ticker]["OPEN24HOUR"])
        raw_high = float(obj["RAW"][coin_ticker][fiat_ticker]["HIGHDAY"])
        raw_low = float(obj["RAW"][coin_ticker][fiat_ticker]["LOWDAY"])
        raw_mktcap = float(obj["RAW"][coin_ticker][fiat_ticker]["MKTCAP"])
        current = num_format.format(raw_current)
        opening = num_format.format(raw_opening)
        high = num_format.format(raw_high)
        low = num_format.format(raw_low)

        if (raw_mktcap > 1000000):
            mktcap = str("{:,."+str(literalRound)+"f}").format(raw_mktcap _ 1000000) + " M"
        else:
            mktcap = str("{:,."+str(literalRound)+"f}").format(raw_mktcap)

        if (raw_current > raw_opening):
            trend = "▲"
        else:
            trend = "▼"

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

        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)

        data = urllib2.urlopen(url)
        obj=json.load(data)

        raw_high = float(obj["Data"][1]["high"])
        high = num_format.format(raw_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:
    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

Script from JSON import :

#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib2,json,sys

coin_ticker = "BTC" if "BTC"[0] != "{" else "BTC"
fiat_ticker = "USD" if "USD"[0] != "{" else "USD"
fiat_symbol = "$" if "$"[0] != "{" else  "$"
num_format = "{}" if "{}"[2:8] != "format" else  "{}"
mod_percent = float("0") if "0"[0] != "{" else float(0)
output_type = "no" if "no"[0] != "{" else  "mktcap"
api_type = "live" if "live"[0] != "{" else  "live"
extraOptions = "False" if "False"[0] != "{" else  "&limit=1&aggregate=1&toTs=1514376000"
offline_cache = "false" if "false"[0] != "{" else "false"
percentageRound = int("0") if "0"[0] != "{" else int(0)
literalRound = int("0") if "0"[0] != "{" else int(0)

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

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

        data = urllib2.urlopen(url)
        obj=json.load(data)

        raw_current = float(obj["RAW"][coin_ticker][fiat_ticker]["PRICE"])
        raw_opening = float(obj["RAW"][coin_ticker][fiat_ticker]["OPEN24HOUR"])
        raw_high = float(obj["RAW"][coin_ticker][fiat_ticker]["HIGHDAY"])
        raw_low = float(obj["RAW"][coin_ticker][fiat_ticker]["LOWDAY"])
        raw_mktcap = float(obj["RAW"][coin_ticker][fiat_ticker]["MKTCAP"])
        current = num_format.format(raw_current)
        opening = num_format.format(raw_opening)
        high = num_format.format(raw_high)
        low = num_format.format(raw_low)

        if (raw_mktcap > 1000000):
            mktcap = str("{:,."+str(literalRound)+"f}").format(raw_mktcap / 1000000) + " M"
        else:
            mktcap = str("{:,."+str(literalRound)+"f}").format(raw_mktcap)

        if (raw_current > raw_opening):
            trend = "▲"
        else:
            trend = "▼"

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

        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)

        data = urllib2.urlopen(url)
        obj=json.load(data)

        raw_high = float(obj["Data"][1]["high"])
        high = num_format.format(raw_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:
    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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
jragosacommented, Sep 5, 2018

Works for me with BTT 2.631!

1reaction
fifafucommented, Sep 5, 2018

Should work again in latest alpha!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Importing JSON file in TypeScript - Stack Overflow
With TypeScript 2.9.+ you can simply import JSON files with benefits like typesafety and intellisense by doing this: import colorsJson from ...
Read more >
How to Import JSON file as a Module - Bits and Pieces
Imagine, a developer intends to import a JSON file across domains as a module. As you know, a JSON file does not execute...
Read more >
Python Read JSON File – How to Load ... - freeCodeCamp
When we work with JSON files in Python, we can't just read them and use the data in our program directly. This is...
Read more >
Reading and Writing JSON to a File in Python - Stack Abuse
In this tutorial, you'll learn how to parse, read, and write JSON to files in Python through examples, using load(), loads(), dump() and ......
Read more >
ImportJSON - Fast Fedora | Trevor Lohrbeer
ImportJSON imports data from public JSON APIs into Google Spreadsheets. ... The function returns a two-dimensional array containing the data, with the first ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found