multi-account-post-schedule.../routes/index.js

374 lines
9.5 KiB
JavaScript
Raw Normal View History

2023-07-05 14:17:30 +02:00
2023-07-03 23:12:20 +02:00
var express = require('express')
var router = express.Router()
var sqlite3 = require('sqlite3')
var Masto = require('mastodon')
2022-07-10 23:02:32 +02:00
const accounts_to_select = [
2023-07-03 23:12:20 +02:00
{
label: 'tykayn',
value: 'tykayn',
src: 'https://mastodon.cipherbliss.com/system/accounts/avatars/000/000/001/original/6388tykayn.gif'
},
{
label: 'modominem',
value: 'modominem',
src: 'https://mastodon.cipherbliss.com/system/accounts/avatars/000/152/770/original/c62bb94381dc1f75.png'
},
{
label: 'qzine',
value: 'qzine',
src: 'https://mastodon.cipherbliss.com/system/accounts/avatars/000/003/032/original/2bb8b90d21d3fdca.jpg'
},
{
label: 'curator',
value: 'curator',
src: 'https://mastodon.cipherbliss.com/system/accounts/avatars/000/002/974/original/8e48623291e49afe.jpg'
},
{
label: 'kurator',
value: 'kurator',
src: 'https://mastodon.cipherbliss.com/system/accounts/avatars/000/162/067/original/bb374d2c6a361b6d.jpg'
},
{
label: 'voix du nucléaire',
value: 'voixdunucleaire',
src: 'https://mastodon.cipherbliss.com/system/accounts/avatars/000/107/055/original/7dac1a35f1423b94.jpg'
},
2023-07-04 19:42:32 +02:00
{
label: 'voices of nuclear',
value: 'voicesofnuclear',
src: 'https://mastodon.cipherbliss.com/system/accounts/avatars/109/319/651/002/602/993/original/c7ca27d72856dc18.jpg'
},
2023-07-03 23:12:20 +02:00
{
label: 'The greatest Meme',
value: 'meme',
src: 'https://mastodon.cipherbliss.com/system/accounts/avatars/000/002/978/original/b2f2e817572c93e9.png'
},
{
label: 'cil de gometz',
value: 'cil_gometz',
src: 'https://mastodon.cipherbliss.com/system/accounts/avatars/000/107/055/original/7dac1a35f1423b94.jpg'
},
2022-07-10 23:02:32 +02:00
]
2023-03-04 10:30:20 +01:00
// change this object to fit your multi accounts
2023-07-03 23:12:20 +02:00
const time_spans_choices = ['08:02:00', '08:42:00', '09:10:00', '09:15:00', '10:22:00', '12:22:00', '13:02:00', '14:01:00', '14:34:00', '16:10:00', '17:15:00', '17:48:00']
const database_masto = 'mastodon_multi_accounts.db'
2022-07-07 18:23:44 +02:00
/* GET home page. */
2023-07-03 23:12:20 +02:00
let schedule_time = ''
let schedule_day = new Date()
schedule_day = schedule_day.toISOString()
2022-07-11 17:04:46 +02:00
2022-07-07 19:03:04 +02:00
router.get('/', function (req, res, next) {
2022-07-11 17:04:46 +02:00
2023-07-03 23:12:20 +02:00
if (!req.body.schedule_time) {
schedule_time = time_spans_choices[Math.floor(Math.random() * time_spans_choices.length)]
}
let contentVars =
{
accounts_to_select,
reqBody: req.body,
schedule_time,
schedule_day,
time_spans_choices
}
res.render('index',
contentVars
)
})
2022-07-07 18:23:44 +02:00
// publier un message avec un certain compte
2022-07-07 19:03:04 +02:00
router.get('/publish', function (req, res, next) {
2023-07-03 23:12:20 +02:00
res.render('index', { title: 'Express' })
})
2022-07-07 19:03:04 +02:00
2023-07-03 23:12:20 +02:00
const connect = require('@databases/sqlite')
const { sql } = require('@databases/sqlite')
const { runOnChangeOnly } = require('nodemon/lib/config/defaults')
const fs = require('fs')
2022-07-07 19:03:04 +02:00
2023-07-03 23:12:20 +02:00
const db = connect(database_masto)
2022-07-07 19:16:56 +02:00
2023-07-03 23:12:20 +02:00
function createDatabase () {
var newdb = new sqlite3.Database(database_masto, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
2022-07-07 19:03:04 +02:00
2023-07-03 23:12:20 +02:00
createTables(newdb)
})
2022-07-07 18:23:44 +02:00
}
2022-07-07 19:03:04 +02:00
2023-07-03 23:12:20 +02:00
function createTables (newdb) {
async function prepare () {
await db.query(sql`
2022-07-07 19:16:56 +02:00
create table posts_scheduled
(
action_id integer
2022-07-07 19:16:56 +02:00
constraint posts_scheduled_pk
primary key autoincrement,
post_username varchar not null,
content varchar not null,
medias varchar,
datetime datetime
2022-07-07 19:16:56 +02:00
);
create unique index posts_scheduled_action_id_uindex
on posts_scheduled (action_id);
insert into posts_scheduled
2022-07-07 19:28:40 +02:00
values (NULL, "modominem", "un message d'example", NULL);
2022-07-07 19:16:56 +02:00
2023-07-03 23:12:20 +02:00
`)
console.log('requête de création faite')
}
2022-07-07 19:28:40 +02:00
2023-07-03 23:12:20 +02:00
const prepared = prepare()
2022-07-07 18:23:44 +02:00
}
// ajouter un message à la file d'attente avec un certain compte
router.post('/add-to-queue', function (req, res, next) {
2023-07-03 23:12:20 +02:00
// get account
// get content*
// add to sql DB
2023-07-03 23:12:20 +02:00
// insert into posts_scheduled
// values (NULL, "modominem", "un message d'example", NULL);
var db = new sqlite3.Database(database_masto, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
}
)
db.serialize(() => {
db.run('INSERT INTO posts_scheduled VALUES(?,?,?,?,?)', [null, req.body.author, req.body.message, req.body.fichier, null], function (err) {
if (err) {
return console.log(err.message)
res.render('index', { message: 'erreur ' + err.message })
2023-07-03 23:12:20 +02:00
}
console.log('nouveau post ajouté', req.body.author, req.body.message)
2023-07-03 23:12:20 +02:00
res.render('index', { message: 'message ajouté OK' })
2023-07-03 23:12:20 +02:00
})
})
2023-07-03 23:12:20 +02:00
})
2023-07-03 23:12:20 +02:00
function getAllPosts (db) {
return db.query(sql`SELECT *
2022-07-07 23:39:28 +02:00
FROM posts_scheduled
ORDER BY action_id DESC LIMIT 15;`)
2022-07-07 18:23:44 +02:00
}
2022-07-07 19:03:04 +02:00
router.get('/init-db', function (req, res, next) {
2023-07-03 23:12:20 +02:00
var db = new sqlite3.Database(database_masto, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
console.log('got to create db')
createDatabase()
}
)
res.render('created_db', {})
})
2022-07-07 18:23:44 +02:00
2022-07-07 19:03:04 +02:00
router.get('/list', function (req, res, next) {
2023-07-03 23:12:20 +02:00
// let posts_list = [{
// action_id: 1,
// post_username: "modominem",
// content: "demo post list",
// medias: "media file name from assets folder",
// date_schedule: "2022-07-07 "
// }]
getAllPosts(db).then(
(results) => {
console.log('liste de posts ', results)
res.render('database', { posts_list: results })
},
(err) => console.error(err),
)
})
2022-07-07 18:23:44 +02:00
2022-07-07 19:33:11 +02:00
router.get('/add-example', function (req, res, next) {
2023-07-03 23:12:20 +02:00
// let db = connect(database_masto);
2022-07-07 19:03:04 +02:00
2023-07-03 23:12:20 +02:00
async function prepare () {
await db.query(sql`
2022-07-07 19:03:04 +02:00
insert into posts_scheduled
2022-07-07 23:39:28 +02:00
values (NULL, "modominem", "un message d'example", "image.jpg", NULL)
2022-07-07 19:03:04 +02:00
;
2022-07-07 19:28:40 +02:00
`).then(resp => {
2023-07-03 23:12:20 +02:00
console.log(resp)
2022-07-07 19:33:11 +02:00
2023-07-03 23:12:20 +02:00
let posts_list = []
res.redirect('/list')
},
err => {
console.log(err)
})
}
2022-07-07 19:33:11 +02:00
2023-07-03 23:12:20 +02:00
const prepared = prepare()
2022-07-07 19:03:04 +02:00
2023-07-03 23:12:20 +02:00
}
)
2022-07-07 18:23:44 +02:00
// publier le message en db dans la file
2022-07-07 19:03:04 +02:00
router.get('/publish-last-entry', function (req, res, next) {
2022-07-07 23:39:28 +02:00
2023-07-03 23:12:20 +02:00
var OAuth2 = require('oauth').OAuth2
// console.log('env' , env)
var oauth = new OAuth2(env.APP_ID, env.SECRET, env.INSTANCE_MASTODON, null, '/oauth/token')
var url = oauth.getAuthorizeUrl({
redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
response_type: 'code',
scope: 'read write follow'
})
console.log('url ', url)
let status = '#mastoart of @tykayn'
let visibility = 'unlisted' // public, unlisted, private, direct.
let media_filename = 'colline.JPG'
let file_path = 'assets/not_published/' + media_filename
let accessToken = env.TOKEN
let sensitive = false
let scheduled_at = '2022-07-07T21:36:29.100Z'
let account_id = '2974' // curator bliss
let language = 'fr'
let enable_post = false
2022-07-07 23:39:28 +02:00
2023-07-03 23:12:20 +02:00
const masto = new Masto({
access_token: accessToken,
api_url: env.parsed.INSTANCE_MASTODON + '/api/v1/',
})
if (enable_post) {
masto.post('media', { file: fs.createReadStream(file_path) }).then(resp => {
id = resp.data.id
// doc https://docs.joinmastodon.org/methods/statuses/
console.log('media id ', resp.data.id)
console.log(resp.data)
masto.post('statuses', {
status: status,
media_ids: [id],
account_id,
visibility,
language,
sensitive
}).then(resp => {
// succès, marquer le post comme fait en BDD
console.log(resp)
var oldPath = file_path
var newPath = 'assets/published/' + media_filename
fs.rename(oldPath, newPath, function (err) {
if (err) throw err
console.log('Successfully renamed - AKA moved!')
})
},
err => {
console.error(err)
})
})
}
res.render('index', {})
})
2022-07-07 18:23:44 +02:00
2022-07-07 19:41:44 +02:00
// répartir les publications dans le temps
// TODO
2022-07-07 23:39:28 +02:00
const limit_posts_per_day = 10
2022-07-07 19:41:44 +02:00
router.get('/dispatch-publication-in-time', function (req, res, next) {
2022-07-07 23:39:28 +02:00
2023-07-03 23:12:20 +02:00
getAllPosts(db).then(
(results) => {
console.log('répartir', results[0])
2022-07-07 19:41:44 +02:00
2023-07-03 23:12:20 +02:00
// on compte les posts et quel intervalle de temps mettre entre chaque pour tenir le rythme de X posts par jour
2022-07-07 19:41:44 +02:00
2023-07-03 23:12:20 +02:00
res.render('index', { p: results[0] })
},
(err) => console.error(err),
)
2022-07-07 19:41:44 +02:00
2023-07-03 23:12:20 +02:00
})
2022-07-07 19:41:44 +02:00
2023-07-05 14:17:30 +02:00
2022-07-11 17:04:46 +02:00
router.post('/direct-post', function (req, res, next) {
2023-07-03 23:12:20 +02:00
let env = require('dotenv').config({ path: __dirname + '/../.env' })
console.log('dotenv', env)
// console.log('req', req)
console.log('req.body', req.body)
let author = req.body.author.toUpperCase()
console.log('vérif token pour', author)
let access_token = env.parsed['TOKEN_' + author]
2023-07-05 14:17:30 +02:00
const masto = new Masto({
access_token: access_token,
api_url: env.parsed.INSTANCE_MASTODON + '/api/v1/',
})
2023-07-03 23:12:20 +02:00
console.log('token', access_token)
if (access_token) {
let visibility = 'public'
let language = 'fr'
let sensitive = false
console.log('accessToken', access_token)
2023-07-05 14:17:30 +02:00
2023-07-03 23:12:20 +02:00
let params = {
status: req.body.message,
visibility,
language,
sensitive
}
if (req.body.cw) {
params['spoiler_text'] = req.body.cw
}
if (req.body.scheduled_at && req.body.scheduled_at_bool) {
let dateschedule = new Date(req.body.scheduled_at)
params['scheduled_at'] = dateschedule.toISOString()
console.log('scheduled_at', params['scheduled_at'])
return
}
console.log(req.body)
if (!req.body.fichier) {
2023-07-05 14:17:30 +02:00
console.log(' pas de fichier dans le post')
sendPostMastodon(params, masto)
2023-07-03 23:12:20 +02:00
res.render('index', { bodyReq: req.body })
}
// TODO prise en charge des fichiers joints
2023-07-05 14:17:30 +02:00
if (req.body.fichier) {
console.log(' envoi avec fichier')
sendPostMastodon(params, masto)
res.render('index', {bodyReq: req.body})
}
2023-07-03 23:12:20 +02:00
} else {
console.error('pas de token pour ' + req.body.author)
}
2022-07-11 17:04:46 +02:00
})
2023-07-05 14:17:30 +02:00
function sendPostMastodon(config, masto){
masto.post('statuses', config).then(rep => {
console.log('rep', rep)
console.log('\n message bien envoyé')
}, err => {
console.error(err)
})
}
2023-07-03 23:12:20 +02:00
module.exports = router