`
takingoff
  • 浏览: 22071 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

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

    博客分类:
  • Ruby
阅读更多

因为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 环境下测试通过,运行很稳定~~

分享到:
评论
7 楼 cg7662069 2011-06-27  
订阅那个网页不是就行了么?
6 楼 wanggp 2011-06-27  
非常时刻的工具
5 楼 lioncin 2011-06-27  
以前公司还有人 用python 写个脚本 5S 刷新一次 火车票 然后用skype 绑定 一旦有新票就自动播号码过去的 结果还真让他找到了回家的车票呢
4 楼 xiaovsme 2011-06-25  
科技改变生活
3 楼 attol 2011-06-23  
现在官网有货
2 楼 takingoff 2011-06-21  
sevk 写道
发送gmail可以用 gem install gmail_sender
不用自己写代码的,方便。

      g = GmailSender.new('xxxxx@gmail.com', 'password')
      #next unless File.exist? fn
      #g.attach(fn) # you can attach any number of files, but there are limits for total attachments size
      ti = now.strftime("%y-%m-%d")
      p g.send(:to => t, :subject => ti, :content => File.read(fn))


好的 谢谢 当时第一反应就是用smtp类来发 有没有支持很多邮箱的gem 支持飞信的gem
1 楼 sevk 2011-06-17  
发送gmail可以用 gem install gmail_sender
不用自己写代码的,方便。

      g = GmailSender.new('xxxxx@gmail.com', 'password')
      #next unless File.exist? fn
      #g.attach(fn) # you can attach any number of files, but there are limits for total attachments size
      ti = now.strftime("%y-%m-%d")
      p g.send(:to => t, :subject => ti, :content => File.read(fn))

相关推荐

Global site tag (gtag.js) - Google Analytics