Skip to content

Instantly share code, notes, and snippets.

@gutchom
Created April 20, 2022 10:13
Show Gist options
  • Save gutchom/b7d8966b70d53492268c2159959e8bbf to your computer and use it in GitHub Desktop.
Save gutchom/b7d8966b70d53492268c2159959e8bbf to your computer and use it in GitHub Desktop.
Tweet #nowplaying via Node.js
const Jxa = import('run-jxa');
const Twitter = require('twitter');
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
Jxa.then(({ runJxa }) => {
runJxa(() => {
const app = Application("Music");
const { name, artist, artworks } = app.currentTrack;
const hex = artworks[0].rawData().slice(8,-2);
const text = artist() + ' の ' + name() + ' を 聴いてるよ #nowplaying';
return [text, hex];
}).then(([text, hex]) => {
if (hex) {
client.post('media/upload', { media: Buffer.from(hex, "hex") }, function(error, media) {
const { media_id_string } = media;
client.post('statuses/update', { status: text, media_ids: media_id_string }, function (error, tweet) {
console.log(tweet);
});
});
} else {
client.post('statuses/update', { status: text }, function(error, tweet) {
console.log(tweet);
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment