Bitcoin Kurs zu Numbers mit Apple Script

G

Gwadro

unregistriert
Thread Starter
Dabei seit
05.06.2012
Beiträge
1.585
Reaktionspunkte
860
Hallo zusammen,
Ich möchte über die API der Börse Bitstamp.net die Kurse abfragen und in eine Tabelle bei Numbers eintragen.
Die Funktion als solche habe ich im Netz gefunden https://discussions.apple.com/thread/7611695?start=0&tstart=0 und das funktioniert auch. Allerdings nutzt das Script die Cryptonator API

Okay, also bauen wir es für Bitstamp.net um. Leichter gesagt als getan.
Ich habe keine richtige Ahnung von dem ganzen, bin aber durchaus in der Lage mir etwas Wissen herauszulesen und mit try and error voran zu kommen.

Jetzt habe ich die API von Bitstamp erfolgreich abgefragt und das funktioniert auch:
Code:
set json to (do shell script "curl https://www.bitstamp.net/api/v2/ticker/etheur")

tell application "JSON Helper"
    set result to read JSON from json
    set price to |last| of result
end tell

set result_string to "$" & price & ""

Code:
validate_sheet()

set coins to {}

-- retrieve the list of coins from number
tell application "Numbers"
    tell table 1 of sheet 1 of document 1
        set cntRow to count row
        set cntCol to count column
       
        repeat with curRow from 1 to cntRow
            if curRow mod 2 is not 0 then
                set tmpVal to value of cell curRow of column 1
                set the end of the coins to {|name|:tmpVal, price:null}
            end if
        end repeat --curRow
       
    end tell --document
end tell -- application

-- get the value for each coin
repeat with coin in coins
    set BASE_URL to "https://www.bitstamp.net/api/v2/ticker/"
    set BASE_CURRENCY to "usd"
    set coinUrl to BASE_URL & (|name| of coin) & BASE_CURRENCY
   
   
    set json to (do shell script "curl " & coinUrl)
   
    tell application "JSON Helper"
        set result to fetch JSON from json
        set coinPrice to |last| of result
    end tell
   
    #    tell application "JSON Helper"
    #        set response to fetch JSON from coinUrl
    #    end tell
   
    #    if success of response is not false then
    #        set coinPrice to |last| of result
    #    else
    #        set coinPrice to "error"
    #    end if
   
    set price of coin to coinPrice
end repeat

-- update values in numbers
tell application "Numbers"
    tell table 1 of sheet 1 of document 1
        set cntRow to count row
        set cntCol to count column
       
        repeat with curRow from 1 to cntRow
            set coinName to value of cell curRow of column 1
           
            repeat with coin in coins
                if |name| of coin is equal to coinName then
                    if price of coin is not equal to "error" then
                        set value of cell curRow of column 2 to price of coin
                        set value of cell curRow of column 3 to (get current date)
                    end if
                end if
            end repeat
        end repeat --curRow      
    end tell --document
end tell -- application

return

on validate_sheet()
    tell application "Numbers"
        if not (exists document 1) then error number 1000
        -- validate sheet
        set curSheet to sheet 1 of document 1
        set curSheetName to curSheet's name
       
        if curSheetName is not "Overview_All" then
            error "Incorrect Sheet: " & curSheetName
        end if
       
        -- validate table
        set curTable to table 1 of curSheet
        set curTableName to curTable's name
       
        if curTableName is not "Coin Values (USD)" then
            error "Incorrect Table: " & curTableName
        end if
       
    end tell
end validate_sheet
Auf den Screenshot ist der aktuelle Stand der Dinge und ich komme nicht weiter. Siehe Error
Ich habe mal die numbers und die drei Scripte in der zip abgelegt. Einmal das Original , mein Bitstamp-Script und das einfache Script für Bitstamp. https://www.dropbox.com/s/uvmvmw4mnl06wet/Zip-Bitstamp.zip?dl=0

Wenn da mal drüber schauen würde der mehr Ahnung als ich davon hat.

EDIT:
Die App "JSON Helper for AppleScript" aus den App Store wird benötigt. https://itunes.apple.com/de/app/json-helper-for-applescript/id453114608?mt=12
 

Anhänge

  • Bildschirmfoto 2017-12-03 um 13.31.01.png
    Bildschirmfoto 2017-12-03 um 13.31.01.png
    187,7 KB · Aufrufe: 171
Zuletzt bearbeitet:
der fehler code sagt doch last von result konnte nicht gelesen werden, da würde ich jetzt sagen, das result könnte leer sein.
hast du die coinURL mal überprüft, die du da zusammen bastelst, ob die auch was zurück liefert und keinen fehler code produziert.
 
Hallo,
ja, das Ergebnis der URL sollte in Ordnung sein.
Code:
{"high": "397.16", "last": "395.49", "timestamp": "1512307982", "bid": "395.28", "vwap": "388.73", "volume": "7612.81588660", "low": "380.00", "ask": "395.54", "open": "384.98"}

Auch die Übergabe an Numbers funktioniert, wenn ich nur eine Zeile in der Tabelle habe.
 
Okay, ich habs. Ich weiß nur noch nicht warum es geht und es funktioniert auch nicht bei jeden Durchlauf des Script.
Code:
validate_sheet()

set coins to {}

-- retrieve the list of coins from number
tell application "Numbers"
    tell table 1 of sheet 1 of document 1
        set cntRow to count row
        set cntCol to count column
        
        repeat with curRow from 1 to cntRow
            if curRow mod 2 is not 0 then
                set tmpVal to value of cell curRow of column 1
                set the end of the coins to {|name|:tmpVal, price:null}
            end if
        end repeat --curRow
        
    end tell --document
end tell -- application

-- get the value for each coin
repeat with coin in coins
    set BASE_URL to "https://www.bitstamp.net/api/v2/ticker/"
    set BASE_CURRENCY to "eur"
    set coinUrl to BASE_URL & (|name| of coin) & BASE_CURRENCY
    
    
    set json to (do shell script "curl " & coinUrl)
    
    tell application "JSON Helper"
        set result to read JSON from json
        set coinPrice to |last| of result
    end tell
    
    #    tell application "JSON Helper"
    #        set response to fetch JSON from coinUrl
    #    end tell
    
    #    if success of response is not false then
    #        set coinPrice to |last| of result
    #    else
    #        set coinPrice to "error"
    #    end if
    
    set price of coin to coinPrice
end repeat

-- update values in numbers
tell application "Numbers"
    tell table 1 of sheet 1 of document 1
        set cntRow to count row
        set cntCol to count column
        
        repeat with curRow from 1 to cntRow
            set coinName to value of cell curRow of column 1
            
            repeat with coin in coins
                if |name| of coin is equal to coinName then
                    if price of coin is not equal to "error" then
                        set value of cell curRow of column 2 to price of coin
                        set value of cell curRow of column 3 to (get current date)
                    end if
                end if
            end repeat
        end repeat --curRow       
    end tell --document
end tell -- application

return

on validate_sheet()
    tell application "Numbers"
        if not (exists document 1) then error number 1000
        -- validate sheet
        set curSheet to sheet 1 of document 1
        set curSheetName to curSheet's name
        
        if curSheetName is not "Overview_All" then
            error "Incorrect Sheet: " & curSheetName
        end if
        
        -- validate table
        set curTable to table 1 of curSheet
        set curTableName to curTable's name
        
        if curTableName is not "Coin Values (EUR)" then
            error "Incorrect Table: " & curTableName
        end if
        
    end tell
end validate_sheet
Hier sind USD zu EUR geändert.
 
wie gesagt, du solltest result mal checken bevor du last drauf los lässt.
wahrscheinlich kriegst du keine von dir erwartete antwort vom server wenn es nicht läuft.
 
Hallo,
ja soweit bin ich mittlerweile. Die Abfrage per Script ist sehr unzuverlässig. Gebe ich die URL im Browser ein, ist das Ergebnis zuverlässig vorhanden. Anscheinend ist das eine Überlastung. Grundsätzlich funktioniert es aber wie erwartet. Siehe Screenshot :)
Zwischenzeitlich habe ich das Script drei mal jeweils für EUR, USD und BTC. Das muss ich mal in ein Script zusammenfassen. Dafür dass ich eigentlich nicht weiß was ich hier tue funktioniert es hervorragend :D
 

Anhänge

  • Bildschirmfoto 2017-12-04 um 12.53.11.png
    Bildschirmfoto 2017-12-04 um 12.53.11.png
    256,6 KB · Aufrufe: 163
  • Bildschirmfoto 2017-12-04 um 12.59.07.png
    Bildschirmfoto 2017-12-04 um 12.59.07.png
    199,7 KB · Aufrufe: 171
Zurück
Oben Unten