Bitcoin Forum
April 25, 2024, 05:30:17 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
  Home Help Search Login Register More  
  Show Posts
Pages: [1]
1  Other / Beginners & Help / DIY Coinotron JSON API on: April 21, 2013, 07:08:57 PM
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"}}
Pages: [1]
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!