From 76970fe793796a4de29f96ad9a89f75ea7dd8878 Mon Sep 17 00:00:00 2001 From: Penelope Gomez / Pogmommy Date: Mon, 6 Mar 2023 13:26:53 -0700 Subject: [PATCH] enable spoilers & content warning crossposting --- README.md | 8 ++++++-- config.txt | 1 + ref/classes/arguments.js | 1 + ref/classes/tweets.js | 17 ++++++++++++++++- ref/functions/mastodon.js | 5 +++++ usage.txt | 10 ++++++++-- 6 files changed, 37 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7d1f1d7..3505552 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,14 @@ npm install - Replace "API_KEY" on line 1 with the Access Token you retrieved in the previous step - Replace "API_URL" on line 2 with your Mastodon instance's API URL - This will look like "https://mastodon.social/api/v1/", replacing "mastodon.social" with the domain you registered your account through + - Replace "CONTENT_WARNING_PREFIX" on line 3 with your preferred spoiler/content warning prefix if you plan to use this feature + - commonly used prefixes include "CW//", "TW//", and "SPOILER//" ## Usage ``` -node ./TwitToMast.js [-htqrpmbc] [-u username] [-n tweetcount] [-d debuglevel] [-w timeout] -node ./multi.js [-htqrpmbc] [-n tweetcount] [-d debuglevel] [-w timeout] +node ./TwitToMast.js [-htqrspmbc] [-u username] [-n tweetcount] [-d debuglevel] [-w timeout] +node ./multi.js [-htqrspmbc] [-n tweetcount] [-d debuglevel] [-w timeout] ``` ## Arguments @@ -44,6 +46,8 @@ node ./multi.js [-htqrpmbc] [-n tweetcount] [-d debuglevel] [-w timeout] -q: - quote tweets will be included in the scan -r: - Link to quoted tweet will appear in the header, preceded by "re: " - default behavior posts link at bottom preceded by "Quoting " + -s: - Enable content warning prefixes + - tweets beginning with a certain string (defined in config.txt) will use following text on the same line as Mastodon content warning -p: - enable/disable posting to Mastodon -m: - include user's name, handle, and link to tweet -b: - display browser (disable headless mode) diff --git a/config.txt b/config.txt index 62ecbae..9ec4fe8 100644 --- a/config.txt +++ b/config.txt @@ -1,2 +1,3 @@ API_KEY API_URL +CONTENT_WARNING_PREFIX \ No newline at end of file diff --git a/ref/classes/arguments.js b/ref/classes/arguments.js index b958e29..15f9a5d 100644 --- a/ref/classes/arguments.js +++ b/ref/classes/arguments.js @@ -15,6 +15,7 @@ class Args { this.enableQuotes = this.getFlag("q"); //enable cross-posting quote tweets this.enableThreads = this.getFlag("t"); //enable cross-posting thread tweets this.reQuotes = this.getFlag("r"); //put links to quote tweets at top of mastodon posts + this.warnings = this.getFlag("s"); var userNamePreFormat = this.getArgument("-u","Twitter",false); this.userName = userNamePreFormat.replace('@','') diff --git a/ref/classes/tweets.js b/ref/classes/tweets.js index ed5346c..aea1ede 100644 --- a/ref/classes/tweets.js +++ b/ref/classes/tweets.js @@ -1,5 +1,6 @@ const webdriver = require('selenium-webdriver'); const By = webdriver.By; +const fs = require('fs'); //const { format } = require('fast-csv'); const elements = require('../functions/elements.js'); //link support.js @@ -45,6 +46,7 @@ class Tweets { //body this.hasBody = false; this.body = ""; + this.spoiler = ""; this.hasLinks = false; this.links = ""; @@ -164,7 +166,20 @@ class Tweets { debuglog("Found emoji!",2); //this.threadLength = elements.length; });*/ - const bodyText = await elements.getText(driver,this.x.tweetText);//SET TWEET BODY TO TEXT OF TWEET + var bodyText = await elements.getText(driver,this.x.tweetText);//SET TWEET BODY TO TEXT OF TWEET + if (args.warnings) { + const WarningPrefix = fs.readFileSync("./config.txt").toString().split(/[\r\n]+/)[2]; + var cwRegexText = new RegExp(`(?<=^${WarningPrefix}[^\\n]*)[\\w].+`, "g"); + debuglog(cwRegexText); + debuglog("content warning text:") + debuglog(bodyText.match(cwRegexText)); + this.spoiler = bodyText.match(cwRegexText)[0]; + var cwRegexPref = new RegExp(`^${WarningPrefix}.+\\n(\\s)+`, "g"); + debuglog(cwRegexPref); + debuglog("content warning text w pref:") + debuglog(bodyText.match(cwRegexPref)); + bodyText = bodyText.replace(cwRegexPref,""); + } this.appendSection(bodyText,'body'); debuglog(`Tweet Body:\r\n${this.body}`); } diff --git a/ref/functions/mastodon.js b/ref/functions/mastodon.js index 13829bc..a29875e 100644 --- a/ref/functions/mastodon.js +++ b/ref/functions/mastodon.js @@ -56,6 +56,11 @@ async function postStatus(tweet,file,csvc){ debuglog("reply to: " + tweet.prompt,2) Object.assign(params, { in_reply_to_id: tweet.prompt }); } + if (tweet.spoiler != "") {//POST IS A REPLY + debuglog("spoiler: " + tweet.spoiler,2) + Object.assign(params, { sensitive: true }); + Object.assign(params, { spoiler_text: tweet.spoiler }); + } await M.post('statuses', params, (err, data) => { if (err) { debuglog(format.error(`Post to Mastodon failed with error: ${err}`), 1); diff --git a/usage.txt b/usage.txt index 281343d..b6ed7df 100644 --- a/usage.txt +++ b/usage.txt @@ -1,7 +1,7 @@ {Usage} - {node ./TwitToMast.js} [{-htqrpmbc}] [{-u} ~username~] [{-n} ~tweetcount~] [{-d} ~debuglevel~] [{-w} ~timeout~] - {node ./multi.js} [{-htqrpmbc}] [{-n} ~tweetcount~] [{-d} ~debuglevel~] [{-w} ~timeout~] + {node ./TwitToMast.js} [{-htqrspmbc}] [{-u} ~username~] [{-n} ~tweetcount~] [{-d} ~debuglevel~] [{-w} ~timeout~] + {node ./multi.js} [{-htqrspmbc}] [{-n} ~tweetcount~] [{-d} ~debuglevel~] [{-w} ~timeout~] {Arguments} {-h:} - show help screen (you made it here!) @@ -15,6 +15,8 @@ {-q:} - quote tweets will be included in the scan {-r:} - Link to quoted tweet will appear in the header, preceded by "re: " - default behavior posts link at bottom of Mastodon post preceded by "Quoting " + {-s:} - Enable content warning/spoiler prefixes + - tweets beginning with a certain string (defined in config.txt) will use following text on the same line as Mastodon content warning {-p:} - enable/disable posting to Mastodon {-m:} - include user's name, handle, and link to tweet {-b:} - display browser (disable headless mode) @@ -33,6 +35,10 @@ - Your Access Token obtained from Mastodon > Preferences > Development > Application {Line 2: API_URL} - https://~your mastodon server url~/api/v1/ + {Line 3: CONTENT_WARNING_PREFIX} + - if enabled, tweets beginning with this string will use following text on the same line as Mastodon content warning + - Commonly used content warning prefixes on twitter are "CW//", "TW//", and "SPOILER//" + - Starting a tweet with "SPOILER// Endgame Spoilers" with hide the contents of the crossposted toot behind a warning reading "Endgame Spoilers" {Examples} {Scrape 10 most recent tweets, quote tweets, and thread tweets from @twitter account, and post to Mastodon}