Skip to content

Instantly share code, notes, and snippets.

@soramugi
Last active March 10, 2016 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soramugi/11075983 to your computer and use it in GitHub Desktop.
Save soramugi/11075983 to your computer and use it in GitHub Desktop.
amazonとブックオフの価格
source 'https://rubygems.org'
gem 'nokogiri'
gem 'feedjira'
require 'open-uri'
require 'uri'
require 'tempfile'
require 'bundler'
Bundler.require
# http://aws.typepad.com/marketplace_jp/2014/04/weekly_top1000.html
#
urls = [
#'https://s3.amazonaws.com/JP_AM/weekly_top1000/Top1000_14.txt', # 売上数TOP1000(新品・中古合計)
'https://s3.amazonaws.com/JP_AM/weekly_top1000/used_Top1000_14.txt', # 売上数TOP1000レポート(中古のみ)
'http://s3.amazonaws.com/JP_AM/weekly_top1000_view/GV_Top1000_14.txt', # 閲覧数TOP1000レポート
'http://s3.amazonaws.com/JP_AM/weekly_top1000/books_long_tale_item.txt', # ロングテール商品:出品推奨レポート
#'https://s3.amazonaws.com/JP_AM/weekly_top1000/Top1000_63.txt', # ゲーム 売上数TOP1000
]
def gross_profit price, old_price
price - (price / 100 * 15 + 60) - old_price
end
def fba_gross_profit price, old_price
price - (price / 100 * 15 + 222) - old_price
end
urls.each do |url|
p url
tempfile = Tempfile.new 'csv'
tempfile.write URI.parse(url).open.read
tempfile.close
File.open(tempfile.path, 'rb:Shift_JIS:UTF-8', undef: :replace) do |f|
jan_num = used_price_num = new_price_num = 0
f.readlines.map{|l| l.sub(/\n/,'').split(/\t/) }.each_with_index do |row,i|
if i.zero?
jan_num = row.index('JAN') || row.index('r_jan')
new_price_num = row.index('LOWEST_PRICE_NEW') || row.index('r_lowest_price_new')
used_price_num = row.index('LOWEST_PRICE_USED') || row.index('r_lowest_price_used')
next
end
price = row[used_price_num].to_i
price = row[new_price_num].to_i if (!new_price_num.nil? && !row[new_price_num].to_i.zero? && price > row[new_price_num].to_i) || (!new_price_num.nil? && price.zero?)
next if (jan = row[jan_num]) == '' || (price != 0 && price < 500)
sleep 10
begin
url = "http://www.bookoffonline.co.jp/feed/search,st=u,q=#{jan}"
feed = Feedjira::Feed.fetch_and_parse(url)
if feed.is_a?(Fixnum)
sleep 10
next
end
feed.entries.each do |entrie|
doc = Nokogiri::HTML entrie.summary
old_price = doc.css('li').map do |li|
li.text.gsub(/[^0-9]/,'').to_i if li.text.include?('中古')
end.compact.first || 0
next if old_price > 1500 # 高めのは除外
# ブックオフの価格の方が安い
next if gross_profit(price, old_price) < 800
p "jan: #{jan} 期待利益: #{gross_profit(price, old_price)} Amazon: #{price}円 > ブックオフ: #{old_price}円 商品: #{entrie.title}"
# # FBAで出品しても利益出る
# sale_price = ((price - 1) / 100) * 100
# next if fba_gross_profit(sale_price, old_price) < 500
# p "FBA 売値: #{sale_price} 期待利益: #{fba_gross_profit(sale_price, old_price)}"
end
rescue => e
next if e.message.include?('404')
p "error message: #{e.message} jan: #{jan}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment