Guest User

trail

a guest
Jul 3rd, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.67 KB | None | 0 0
  1. const steem = require('steem');
  2. const config = require('./config')
  3. const NaturalLanguageUnderstandingV1 = require('watson-developer-cloud/natural-language-understanding/v1.js')
  4. const winston = require('winston')
  5. const fs = require('fs')
  6. const path = require('path')
  7. const filename = path.join(__dirname, './trail.log')
  8. const labels = config.labels
  9. const following = config.following
  10.  
  11. steem.api.setOptions({ url: 'http://rpc.buildteam.io/' })
  12.  
  13. const logger = winston.createLogger({
  14. transports: [
  15. new winston.transports.Console(),
  16. new winston.transports.File({
  17. filename: filename
  18. })
  19. ]
  20. });
  21.  
  22. const nlu = new NaturalLanguageUnderstandingV1({
  23. username: config.username,
  24. password: config.password,
  25. version: '2018-04-05',
  26. url: config.url
  27. });
  28.  
  29. const ACC_NAME = config.accname,
  30. ACC_KEY = config.posting;
  31. console.log("Curation Trail Bot Script Running...")
  32. function startTrail() {
  33. try {
  34. steem.api.streamTransactions('head', function(err, result) {
  35. if (!err) {
  36. try {
  37. const type = result.operations[0][0];
  38. const data = result.operations[0][1];
  39. let weight = 0;
  40.  
  41. if (type === 'vote') {
  42. var i;
  43. for (i = 0; i < following.length; i++) {
  44. const followed = following[i];
  45.  
  46. if (data.voter !== data.author && data.voter === followed.account || (data.voter === followed.account && followed.whitelisted === true)) {
  47. weight = Math.round(data.weight * followed.weight_divider);
  48. weight = weight > followed.max_weight ? followed.max_weight : weight;
  49. var comment = followed.comment.replace('{AUTHOR}', data.author).replace('{VOTER}', data.voter)
  50. logger.log({
  51. level: 'info',
  52. message: `${data.voter} just voted ${data.author}/${data.permlink} by weight ${weight}`
  53. })
  54. console.log(data);
  55. setTimeout(function() {
  56. StreamVote(data.author, data.permlink, weight, comment, followed.check_context)
  57. }, 45000 * i);
  58. console.log('@' + data.voter + ' Just voted now!');
  59. }
  60. }
  61. }
  62. }catch(e) {
  63. console.log(e)
  64. }
  65. }
  66. try {
  67. if (err)
  68. {
  69. console.log(err);
  70. throw `${err}`
  71. }
  72. }
  73. catch (e) {
  74. console.log(e)
  75. startTrail()
  76. }
  77. })
  78. }
  79. catch (e) {
  80. logger.log({
  81. level: 'info',
  82. message: `${e}`
  83. })
  84. startTrail()
  85. }
  86. }
  87.  
  88. startTrail()
  89.  
  90.  
  91. function StreamVote(author, permalink, weight, comment, check_context) {
  92. try {
  93. if(weight === 0)
  94. {
  95. logger.log({
  96. level: 'info',
  97. message: `It was a unvote on ${author}/${permalink}`
  98. })
  99. steem.api.getContentReplies(author, permalink, function(err, result) {
  100. if(err)
  101. {
  102. StreamVote(author, permalink, weight, comment, check_context)
  103. }
  104. for(reply in result)
  105. {
  106. if(result[reply].author === ACC_NAME)
  107. {
  108. comment = `Unfortunately, your contribution's vote was removed,as the trailing community vote was removed.\n\nFor any inquiries, contact our support team at https://support.utopian.io/`;
  109. applyVote(ACC_KEY, ACC_NAME, author, permalink, weight, comment, result[reply].permlink);
  110. }
  111. }
  112. })
  113. }
  114. else {
  115. steem.api.getContent(author, permalink, function(err, result) {
  116. if (!err) {
  117. var hasVoted = false;
  118.  
  119. if (weight !== 0) {
  120. var v;
  121. for(v = 0; v < result.active_votes.length; v++) {
  122. const activeVote = result.active_votes[v];
  123.  
  124. if (activeVote.voter === 'utopian-io') {
  125. logger.log({
  126. level: 'info',
  127. message: `${activeVote} has already voted this`
  128. })
  129. hasVoted = true;
  130. }
  131. }
  132. }
  133.  
  134. console.log(hasVoted);
  135.  
  136. if (!hasVoted && JSON.parse(result.json_metadata).tags[0] !== 'utopian-io' && result.depth === 0) {
  137. if(weight == 0)
  138. {
  139. logger.log({
  140. level: 'info',
  141. message: `Going to unvote it`
  142. })
  143. applyVote(ACC_KEY, ACC_NAME, author, permalink, weight, comment);
  144. }
  145. else {
  146. if (check_context === true) {
  147. try {
  148. nlu.analyze({
  149. text: result.body,
  150. features: {
  151. categories: {}
  152. }
  153. },
  154. function(err, response) {
  155. if (err) {
  156. console.log('error:', err);
  157. logger.log({
  158. level: 'info',
  159. message: `${err} from watson`
  160. })
  161. }
  162.  
  163. if (!err && response) {
  164. console.log(response);
  165. var vote = false;
  166. console.log('First check for labels');
  167. var c;
  168. logger.log({
  169. level: 'info',
  170. message: `${response.categories}`
  171. })
  172. for(c = 0; c < response.categories.length; c++) {
  173. const category = response.categories[c];
  174. if (labels.includes(category.label) === true && category.score >= 0.65) {
  175. vote = true;
  176.  
  177. }
  178. }
  179. console.log(vote);
  180.  
  181. if (vote === true) {
  182. applyVote(ACC_KEY, ACC_NAME, author, permalink, weight, comment);
  183. }
  184. }
  185. })
  186. }catch(e) {
  187. console.log(e)
  188. }
  189. }
  190.  
  191. if (check_context === false) {
  192. applyVote(ACC_KEY, ACC_NAME, author, permalink, weight, comment);
  193. }
  194. }
  195. }
  196. }
  197.  
  198. if (err) {
  199. StreamVote(author, permalink, weight, comment, check_context)
  200. console.log(err);
  201. logger.log({
  202. level: 'info',
  203. message: `${err}`
  204. })
  205. }
  206. })
  207. }
  208. }catch(e) {
  209. StreamVote(author, permalink, weight, comment, check_context)
  210. console.log(e)
  211. }
  212. }
  213.  
  214. function applyVote (ACC_KEY, ACC_NAME, author, permalink, weight, comment, newpermlink = new Date().toISOString().replace(/[^a-zA-Z0-9]+/g, '').toLowerCase()) {
  215. try {
  216. steem.broadcast.vote(ACC_KEY, ACC_NAME, author, permalink, weight, function(err, result) {
  217. if (err) {
  218. logger.log({
  219. level: 'info',
  220. message: `${err}`
  221. })
  222. console.log(err);
  223. StreamVote(author, permalink, weight, comment, false)
  224. }
  225. if (!err && result) {
  226. logger.log({
  227. level: 'info',
  228. message: `Voted ${author}/${permalink}`
  229. })
  230. console.log('Voted Succesfully, permalink: ' + permalink + ', author: ' + author + ', weight: ' + weight / 100 + '%.');
  231. steem.broadcast.comment(ACC_KEY, author, permalink, ACC_NAME, newpermlink, '', comment, {
  232. tags: ['utopian.tip'],
  233. app: 'utopian-io'
  234. }, function(err, result) {
  235. if (err) {
  236. console.log(err);
  237. logger.log({
  238. level: 'info',
  239. message: `${err}`
  240. })
  241. } else {
  242. logger.log({
  243. level: 'info',
  244. message: `Commented`
  245. })
  246. console.log('Commented on the post');
  247. }
  248. });
  249. }
  250. });
  251. }catch(e){
  252. logger.log({
  253. level: 'info',
  254. message: `${e}`
  255. })
  256. StreamVote(author, permalink, weight, comment, false)
  257. console.log(e);
  258. }
  259. }
Add Comment
Please, Sign In to add comment