Author

Topic: DIY Coinotron JSON API (Read 1658 times)

newbie
Activity: 14
Merit: 0
May 07, 2013, 01:00:14 PM
#3
great man ..
Thanks
full member
Activity: 224
Merit: 100
May 07, 2013, 12:56:43 PM
#2
Thanks Idler! Going to try this out... was hoping for an API from Coinotron, but no dice. I'll report back my results with this...
newbie
Activity: 11
Merit: 0
April 21, 2013, 03:08:57 PM
#1
I think coinotron is a good LTC Pool, but it doesn't provide JSON API to allow monitoring or logging, so I've made my own one, let's share it for you. Hope all would help.

The script is written in ruby and designed to run in cgi-bin of an web server.

The main purpose is to raise the awareness of the importance of the read only API. If the developer of coinotron released these extremely simple JSON API, this script would be useless. Thank you!

Code:
#!/usr/bin/env ruby

username = "xxxxxxxx"
password = "xxxxxxxxx"

puts "Content-type: text/text\n\n" unless ENV["HTTP_HOST"].nil?

require 'httpclient'
clnt = HTTPClient.new
clnt.set_cookie_store("cookie.dat")

r = clnt.get "https://coinotron.com/coinotron/AccountServlet?action=home"
while r.header.status_code == 302 do
  r = clnt.get r.header["Location"][0]
end
clnt.post "https://coinotron.com/coinotron/AccountServlet?action=logon", {"name" => username, "password" => password, "Logon" => "Logon"}

html = clnt.get_content "https://coinotron.com/coinotron/AccountServlet?action=myaccount"

require 'nokogiri'
doc = Nokogiri::HTML.parse html

h = Hash.new
h["worker"] = Hash.new

ltc_cr = ltc_ucf = ltc_c = 0

doc.css("input").each do |i|
  ltc_cr  = i["value"].to_f if i["name"] == "CurrentRoundRewardsLTC"
  ltc_ucf = i["value"].to_f if i["name"] == "UnconfirmedRewardsLTC"
  ltc_c   = i["value"].to_f if i["name"] == "ConfirmedRewardsLTC"
end

ltc_ct = ltc_cr + ltc_ucf + ltc_c

h["CurrentRoundRewardsLTC"] = ltc_cr
h["UnconfirmedRewardsLTC"] = ltc_ucf
h["ConfirmedRewardsLTC"] = ltc_c
h["TotalRewordsLTC"] = ltc_ct

doc.css("form").each do |f|

  if f["action"] == "/coinotron/AccountServlet?action=changeworkercoin"
    worker_name = f.xpath("input[@name=\"workername\"]").first["value"]
    h["worker"][worker_name] = Hash.new if h[worker_name].nil?
    coin = ""
    f.css("option").each do |o|
      unless o["selected"].nil?
        coin = o.text ; break
      end
    end
    h["worker"][worker_name]["coin"] = coin
  end

  if f["action"] == "/coinotron/AccountServlet?action=changeworkerrewardtype"
    worker_name = f.xpath("input[@name=\"workername\"]").first["value"]
    h["worker"][worker_name] = Hash.new if h[worker_name].nil?
    workerrewardtype = ""
    f.css("option").each do |o|
      unless o["selected"].nil?
        workerrewardtype = o.text ; break
      end
    end
    h["worker"][worker_name]["workerrewardtype"] = workerrewardtype
  end

end


doc.css("tr").each do |row|
  ct = 0
  worker_name = khash = ""
  row.children.each do |c|
    worker_name = c.text.strip if c.name == "td"  and  ct == 2
    khash = c.text.strip if c.name == "td" and ct == 6
    ct +=1
  end
  next if h["worker"][worker_name].nil?
  h["worker"][worker_name]["khash"] = khash
end

#puts h

require 'json'
puts h.to_json




Then output looks like

Code:
{"CurrentRoundRewardsLTC"=>0.0, "UnconfirmedRewardsLTC"=>0.0, "ConfirmedRewardsLTC"=>2.91905758, "TotalRewordsLTC"=>2.91905758, "idler921.gpu"=>{"coin"=>"LTC", "workerrewardtype"=>"Round based pay per share", "khash"=>"152.2 KH/s"}, "idler921.cpu"=>{"coin"=>"LTC", "workerrewardtype"=>"Pay per share", "khash"=>"45.0 KH/s"}}
Jump to: