⚠️ This lesson is retired and might contain outdated information.

Tweet Media Files with Twit.js

Hannah Davis
InstructorHannah Davis
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated a year ago

In this lesson, we’ll create multiple functions to request, download, and save photos and data from NASA's API, and then have our bot upload these photos to Twitter and post them along with their descriptions. We'll also learn how to tweet videos using a video from NASA’s space archives.

[00:00] In addition to Twit, we'll also need request since we'll be making requests, and FS since we'll be working with the file system. The first thing we're going to do is get NASA's photo of the day. We'll wrap all these in functions so we can use them again. We'll need our parameters which are a URL, api.nasa.gov/planetary/apod.

[00:37] You will need your queries. We only need to give it the API key which you'll need to register for with NASA, and then add to your environment variables. Then an encoding, which will be binary.

[00:58] We can say request.getParameters, and then our callback, error response, and the body. With this API, we need to parse the body. We'll make another function saveFile, and we'll pass it the body and the name we want to save our file as. Let's write the saveFile function.

[01:37] First, we'll say barFile = fs.createWriteStream with the file name. The way we save it is requestBody.pipeFile, and then we'll also say .onClose function error. If there is an error then we'll log it out, otherwise we can say media is saved.

[02:20] Let's see what this does. If we run that, we see media saved. If we look in our folder, we can see that the image is there. Let's also print out the information that we're getting. We've got a title, we've got the URL of the image, we've got an interesting explanation, we've got the date. One thing we're going to need going forward is a small description.

[02:49] We'll say the description text is equal to the body's title. Here that would be penumbral eclipse rising. We're going to upload our media, and we're going to pass this the description text and the file name.

[03:14] There are two ways to upload media to Twitter, but the Twitter API will eventually only use the chunked version, so that's the one I'll show you today. To upload media in chunks, we first need to give it the file path.

[03:36] To get the file path we'll get the path to our directory, dirname plus a slash, plus our file name. We'll say bot.postMediaChunked, a file path which we'll pass our file path variable, and our callback. If there's an error, we'll log it, otherwise, let's log out our data.

[04:23] Let's erase this one for now. We can see that after uploading this media, Twitter gives us back information about it including media ID, which we'll need in a second.

[04:40] From here we're going to want to post our image to Twitter. We need a few parameters, the first being the status which is going to be our description text, and the media IDs, which is the media ID string. Here, we'll post our status with our parameters. To post our image is straightforward, and works the same way that we post a status.

[05:22] It's bot.postStatuses/update the params and our callback. If there's an error we'll log it out, otherwise, we'll say status posted.

[05:43] Let's see if this works. If we go to our Twitter account, we can see that the image has been posted. That's a beautiful one. You can also upload a video, so we'll do that directly by saying uploadMedia. We need a description and a file name.

[06:21] If we go back to our timeline, we can see that the video was posted as well.

kayelovejoy
kayelovejoy
~ 7 years ago

I am really enjoying this tutorial. Thank you. I am getting an error on 7-media and I couldn't complete this lesson.
Error: options.uri is a required argument (coming from the Request.init)

AJ
AJ
~ 7 years ago

Fix Video 7, it does not play.

Cher
Cher
~ 7 years ago

i'm getting 403 error for lesson 7

Cameron Kalegi
Cameron Kalegi
~ 7 years ago

@kayelovejoy - Once I registered for the NASA API key and added it to my environment variables, I found that re-running source ~/.bash_profile solved the options.uri is a required argument error.

Markdown supported.
Become a member to join the discussionEnroll Today