Alt-text scraping and crossposting

This commit is contained in:
Penelope Gomez / Pogmommy 2023-03-06 11:15:36 -07:00
parent 818bd55237
commit 4fdd9f0634
2 changed files with 16 additions and 3 deletions

View file

@ -59,6 +59,7 @@ class Tweets {
this.hasMultiImage = false; this.hasMultiImage = false;
this.hasImages = false; this.hasImages = false;
this.imgArray = []; this.imgArray = [];
this.imgAltArray = [];
this.imgCount = 0; this.imgCount = 0;
this.imgUrl = ""; this.imgUrl = "";
this.iterateExists = false; this.iterateExists = false;
@ -212,6 +213,9 @@ class Tweets {
if (this.hasSingleImage) { if (this.hasSingleImage) {
debuglog(`${this.orig} Tweet #${this.no} contains a single image.`, 2) debuglog(`${this.orig} Tweet #${this.no} contains a single image.`, 2)
this.imgCount = 1; this.imgCount = 1;
var imgAltText = await elements.getAttribute(driver,this.x.singleImage,"alt")
this.imgAltArray.push(imgAltText);
debuglog(this.imgAltArray,2);
var origImageURL = await elements.getAttribute(driver,this.x.singleImage,"src") var origImageURL = await elements.getAttribute(driver,this.x.singleImage,"src")
const reg = /&name=\w+/ const reg = /&name=\w+/
this.imgUrl = origImageURL.replace(reg, "&name=large"); this.imgUrl = origImageURL.replace(reg, "&name=large");
@ -229,6 +233,9 @@ class Tweets {
this.iterateExists = await elements.doesExist(driver,this.x.multiImages(x,y)); this.iterateExists = await elements.doesExist(driver,this.x.multiImages(x,y));
if (this.iterateExists) { if (this.iterateExists) {
debuglog(`${x},${y} Exists!`); debuglog(`${x},${y} Exists!`);
var imgAltText = await elements.getAttribute(driver,this.x.multiImages(x,y),"alt")
this.imgAltArray.push(imgAltText);
debuglog(this.imgAltArray,2);
var origImageURL = await elements.getAttribute(driver,this.x.multiImages(x,y),'src') var origImageURL = await elements.getAttribute(driver,this.x.multiImages(x,y),'src')
const reg = /&name=\w+/ const reg = /&name=\w+/
this.imgUrl = origImageURL.replace(reg, "&name=large"); this.imgUrl = origImageURL.replace(reg, "&name=large");
@ -253,7 +260,8 @@ class Tweets {
for (var f = 1; f < (this.imgCount+1); f++) { for (var f = 1; f < (this.imgCount+1); f++) {
var jpgPath = `${imgSavePath}${this.orig == 'home' ? '' : 'r'}${this.no}.${f}.jpg` var jpgPath = `${imgSavePath}${this.orig == 'home' ? '' : 'r'}${this.no}.${f}.jpg`
debuglog(`uploading image to mastodon: ${jpgPath}`); debuglog(`uploading image to mastodon: ${jpgPath}`);
var imgid = await mastodon.postMedia(jpgPath) debuglog(this.imgAltArray);
var imgid = await mastodon.postMedia(jpgPath,this.imgAltArray[f-1]);
debuglog(`mastodon image id: ${imgid}`); debuglog(`mastodon image id: ${imgid}`);
this.imgArray.push(imgid); this.imgArray.push(imgid);
} }

View file

@ -19,11 +19,16 @@ function setupMastodon(){
return M; return M;
} }
async function postMedia(path){ async function postMedia(path,alt){
id = 0; id = 0;
if (alt == null) {
alt = "Image";
}
if (args.enablePosts){ if (args.enablePosts){
var M = setupMastodon(); var M = setupMastodon();
await M.post('media', { file: fs.createReadStream(path) }).then(resp => { params = { file: fs.createReadStream(path) }
Object.assign(params, { description: alt });
await M.post('media', params).then(resp => {
id = resp.data.id; id = resp.data.id;
}, function(err) { }, function(err) {
if (err) { if (err) {