Skip to content

Instantly share code, notes, and snippets.

@ejfox
Created June 27, 2022 18:46
Show Gist options
  • Save ejfox/98ecfa51d0e91d86e23891f67bd8dc1a to your computer and use it in GitHub Desktop.
Save ejfox/98ecfa51d0e91d86e23891f67bd8dc1a to your computer and use it in GitHub Desktop.
const fs = require('fs');
const axios = require('axios');
const apiKey = 'X';
const apiKeySecret = 'X';
const bearerToken =
'X';
// go through all tweets and add tags using openAI
const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
apiKey: 'X',
});
const openai = new OpenAIApi(configuration);
// CHANGE THIS TO YOUR UNIQUE TWITTER USER ID
// https://www.wikihow.com/Find-Your-User-ID-on-Twitter
const $mrejfox = '10449';
const { TwitterApi, TwitterV2IncludesHelper } = require('twitter-api-v2');
const userClient = new TwitterApi({
appKey: apiKey,
appSecret: apiKeySecret,
});
async function getUserTimeline(userId) {
// Create a client with an already known bearer token
const appOnlyClient = new TwitterApi(bearerToken);
const userTimeline = await appOnlyClient.v2.userTimeline($mrejfox, {
'tweet.fields': ['created_at', 'public_metrics', 'lang'],
expansions: ['attachments.media_keys', 'referenced_tweets.id'],
'media.fields': ['url', 'preview_image_url']
});
const includes = new TwitterV2IncludesHelper(userTimeline);
const allTweets = [];
for await (const fetchedTweet of userTimeline) {
fetchedTweet.mediaUrls = userTimeline.includes.medias(fetchedTweet);
// for await (const media of fetchedTweet.mediaUrls) {
// if(!media.url) return
// await download_image(media.url, './public/images/'+media.media_key+'.jpg')
// }
// Now if we were cool, we would download all of the mediaUrls into a folder of images with the filenames as the media_key with axios
allTweets.push(fetchedTweet);
}
// write allTweets to file
fs.writeFileSync(
'./public/data/tweets_all.json',
JSON.stringify(allTweets, null, 2)
);
}
getUserTimeline($mrejfox);
//https://stackoverflow.com/questions/12740659/downloading-images-with-node-js
const download_image = (url, image_path) =>
axios({
url,
responseType: 'stream',
}).then(
(response) =>
new Promise((resolve, reject) => {
response.data
.pipe(fs.createWriteStream(image_path))
.on('finish', () => resolve())
.on('error', (e) => reject(e));
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment