main.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env node
  2. "use strict";
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. const fs = require("fs");
  5. const path = require("path");
  6. const commandLineUsage = require("command-line-usage");
  7. const exampleConfig = require("../config.example.json");
  8. const command_1 = require("./command");
  9. const loggers_1 = require("./loggers");
  10. const mirai_1 = require("./mirai");
  11. const twitter_1 = require("./twitter");
  12. const logger = loggers_1.getLogger();
  13. const sections = [
  14. {
  15. header: 'MiraiTS Twitter Bot',
  16. content: 'The QQ Bot that forwards twitters.',
  17. },
  18. {
  19. header: 'Synopsis',
  20. content: [
  21. '$ mirai-twitter-bot {underline config.json}',
  22. '$ mirai-twitter-bot {bold --help}',
  23. ],
  24. },
  25. {
  26. header: 'Documentation',
  27. content: [
  28. 'Project home: {underline https://github.com/CL-Jeremy/mirai-twitter-bot}',
  29. 'Example config: {underline https://git.io/JJ0jN}',
  30. ],
  31. },
  32. ];
  33. const usage = commandLineUsage(sections);
  34. const args = process.argv.slice(2);
  35. if (args.length === 0 || args[0] === 'help' || args[0] === '-h' || args[0] === '--help') {
  36. console.log(usage);
  37. process.exit(0);
  38. }
  39. const configPath = args[0];
  40. let config;
  41. try {
  42. config = JSON.parse(fs.readFileSync(path.resolve(configPath), 'utf8'));
  43. }
  44. catch (e) {
  45. console.log('Failed to parse config file: ', configPath);
  46. console.log(usage);
  47. process.exit(1);
  48. }
  49. const requiredFields = [
  50. 'twitter_consumer_key', 'twitter_consumer_secret', 'twitter_access_token_key', 'twitter_access_token_secret',
  51. ];
  52. const warningFields = [
  53. 'mirai_http_host', 'mirai_http_port', 'mirai_access_token',
  54. ];
  55. const optionalFields = [
  56. 'lockfile', 'work_interval', 'webshot_delay', 'loglevel', 'mode', 'resume_on_start',
  57. ].concat(warningFields);
  58. if (requiredFields.some((value) => config[value] === undefined)) {
  59. console.log(`${requiredFields.join(', ')} are required`);
  60. process.exit(1);
  61. }
  62. optionalFields.forEach(key => {
  63. if (config[key] === undefined || typeof (config[key]) !== typeof (exampleConfig[key])) {
  64. if (key in warningFields)
  65. logger.warn(`${key} is undefined, use ${exampleConfig[key] || 'empty string'} as default`);
  66. config[key] = exampleConfig[key];
  67. }
  68. });
  69. loggers_1.setLogLevels(config.loglevel);
  70. let lock;
  71. if (fs.existsSync(path.resolve(config.lockfile))) {
  72. try {
  73. lock = JSON.parse(fs.readFileSync(path.resolve(config.lockfile), 'utf8'));
  74. }
  75. catch (err) {
  76. logger.error(`Failed to parse lockfile ${config.lockfile}: `, err);
  77. lock = {
  78. workon: 0,
  79. feed: [],
  80. threads: {},
  81. };
  82. }
  83. fs.access(path.resolve(config.lockfile), fs.constants.W_OK, err => {
  84. if (err) {
  85. logger.fatal(`cannot write lockfile ${path.resolve(config.lockfile)}, permission denied`);
  86. process.exit(1);
  87. }
  88. });
  89. }
  90. else {
  91. lock = {
  92. workon: 0,
  93. feed: [],
  94. threads: {},
  95. };
  96. try {
  97. fs.writeFileSync(path.resolve(config.lockfile), JSON.stringify(lock));
  98. }
  99. catch (err) {
  100. logger.fatal(`cannot write lockfile ${path.resolve(config.lockfile)}, permission denied`);
  101. process.exit(1);
  102. }
  103. }
  104. if (!config.resume_on_start) {
  105. Object.keys(lock.threads).forEach(key => {
  106. lock.threads[key].offset = '-1';
  107. });
  108. }
  109. const qq = new mirai_1.default({
  110. access_token: config.mirai_access_token,
  111. host: config.mirai_http_host,
  112. port: config.mirai_http_port,
  113. bot_id: config.mirai_bot_qq,
  114. list: (c, a, cb) => command_1.list(c, a, cb, lock),
  115. sub: (c, a, cb) => command_1.sub(c, a, cb, lock, config.lockfile),
  116. unsub: (c, a, cb) => command_1.unsub(c, a, cb, lock, config.lockfile),
  117. });
  118. const worker = new twitter_1.default({
  119. consumerKey: config.twitter_consumer_key,
  120. consumerSecret: config.twitter_consumer_secret,
  121. accessTokenKey: config.twitter_access_token_key,
  122. accessTokenSecret: config.twitter_access_token_secret,
  123. lock,
  124. lockfile: config.lockfile,
  125. workInterval: config.work_interval,
  126. bot: qq,
  127. webshotDelay: config.webshot_delay,
  128. mode: config.mode,
  129. wsUrl: config.playwright_ws_spec_endpoint,
  130. });
  131. worker.launch();
  132. qq.connect();