From 61d5783e846a52563faf844dfba42f028ec540a2 Mon Sep 17 00:00:00 2001
From: j8takagi
Date: Fri, 26 Oct 2012 22:25:42 +0900
Subject: [PATCH 1/1] =?utf8?q?Amazon=E3=81=8B=E3=82=89=E3=83=A9=E3=83=B3?=
=?utf8?q?=E3=82=AD=E3=83=B3=E3=82=B0=E3=83=87=E3=83=BC=E3=82=BF=E3=82=92?=
=?utf8?q?=E5=8F=96=E5=BE=97=E3=81=97=E3=81=A6=E3=82=B0=E3=83=A9=E3=83=95?=
=?utf8?q?=E5=8C=96=E3=81=99=E3=82=8B=E3=83=84=E3=83=BC=E3=83=AB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit
---
ISBN.txt | 50 ++++++++++++++++++++++++++++++++++++++++++
bookdesc.rb | 15 +++++++++++++
bookrank.rb | 26 ++++++++++++++++++++++
bookrank_batch.rb | 22 +++++++++++++++++++
makehtml.rb | 44 +++++++++++++++++++++++++++++++++++++
makehtmlr.rb | 43 ++++++++++++++++++++++++++++++++++++
makeindex.rb | 36 ++++++++++++++++++++++++++++++
plot.R | 56 +++++++++++++++++++++++++++++++++++++++++++++++
plot_batch.rb | 43 ++++++++++++++++++++++++++++++++++++
9 files changed, 335 insertions(+)
create mode 100644 ISBN.txt
create mode 100644 bookdesc.rb
create mode 100755 bookrank.rb
create mode 100755 bookrank_batch.rb
create mode 100644 makehtml.rb
create mode 100644 makehtmlr.rb
create mode 100644 makeindex.rb
create mode 100644 plot.R
create mode 100755 plot_batch.rb
diff --git a/ISBN.txt b/ISBN.txt
new file mode 100644
index 0000000..728ec03
--- /dev/null
+++ b/ISBN.txt
@@ -0,0 +1,50 @@
+475981339X
+4004150930
+4140807431
+4774141291
+4860650530
+4864010048
+4864010056
+4492521453
+453231139X
+4004308569
+4797336617
+4797357401
+4903506002
+4000062441
+4652078404
+4756107850
+4274067572
+4022502797
+4062573679
+400500475X
+4314009977
+4005004830
+4822283801
+4887596995
+4150503583
+400007489X
+4062577240
+450154970X
+4000253549
+448601863X
+4621045938
+4797341378
+4905849152
+4812445620
+419891477X
+4198914915
+4198915113
+4022645547
+4022645555
+4990491106
+4839938830
+4334035604
+4532191769
+4303714305
+4871483517
+4320026926
+4756136494
+4535600120
+B005IHLNOC
+B005HTNHO0
diff --git a/bookdesc.rb b/bookdesc.rb
new file mode 100644
index 0000000..ddbe377
--- /dev/null
+++ b/bookdesc.rb
@@ -0,0 +1,15 @@
+# -*- coding: utf-8 -*-
+require 'amazon/aws/search'
+include Amazon::AWS
+include Amazon::AWS::Search
+
+class File
+ def bookdesc(isbn)
+ look = ItemLookup.new( 'ASIN', { 'ItemId' => isbn } )
+ look.response_group = ResponseGroup.new( 'Small' )
+ req = Search::Request.new
+ res = req.search(look)
+ attr = res.item_lookup_response.items.item.item_attributes
+ print attr.author, "ã", attr.title, "ãï¼", attr.manufacturer, "ï¼"
+ end
+end
diff --git a/bookrank.rb b/bookrank.rb
new file mode 100755
index 0000000..06fb24c
--- /dev/null
+++ b/bookrank.rb
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+require 'amazon/aws/search'
+include Amazon::AWS
+workdir = File.expand_path("~/amazonrank")
+
+begin
+ il = ItemLookup.new( 'ASIN', { 'ItemId' => ARGV[0] } )
+ request = Search::Request.new
+ time = Time.now.strftime("%Y/%m/%d %H:%M:%S")
+ req = request.search il
+ rank = req.item_lookup_response.items.item.sales_rank
+ raise "ã©ã³ãã³ã°ãåå¾ã§ãã¾ããã§ãããåå ä¸æ" if req.nil?
+ csvfile = File.expand_path(workdir.dup << "/" << ARGV[0].dup << ".csv")
+ open(csvfile, "a") { |f|
+ f.print ARGV[0], ",", time, ",", req, "\n"
+ }
+rescue => exc
+ STDERR.puts exc
+ errfile = File.expand_path(workdir.dup << "/error.log")
+ open(errfile, "a") { |f|
+ f.print "ã¨ã©ã¼: ", exc, "\n"
+ f.print "ASBN: ", ARGV[0], "\n"
+ f.print "æ¥æ: ", time, "\n"
+ f.print "\n"
+ }
+end
diff --git a/bookrank_batch.rb b/bookrank_batch.rb
new file mode 100755
index 0000000..1b8de8b
--- /dev/null
+++ b/bookrank_batch.rb
@@ -0,0 +1,22 @@
+#!/usr/local/bin/ruby
+# -*- coding: utf-8 -*-
+require 'amazon/aws/search'
+include Amazon::AWS
+
+workdir = File.expand_path("~/amazonrank")
+isbnfile = File.expand_path(workdir.dup << "/ISBN.txt")
+
+open(isbnfile, "r") { |lines|
+ while isbn = lines.gets
+ isbn = isbn.chomp
+ csvfile = File.expand_path(workdir.dup << "/" << isbn.dup << ".csv")
+ il = ItemLookup.new( 'ASIN', { 'ItemId' => isbn } )
+ request = Search::Request.new
+ time = Time.now
+ req = request.search il
+ open(csvfile, "a") { |f|
+ f.print isbn, ",", time.strftime("%Y/%m/%d %H:%M:%S"), ",", req.item_lookup_response.items.item.sales_rank, "\n"
+ }
+ sleep 0.5
+ end
+}
diff --git a/makehtml.rb b/makehtml.rb
new file mode 100644
index 0000000..a5d0ab0
--- /dev/null
+++ b/makehtml.rb
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+require './bookdesc'
+
+workdir = "~/amazonrank"
+htmlfile = workdir.dup << "/" << ARGV[0].dup << ".html"
+
+open(htmlfile, "w") { |f|
+ f.print <
+
+
+
+
+EOS
+ f.print "Amazonã©ã³ãã³ã° - ", ARGV[0], "\n"
+
+ f.print <
+
+EOS
+
+ f.print "Amazonã©ã³ãã³ã° - ASBN", ARGV[0], "
"
+ f.print ""
+ f.bookdesc(ARGV[0])
+ f.print "
\n"
+ f.print "Amazonã®ãã¼ã¸
\n"
+ f.print "ãã¼ã¿ãã¡ã¤ã«ï¼CSVå½¢å¼ï¼
\n"
+ f.print "ã°ã©ãã®ä¸ä¸ãéã«ãã
\n"
+
+ f.print <ç®æ¬¡ã¸æ»ã
+
+EOS
+
+ f.print "
+
+
+EOS
+}
diff --git a/makehtmlr.rb b/makehtmlr.rb
new file mode 100644
index 0000000..1e9cff2
--- /dev/null
+++ b/makehtmlr.rb
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+require './bookdesc'
+
+workdir = "~/amazonrank"
+htmlfile = workdir.dup << "/" << ARGV[0].dup << "r.html"
+
+open(htmlfile, "w") { |f|
+ f.print <
+
+
+
+
+EOS
+ f.print "Amazonã©ã³ãã³ã° - ", ARGV[0], "\n"
+
+ f.print <
+
+EOS
+
+ f.print "Amazonã©ã³ãã³ã° - ASBN", ARGV[0], "
"
+ f.print ""
+ f.bookdesc(ARGV[0])
+ f.print "
\n"
+ f.print "Amazonã®ãã¼ã¸
\n"
+ f.print "ãã¼ã¿ãã¡ã¤ã«ï¼CSVå½¢å¼ï¼
\n"
+ f.print "ã°ã©ãã®ä¸ä¸ãéã«ãã
\n"
+ f.print <ç®æ¬¡ã¸æ»ã
+
+EOS
+
+ f.print "
+
+
+EOS
+}
diff --git a/makeindex.rb b/makeindex.rb
new file mode 100644
index 0000000..431055c
--- /dev/null
+++ b/makeindex.rb
@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+require './bookdesc'
+
+workdir = "~/amazonrank"
+htmlfile = workdir.dup << "/index.html"
+isbnfile = "ISBN.txt"
+
+open(htmlfile, "w") { |f|
+ f.print <
+
+
+
+
+Amazonã©ã³ãã³ã°
+
+
+
+Amazonã©ã³ãã³ã°
+
+EOS
+
+ open(isbnfile, "r") { |lines|
+ while l = lines.gets
+ l = l.chomp
+ f.print "- "
+ f.bookdesc(l)
+ f.print " Amazon
\n"
+ end
+ }
+ f.print <
+
+
+EOS
+}
diff --git a/plot.R b/plot.R
new file mode 100644
index 0000000..9459263
--- /dev/null
+++ b/plot.R
@@ -0,0 +1,56 @@
+library(RSvgDevice)
+
+plotrank <- function(datafile, outfiletype, topup=TRUE)
+{
+ df <- read.table(datafile, sep=",")
+ x <- strptime(df$V2, "%Y/%m/%d %H:%M:%S")
+ y <- df$V3
+ # æ°å¤ã®å¤§ããæ¹ãä¸ã«ããããå°ããæ¹ãä¸ã«ããããæå®
+ if (topup) {
+ yl = c(ylimmax(max(y, na.rm=TRUE)), 1)
+ } else {
+ yl = c(1, ylimmax(max(y, na.rm=TRUE)))
+ }
+ plot(x, y, type="l", ylim=yl, xlab="datetime", ylab="rank", xaxt="n", sub=paste("last update:", max(x), " (", outfiletype, ")", sep=""))
+ par(xaxt="s")
+ r <- as.POSIXct(round(range(x), "days"))
+ axis.POSIXct(1, at=seq(r[1],r[2], by="1 day"), format="%m/%d")
+}
+
+ylimmax <- function(ymax)
+{
+ ylm <- 10
+ while(ylm < ymax) {
+ ylm <- ylm * 10
+ while(ylm >= ymax * 2) {
+ ylm <- ylm / 2
+ }
+ }
+ return(ylm)
+}
+
+doplotrank <- function(asbn, datadir, outdir, outfiletype, topup)
+{
+ ## ã°ã©ãã®ä¸ä¸ãéã«ããã¨ãã¯åºåãã¡ã¤ã«åã®æ¡å¼µååã«ãrããä»ããï¼ä¾:475981339Xr.svgï¼
+ if (topup == FALSE) {
+ fname <- paste(asbn, "r", sep="")
+ } else {
+ fname <- asbn
+ }
+ ## åºåã¿ã¤ãï¼SVGã¾ãã¯PNGï¼ã®è¨å®
+ if (outfiletype == "PNG") {
+ png(paste(outdir, "/", fname, ".png", sep=""), type="cairo", width=720, height=576)
+ } else if (outfiletype == "SVG") {
+ devSVG(paste(outdir, "/", fname, ".svg", sep=""))
+ }
+ ## ã°ã©ãããã¡ã¤ã«ã¸åºå
+ plotrank(paste(datadir, "/", asbn, ".csv", sep=""), outfiletype, topup)
+ invisible(dev.off())
+}
+
+args <- commandArgs(trailingOnly = TRUE)
+for (ftype in c("SVG", "PNG")) {
+ for (utop in c(TRUE, FALSE)) {
+ doplotrank(args[1], args[2], args[3], ftype, utop)
+ }
+}
diff --git a/plot_batch.rb b/plot_batch.rb
new file mode 100755
index 0000000..eb1b666
--- /dev/null
+++ b/plot_batch.rb
@@ -0,0 +1,43 @@
+#!/usr/local/bin/ruby
+# -*- coding: utf-8 -*-
+
+# Rããã°ã©ã ã®ãã¹
+rpath = "/usr/local/bin/R"
+
+# ä½æ¥ç¨ãã£ã¬ã¯ããªã®ããã
+workdir = File.expand_path("~/amazonrank")
+
+# Rã®ã°ã©ãåºåããã°ã©ã
+plotfile = workdir.dup << "/plot.R"
+
+# ASINãªã¹ããã¡ã¤ã«
+isbnfile = workdir.dup << "/ISBN.txt"
+
+# ãã¼ã¿CSVãã¡ã¤ã«ãæ ¼ç´ãããã£ã¬ã¯ããª
+csvdir = workdir.dup
+
+# HTMLãã¡ã¤ã«ããã³ãã¼ã¿CSVãã¡ã¤ã«ãæ ¼ç´ãããã£ã¬ã¯ããª
+htmldir = workdir.dup
+
+# ããããæã®ã¨ã©ã¼è¨é²ãã¡ã¤ã«
+ploterrfile = workdir.dup << "/error_plot.log"
+
+open(isbnfile, "r") { |lines|
+ begin
+ while isbn = lines.gets
+ isbn = isbn.chomp
+ cmd = rpath.dup << " --vanilla --slave --args " << isbn.dup << " " << csvdir.dup << " " << htmldir.dup << " <" << plotfile.dup << " 2>>" << ploterrfile.dup
+ system(cmd)
+ raise "ASIN:" << isbn.dup << " ã³ãã³ãå®è¡ã¨ã©ã¼" if $? != 0
+ end
+ rescue => exc
+ STDERR.puts exc
+ errfile = File.expand_path(ploterrfile.dup)
+ open(errfile, "a") { |f|
+ f.print "ASIN: ", isbn, "\n"
+ f.print "æ¥æ: ", Time.now.strftime("%Y/%m/%d %H:%M:%S"), "\n"
+ f.print "\n"
+ }
+ retry
+ end
+}
--
2.18.0