-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.js
144 lines (123 loc) · 4.6 KB
/
post.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fs = require('fs')
const axios = require('axios')
const dotenv = require('dotenv')
const javalon = require('javalon')
const youtubedl = require('youtube-dl')
const { getChannelFeed } = require('@obg-lab/youtube-channel-feed')
dotenv.config( { path: '.env'} )
const cb = (err, res) => console.log("Error: ", err, "Result: ", res)
const api_url = 'https://avalon.d.tube';
const username = process.env.USERNAME;
const priv_key = process.env.PRIV_KEY;
const min_user_vp = process.env.MIN_USER_VP;
const min_user_bw = process.env.MIN_USER_BW;
const channel_id = 'UCXuqSBlHAE6Xw-yeJA0Tunw';
const options = ['--username=user', '--password=hunter2']
function intervalFunc() {
// Check VP
axios.get(api_url + '/accounts/' + username).then((user_data) => {
var user_vp = user_data.data[0].vt.v;
var user_bw = user_data.data[0].bw.v
// console.log(user_vp)
if(user_vp > min_user_vp && user_bw > min_user_bw) { // Required VP amount ie. 5000 VP
validate()
} else{
return;
}
})
}setInterval(intervalFunc, 60000); // 1 min
async function validate() {
// Fetch youtube feed
const channel_feed = await getChannelFeed(channel_id)
let channel_author = await (channel_feed.feed.author[0].name).toString().toLowerCase();
video_id = await channel_feed.feed.entry[0].id[0].split(':')[2];
// Check video id's against the new video id
let channel_name = (channel_author.replace(/[^A-Za-z0-9 ]/g, ``).trim()).replace(/ /g,"_");
// console.log(channel_name)
let db_dir = './db/youtube/' + channel_name + '.json';
// console.log(db_dir)
// Create json file if not exists
if (!fs.existsSync(db_dir)){
fs.writeFileSync(db_dir, `[{"id": "UK1hL4k8Zao"}]`); // dummy data
}
var data = fs.readFileSync(db_dir);
var ids = JSON.parse(data);
var youtube_data = {
id: video_id
}
function addDb(youtube_data) {
var index = ids.findIndex(o => o.id === video_id)
if (index === -1) {
post()
console.log("New video found.")
ids.push(youtube_data);
fs.writeFileSync(db_dir, JSON.stringify(ids, null, 2), 'utf8'), (err) => {
if (err) { console.error(err); return; };
console.log("Updating DB...");
}
}else {
console.log("Video already exists" + " " + video_id)
}
}
addDb(youtube_data);
}
function post() {
//Grab video info & post function
let url = 'https://youtu.be/' + video_id;
youtubedl.getInfo(url, options, function(err, info) {
if (err) throw err
youtube_id = info.id;
title = info.title;
description = info.description;
duration = info._duration_raw;
tag = 'promo';
autoPost()
})
}
// posting
function autoPost() {
console.log('Posting...', )
console.log(youtube_id)
try {
var vp = Math.floor(Math.floor(Math.random() * 50) + 50); // Random num between 500-1500
// var tag = '';
// Generate random link
function generatePermlink() {
let permlink = ""
let possible = "abcdefghijklmnopqrstuvwxyz0123456789"
for (let i = 0; i < 8; i++)
permlink += possible.charAt(Math.floor(Math.random() * possible.length))
return permlink
}
var postLink = generatePermlink()
//console.log(postLink)
// Broadcast vote to blockchain
var newTx = {
type: javalon.TransactionType.COMMENT,
data: {
link: postLink,
json: {
files: {
youtube: youtube_id
},
title: title,
desc: description,
dur: duration,
tag: tag,
refs: []
},
vt: vp,
tag: tag
}
}
// Sign transaction
newTx = javalon.sign(priv_key, username, newTx)
// console.log(newTx)
// Send transaction to blockchain
javalon.sendRawTransaction(newTx, function(err, res) {
cb(err, res)
})
} catch (error) {
console.error(error);
}
}