Last active
April 20, 2017 18:19
-
-
Save ar1a/d359ee7d0b81d9b0a2808ed81a6a1c99 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env ruby | |
| # Copyright 2017 Aria | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| require 'terminal-table' | |
| require 'json' | |
| require 'rest-client' | |
| require 'bigdecimal' | |
| require 'fuzzy_match' | |
| $names = Hash.new | |
| trap "SIGINT" do | |
| puts "Exiting" | |
| exit 130 | |
| end | |
| def populate_names | |
| #Cant get the summary of items, due to it not being on rsbuddy.com instead of api.rsbuddy.com. Just using it for item id's, so i mirrored it. | |
| raw_data = RestClient.get("https://ptpb.pw/4aH5.json") | |
| data = JSON.parse(raw_data) | |
| data.each do |d| | |
| $names[d[1]["name"]] = d[1]["id"] | |
| end | |
| end | |
| def lookup_item(itemid) | |
| raw_data = RestClient.get("https://api.rsbuddy.com/grandExchange?a=guidePrice&i=#{itemid}") | |
| JSON.parse(raw_data) | |
| end | |
| def get_name(item_name) | |
| FuzzyMatch.new($names.keys).find(item_name.downcase) | |
| end | |
| def get_id(item_name) | |
| key = get_name(item_name) | |
| puts "Matching #{key}" | |
| $names[key] | |
| end | |
| def format_string(num) | |
| num.to_i.to_s.reverse.gsub(/...(?=.)/, '\&,').reverse | |
| end | |
| populate_names | |
| loop do | |
| print "Lookup: " | |
| item_name = gets.chomp | |
| item = get_id(item_name) | |
| puts "Item ID: #{item}" | |
| data = lookup_item(item) | |
| average = data["overall"] | |
| low = data["selling"] | |
| high = data["buying"] | |
| margin = high - low | |
| profit = BigDecimal.new(margin) / BigDecimal.new(low) * 100 | |
| buying_quantity = data["buyingQuantity"] | |
| selling_quantity = data["sellingQuantity"] | |
| table = Terminal::Table.new do |t| | |
| t << ["Name", get_name(item_name)] | |
| t.add_separator | |
| t << ["Average", format_string(average)] | |
| t << ["High price", format_string(high)] | |
| t << ["Low price", format_string(low)] | |
| t << ["Margin", format_string(margin)] | |
| t << ["Profit", "#{profit.to_f.round(2)}%"] | |
| t.add_separator | |
| t << ["Buying rate", format_string(buying_quantity)] | |
| t << ["Selling rate", format_string(selling_quantity)] | |
| end | |
| puts table | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment