Is there a way to get the Bitcoin price in AHK
I find some API, but dont have any clue to do that
https://www.coindesk.com/api/
https://blockchain.info/api/exchange_rates_api
Monitor Bitcoin Price
Re: Monitor Bitcoin Price
Interesting to have a real graphic update onscreen , i have no clue how it varies in a day but i will check it out today
example gives the response and extract the value:
example gives the response and extract the value:
Code: Select all
url=https://api.coindesk.com/v1/bpi/currentprice/USD.json
UrlDownloadToVar(URL, response)
RegExMatch( response,"U)float.*(\d+.*)\}" ,m)
msgbox % response
msgbox % m1 "US dollar"
UrlDownloadToVar(URL, ByRef aResponse="") {
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", URL)
WebRequest.Send()
aResponse := WebRequest.ResponseText
WebRequest := ""
}
-
- Posts: 44
- Joined: 18 Aug 2016, 22:56
Re: Monitor Bitcoin Price
another way.. download the json, convert to ahk object.
Code: Select all
bc := ParseJson(getPrice())
updated := bc.time.updated
USD := bc.bpi.USD.rate
MsgBox % "Updated On : " updated "`nPrice(USD) : $" USD
;webrequest for json
getPrice(){
bCoinURL := "https://api.coindesk.com/v1/bpi/currentprice.json"
wr :=ComObjCreate("WinHttp.WinHttpRequest.5.1")
wr.Open("GET",bCoinURL)
wr.Send()
return wr.ResponseText
}
;Originally by Getfree,
ParseJson(jsonStr)
{
SC := ComObjCreate("ScriptControl")
SC.Language := "JScript"
ComObjError(false)
jsCode =
(
function arrangeForAhkTraversing(obj){
if(obj instanceof Array){
for(var i=0 ; i<obj.length ; ++i)
obj[i] = arrangeForAhkTraversing(obj[i]) ;
return ['array',obj] ;
}else if(obj instanceof Object){
var keys = [], values = [] ;
for(var key in obj){
keys.push(key) ;
values.push(arrangeForAhkTraversing(obj[key])) ;
}
return ['object',[keys,values]] ;
}else
return [typeof obj,obj] ;
}
)
SC.ExecuteStatement(jsCode "; obj=" jsonStr)
return convertJScriptObjToAhks( SC.Eval("arrangeForAhkTraversing(obj)") )
}
; Used by ParseJson()
convertJScriptObjToAhks(jsObj)
{
if(jsObj[0]="object"){
obj := {}, keys := jsObj[1][0], values := jsObj[1][1]
loop % keys.length
obj[keys[A_INDEX-1]] := convertJScriptObjToAhks( values[A_INDEX-1] )
return obj
}else if(jsObj[0]="array"){
array := []
loop % jsObj[1].length
array.insert(convertJScriptObjToAhks( jsObj[1][A_INDEX-1] ))
return array
}else
return jsObj[1]
}
Re: Monitor Bitcoin Price
thank you noname and imustbeamoron
The first one works fine
and the second one get some error
Line 22 SC := ComObjCreate("ScriptControl") <<<<<<<<<<Class not registered
In addition
how can I extract the JPY price from the follow ? https://api.coindesk.com/v1/bpi/currentprice/JPY.json
and is there a way create error level when not able download the information?
I was trying to understand "U)float.*(\d+.*)\}" , does this manual have all the descriptions? https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
The first one works fine
and the second one get some error
Line 22 SC := ComObjCreate("ScriptControl") <<<<<<<<<<Class not registered
In addition
how can I extract the JPY price from the follow ? https://api.coindesk.com/v1/bpi/currentprice/JPY.json
and is there a way create error level when not able download the information?
I was trying to understand "U)float.*(\d+.*)\}" , does this manual have all the descriptions? https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
The real time price is good enough for my purposenoname wrote:Interesting to have a real graphic update onscreen
-
- Posts: 4344
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Monitor Bitcoin Price
Hi, holahapi
COM object "ScriptControl" works on 32 bit processes only. It won't work if you use 64 bit AHK. But there are some another ways to use JScript for parsing JSON. I used one of them in my class to get and convert JSON string to AHK object.
COM object "ScriptControl" works on 32 bit processes only. It won't work if you use 64 bit AHK. But there are some another ways to use JScript for parsing JSON. I used one of them in my class to get and convert JSON string to AHK object.
Code: Select all
sJSON := JSON.GetFromUrl("https://api.coindesk.com/v1/bpi/currentprice/JPY.json")
oJSON := JSON.Parse(sJSON)
MsgBox, % "JPY: " . oJSON.bpi.JPY.rate
class JSON
{
static JS := JSON._GetJScripObject()
Parse(JsonString) {
try oJSON := this.JS.("(" JsonString ")")
catch {
MsgBox, Wrong JsonString!
Return
}
Return this._CreateObject(oJSON)
}
GetFromUrl(url) {
XmlHttp := ComObjCreate("Microsoft.XmlHttp")
XmlHttp.Open("GET", url, false)
XmlHttp.Send()
Return XmlHttp.ResponseText
}
_GetJScripObject() {
VarSetCapacity(tmpFile, (MAX_PATH := 260) << !!A_IsUnicode, 0)
DllCall("GetTempFileName", Str, A_Temp, Str, "AHK", UInt, 0, Str, tmpFile)
FileAppend,
(
<component>
<public><method name='eval'/></public>
<script language='JScript'></script>
</component>
), % tmpFile
JS := ObjBindMethod( ComObjGet("script:" . tmpFile), "eval" )
FileDelete, % tmpFile
JSON._AddMethods(JS)
Return JS
}
_AddMethods(ByRef JS) {
JScript =
(
Object.prototype.GetKeys = function () {
var keys = []
for (var k in this)
if (this.hasOwnProperty(k))
keys.push(k)
return keys
}
Object.prototype.IsArray = function () {
var toStandardString = {}.toString
return toStandardString.call(this) == '[object Array]'
}
)
JS.("delete ActiveXObject; delete GetObject;")
JS.(JScript)
}
_CreateObject(ObjJS) {
res := ObjJS.IsArray()
if (res = "")
Return ObjJS
else if (res = -1) {
obj := []
Loop % ObjJS.length
obj[A_Index] := this._CreateObject(ObjJS[A_Index - 1])
}
else if (res = 0) {
obj := {}
keys := ObjJS.GetKeys()
Loop % keys.length
k := keys[A_Index - 1], obj[k] := this._CreateObject(ObjJS[k])
}
Return obj
}
}
Re: Monitor Bitcoin Price
teadrinker 's code using json parser is safer (and professional...) as syntax/format can change and regex could fail.
Anyway here is regex adapted for the japanese yen values and a check if something goes wrong, i tried to make some errors and found keywords in the response that can be used to detect it ( like "Sorry" ).
You can find regex info in the link you mentioned : https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
You also have an online tester to try in real time : https://regex101.com/
I made a" real time update" to follow the bitcoin movement and gain/loss (update every minute) and as you can see i would be wealthy if i bought some yesterday!!
code with regex for yen:
Code: Select all
url=https://api.coindesk.com/v1/bpi/currentprice/jpy.json
UrlDownloadToVar(URL, response)
RegExMatch( response,"U)Yen.+([\d.]+)}.*" ,m)
msgbox % response
If regexmatch(response,"fail|error|404|Sorry")
msgbox error reported see `n`n`n`n`n%response%
msgbox % m1
UrlDownloadToVar(URL, ByRef aResponse="") {
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", URL)
WebRequest.Send()
aResponse := WebRequest.ResponseText
WebRequest := ""
}
Re: Monitor Bitcoin Price
thank you teadrinker and noname
The regex tester is very helpful for me to learn about it.
It is surprise that the Jason class can get the data directly, I will use it for this one.
The regex tester is very helpful for me to learn about it.
It is surprise that the Jason class can get the data directly, I will use it for this one.
Re: Monitor Bitcoin Price
Is it possible to get a error catch for the GetFromUrl, It stop my AHK when there are no internet connection.teadrinker wrote:Hi, holahapi
COM object "ScriptControl" works on 32 bit processes only. It won't work if you use 64 bit AHK. But there are some another ways to use JScript for parsing JSON. I used one of them in my class to get and convert JSON string to AHK object.
Error message as follow
Code: Select all
---------------------------
Master.ahk
---------------------------
Error: 0x800C0005 - The system cannot locate the resource specified.
Source: msxml3.dll
Description: The system cannot locate the resource specified.
HelpFile: (null)
HelpContext: 0
Specifically: Send
Line#
1438: Return
1439: }
1440: Return,this._CreateObject(oJSON)
1441: }
1443: {
1444: XmlHttp := ComObjCreate("Microsoft.XmlHttp")
1445: XmlHttp.Open("GET", url, false)
---> 1446: XmlHttp.Send()
1447: Return,XmlHttp.ResponseText
1448: }
1450: {
1451: VarSetCapacity(tmpFile, (MAX_PATH := 260) << !!A_IsUnicode, 0)
1452: DllCall("GetTempFileName", Str, A_Temp, Str, "AHK", UInt, 0, Str, tmpFile)
1454: FileAppend,<component>
<public><method name='eval'/></public>
<script language='JScript'></script>
</component>,tmpFile
1462: JS := ObjBindMethod( ComObjGet("script:" . tmpFile), "eval" )
Continue running the script?
---------------------------
Yes No
---------------------------
I am using other way to checking Internet from the reference (https://autohotkey.com/board/topic/2145 ... onnection/)
But I do not think it is the best solution.
Code: Select all
GetFromUrl(url) {
if A_IPAddress1=127.0.0.1
msgbox, no internet connection
else
{
XmlHttp := ComObjCreate("Microsoft.XmlHttp")
XmlHttp.Open("GET", url, false)
XmlHttp.Send()
Return XmlHttp.ResponseText
}
}
-
- Posts: 4344
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: Monitor Bitcoin Price
Code: Select all
jsonStr := JSON.GetFromUrl("https://api.coindesk.com/v1/bpi/currentprice.json")
if (jsonStr = "")
Return
obj := JSON.Parse(jsonStr)
MsgBox,, coindesk.com
, % "upd: " . obj.time.updated . "`n`n"
. "USD: " . obj.bpi.USD.rate . "`n"
. "GBP: " . obj.bpi.GBP.rate . "`n"
. "EUR: " . obj.bpi.EUR.rate
class JSON
{
static JS := JSON._GetJScripObject()
Parse(JsonString) {
try oJSON := this.JS.("(" JsonString ")")
catch {
MsgBox, Wrong JsonString!
Return
}
Return this._CreateObject(oJSON)
}
GetFromUrl(url) {
try {
XmlHttp := ComObjCreate("Microsoft.XmlHttp")
XmlHttp.Open("GET", url, false)
XmlHttp.Send()
res := XmlHttp.ResponseText
}
catch e {
MsgBox,, Microsoft.XmlHttp failed!, % "Error: " . e.message
}
if !e {
status := XmlHttp.Status
if (status != 200)
MsgBox,, Failed to load data!, % "Status code: " . status
else
Return res
}
}
_GetJScripObject() {
VarSetCapacity(tmpFile, (MAX_PATH := 260) << !!A_IsUnicode, 0)
DllCall("GetTempFileName", Str, A_Temp, Str, "AHK", UInt, 0, Str, tmpFile)
FileAppend,
(
<component>
<public><method name='eval'/></public>
<script language='JScript'></script>
</component>
), % tmpFile
JS := ObjBindMethod( ComObjGet("script:" . tmpFile), "eval" )
FileDelete, % tmpFile
JSON._AddMethods(JS)
Return JS
}
_AddMethods(ByRef JS) {
JScript =
(
Object.prototype.GetKeys = function () {
var keys = []
for (var k in this)
if (this.hasOwnProperty(k))
keys.push(k)
return keys
}
Object.prototype.IsArray = function () {
var toStandardString = {}.toString
return toStandardString.call(this) == '[object Array]'
}
)
JS.("delete ActiveXObject; delete GetObject;")
JS.(JScript)
}
_CreateObject(ObjJS) {
res := ObjJS.IsArray()
if (res = "")
Return ObjJS
else if (res = -1) {
obj := []
Loop % ObjJS.length
obj[A_Index] := this._CreateObject(ObjJS[A_Index - 1])
}
else if (res = 0) {
obj := {}
keys := ObjJS.GetKeys()
Loop % keys.length
k := keys[A_Index - 1], obj[k] := this._CreateObject(ObjJS[k])
}
Return obj
}
}
Who is online
Users browsing this forum: Bing [Bot], haomingchen1998, mikeyww, Rohwedder, vposud and 347 guests