index.js 862 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env node
  2. import helper from './helper.js'
  3. import readline from 'readline'
  4. const rl = readline.createInterface({ input: process.stdin })
  5. rl.on('line', async line => {
  6. // 任务信息
  7. const job = JSON.parse(line)
  8. console.log("running job: " + job.id + ": " + JSON.stringify(job))
  9. // 检查参数
  10. const {
  11. notion_key: NOTION_KEY,
  12. database_id: DATABASE_ID,
  13. base_currency: BASE_CURRENCY
  14. } = job.params
  15. if (NOTION_KEY === null || DATABASE_ID === null || BASE_CURRENCY == null) {
  16. helper.returnFailed(1, 'params `notion_key`, `database_id`, ' +
  17. '`base_currency` required')
  18. return
  19. }
  20. // 获取源数据
  21. const rateMap = await helper.fetchExchangeRateData(BASE_CURRENCY)
  22. // 更新汇率
  23. await helper.updateRate(NOTION_KEY, DATABASE_ID, rateMap)
  24. // 处理成功
  25. helper.returnSuccess()
  26. rl.close();
  27. });