This repository has been archived on 2025-03-19. You can view files and clone it, but cannot push or open issues or pull requests.
TwitToMast/ref/functions/mastodon.js
Penelope Gomez / Pogmommy ed9d5c68fd 2.0.0
2023-02-14 14:35:57 -07:00

80 lines
2.1 KiB
JavaScript

const fs = require('fs');
const Masto = require('mastodon');
const support = require('../functions/support.js');
const csv = require('../functions/csv.js');
const debuglog = support.debuglog;
const funcs = require('../functions/functions.js');
const Args = require('../classes/arguments.js');
const args = new Args();
const Formats = require('../classes/formats.js');
const format = new Formats();
function setupMastodon(){
const config = fs.readFileSync("./config.txt").toString().split(/[\r\n]+/);
var M = new Masto({
access_token: config[0],
api_url: config[1]
})
return M;
}
async function postMedia(path){
id = 0;
if (args.enablePosts){
var M = setupMastodon();
await M.post('media', { file: fs.createReadStream(path) }).then(resp => {
id = resp.data.id;
}, function(err) {
if (err) {
debuglog(err,0);
return "err";
}
})
} else if (args.forceCSV) {
return funcs.rand(1,100);
}
return id;
}
async function postStatus(tweet,file,csvc){
var id = 0;
if (args.enablePosts){
var M = setupMastodon();
params = { status: tweet.text }
debuglog(`${tweet.no} is a reply to ${tweet.prompt}`);
if (tweet.hasImages) {//POST HAS IMAGES
debuglog("post has images!!",2)
debuglog(`images array: ${tweet.imgArray}`,2)
Object.assign(params, { media_ids: tweet.imgArray });
}
if (tweet.prompt != 0) {//POST IS A REPLY
debuglog("reply to: " + tweet.prompt,2)
Object.assign(params, { in_reply_to_id: tweet.prompt });
}
await M.post('statuses', params, (err, data) => {
if (err) {
debuglog(format.error(`Post to Mastodon failed with error: ${err}`), 1);
return "err";
} else {
//ADD TWEET TO CSV TO PREVENT FUTURE PROCESSING
csv.appendToCSV(tweet.url,data.id,tweet.orig,file,csvc);
debuglog(`posted to mastodon and got back id: ${data.id}`);
debuglog(format.bold(`Successfully posted ${tweet.url} to Mastodon!`),1);
id = data.id;
}
})
} else if (args.forceCSV) {
var fakeID = funcs.rand(1,100);
csv.appendToCSV(tweet.url,fakeID,(`forced ${tweet.orig}`),file,csvc);
id = fakeID;
}
return id;
}
module.exports = { postMedia,postStatus };