twitter.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const fs = require("fs");
  4. const log4js = require("log4js");
  5. const path = require("path");
  6. const Twitter = require("twitter");
  7. const logger = log4js.getLogger('twitter');
  8. logger.level = 'info';
  9. class default_1 {
  10. constructor(opt) {
  11. this.work = () => {
  12. const lock = this.lock;
  13. if (this.workInterval < 1)
  14. this.workInterval = 1;
  15. if (lock.feed.length === 0) {
  16. setTimeout(() => {
  17. this.work();
  18. }, this.workInterval * 1000);
  19. return;
  20. }
  21. if (lock.workon >= lock.feed.length)
  22. lock.workon = 0;
  23. if (!lock.threads[lock.feed[lock.workon]] ||
  24. !lock.threads[lock.feed[lock.workon]].subscribers ||
  25. lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
  26. logger.error(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  27. lock.feed.splice(lock.workon, 1);
  28. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  29. this.work();
  30. return;
  31. }
  32. let match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
  33. if (match) {
  34. const config = {
  35. owner_screen_name: match[1],
  36. slug: match[2],
  37. };
  38. const offset = lock.threads[lock.feed[lock.workon]].offset;
  39. if (offset > 0)
  40. config.since_id = offset;
  41. this.client.get('lists/statuses', config, (error, tweets, response) => {
  42. console.log(tweets);
  43. });
  44. }
  45. else {
  46. match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)/);
  47. if (match) {
  48. const config = {
  49. screen_name: match[1],
  50. };
  51. const offset = lock.threads[lock.feed[lock.workon]].offset;
  52. if (offset > 0)
  53. config.since_id = offset;
  54. this.client.get('statuses/user_timeline', config, (error, tweets, response) => {
  55. console.log(tweets);
  56. });
  57. }
  58. }
  59. lock.workon++;
  60. let timeout = this.workInterval * 1000 / lock.feed.length;
  61. if (timeout < 1000)
  62. timeout = 1000;
  63. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  64. setTimeout(() => {
  65. this.work();
  66. }, timeout);
  67. };
  68. this.client = new Twitter({
  69. consumer_key: opt.consumer_key,
  70. consumer_secret: opt.consumer_secret,
  71. access_token_key: opt.access_token_key,
  72. access_token_secret: opt.access_token_secret,
  73. });
  74. this.lockfile = opt.lockfile;
  75. this.lock = opt.lock;
  76. this.workInterval = opt.workInterval;
  77. }
  78. }
  79. exports.default = default_1;