Skip to content

Instantly share code, notes, and snippets.

@deluan
Created October 25, 2019 14:31
Show Gist options
  • Save deluan/738c121bf14bdad98fe623a7d0472522 to your computer and use it in GitHub Desktop.
Save deluan/738c121bf14bdad98fe623a7d0472522 to your computer and use it in GitHub Desktop.
Pulsed Media Auction watcher for BitBar
#!/usr/bin/env ruby
require 'open-uri'
require 'date'
require 'json'
AUCTION_URL="http://pulsedmedia.com/seedbox-auctions.php"
PRODUCT_INFO_URL="http://pulsedmedia.com/clients/widgets/productsinfo.php"
BUY_URL="http://pulsedmedia.com/clients/cart.php?a=add&pid=210&billingcycle=monthly"
PRICE_FILE = "/tmp/pm-auction-prices"
ALERT_LEVEL_GOOD = 5
ALERT_LEVEL_EXCELENT = 4
@products = {
# 210 => "M1000 A1",
211 => "Dragon A1",
# 212 => "Storage A1",
# 213 => "Value250 A1",
}
@content = Hash.new
@best_prices = Hash.new
def get_price(product)
@content[product] ||= URI("#{PRODUCT_INFO_URL}?pid=#{product}&get=price&billingcycle=monthly").read
@content[product].gsub(/.*'([\d.]+).'.*/, '\1').to_f
end
def get_products
out = []
@products.keys.each do |product|
price = get_price(product)
pid = product.to_s
if price < ALERT_LEVEL_EXCELENT
color = "white"
elsif price < ALERT_LEVEL_GOOD
color = "#696969"
else
color = "#292929"
end
if @best_prices[pid].nil? || (price < @best_prices[pid]["price"])
@best_prices[pid] = {
"price" => price,
"date" => DateTime.now.strftime("%d/%m/%Y %H:%M"),
}
end
out << "%-12s %6.2f€ | href=#{AUCTION_URL} color=#{color} font=Courier" % ["#{@products[product]}:", price]
out << "Best: %6.2f€ on %s | color=#{color} font=Courier alternate=true" % [@best_prices[pid]["price"], @best_prices[pid]["date"]]
end
out
end
def get_best_price
@products.keys.map { |product| get_price(product) }.min
end
if File.exist?(PRICE_FILE)
begin
File.open(PRICE_FILE, "r:UTF-8") do |f|
@best_prices = JSON.parse(f.read)
end
rescue
@best_prices = Hash.new
end
end
begin
price = get_best_price
if price < ALERT_LEVEL_EXCELENT
puts "#{price}€ | color=red dropdown=false"
puts "#{price}€ | color=white dropdown=false"
elsif price < ALERT_LEVEL_GOOD
puts "#{price}€"
else
puts "€"
end
puts "---"
puts get_products.join("\n")
rescue => e
puts ":interrobang:€ | color=red"
puts "---"
puts "Error: #{e} | color=red"
exit
end
File.open(PRICE_FILE, "w:UTF-8") do |f|
f.write @best_prices.to_json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment