Skip to content

Instantly share code, notes, and snippets.

@tg123
Created November 9, 2013 02:32
Show Gist options
  • Save tg123/7380842 to your computer and use it in GitHub Desktop.
Save tg123/7380842 to your computer and use it in GitHub Desktop.
自动订餐脚本 :) 现在刷失效了 开放出来

Fast Eat

北京自动定饭脚本

节约粮食人人有则

北京订餐网址

下载 最新版本

Usage

订餐

./fasteat.sh 用户名 密码

CRON

*/30 * * * * /path/to/fasteat.sh 用户名 密码 > /tmp/fasteat.log 2>&1

关键字黑名单

全部被黑名单拦截会自动降级成无黑名单模式

$ vim ~/.fasteatrc
$ cat ~/.fasteatrc 
BLACK_WORDS='全素 鱼' # 空格分割关键字 含有的字将被过滤 (请用UTF8编码)

~/.fasteatrc 变量说明

如果有这个文件会控制脚本的一些行为

  • USER 用户名 覆盖 argv[0] 并可以省略输入 argv[0]
  • PASS 密码 覆盖 argv[1] 并可以输入 argv[1]
  • MAX_TRY_TIME 单次执行重试次数
  • MAX_FAIL_TO_ALLRAND_TIME 失败多少次后退回到全部随机模式(因为可能由于餐定光了)
  • BLACK_WORDS 空格分割关键字 含有的字将被过滤 (请用UTF8编码)

TODO

  • 加入喜好白名单
  • 更安全的用户名密码使用方式(keychain)
#!/bin/bash
#
#
# [email protected]
#
#
USER=$1
PASS=$2
MAX_FAIL_TO_ALLRAND_TIME=5
MAX_TRY_TIME=10
DING_URL='http://bjdc.taobao.ali.com/'
if [ -e $HOME/.fasteatrc ];then
. $HOME/.fasteatrc
fi
if [ "x$USER" == "x" ] || [ "x$PASS" == "x" ];then
echo "Usage: $0 USERNAME PASSWORD"
exit;
fi
COOKIE=`mktemp --suffix=.FASTEAT_COOKIE`
cleanup(){
rm -f $COOKIE
}
declare -a RESULT
declare -a ALLRESULT
while read -r c; do
c=`echo $c | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e`
blocked=0
for b in $BLACK_WORDS; do
found=`echo $c | grep -v $b`
if [ -z "$found" ];then
blocked=1
echo "keyword $b blocked"
fi
done
url=`echo $c | grep '/dingcan/\d+' -Po`
if [ $blocked == 0 ];then
RESULT[${#RESULT[*]}]="$url"
fi
ALLRESULT[${#ALLRESULT[*]}]="$url"
done< <(curl -s "$DING_URL" |grep '<a.* href="/dingcan/\d+".*<\/a>' -Po)
if [ ${#RESULT[*]} == 0 ];then
echo "WARN: blocked all, failover to all random mode"
RESULT=(${ALLRESULT[*]})
fi
dingcan(){
if [ $1 -gt $MAX_FAIL_TO_ALLRAND_TIME ];then
echo "WARN: failed more than $MAX_FAIL_TO_ALLRAND_TIME times, failover to all random mode"
RESULT=(${ALLRESULT[*]})
fi
i=${#RESULT[*]}
selected=$(($RANDOM % $i))
DISH=${RESULT[$selected]}
echo "Selecting $DING_URL/$DISH"
t=`curl $DING_URL/$DISH -b $COOKIE -I 2>/dev/null | head -1 | cut -f 2 -d ' '`
if [ "$t" == "403" ];then
echo $USER " Login Failed"
cleanup
exit;
fi
}
curl -d "name=$USER&pass=$PASS&form_id=user_login_block" $DING_URL -c $COOKIE 1>/dev/null 2>&1
for i in `seq $MAX_TRY_TIME`; do
dingcan $i
t=`curl $DING_URL -b $COOKIE 2>/dev/null | grep cancel`
if [ "x$t" == "x" ];then
echo "Try $i time waiting 1s";
sleep 1
else
echo $USER " EAT SUCCESS"
break;
fi
done
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment