Last active
May 22, 2017 14:40
-
-
Save hikerpig/10013696 to your computer and use it in GitHub Desktop.
用casperjs暴力签到虾米。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
utils = require('utils') | |
fs = require('fs') | |
casper = require("casper").create( | |
verbose: true | |
logLevel: "warning" # 想看更详细的log的话可以改成"debug" | |
pageSettings: | |
loadImages: false | |
loadPlugins: false | |
userAgent: "Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0" | |
) | |
casper.options.viewportSize = | |
width: 1680 | |
height: 924 | |
accountDatas = JSON.parse fs.read('data.json', 'utf-8') | |
# 读取处于当前路径下的data.json文件 | |
# 注意到casperjs 使用的是phantomjs的fs而不是node的fs, 所以fs.readFileSync方法不存在 | |
accountDatas = accountDatas.accounts | |
prevResultObj = JSON.parse fs.read('result.json', 'utf-8') | |
prevResult = prevResultObj.result | |
result = {data: []} | |
diffDay = (oldStamp, newStamp)-> | |
oldDate = new Date(oldStamp).getDate() | |
newDate = new Date(newStamp).getDate() | |
Math.abs(oldDate - newDate) | |
nowStamp = +new Date() | |
accountsToSign = [] | |
for account in accountDatas | |
include = false | |
for _a in prevResult | |
if _a.email is account.email | |
include = true | |
unless (_a.status is 'success') and diffDay(_a.timestamp, nowStamp) is 0 | |
accountsToSign.push account | |
continue | |
unless include | |
accountsToSign.push account | |
console.log "accountsToSign is:" | |
utils.dump(accountsToSign) | |
if accountsToSign.length is 0 | |
console.log "今天的签到已经全部成功" | |
casper.exit() | |
casper.on 'xiami.data', (info)-> | |
console.log info | |
casper.on 'xiami.success', (infoObj)-> | |
infoObj.status = 'success' | |
infoObj.timestamp = +new Date | |
result.data.push infoObj | |
casper.on 'xiami.fail', (infoObj)-> | |
infoObj.status = 'fail' | |
infoObj.timestamp = +new Date | |
console.log "#{infoObj.msg},#{infoObj.email}" | |
result.data.push infoObj | |
casper.on 'error', (msg, backtrace)-> | |
result.data = {status: 'fail', msg: msg, timestamp: +new Date} | |
console.log msg | |
casper.start().each accountsToSign, (self, account) -> | |
email = account.email | |
password = account.password | |
casper.thenOpen "https://login.xiami.com/member/login" | |
casper.waitForSelector "form input[name='email']", (success = -> | |
console.log "为账户" + email + "签到ing" | |
@fill "form", | |
email: account.email | |
password: account.password | |
, false | |
@click "input[type='submit']" | |
), fail = ()-> | |
@emit 'xiami.fail', {msg: "神马!竟然没有email input", email: account.email} | |
casper.waitFor (check = -> | |
# 回到主页 | |
@getCurrentUrl().indexOf("www.xiami.com") > -1 | |
), (success = -> | |
console.log "登录成功" | |
) | |
casper.waitForSelector("#user .action .tosign", ()-> | |
console.log "执行登录后的其它操作" | |
el = this | |
date = new Date() | |
year = date.getYear() | |
month = date.getMonth() | |
day = date.getDate() | |
hour = date.getHours() | |
timeString = [ year, month, day, hour ].join("-") | |
unless @exists("#user .action .tosign.done") | |
@click "#user .tosign" | |
@waitForSelector "#user .action .done", (success = -> | |
@emit 'xiami.success', {email: account.email} | |
el.captureSelector "captures/" + email + " " + timeString + ".png", "#user" | |
), fail = -> | |
@emit 'xiami.fail', {msg: "未出现打卡成功提示,打卡失败", email: account.email} | |
else | |
console.log "今天已经打过卡啦!" + account.email | |
@emit 'xiami.success', {email: account.email} | |
, ()-> | |
console.log "签到按钮没有出现?, 我们在这里#{this.getCurrentUrl()}" | |
@capture "captures/fail-#{email}-#{timeString}.png" | |
) | |
casper.then -> | |
if @exists(".user_popup .content .logout") | |
@click ".user_popup .content li>a.logout" | |
else | |
console.log "找不到登出按钮" | |
@captureSelector "captures/fail-#{email}.png", "#user" | |
casper.waitFor(()-> | |
return @getCurrentUrl().indexOf("www.xiami.com") > -1 | |
,()-> | |
console.log('登出回到主页') | |
) | |
casper.waitForSelector "#login_xiami", (-> | |
console.log "登录按钮又出现了,退出登录成功" + email | |
casper.thenOpen "https://login.xiami.com/member/login" | |
), ()-> | |
casper.echo "超时了哇" | |
casper.run(()-> | |
fs.write('result.json', JSON.stringify({timestamp: +new Date(), result: result.data})) | |
casper.exit() | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
schedule = require 'node-schedule' | |
process = require 'child_process' | |
fs = require 'fs' | |
log = null | |
rule = new schedule.RecurrenceRule() | |
if process.env is 'development' | |
rule.second = 1 # 每隔一分钟,测试用 | |
else | |
rule.hour = 1 # 凌晨1点签到 | |
rule.minute = 0 | |
rule.second = 0 | |
# 写入log文件中 | |
writeLog = (text)-> | |
log = fs.createWriteStream('log.txt', {'flags': 'a', encoding: 'utf-8'}) | |
log.end(text) | |
run = ()-> | |
child = process.spawn 'casperjs', ['xiami_casper.coffee'] | |
child.stdout.on('data', (data)-> | |
text = new Date() + data.toString() | |
writeLog(text) | |
console.log text | |
) | |
child.stdout.on('end', ()-> | |
writeLog("Run job on #{new Date().toString()}") | |
) | |
child.on 'error', ()-> | |
console.log arguments | |
job = schedule.scheduleJob(rule, ()-> | |
run() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment