로또 (lotto) 데이터 수집/분석 - node.js

 

PHP버젼과 파이선버젼으로 만든것도 있었는데 소스가 어디갔는지...

소스는 github 에서 다운로드 가능합니다.

https://github.com/goldtagworks/node-lotto.git

 

GitHub - goldtagworks/node-lotto

Contribute to goldtagworks/node-lotto development by creating an account on GitHub.

github.com

 

 

const util = require('util');
const https = require('https');
const axios = require('axios');
const mysql = require('mysql');

class Tasker {
    constructor() {
        this.pool = null;
    }
    
    async initialize() {
        this.pool = await mysql.createPool({
              host: '127.0.0.1'
            , port: 3306
            , user: 'root'
            , password: ''
            , database: 'lotto'
        });

        this.pool.query = await util.promisify(this.pool.query);
    }
    
    async finalize() {
        await this.pool.end();
    }

    async getLotto(drwNo = 1) {
        let url = 'https://www.nlotto.co.kr/common.do?method=getLottoNumber&drwNo=' + drwNo;

        return await axios.get(url, {
            headers: { 'Content-Type': 'application/json' },
            responseType: 'json',
            httpsAgent: new https.Agent({ rejectUnauthorized: false })
        });
    }

    async loadHistory() {
        let result = 1;
        try {
            let sql = '';
            sql += 'SELECT * FROM history ORDER BY drwNo desc LIMIT 1';
            
            let rows = await this.pool.query(sql);
            if (rows.length != 0) {
                result = rows[0].drwNo + 1;
            }
        } catch (err) {
            throw new Error(err);
        }
        
        return result;
    }

    async saveHistory(data) {
        try {
            let sql = '';
            sql += 'INSERT INTO history (drwNo, drwNoDate, drwtNo1, drwtNo2, drwtNo3, drwtNo4, drwtNo5, drwtNo6, bnusNo, firstWinamnt, firstPrzwnerCo, firstAccumamnt, totSellamnt) ';
            sql += 'VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
            
            let params = [data.drwNo, data.drwNoDate, data.drwtNo1, data.drwtNo2, data.drwtNo3, data.drwtNo4, data.drwtNo5, data.drwtNo6, data.bnusNo, data.firstWinamnt, data.firstPrzwnerCo, data.firstAccumamnt, data.totSellamnt];
            await this.pool.query(sql, params);
        } catch (err) {
            throw new Error(err);
        }
    }

    async main() {
        await this.initialize();
        
        let drwNo = await this.loadHistory();
        let response = await this.getLotto(drwNo);
        if (response.status == 200 && response.data.returnValue == 'success') {
            await this.saveHistory(response.data);
        }
        
        await this.finalize();
    }
}

let tasker = new Tasker();
tasker.main();

'개발 > node' 카테고리의 다른 글

자바스크립트를 처음 배운게...  (1) 2024.10.13