Skip to content

Instantly share code, notes, and snippets.

@towerofnix
Last active November 2, 2024 18:22
Show Gist options
  • Save towerofnix/40c6a35c4c775b8af72e0f99f2c53b66 to your computer and use it in GitHub Desktop.
Save towerofnix/40c6a35c4c775b8af72e0f99f2c53b66 to your computer and use it in GitHub Desktop.
Collate huge lists of Discord search results
messages = JSON.parse(fs.readFileSync('references-search-message-results.json', 'utf8'))
channels = (
Array.from(new Set(messages.map(({channel}) => channel.name)))
.map(name => ({
name,
messages: messages.filter(({channel}) => channel.name === name),
})))
runs = (
channels.flatMap(({messages}) =>
messages.reduce(
(acc, message) =>
((new Date(message.date)
- new Date(acc.at(-1).at(-1)?.date)
> 1000 * 60 * 12)
? [...acc, [message]]
: [...acc.slice(0, -1), [...acc.at(-1), message]]),
[[]])))
interestingRuns = (
runs
.filter(({length}) => length >= 4)
.sort((a, b) =>
(new Date(a[0].date)
- new Date(b[0].date))))
resultsText = (
interestingRuns.map(messages =>
({[new Date(messages[0].date).toLocaleDateString() +
`, #${messages[0].channel.name}`]:
messages.map(msg => {
const words = msg.plainText.split(' ')
return {[msg.who.displayName]:
(words.length > 24
? words.slice(0, 24).join(' ') + ' [...]'
: words.join(' '))}
})})))
fs.writeFileSync('references-interesting-runs.json', JSON.stringify(resultsText, null, 2))
resultsMap = new Map()
if (typeof interval === 'number') clearInterval(interval)
interval = setInterval(() => {
resultsContainer = document.querySelector('[class*=searchResultsWrap]')
if (!resultsContainer) return;
results = Array.from(resultsContainer.querySelectorAll('[role=article][id*=search-result'))
resultDetails = results.map(result => ({
id: result.id.match(/\d+/)[0],
who: {
id: result.querySelector('[class*=headerText_]')?.id.match(/\d+/)[0],
displayName: result.querySelector('[class*=header_] [class*=username_]')?.innerText,
},
channel: {
name: results[0].closest('[role=group]').querySelector('[class*=channelNameText_]').innerText,
},
date: new Date(result.querySelector('[class*=header_] time, [class*=timestamp_] time').getAttribute('datetime')),
plainText: result.querySelector('[class*=messageContent_]').innerText,
}));
newResults = 0
for (const result of resultDetails) {
if (!resultsMap.has(result.id))
newResults++
resultsMap.set(result.id, result)
}
if (newResults)
console.log(`${newResults} search results newly tracked`)
}, 200)
all = () =>
JSON.stringify(Array.from(resultsMap.values())
.sort((a, b) => a.date - b.date), null, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment