It is currently March 28th, 2024, 12:41 pm

Struggling with WebParsing JSON NiceHash API

Get help with installing and using Rainmeter.
Post Reply
User avatar
spoonwzd
Posts: 14
Joined: November 29th, 2017, 6:24 pm

Struggling with WebParsing JSON NiceHash API

Post by spoonwzd »

The NiceHash API produces a JSON formatted output like this:

Code: Select all

{"result":{"stats":[{"balance":"0.00010688","rejected_speed":"0.0000","algo":0,"accepted_speed":"0.0820"},{"balance":"0.00000000","rejected_speed":"0.0000","algo":2,"accepted_speed":"0.0000"},{"balance":"0.00000000","rejected_speed":"0.0000","algo":3,"accepted_speed":"0.0000"},{"balance":"0.00000000","rejected_speed":"0.0000","algo":4,"accepted_speed":"0.0000"},{"balance":"0.00000000","rejected_speed":"0.0000","algo":6,"accepted_speed":"0.0000"},{"balance":"0.00000000","rejected_speed":"0.0000","algo":7,"accepted_speed":"0.0000"}],"payments":[{"amount":"0.01604289","fee":"0.00032741","TXID":"e47ce45771b4bb80b52cb787c95eca0bb0779cb5cb7a9af437d1b13e450db7a5","time":"2015-01-22 17:03:27"}],"addr":"16dZdWFr6bhy5bxwsyUyunuWED8zWfQiYA"},"method":"stats.provider"}
The API output can vary in size depending on how many different algorithms are being mined. There are around 30 different potential algorithms:
  • Algorithms are marked with following numbers:

    0 = Scrypt
    1 = SHA256
    2 = ScryptNf
    3 = X11
    4 = X13
    5 = Keccak
    6 = X15
    7 = Nist5
    8 = NeoScrypt
    9 = Lyra2RE
    10 = WhirlpoolX
    11 = Qubit
    12 = Quark
    13 = Axiom
    14 = Lyra2REv2
    15 = ScryptJaneNf16
    16 = Blake256r8
    17 = Blake256r14
    18 = Blake256r8vnl
    19 = Hodl
    20 = DaggerHashimoto
    21 = Decred
    22 = CryptoNight
    23 = Lbry
    24 = Equihash
    25 = Pascal
    26 = X11Gost
    27 = Sia
    28 = Blake2s
    29 = Skunk
How do I build a regexp query that pulls back the balance, rejected speed, algo number and accepted speed variables from a specific algo? I can build a query with a lookahead assertion, but that just builds a sequential list of results, whereas I just want to be able to pull the stats for a specific algo.

Code: Select all

(?siU)"balance":"(.*)",.*"rejected_speed":"(.*)",.*"algo":(.*),.*"accepted_speed":"(.*)"
Help me Obi-Wans - you're my only hopes. :)
User avatar
FreeRaider
Posts: 826
Joined: November 20th, 2012, 11:58 pm

Re: Struggling with WebParsing JSON NiceHash API

Post by FreeRaider »

A question:
do you want capture those info if algo is a specific number, or for each number?

For example you want capture those info if algo = 2, algo = 5 etc.
User avatar
spoonwzd
Posts: 14
Joined: November 29th, 2017, 6:24 pm

Re: Struggling with WebParsing JSON NiceHash API

Post by spoonwzd »

Yes, exactly - If algo=2 i want to be able to run my regexp on the content between the preceding { and post } characters.

eg. if algo=2, do:
(?siU)"balance":"(.*)",.*"rejected_speed":"(.*)",.*"algo":(.*),.*"accepted_speed":"(.*)"
against:
{"balance":"0.00000000","rejected_speed":"0.0000","algo":2,"accepted_speed":"0.0000"}#

And if not possible with regexp, is there better way to parse JSON using the WebParser? Or some other method?
User avatar
FreeRaider
Posts: 826
Joined: November 20th, 2012, 11:58 pm

Re: Struggling with WebParsing JSON NiceHash API

Post by FreeRaider »

Try this code. I used a lua script.

algo.ini

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize=1

[Variables]

[MeasureRSS]
Measure=Plugin
Plugin=WebParser
URL=file://#CURRENTPATH#algo.txt
RegExp=(?siU)^(.*)$
UpdateRate=600
FinishAction=[!UpdateMeasure MeasureScript]

[MeasureScript]
Measure=Script
ScriptFile=algo.lua
Match1=2
Match2=
Match3=7
UpdateDivider=-1

[MeterName1]
Meter=String
X=0
Y=0
[MeterName2]
Meter=String
X=0
Y=0R
[MeterName3]
Meter=String
X=0
Y=0R
[MeterName4]
Meter=String
X=0
Y=0R
[MeterName5]
Meter=String
X=0
Y=0R
[MeterName6]
Meter=String
X=0
Y=0R
[MeterName7]
Meter=String
X=0
Y=0R
algo.lua

Code: Select all

function Initialize()
	
   measureSite = SKIN:GetMeasure('MeasureRSS')
   stype1 = SELF:GetOption('Match1')
   stype2 = SELF:GetOption('Match2')
   stype3 = SELF:GetOption('Match3')
   titlePattern = '%{(.-)%}'
   itemPattern = '%{%"balance.-%},'
   sBalance = '{"balance":"(.-)",'
   sRejected_speed = '%"rejected_speed%":%"(.-)%"%,'
   sAlgo = '"algo":(.-),'
   sAccepted_speed ='"accepted_speed":"(.-)"}'
end

function Update()

   allData = measureSite:GetStringValue()
   if allData == '' then return end

   dummyString, itemCount = string.gsub(allData, 'balance', '')

   startPos = 0
   
   for i = 1, itemCount do
      
      currentItemStart, currentItemEnd = string.find(allData, itemPattern, startPos)
      currentItem = string.sub(allData, currentItemStart, currentItemEnd)
      
	  current_title = string.match(currentItem, titlePattern)
	  Balance = string.match(currentItem, sBalance)
	  Rejected_speed = string.match(currentItem, sRejected_speed )
   	  Algo = string.match(currentItem, sAlgo)
	  Accepted_speed = string.match(currentItem, sAccepted_speed)
	 
	 	 
	 
	 if (string.match(Algo, stype1) and stype1 ~= '') then
		SKIN:Bang('!SetOption', 'MeterName'..i, 'Text', 'algo: '..Algo.. '#CRLF#' ..'balance: '..Balance.. '#CRLF#' ..'rejected_speed: ' ..Rejected_speed.. '#CRLF#' ..'accpted_speed: ' ..Accepted_speed)
	elseif (string.match(Algo, stype2) and stype2 ~= '')then
		SKIN:Bang('!SetOption', 'MeterName'..i, 'Text', 'algo: '..Algo.. '#CRLF#' ..'balance: '..Balance.. '#CRLF#' ..'rejected_speed: ' ..Rejected_speed.. '#CRLF#' ..'accpted_speed: ' ..Accepted_speed)
	elseif (string.match(Algo, stype3) and stype3 ~= '')then
		SKIN:Bang('!SetOption', 'MeterName'..i, 'Text', 'algo: '..Algo.. '#CRLF#' ..'balance: '..Balance.. '#CRLF#' ..'rejected_speed: ' ..Rejected_speed.. '#CRLF#' ..'accpted_speed: ' ..Accepted_speed)
		
	  
	  end
      
      startPos = currentItemEnd + 1
	        
   end
   
  return itemCount
  

end
P.s.: This code is based on your JSON output.

Let me (us) know if it helps you.
User avatar
spoonwzd
Posts: 14
Joined: November 29th, 2017, 6:24 pm

Re: Struggling with WebParsing JSON NiceHash API

Post by spoonwzd »

Thanks for this. Was just working on it when NiceHash got hacked. So no more of that then.

I'm sure this will come in handy for a future project, so thanks again.
JPapavas
Posts: 8
Joined: September 5th, 2013, 10:07 pm

Re: Struggling with WebParsing JSON NiceHash API

Post by JPapavas »

Hello all, this is my first attempt.

You have to replace your BTC Address inside to Variables.inc file and replace exist address with yours.

Enjoy :)
NiceHash Production Viewer_1.01.rmskin
Nicehash Viewer
(5.62 KiB) Downloaded 46 times
<- NOT COMPLETE
img01.png
P.S.
I have replaced the file, it was not the (final-correct). i will just add the second <- COMPLETE
Post Reply