Last active
August 29, 2015 14:05
-
-
Save jamie/4fd8a76f83c8be590b0d to your computer and use it in GitHub Desktop.
Port of http://www.reddit.com/r/woweconomy/comments/2d0og2/my_java_source_code_for_how_i_found_the_cheapest/ to ruby2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run as: ruby auction_search.rb <item_id> | |
# Requires ruby 2.0 or higher, has no other dependencies. | |
# Will make its own temp file in the current directory for storing auction data, | |
# to make repeat queries faster. Delete the folder to force download fresh data. | |
# Sample output: | |
# Arthas ( high) | | |
# | 450000.0 horde-Auromus | |
require 'fileutils' | |
require 'json' | |
require 'ostruct' | |
require 'open-uri' | |
require 'pp' | |
def servers | |
# Returns array of structs, relevant fields would be name, slug, population, status | |
json = JSON.parse(open("http://us.battle.net/api/wow/realm/status").read) | |
json["realms"].map{|r| OpenStruct.new(r) } | |
end | |
def auction_file(slug) | |
FileUtils.mkdir_p("tmp") | |
filename = "tmp/#{slug}.json" | |
if !File.exist?(filename) | |
index_url = "http://us.battle.net/api/wow/auction/data/#{slug}" | |
url = JSON.parse(open(index_url).read)["files"].first["url"] | |
`curl -so #{filename} #{url}` | |
end | |
File.read(filename) | |
end | |
def auctions(slug) | |
json = JSON.parse(auction_file(slug)) | |
%w(alliance horde neutral).each do |faction| | |
json[faction]["auctions"].each do |auc| | |
yield OpenStruct.new(auc.merge(faction: faction)) | |
end | |
end | |
rescue JSON::ParserError | |
FileUtils.rm("tmp/#{slug}.json") | |
puts "Error processing file for #{slug}, re-run script to redownload" | |
end | |
COPPER_TO_GOLD = 1_00_00 | |
item_id = ARGV.pop.to_i | |
servers.each do |server| | |
next unless server.population == "high" | |
puts "%20s (%6s) |" % [server.name, server.population] | |
auctions(server.slug) do |auction| | |
next unless auction.item == item_id | |
buyout = auction.buyout.to_f / auction.quantity / COPPER_TO_GOLD | |
puts "%20s %6s | %6.1f %s-%s" % ["", "", buyout, auction.faction, auction.owner] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment