2016年9月12日 星期一

iPhone 7 開賣通知程式

過去數年,我能成功購買到第一水 iPhone 及 iPad,是因為編寫了簡單的開賣通知程式,在開賣的一刻以電郵通知我。不過,程式寫得不好,容易出現誤鳴。所以今年重新設計,同時以新學到的 Bash 技術實現。代碼如下:
#!/bin/bash

##----------------------------------------------------------------------------------------
##  iPhone 7 Availability Monitor
##----------------------------------------------------------------------------------------
##  Platform: CentOS7 + bash
##  Written by Pacess
##  Copyright 2016 Pacess Studio.  All rights reserved.
##----------------------------------------------------------------------------------------

##  Variables
userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" 
directory="snapshot"
log="guard.log"

timeOut=5
noOfRetry=3

##----------------------------------------------------------------------------------------
function PHLog  {
   echo "$1"
   echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >> "$log"
}

##----------------------------------------------------------------------------------------
##  Program start
echo "$(date '+%Y-%m-%d %H:%M:%S')"
echo "-----------------------------------------------------------"
echo "--  iPhone 7 Availability Monitor Version 1.00           --"
echo "--  Written by Pacess                                    --"
echo "--  Copyright 2016 Pacess Studio.  All rights reserved.  --"
echo "-----------------------------------------------------------"
echo ""

##  Move to home directory first
cd /root/iphone-7-availability-monitor

##----------------------------------------------------------------------------------------
##  Retrieving website content
echo "Retrieving website content..."
curl -s --retry $noOfRetry -m $timeOut -A "$userAgent" http://www.apple.com/hk/shop/buy-iphone/iphone-7/5.5-inch-display-128gb-jet-black#01,10,20 > "$directory/store.apple.com.html"
errorCode=$?;  test "$errorCode" -ne "0" && { PHLog "curl exit error code: $errorCode";  exit; }

##----------------------------------------------------------------------------------------
##  Load website content
content=$(cat "$directory/store.apple.com.html")
checkSum=$(echo "$content" | md5sum)
echo "Check sum: $checkSum"

##  Check if keyword "Relate" found
wording=$(grep -o ">Add to Bag<" "$directory/store.apple.com.html")
wordingSize=${#wording}
echo "Wording size: $wordingSize"
if (($wordingSize < 12)); then
   echo "Content ok!"
else
   PHLog "Apple website content have been changed..."

   ##  Save file
   mv "$directory/store.apple.com.html" "$directory/$(date '+%Y%m%d%H%M%S')_store.apple.com.html"

   ##  Update checksum, prevent sending email every 15 mins, use > will replace existing contents
   echo "$checkSum" > store.apple.com.md5

   ##----------------------------------------------------------------------------------------
   ##  Send email alert
   echo -e "Dear Pacess,\n\niPhone 7 is now available at http://www.apple.com/hk/shop/buy-iphone/iphone-7/5.5-inch-display-128gb-jet-black#01,10,20.\n\nThanks!\n\nSita-bot" | mail -r "robot@pacess.com (Sita-bot)" -s "[Monitor] iPhone 7 is available now..." pacess@pacess.com
fi

3 則留言:

Sonia SS 提到...

可以用 bash 寫到monitoring, 好厲害 !

Pacess HO 提到...

Hi Sonia SS,我只是抄抄改改而已。:P

Sonia SS 提到...

實不相瞞,寫左 unix script 廿年,從未想過可以咁用,慚愧、慚愧。