Quote tweets now link to the quoted tweet

This commit is contained in:
Penelope Gomez / Pogmommy 2023-02-07 13:28:44 -07:00 committed by GitHub
parent 313735948a
commit 599ccd37c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 195 additions and 211 deletions

View file

@ -8,8 +8,8 @@ const fs = require('fs');
const csvWriter = require('csv-write-stream'); const csvWriter = require('csv-write-stream');
const Masto = require('mastodon'); const Masto = require('mastodon');
const client = require('https'); const client = require('https');
var Jimp = require("jimp"); const request = require("request");
let imgConvert = require('image-convert'); const Q = require("q");
//VALIDATE INPUT //VALIDATE INPUT
@ -114,42 +114,17 @@ function debuglog(debugString,logLevel) {
if (logLevel <= debug) {console.log(prefix + " " + debugString);} if (logLevel <= debug) {console.log(prefix + " " + debugString);}
} }
function screenshotElement(driver, locator) { function expandUrl(shortUrl) {
return new Promise(async (resolve, reject) => { var deferred = Q.defer();
try { request( { method: "HEAD", url: shortUrl, followAllRedirects: true },
let base64Image = await driver.takeScreenshot(); function (error, response) {
let decodedImage = new Buffer(base64Image, "base64"); if (error) {
let dimensions = await driver.findElement(By.xpath(locator)).getRect(); deferred.reject(new Error(error));
debuglog(dimensions,2) } else {
let xLoc = dimensions.x*0.7; deferred.resolve(response.request.href);
let yLoc = dimensions.y*0.7;
let eWidth = dimensions.width*0.7;
let eHeight = dimensions.height*0.7;
let image = await Jimp.read(decodedImage);
image.crop(xLoc, yLoc, eWidth, eHeight).getBase64(Jimp.AUTO, (err, data) => {
if (err) {
console.error(err)
reject(err)
} }
imgConvert.fromBuffer({
buffer: data,
output_format: "jpg"
}, function (err, buffer, file) {
if (!err) {
let croppedImageDataBase64 = buffer.toString('base64')
resolve(croppedImageDataBase64)
}
else {
console.error(err.message);
reject(err)
}
})
}); });
} catch (err) { return deferred.promise;
console.error(err.message);
reject(err)
}
})
} }
debuglog("Setting up...",1); debuglog("Setting up...",1);
@ -172,6 +147,8 @@ const tweetXPath = (timeLineXPath + `/div`); //the div containing individual twe
//the following xpaths follow an individual tweet xpath: (tweetXpath + '[1]' + variableXPath) //the following xpaths follow an individual tweet xpath: (tweetXpath + '[1]' + variableXPath)
const urlCardXPath = `/div/div/div/article/div/div/div/div[*]/div[*]/div[*]/div[*]/div/div[2]/a`
const quoteTweetHandleXPath = `/div/div/div/article/div/div/div/div[2]/div[2]/div[2]/div[2]/div[*]/div[2]/div/div[1]/div/div/div/div/div/div[2]/div[1]/div/div/div/span`; //xpath to text label that reveals if a tweet is a quote tweet (leads to the quote tweeted user's handle) const quoteTweetHandleXPath = `/div/div/div/article/div/div/div/div[2]/div[2]/div[2]/div[2]/div[*]/div[2]/div/div[1]/div/div/div/div/div/div[2]/div[1]/div/div/div/span`; //xpath to text label that reveals if a tweet is a quote tweet (leads to the quote tweeted user's handle)
const quoteTweetContentXPath= `/div/div/div/article/div/div/div/div[2]/div[2]/div[2]/div[2]/div[*]/div[2][div/div[1]/div/div/div/div/div/div[2]/div[1]/div/div/div/span]` //xpath to locate entirety of Quote Tweeted Content const quoteTweetContentXPath= `/div/div/div/article/div/div/div/div[2]/div[2]/div[2]/div[2]/div[*]/div[2][div/div[1]/div/div/div/div/div/div[2]/div[1]/div/div/div/span]` //xpath to locate entirety of Quote Tweeted Content
@ -246,7 +223,7 @@ driver.executeScript("document.body.style.zoom='35%'");
//REMOVE NON-PRIMARY TWEETS //REMOVE NON-PRIMARY TWEETS
debuglog("Filtering out disabled tweets...",1) debuglog("Filtering out disabled tweets...",2)
while (!keepTweet) { while (!keepTweet) {
await driver.wait(until.elementLocated(By.xpath(thisTweetXPath)), 30000); await driver.wait(until.elementLocated(By.xpath(thisTweetXPath)), 30000);
@ -285,6 +262,7 @@ driver.executeScript("document.body.style.zoom='35%'");
//webdriver.promise.rejected(err); //webdriver.promise.rejected(err);
} }
}); });
//IF TWEET IS DISABLED, MARK FOR REMOVAL //IF TWEET IS DISABLED, MARK FOR REMOVAL
if (isRT || ((!modulesToEnable[0] && isQT) || (!modulesToEnable[1] && isThread)) ) { if (isRT || ((!modulesToEnable[0] && isQT) || (!modulesToEnable[1] && isThread)) ) {
//TWEET IS QT, RT, OR THREAD //TWEET IS QT, RT, OR THREAD
@ -298,29 +276,14 @@ driver.executeScript("document.body.style.zoom='35%'");
//GET TWEET URL //GET TWEET URL
mobileTweetURL = await driver.findElement(By.xpath(thisTweetXPath + tweetURLXPath)).getAttribute('href'); mobileTweetURL = await driver.findElement(By.xpath(thisTweetXPath + tweetURLXPath)).getAttribute('href');
tweetURL = await mobileTweetURL.replace('mobile.',''); tweetURL = await mobileTweetURL.replace('mobile.','');
debuglog(tweetURL,2);
await driver.wait(until.elementLocated(By.xpath(timeLineXPath + tweetTextXPath)), 1000);
tweetHasText = await driver.findElement(webdriver.By.xpath(thisTweetXPath + tweetTextXPath)).then(function() {
return true; // It existed
}, function(err) {
if (err instanceof webdriver.error.NoSuchElementError) {
return false; // It was not found
} else {
webdriver.promise.rejected(err);
}
});
if (tweetHasText){
tweetText = await driver.findElement(By.xpath(thisTweetXPath + tweetTextXPath)).getText();
debuglog("Tweet Text: " + tweetText,2);
} else {
tweetText = " ";
}
if (!csvOutput.includes(tweetURL)) { if (!csvOutput.includes(tweetURL)) {
//SETUP TEXT FOR TWEET STATUS //SETUP TEXT FOR TWEET STATUS
var tweetHasText = false; var tweetHasText = false;
await driver.wait(until.elementLocated(By.xpath(timeLineXPath + tweetTextXPath)), 1000); await driver.wait(until.elementLocated(By.xpath(timeLineXPath + tweetTextXPath)), 1000);
tweetHasText = await driver.findElement(webdriver.By.xpath(thisTweetXPath + tweetTextXPath)).then(function() { tweetHasText = await driver.findElement(webdriver.By.xpath(thisTweetXPath + tweetTextXPath)).then(function() {
return true; // It existed return true; // It existed
}, function(err) { }, function(err) {
@ -332,11 +295,32 @@ driver.executeScript("document.body.style.zoom='35%'");
}); });
if (tweetHasText){ if (tweetHasText){
tweetText = await driver.findElement(By.xpath(thisTweetXPath + tweetTextXPath)).getText(); tweetText = await driver.findElement(By.xpath(thisTweetXPath + tweetTextXPath)).getText();
debuglog("Tweet Text: " + tweetText,2); //debuglog("Tweet Text: " + tweetText,2);
} else { } else {
tweetText = " "; tweetText = " ";
} }
tweetHasURL = await driver.findElement(webdriver.By.xpath(thisTweetXPath + urlCardXPath)).then(function() {
return true; // It existed
}, function(err) {
if (err instanceof webdriver.error.NoSuchElementError) {
return false; // It was not found
} else {
webdriver.promise.rejected(err);
}
});
if (tweetHasURL){
tweetCardURL = await driver.findElement(By.xpath(thisTweetXPath + urlCardXPath)).getAttribute('href');
await expandUrl(tweetCardURL)
.then(function (longUrl) {
debuglog("Long URL:" + longUrl,2);
tweetText = tweetText + "\r\n\r\n" + longUrl;
debuglog("Tweet Text: " + tweetText,2);
});
}
debuglog("tweetHasText: " + tweetHasText,2);
debuglog("tweetHasURL: " + tweetHasURL,2);
//CHECK FOR QUOTE TWEETS //CHECK FOR QUOTE TWEETS
isQT = await driver.findElement(webdriver.By.xpath(thisTweetXPath + quoteTweetContentXPath)).then(function() { isQT = await driver.findElement(webdriver.By.xpath(thisTweetXPath + quoteTweetContentXPath)).then(function() {
return true; // It existed return true; // It existed
@ -350,12 +334,19 @@ driver.executeScript("document.body.style.zoom='35%'");
if (isQT){ if (isQT){
await driver.sleep(1 * 1000) await driver.sleep(1 * 1000)
quotedContent = await driver.findElement(webdriver.By.xpath(thisTweetXPath + quoteTweetContentXPath)); quotedContent = await driver.findElement(webdriver.By.xpath(thisTweetXPath + quoteTweetContentXPath));
var b64img = await screenshotElement(driver, thisTweetXPath + quoteTweetContentXPath); await driver.findElement(webdriver.By.xpath(thisTweetXPath + quoteTweetContentXPath)).sendKeys(webdriver.Key.CONTROL, webdriver.Key.ENTER);
var base64Data = b64img.replace(/^data:image\/png;base64,/, ""); var parent = await driver.getWindowHandle();
require("fs").writeFile('./' + i + "." + 0 +'.jpg', base64Data, 'base64', function(err) { var windows = await driver.getAllWindowHandles();
debuglog(err,1); await driver.switchTo().window(windows[1]).then(() => {
driver.getCurrentUrl().then(url => {
debuglog('current url: "' + url + '"',2);
tweetText = tweetText + "\r\n\r\n" + "Quote tweeting: " + url;
}); });
driver.switchTo().window(parent);
});
await driver.switchTo().window(windows[1]);
await driver.close();
await driver.switchTo().window(parent);
} }
//CODE TO RUN IF TWEET IS NOT IN CSV //CODE TO RUN IF TWEET IS NOT IN CSV
@ -426,16 +417,6 @@ driver.executeScript("document.body.style.zoom='35%'");
//MAKE MASTODON POST WITH IMAGES //MAKE MASTODON POST WITH IMAGES
debuglog("Uploading images to Mastodon...",1); debuglog("Uploading images to Mastodon...",1);
var imageArray = []; var imageArray = [];
if (isQT) {
await M.post('media', { file: fs.createReadStream('./' + i + '.0.jpg') }).then(resp => {
imageArray.push(resp.data.id);
}, function(err) {
if (err) {
debuglog(err,1);
}
})
}
for (var f = 1; f < (imageCount+1); f++) { for (var f = 1; f < (imageCount+1); f++) {
await M.post('media', { file: fs.createReadStream('./' + i + '.' + f + '.jpg') }).then(resp => { await M.post('media', { file: fs.createReadStream('./' + i + '.' + f + '.jpg') }).then(resp => {
imageArray.push(resp.data.id); imageArray.push(resp.data.id);
@ -483,7 +464,7 @@ driver.executeScript("document.body.style.zoom='35%'");
//REMOVE SAVED IMAGE FILES //REMOVE SAVED IMAGE FILES
debuglog("cleaning up...",1); debuglog("cleaning up...",1);
for (var j = 0; j < 5; j++) { for (var j = 1; j < 5; j++) {
path = ("./" + i + "." + j + ".jpg"); path = ("./" + i + "." + j + ".jpg");
try { try {
if (fs.existsSync(path)) { if (fs.existsSync(path)) {

View file

@ -6,16 +6,19 @@
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"keywords": ["mastodon", "twitter"], "keywords": [
"mastodon",
"twitter"
],
"author": "Pogmommy", "author": "Pogmommy",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"csv-write-stream": "^2.0.0", "csv-write-stream": "^2.0.0",
"filesystem": "^1.0.1", "filesystem": "^1.0.1",
"https": "^1.0.0", "https": "^1.0.0",
"image-convert": "^0.1.33",
"jimp": "^0.22.4",
"mastodon": "^1.2.2", "mastodon": "^1.2.2",
"q": "^1.5.1",
"request": "^2.88.2",
"selenium-webdriver": "^4.8.0", "selenium-webdriver": "^4.8.0",
"until": "^0.1.1" "until": "^0.1.1"
} }