From 2ca140f730d31e0e4afc56b70a2022ebcf3e2e75 Mon Sep 17 00:00:00 2001 From: RagePeanut Date: Thu, 3 May 2018 21:10:34 +0200 Subject: [PATCH] Fixed same link tweeting issue --- README.md | 3 ++- app.js | 20 +++++++++++++++++--- config.json | 3 ++- package-lock.json | 2 +- package.json | 2 +- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ccfd159..af34b85 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Tweem (0.1.4) +# Tweem (0.1.5) Tweem is a bot that automatically tweets all the resteems and/or recent posts of specified accounts. ## Deploy @@ -49,6 +49,7 @@ Here are all the configuration possibilities: * **stream_nodes:** list of RPC nodes to be used by the app to stream operations (those can be low memory nodes) * **template:** template for the tweet (explained in 'Create your own template') * **tweet_retry_timeout:** time in milliseconds to wait for before retrying to tweet if it failed (default: 10000) +* **twitter_handle:** the **Twitter** handle used by the bot, aka your **Twitter** handle (default: 'RagePeanut_') ## Create your own template **Tweems** aims to be the most configurable sharing bot on the **Steem** blockchain, that's why you can change how your tweets will look by changing their template. Let's take a look at the default template to understand what's happening. diff --git a/app.js b/app.js index 80fee7a..76ef289 100644 --- a/app.js +++ b/app.js @@ -2,7 +2,7 @@ const Twit = require('twit'); const steemStream = require('steem'); const steemRequest = require('steem'); -const { request_nodes, settings, steem_accounts, stream_nodes, tweet_retry_timeout } = require('./config'); +const { request_nodes, settings, steem_accounts, stream_nodes, tweet_retry_timeout, twitter_handle } = require('./config'); const twitter = new Twit({ consumer_key: process.env.CONSUMER_KEY, @@ -36,11 +36,15 @@ function stream() { const op = JSON.parse(operation[1].json); // Checking if it's a resteem and if it's from one of the specified accounts if(op[0] === 'reblog' && steem_accounts.resteems.includes(op[1].account)) { - treatOperation(op[1].author, op[1].permlink, op[0]); + isAlreadyTweeted(op[1].author, op[1].permlink) + .then(alreadyTweeted => !alreadyTweeted && treatOperation(op[1].author, op[1].permlink, op[0])) + .catch(err => console.err('Twitter search API error:', err.message)); } // Checking if it's a post (not a comment) made by one of the specified accounts } else if(settings.tweet_posts && operation[0] === 'comment' && steem_accounts.posts.includes(operation[1].author) && operation[1].parent_author === '') { - treatOperation(operation[1].author, operation[1].permlink, operation[0]); + isAlreadyTweeted(operation[1].author, operation[1].permlink) + .then(alreadyTweeted => !alreadyTweeted && treatOperation(operation[1].author, operation[1].permlink, operation[0])) + .catch(err => console.error('Twitter search API error:', err.message)); } }); // If an error occured, add 1 to the index and put it at 0 if it is out of bound @@ -70,6 +74,16 @@ function tweet(message) { }); } +// Checks if the link has already been tweeted in the last 7 days +function isAlreadyTweeted(author, permlink) { + return new Promise((resolve, reject) => { + twitter.get('search/tweets', { q: 'from:' + twitter_handle + ' url:' + author + ' url:' + permlink }, (err, data, response) => { + if(err) return reject(err); + resolve(data.statuses.length > 0); + }); + }); +} + // Gets the website from which the post has been made if it can view posts function getWebsite(app, author, permlink, url, tags, body) { if(!settings.allowed_apps[app]) return null; diff --git a/config.json b/config.json index 5ce4b5b..da9e50a 100644 --- a/config.json +++ b/config.json @@ -58,5 +58,6 @@ "https://rpc.steemviz.com", "https://steemd.minnowsupportproject.org" ], - "tweet_retry_timeout": 10000 + "tweet_retry_timeout": 10000, + "twitter_handle": "RagePeanut_" } diff --git a/package-lock.json b/package-lock.json index 5f775cb..fdf2902 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "tweem", - "version": "0.1.4", + "version": "0.1.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index c527f0d..1d6d06e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tweem", - "version": "0.1.4", + "version": "0.1.5", "description": "A bot tweeting all the resteems and/or recent posts of specified accounts.", "main": "app.js", "scripts": {