Monitoring all published posts by tag in STEEMIT on nodejs

in Steem Marketing2 years ago (edited)

Green Simple Clean Nature YouTube Thumbnail.png

Hi everybody!

clone repo
cd tutorials\04_get_posts
npm i
npm run dev-server or npm run start
After a few moments, the server should be running at http://localhost:3000/

I get script:

const dsteem = require('dsteem');

let opts = {};

//connect to production server
opts.addressPrefix = 'STM';

//connect to server which is connected to the network/production
const client = new dsteem.Client('https://api.steemit.com');
let window2;

    const filter = "created";
    const query = {tag: "s",limit: 100}
 
    console.log('Post assembled.\nFilter:', filter, '\nQuery:', query);

client.database
        .getDiscussions(filter, query)
        .then(result => {
            console.log('Response received:', result);
            if (result) {
                var posts = [];
                result.forEach(post => {
                    const title = post.title;
                    const author = post.author;
                    const created = new Date(post.created).toDateString();
                    let upvote = post.active_votes.length;

            
        const sql = `INSERT INTO allposts(author, title, created, length, url, comments, upvotes, points,percent) VALUES('${author}', '${title}', '${created}', '${post.body_length}', '${post.url}', '${post.children}', '${upvote}','0','999')`;      

                                
        connection.query(sql, function(err, results) {
            if(err) console.log(err);
        console.log(results);
                });     

                });

            } else {
                console.log('No result');

            }
        })
        .catch(err => {
            console.log(err);
        });

And then I create code for add information to database. I use MySql database.

        const sql = `INSERT INTO allposts(author, title, created, length, url, comments, upvotes, points,percent) VALUES('${author}', '${title}', '${created}', '${post.body_length}', '${post.url}', '${post.children}', '${upvote}','0','999')`;      

                                
        connection.query(sql, function(err, results) {
            if(err) console.log(err);
        console.log(results);
                }); 

And full script:

const dsteem = require('dsteem');

let opts = {};

//connect to production server
opts.addressPrefix = 'STM';
opts.chainId =
    '0000000000000000000000000000000000000000000000000000000000000000';

//connect to server which is connected to the network/production
const client = new dsteem.Client('https://api.steemit.com');

//CREATE DATABASE [IF NOT EXISTS] имя_базы_даных;
const mysql = require("mysql2");
const steem = require('steem');
const fs = require("fs");    
var settings = require('./config.js');

let body3R = '';
let num_day = settings.num_day;
let bd_name = settings.bd_name;
let hive_name = settings.hive_name;
let url_post = settings.url_post;
let password_mysql = settings.password_mysql;
let user_mysql = settings.user_mysql;
let title_name = settings.title_name;
let image_list = settings.image_list;

config = {
  host: "localhost",
  user: user_mysql,
  database: bd_name,
  password: password_mysql
}


const connection = mysql.createConnection(config);

let sql = `create table if not exists allposts( id int primary key auto_increment, author varchar(255) not null, title varchar(255) not null, created varchar(255) not null, length int not null, url varchar(255) UNIQUE KEY, comments int not null, upvotes int not null, points float not null, percent float not null)`;

 connection.query(sql, function(err, results) {
 if(err) console.log(err);
  console.log(results);
            });     
 

    const filter = "created";
    const query = {tag: "life",limit: 100}
 
    console.log('Post assembled.\nFilter:', filter, '\nQuery:', query);

    client.database
        .getDiscussions(filter, query)
        .then(result => {
            console.log('Response received:', result);
            if (result) {
                var posts = [];
                result.forEach(post => {
                    const json = JSON.parse(post.json_metadata);
                    const image = json.image ? json.image[0] : '';
                    const title = post.title;
                    const author = post.author;
                    const created = new Date(post.created).toDateString();
                    let upvote = post.active_votes.length;

            
        const sql = `INSERT INTO allposts(author, title, created, length, url, comments, upvotes, points,percent) VALUES('${author}', '${title}', '${created}', '${post.body_length}', '${post.url}', '${post.children}', '${upvote}','0','999')`;      

        connection.query(sql, function(err, results) {
            if(err) console.log(err);
        console.log(results);
                });     

                });

            } else {
                console.log('No result');

            }
        })
        .catch(err => {
            console.log(err);
        });


Thank you from your attention.
Have a nice day.

Sort:  
Loading...