takingoff 发表于 2013-1-27 12:33:40

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

因为IPad2在官网 停止销售有一段时间。
有可能恢复供货也被抢购掉。(IPhone4 在今年2月份时 在官网抢购一台4999转手能卖到5700以上)
下面ruby代码 每半小时访问一次 苹果在线商店,如果货源信息变动,发邮件给指定联系人。
代码量很少,在这种小程序上ruby真的很方便~~
#主要流程#!/usr/bin/ruby -w#coding:UTF-8require '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 miniteseval File.read("smtp_tls2.rb")Net::SMTP.enable_tls()while truebeginheaders,body=http.get(path)if headers.code=='200'   str=bodystr=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"    endelse    send_email('apple网站返回了错误信息',"苹果网站返回的错误信息: #{headers.code} #{headers.message}")    p "apple website has returned error message: #{headers.code} #{headers.message}\n"endrescue    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";    endensure    p 'last check time:'+Time.new.to_s+"\n"    sleep SLEEP_TIME*60endend#发送email部分 这里针对GMAIL配置#!/usr/bin/ruby -wrequire 'net/smtp'FROM_EMAIL = "你的邮箱用户名"PASSWORD = "你的邮箱密码"TO_EMAIL = FROM_EMAILdef 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_MESSAGENet::SMTP.start('smtp.gmail.com', 587, 'gmail.com',FROM_EMAIL , PASSWORD, :plain) do |smtp|          smtp.send_message msgstr, FROM_EMAIL, TO_EMAILendend#增加对邮件的 SSL/TLS支持 摘自stack over flow# Include hook code hererequire 'net/smtp'require 'timeout'beginrequire 'openssl'rescue LoadErrorendNet::SMTP.class_eval doalias_method :old_initialize, :initializedef initialize(hostname,port)    @usetls = @@usetls    old_initialize hostname,portend@@usetls = falsedef self.enable_tls()    @@usetls = trueenddef self.disable_tls()    @@usetls = falseenddef self.use_tls?()    @@usetlsenddef use_tls?()    @usetlsenddef enable_tls()    print "tls enabled\n"    @usetls = trueenddef disable_tls()    @usetls = falseenddef use_tls?()    @usetlsendprivatedef 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 = trueensure    @socket.close if not @started and @socket and not @socket.closed?enddef 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    endenddef starttls    getok('STARTTLS')enddef quit    begin      getok('QUIT')    rescue EOFError      # gmail sucks    endendend以上代码在 ruby 1.9.2 ubuntu 10.04 环境下测试通过,运行很稳定~~
页: [1]
查看完整版本: 第一时间获取苹果官网IPad2供货信息