lotto_automation/app.js

54 lines
2.0 KiB
JavaScript

const Fs = require('fs')
const Lotto = require('./lib/lotto')
const NTFY = require('./lib/ntfy')
const Constants = require('./lib/constants')
new class LottoAutomation {
#configFilename = process.env.LOTTO_AUTOMATION_CONFIG_FILENAME ?? 'Config.json'
#configPath = `${process.env.LOTTO_AUTOMATION_CONFIG_DIR ?? __dirname}/${this.#configFilename}`
#config = {}
#_start() {
this.#_loadConfig()
setImmediate(this.#_buyLotto.bind(this))
}
#_loadConfig() {
if(!Fs.existsSync(this.#configPath))
throw new Error('Config.json 파일이 존재하지 않습니다.')
try {
const configString = Fs.readFileSync(this.#configPath)
this.#config = JSON.parse(configString)
NTFY.Init(this.#config.ntfyUrl, this.#config.ntfyToken)
} catch(e) {
throw new Error('Config.json 파일이 올바르지 않습니다.')
}
}
async #_buyLotto() {
const loginResult = await Lotto.Login(this.#config.id, this.#config.password)
if(loginResult instanceof Error)
return NTFY.Send('로그인 실패 - ' + loginResult.message ?? loginResult.description, 'no_entry_sign')
const buyResult = await Lotto.Buy(this.#config.buyLottoPolicy)
if(buyResult instanceof Error)
return NTFY.Send('구입 실패 - ' + loginResult.message ?? loginResult.description, 'no_entry_sign')
const successMessageArr = [
`구매 성공`,
`ROUND: ${buyResult.round}`,
`COST: ${buyResult.cost}`,
]
successMessageArr.push(
...buyResult.numbers.map(numberStr => {
const numberType = Constants.LOTTO_BUY_TYPES[numberStr.slice(-1)]
const [idx, ...numbers] = numberStr.slice(0, -1).split('|')
return `NUMBER ${idx} [${numberType}]: ${numbers.join(', ')}`
})
)
NTFY.Send(successMessageArr.join('\n'), 'tada')
}
constructor() { setImmediate(this.#_start.bind(this)) }
}