parent
617d6ea556
commit
e4c2aaf260
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,34 @@
|
||||
|
||||
const { collect } = require('./collector')
|
||||
const md5 = require('md5')
|
||||
|
||||
async function chat() {
|
||||
/* format:
|
||||
{
|
||||
"href": "/wiki/Admiral",
|
||||
"name": "Admiral",
|
||||
"birthday": "January 27<sup>th</sup>\n",
|
||||
"img": "https://static.wikia.nocookie.net/animalcrossing/images/5/52/NH-Admiral_poster.png/revision/latest?cb=20200522013249",
|
||||
"quotes": [
|
||||
"Only quitters give up."
|
||||
]
|
||||
}
|
||||
*/
|
||||
const animaldb = await collect()
|
||||
const possibleToots = animaldb.animals.map(record => {
|
||||
return record.quotes.map(quote => {
|
||||
const birthday = record.birthday.replace(/\n/g, "")
|
||||
return {
|
||||
"toot": `${record.name}: "${quote}"\n\n${birthday} is my birthday.\nhttps://animalcrossing.fandom.com${record.href}`,
|
||||
"attach": `data/ac/${md5(record.img)}.png`,
|
||||
"attachDescription": `Screenshot of Animal Crossing villager ${record.name}`
|
||||
}
|
||||
})
|
||||
}).flat()
|
||||
|
||||
return possibleToots[Math.floor(Math.random() * possibleToots.length)]
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
chat
|
||||
}
|
@ -1,6 +1,22 @@
|
||||
|
||||
// the main juice
|
||||
|
||||
const acbuddy = require('./buddies/animalcrossing/collector')
|
||||
const { buddies } = require('./config')
|
||||
if(!buddies) throw "Did you seutp your config?"
|
||||
|
||||
const { toot } = require('./toot');
|
||||
|
||||
(async function() {
|
||||
for await(buddy of buddies) {
|
||||
const { chat } = require(`./buddies/${buddy}/buddy`)
|
||||
|
||||
// contains 'toot', and maybe 'attach' / 'attachDescription'
|
||||
const message = await chat()
|
||||
console.log(`buddy ${buddy} has this to say: ${JSON.stringify(message)}`)
|
||||
|
||||
await toot(message)
|
||||
}
|
||||
|
||||
console.log('Done! for now... ')
|
||||
})()
|
||||
|
||||
acbuddy.collect()
|
@ -0,0 +1,48 @@
|
||||
const axios = require('axios')
|
||||
|
||||
const instance = 'https://chat.brainbaking.com'
|
||||
const clientName = "plerobuddies"
|
||||
|
||||
// Fill these in after register() ed. calls
|
||||
const clientId = ""
|
||||
const clientSecret = ""
|
||||
const approvalCode = ""
|
||||
|
||||
|
||||
async function register() {
|
||||
const raw = await axios.post(`${instance}/api/v1/apps`, {
|
||||
client_name: clientName,
|
||||
redirect_uris: "urn:ietf:wg:oauth:2.0:oob",
|
||||
scopes: "read write"
|
||||
}
|
||||
)
|
||||
|
||||
console.log(raw.data)
|
||||
console.log(`${instance}/oauth/authorize?redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&client_id=${clientId}&scope=read%20write`)
|
||||
}
|
||||
|
||||
|
||||
async function fetchToken() {
|
||||
const raw = await axios.post(`${instance}/oauth/token`, {
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
redirect_uri: "urn:ietf:wg:oauth:2.0:oob",
|
||||
scopes: "read write",
|
||||
grant_type: "authorization_code",
|
||||
code: approvalCode
|
||||
}
|
||||
)
|
||||
|
||||
console.log(raw.data)
|
||||
console.log(raw.data.access_token)
|
||||
}
|
||||
|
||||
(async function() {
|
||||
try {
|
||||
await fetchToken()
|
||||
} catch(err) {
|
||||
console.log(err.message)
|
||||
console.log(err.response?.data)
|
||||
}
|
||||
})()
|
||||
|
@ -0,0 +1,51 @@
|
||||
const axios = require('axios')
|
||||
const FormData = require('form-data')
|
||||
const { createReadStream } = require('fs')
|
||||
|
||||
const { instance, oauthToken } = require('./config')
|
||||
|
||||
async function toot(data) {
|
||||
// contains 'toot', and maybe 'attach' / 'attachDescription'
|
||||
const media_ids = []
|
||||
if(data.attach) {
|
||||
const id = await uploadMedia(data.attach, data.attachDescription)
|
||||
media_ids.push(id)
|
||||
}
|
||||
|
||||
// https://docs.joinmastodon.org/methods/statuses/
|
||||
const result = await axios.post(`${instance}/api/v1/statuses`, {
|
||||
status: data.toot,
|
||||
media_ids: media_ids,
|
||||
}, {
|
||||
headers: {
|
||||
"Authorization": `Bearer ${oauthToken}`
|
||||
}
|
||||
})
|
||||
|
||||
console.log(` toot: status result: ${result.status}`)
|
||||
}
|
||||
|
||||
async function uploadMedia(fileName, description) {
|
||||
const formData = new FormData()
|
||||
formData.append('file', createReadStream(fileName))
|
||||
formData.append('description', description)
|
||||
|
||||
// https://docs.joinmastodon.org/methods/statuses/media/
|
||||
const result = await axios.post(`${instance}/api/v1/media`, formData, {
|
||||
headers: {
|
||||
"Content-Type": `multipart/form-data; boundary=${formData.getBoundary()}`,
|
||||
"Authorization": `Bearer ${oauthToken}`
|
||||
}
|
||||
});
|
||||
|
||||
if(result.status !== 200) {
|
||||
throw "something went wrong with " + result.data
|
||||
}
|
||||
|
||||
console.log(` toot: media result: ${result.status} - media id: ${result.data.id}`)
|
||||
return result.data.id
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
toot
|
||||
}
|
Loading…
Reference in new issue