Skip to content

Instantly share code, notes, and snippets.

@polamjag
Last active December 26, 2015 09:29
Show Gist options
  • Save polamjag/7130149 to your computer and use it in GitHub Desktop.
Save polamjag/7130149 to your computer and use it in GitHub Desktop.
ドクペ価格botを支える技術
# encoding: utf-8
require "open-uri"
require "rubygems"
require "nokogiri"
require "kconv"
require "twitter"
# load OAuth keys and tokens
load File.expand_path(File.dirname(__FILE__) + '/key.rb')
# log file for price comparing
PRICE_LOG = File.expand_path(File.dirname(__FILE__) + "/pricelog")
# Dr.Peppers to retrieve price
target_drinks =
{"500mlペット×24" => {"uri" => "http://www.amazon.co.jp/dp/B001U7651A/", "amt" => 500, "pcs" => 24},
"350ml缶×24" => {"uri" => "http://www.amazon.co.jp/dp/B001DDOIBU/", "amt" => 350, "pcs" => 24}}
def trim_price (target_css_selector)
target_css_selector.gsub(/[[:blank:][:space:]]/, '').gsub(/<[a-zA-Z="\/]*>/, '').gsub(/([0-9,]{256})/, '\1')
end
# confirm whether the specification of goods page of amazons is not changed
def test_amazon_html
tester = "http://www.amazon.co.jp/dp/4101098018/" # ボッコちゃん
html = open(tester).read
page = Nokogiri::HTML(html, nil, 'mbcs')
# has
(!!(trim_price(page.css('.priceLarge').to_html().encode('utf-8')) =~ /[0-9]+/))
end
# retrieve prices and send tweet
if test_amazon_html then
# detect current time
time = Time.now
# format current minute of time to two digits
tweet_text = "#{time.month}/#{time.day} #{time.hour}:%02d現在のドクペ価格は,"%[time.min]
# open price log file
price_log = File.open(PRICE_LOG, "r:UTF-8")
old_price = {}
# parse log file
price_log.each do |line|
old_price.merge!({line.match(/(^.+),/)[1] => Integer(line.match(/,([0-9]+)$/)[1])})
end
price_log.close
price_log = File.open(PRICE_LOG, "w:UTF-8")
price_log.write("")
# {drink_name => old_price}
target_drinks.each { |name, prop|
html = open(prop["uri"]).read
page = Nokogiri::HTML(html, nil, 'mbcs')
price = trim_price(page.css('.priceLarge').to_html().encode('utf-8'))
real_price = Integer(price.gsub(/[^0-9]/, ""))
# write price into log here to compare transition of price
price_log.puts "#{name},#{real_price}"
# compare transition of price
price_comparison = ""
if (old_price[name] == real_price) then
price_comparison = "±¥0"
elsif (old_price[name] > real_price) then
price_comparison = "↓¥#{old_price[name] - real_price}"
else
price_comparison = "↑¥#{real_price - old_price[name]}"
end
tweet_text += "\n#{name}が #{price}(#{price_comparison},¥#{real_price / prop["pcs"]}/本,¥%.3f/ml)"%[(real_price + 0.0) / (prop["pcs"] * prop["amt"] + 0.0)]
}
price_log.close()
else
print "Error: scraping failed\n"
tweet_text = "@polamjag Error: price not found in Amazon's page"
end
Twitter.update(tweet_text)
# output log
log_time = Time.now()
print "Sent at #{log_time.year}-#{log_time.month}-#{log_time.day} #{log_time.hour}:#{log_time.min}: #{tweet_text}\n"
require 'twitter'
# add your own key below
Twitter.configure do |config|
config.consumer_key = ""
config.consumer_secret = ""
config.oauth_token = ""
config.oauth_token_secret = ""
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment