第一时间获取苹果官网IPad2供货信息

因为IPad2在官网 停止销售有一段时间。

有可能恢复供货也被抢购掉。(IPhone4 在今年2月份时 在官网抢购一台4999转手能卖到5700以上)

下面ruby代码 每半小时访问一次 苹果在线商店,如果货源信息变动,发邮件给指定联系人。

代码量很少,在这种小程序上ruby真的很方便~~

#主要流程
#!/usr/bin/ruby -w
#coding:UTF-8
require 'socket'
require 'net/http'
require "./send_email"
host,port,path='store.apple.com','80','/cn/browse/home/shop_ipad/family/ipad/select?mco=MjE2MjYyNzA'
http = Net::HTTP.new(host)
SLEEP_TIME=30 # in minites
eval File.read("smtp_tls2.rb")
Net::SMTP.enable_tls()
while true
begin
headers,body=http.get(path)
if headers.code=='200'
     str=body
str=str.force_encoding("UTF-8")
p str.encoding
    if /\(?.*?)\<\/span\>/mu=~str
          if sell_time.strip !='暂无供应'
            send_email('Apple Ipad2 供应信息有变化','供应信息有变化,估计发货时间为:'+sell_time.strip)
            p 'Apple Ipad2 information has change.sell time is:'+sell_time.strip+"\n";
           else
            p "IPAD2 still are not available!\n"
            #send_email('IPAD2 还是没有!!唉!','IPAD2 还是没有!!唉!'+sell_time.strip)
          end
    else
         send_email('apple订购页面有变化','apple订购页面有变化,无法匹配供应信息,请更改程序正则表达式')
         p "apple has change its web page,can not grap the information,please change your regex\n"
    end

else
    send_email('apple网站返回了错误信息',"苹果网站返回的错误信息: #{headers.code} #{headers.message}")
    p "apple website has returned error message: #{headers.code} #{headers.message}\n"
end

rescue
    begin
        send_email('苹果网站访问异常','无法连接到苹果网站,这可能是由于dabaiblog.com网络异常,或者苹果官网不可访问造成')
    rescue
        p "network error,and send error email failed.\n"
    ensure
        p "can not reach the apple website, this maybe due to network error of dabaiblog.com,or apple webserver crashed\n";
    end
ensure
    p 'last check time:'+Time.new.to_s+"\n"
    sleep SLEEP_TIME*60
end
end
#发送email部分 这里针对GMAIL配置
#!/usr/bin/ruby -w
require 'net/smtp'

FROM_EMAIL = "你的邮箱用户名"
PASSWORD = "你的邮箱密码"
TO_EMAIL = FROM_EMAIL

def send_email(title,message)
msgstr = <<#{FROM_EMAIL} Name Your From:>
To: my phone <#{TO_EMAIL}>
Subject: #{title}
Date: #{Time.new.to_s}
#{message}
END_OF_MESSAGE

  Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com',FROM_EMAIL , PASSWORD, :plain) do |smtp|
     
     smtp.send_message msgstr, FROM_EMAIL, TO_EMAIL

  end

end
#增加对邮件的 SSL/TLS支持 摘自stack over flow
# Include hook code here

require 'net/smtp'
require 'timeout'

begin
  require 'openssl'
rescue LoadError
end

Net::SMTP.class_eval do

  alias_method :old_initialize, :initialize
  def initialize(hostname,port)
    @usetls = @@usetls
    old_initialize hostname,port
  end

  @@usetls = false

  def self.enable_tls()
    @@usetls = true
  end

  def self.disable_tls()
    @@usetls = false
  end

  def self.use_tls?()
    @@usetls
  end

  def use_tls?()
    @usetls
  end

  def enable_tls()
    print "tls enabled\n"
    @usetls = true
  end

  def disable_tls()
    @usetls = false
  end

  def use_tls?()
    @usetls
  end

  private
  def do_start(helodomain, user, secret, authtype)
    raise IOError 'SMTP session already started' if @started
    check_auth_args user, secret, authtype if user or secret

    sock = timeout(@open_timeout) {TCPSocket.open(@address, @port) }
    @socket = Net::InternetMessageIO.new(sock)
    @socket.read_timeout = @read_timeout
    @socket.debug_output = STDERR

    check_response(critical {recv_response() } )
    do_helo(helodomain)

    if @usetls
      raise 'openssl is not installed' unless defined?(OpenSSL)
      ssl = OpenSSL::SSL::SSLSocket.new(sock)
      starttls
      ssl.sync_close = true
      ssl.connect

      @socket = Net::InternetMessageIO.new(ssl)
      @socket.read_timeout = @read_timeout
      @socket.debug_output = STDERR
      do_helo(helodomain)
    end

    authenticate user, secret, authtype if user
    @started = true
  ensure
    @socket.close if not @started and @socket and not @socket.closed?
  end

  def do_helo(helodomain)
    begin
      if @esmtp
        ehlo helodomain
      else
        helo helodomain
      end
    rescue Net::ProtocolError
      if @esmtp
        @esmtp = false
        @error_occured = false
        retry
      end
      raise
    end
  end

  def starttls
    getok('STARTTLS')
  end

  def quit
    begin
      getok('QUIT')
    rescue EOFError
      # gmail sucks
    end
  end
end

以上代码在 ruby 1.9.2 ubuntu 10.04 环境下测试通过,运行很稳定~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值