2017년 11월 3일 금요일

11번가 자동 출첵, 2017.11.03 (casperjs)

기존에 curl 을 이용한 방식은 onclick등 자바스크립트 함수가 들어가면 매우 골치아파지는 문제가 있었다.

그래서 검색하다 'headless browser' 라는 걸 알게 됐고, casperjs 란 알게 됐다.


 

11번가 자동 출첵은 casperjs 로 만들어서 기존에 사용하고 있었는데,

9월 30일 이후로 출첵이 안된 거 보니 그 때 출석체크 페이지가 바뀐듯 하다.

 

11번가 자동 출첵은 왜 하느냐?

마일리지를 받기 위해서다. 마일리지로 뭘 할 수 있는냐?

사람들이 잘 모르는데, 11번가 마일리지로 할 수 있는게 많다.

상품쿠폰,배송비쿠폰, 핸드폰 데이터 쿠폰 그리고 상품 교환도 된다.

베스킨라빈스 레귤러 한번 먹어 봤다.

 

각설하고 11번가 자동 출체 코드는 아래와 같다.
var casper = require('casper').create({
    pageSettings: {
        "userAgent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
        "loadImages": true,
        "loadPlugins": false,
        "webSecurityEnabled": false,
        "ignoreSslErrors": true
    },
    onWaitTimeout: function () {
        //throw new Error
    },
    onStepTimeout: function () {
        //throw new Error
    },
    encoding: "utf8",
    waitTimeout: 10000,
    stepTimeout: 10000,
     logLevel: "debug",              // Only "info" level messages will be logged
     verbose: true                  // log messages will be printed out to the console
});

var login_id = casper.cli.get("id");
var login_pw = casper.cli.get("pw");
var login_url = 'https://login.11st.co.kr/login/Login.tmall';
var attendance_url = 'http://www.11st.co.kr/browsing/MallPlanDetail.tmall?method=getMallPlanDetail&planDisplayNumber=935566';

//temp
login_id = 'myid';
login_pw = 'mypass';

if(!login_id){
    casper.echo("require id parameter");
//    casper.exit();  //not working
    phantom.exit();
}
if(!login_pw){
    casper.echo("require pw parameter");
    phantom.exit(1);
}


casper.start(login_url, function() {
    this.fill('form[name="login_form"]', {
        'loginName' : login_id,
        'passWord': login_pw
    }, false);

    this.click('#memLogin > div.save_idW > input');
    this.wait(1000, function() {
        //this.echo("I've waited for a second.");
    });
});


//출석
casper.thenOpen(attendance_url, function(){
    //iframe
    this.withFrame(1, function () {
		this.click('#regForm > div > div.sect03 > div.dev04 > a.get04 > img');
		this.wait(1000, function() {
			this.setFilter("page.confirm", function(msg) {
				return true;
			});
		});
	});

});

//casper.run();
casper.run(function() {
    require('utils').dump(this.result.log);
    this.exit();
});

 

당연하지만 매일 자동으로 실행하려면 매일 켜져있는 pc(server)가 필요하다.

테스트용 리눅스 pc가 있어서 casperjs 설치 후 cron으로 매일 실행 되게끔 설정하였다.

 

없으신 분들은 알아서...;;

댓글 없음:

댓글 쓰기