友人どうしでギフトカードのやり取りをしたときに、
Amazonのギフトカードを一気に登録する方法がないかどうか考えてみた。
楽ちんに登録できたら複数枚登録できて便利。
#!/usr/bin/env ruby require 'openssl' OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE require 'pp' require 'rubygems' require 'gmail' require 'kconv' require 'mechanize' require 'nokogiri' class Amazon < Mechanize def initialize(e_mail,passwd) super() url ="https://www.amazon.co.jp/gp/css/gc/payment/redeem-gc-balance/ref=gc_ya_subnav_view-gc-balance?" self.get url f = self.page.form_with( :name => "signIn") f.field_with( :name=>"email").value=e_mail f.field_with( :name=>"password").value=passwd f.submit end def add_coupon(url) self.get url f = self.page.form_with :action=>"https://www.amazon.co.jp/gp/css/gc/payment/redeem-gc-balance" f.submit end end i =0 azn = Amazon.new("takuyaXXX@mail.examle.jp","***password***") gmail = Gmail.new(:plain, "takuya@hogehoge", "****"); gmail.label(:AmazonGift).all.each{|l| m = l.message body = m.body html= body.parts[0].body.parts[1].body.to_s.toutf8 doc = Nokogiri(html) l=doc.search('a'). select{|e|e.attr("href")=~%r"^https://www.amazon.co.jp/gp/css/gc/payment/view-gc-balance\?code="}. map{|e|e.attr("href")} href = l.first puts (i=i+1), href azn.add_coupon href }