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 Masto = require('mastodon');
const client = require('https');
var Jimp = require("jimp");
let imgConvert = require('image-convert');
const request = require("request");
const Q = require("q");
//VALIDATE INPUT
@ -114,42 +114,17 @@ function debuglog(debugString,logLevel) {
if (logLevel <= debug) {console.log(prefix + " " + debugString);}
}
function screenshotElement(driver, locator) {
return new Promise(async (resolve, reject) => {
try {
let base64Image = await driver.takeScreenshot();
let decodedImage = new Buffer(base64Image, "base64");
let dimensions = await driver.findElement(By.xpath(locator)).getRect();
debuglog(dimensions,2)
let xLoc = dimensions.x*0.7;
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)
function expandUrl(shortUrl) {
var deferred = Q.defer();
request( { method: "HEAD", url: shortUrl, followAllRedirects: true },
function (error, response) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(response.request.href);
}
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) {
console.error(err.message);
reject(err)
}
})
return deferred.promise;
}
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)
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 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
debuglog("Filtering out disabled tweets...",1)
debuglog("Filtering out disabled tweets...",2)
while (!keepTweet) {
await driver.wait(until.elementLocated(By.xpath(thisTweetXPath)), 30000);
@ -285,6 +262,7 @@ driver.executeScript("document.body.style.zoom='35%'");
//webdriver.promise.rejected(err);
}
});
//IF TWEET IS DISABLED, MARK FOR REMOVAL
if (isRT || ((!modulesToEnable[0] && isQT) || (!modulesToEnable[1] && isThread)) ) {
//TWEET IS QT, RT, OR THREAD
@ -298,29 +276,14 @@ driver.executeScript("document.body.style.zoom='35%'");
//GET TWEET URL
mobileTweetURL = await driver.findElement(By.xpath(thisTweetXPath + tweetURLXPath)).getAttribute('href');
tweetURL = await mobileTweetURL.replace('mobile.','');
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 = " ";
}
debuglog(tweetURL,2);
if (!csvOutput.includes(tweetURL)) {
//SETUP TEXT FOR TWEET STATUS
var tweetHasText = false;
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) {
@ -332,11 +295,32 @@ driver.executeScript("document.body.style.zoom='35%'");
});
if (tweetHasText){
tweetText = await driver.findElement(By.xpath(thisTweetXPath + tweetTextXPath)).getText();
debuglog("Tweet Text: " + tweetText,2);
//debuglog("Tweet Text: " + tweetText,2);
} else {
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
isQT = await driver.findElement(webdriver.By.xpath(thisTweetXPath + quoteTweetContentXPath)).then(function() {
return true; // It existed
@ -350,12 +334,19 @@ driver.executeScript("document.body.style.zoom='35%'");
if (isQT){
await driver.sleep(1 * 1000)
quotedContent = await driver.findElement(webdriver.By.xpath(thisTweetXPath + quoteTweetContentXPath));
var b64img = await screenshotElement(driver, thisTweetXPath + quoteTweetContentXPath);
var base64Data = b64img.replace(/^data:image\/png;base64,/, "");
require("fs").writeFile('./' + i + "." + 0 +'.jpg', base64Data, 'base64', function(err) {
debuglog(err,1);
await driver.findElement(webdriver.By.xpath(thisTweetXPath + quoteTweetContentXPath)).sendKeys(webdriver.Key.CONTROL, webdriver.Key.ENTER);
var parent = await driver.getWindowHandle();
var windows = await driver.getAllWindowHandles();
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
@ -426,16 +417,6 @@ driver.executeScript("document.body.style.zoom='35%'");
//MAKE MASTODON POST WITH IMAGES
debuglog("Uploading images to Mastodon...",1);
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++) {
await M.post('media', { file: fs.createReadStream('./' + i + '.' + f + '.jpg') }).then(resp => {
imageArray.push(resp.data.id);
@ -483,7 +464,7 @@ driver.executeScript("document.body.style.zoom='35%'");
//REMOVE SAVED IMAGE FILES
debuglog("cleaning up...",1);
for (var j = 0; j < 5; j++) {
for (var j = 1; j < 5; j++) {
path = ("./" + i + "." + j + ".jpg");
try {
if (fs.existsSync(path)) {

View file

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