Return to Video

15.1: What is Node.js? - Twitter Bot Tutorial

  • 0:01 - 0:03
    Hello.
  • 0:03 - 0:05
    This video is the
    first in a series
  • 0:05 - 0:08
    of videos, where the ultimate
    goal, at the end of this video,
  • 0:08 - 0:11
    is to make a Twitter bot
    that posts images to Twitter.
  • 0:11 - 0:12
    Now, why you might do this?
  • 0:12 - 0:14
    What's the point of
    making a Twitter bot?
  • 0:14 - 0:15
    What are Twitter bots?
  • 0:15 - 0:18
    It's a big topic.
  • 0:18 - 0:22
    And we'll get to that
    discussion along the way.
  • 0:22 - 0:24
    I'll put some links in
    this video's description
  • 0:24 - 0:26
    of some articles,
    and some examples
  • 0:26 - 0:29
    that you might look at, to
    think about making Twitter bots.
  • 0:29 - 0:30
    But I'm really going
    to stick to the nuts
  • 0:30 - 0:34
    and bolts, the technical stuff,
    in this series of videos.
  • 0:34 - 0:36
    And the first thing that
    I want to start with
  • 0:36 - 0:40
    is something called Node,
    Node.js, and working
  • 0:40 - 0:42
    with the command line.
  • 0:42 - 0:46
    So let's say you're someone
    who's been here with me,
  • 0:46 - 0:49
    watching videos about
    programming in JavaScript.
  • 0:49 - 0:50
    Let's come over
    to the white board
  • 0:50 - 0:52
    here, and think about
    what's going on.
  • 0:52 - 0:57
    You are a person who
    is working on a laptop.
  • 0:57 - 1:01
    And in that laptop,
    you have a browser.
  • 1:01 - 1:04
    And you have been making
    JavaScript sketches.
  • 1:04 - 1:07
    You might have written
    an HTML page, a CSS page.
  • 1:07 - 1:09
    And you have these nice
    little animations running in
  • 1:09 - 1:13
    that browser, written in-- and
    I have a whole set of tutorials
  • 1:13 - 1:16
    that you could look at, using
    this JavaScript framework,
  • 1:16 - 1:16
    called p5.js.
  • 1:16 - 1:19
  • 1:19 - 1:21
    So what's going on there?
  • 1:21 - 1:25
    If, for example, you are
    using the p5.js editor, what
  • 1:25 - 1:29
    the p5.js editor
    is doing is running
  • 1:29 - 1:30
    something called a server.
  • 1:30 - 1:33
  • 1:33 - 1:35
    What is a server?
  • 1:35 - 1:37
    So we think of this thing.
  • 1:37 - 1:40
    We use this terminology,
    known as the cloud.
  • 1:40 - 1:42
    This is a thing that's
    come up a lot at ITP
  • 1:42 - 1:44
    here recently, which is
    that the cloud is really
  • 1:44 - 1:46
    like a lot of
    underground bunkers,
  • 1:46 - 1:50
    and giant, scary-looking
    facilities.
  • 1:50 - 1:53
    They're not really these
    servers floating in the sky,
  • 1:53 - 1:56
    on the wings of
    unicorns, or something,
  • 1:56 - 1:59
    that you might like to think,
    or that this idea of the cloud
  • 1:59 - 2:01
    might make you think--
    but off topic here.
  • 2:01 - 2:05
    The point is let's just think
    about Google, for a second.
  • 2:05 - 2:08
    Google has a server.
  • 2:08 - 2:12
    And when you type into
    your browser google.com,
  • 2:12 - 2:16
    you are making a
    request to that server.
  • 2:16 - 2:21
    And that server
    gives you a response.
  • 2:21 - 2:23
    All of that has, in
    fact, been happening,
  • 2:23 - 2:26
    if you're writing
    JavaScript code,
  • 2:26 - 2:29
    making web pages on your laptop.
  • 2:29 - 2:31
    Your laptop, using
    the p5.js editor,
  • 2:31 - 2:34
    is running a little server,
    to simulate what might happen,
  • 2:34 - 2:38
    should you ever deploy or upload
    your stuff somewhere else.
  • 2:38 - 2:40
    And so, if you have
    your own website,
  • 2:40 - 2:43
    like my website is
    shiffman-- it really
  • 2:43 - 2:45
    needs to be redesigned
    and fixed up.
  • 2:45 - 2:46
    Maybe you can help me with that.
  • 2:46 - 2:52
    So I hesitate to mention it--
    but shiffman.net, for example.
  • 2:52 - 2:56
    If I worked on something, I
    took my code, and I put it here.
  • 2:56 - 3:00
    Then now somebody
    else in the internet
  • 3:00 - 3:04
    could make a request
    to my server, where I
  • 3:04 - 3:06
    put all of my JavaScript code.
  • 3:06 - 3:10
    And it might respond and
    send that JavaScript code,
  • 3:10 - 3:12
    so that that person can see the
    beautiful animation happening
  • 3:12 - 3:14
    in their web browser.
  • 3:14 - 3:16
    So all of these, with
    the exception of Google--
  • 3:16 - 3:18
    because their
    server is obviously
  • 3:18 - 3:21
    a highly-sophisticated
    thing, with lots going on--
  • 3:21 - 3:23
    but in this case,
    with p5.js running it
  • 3:23 - 3:26
    locally, or uploading
    the thing you made
  • 3:26 - 3:30
    with p5.js to your own web
    server, the only thing that's
  • 3:30 - 3:34
    happening on the web server,
    that the web server is doing,
  • 3:34 - 3:36
    is delivering static content.
  • 3:36 - 3:38
    It's delivering an HTML file.
  • 3:38 - 3:40
    It's delivering a
    JavaScript file.
  • 3:40 - 3:41
    It's delivering some images.
  • 3:41 - 3:43
    It's delivering some
    style, some CSS.
  • 3:43 - 3:47
  • 3:47 - 3:49
    What I would like to talk
    about in this video is
  • 3:49 - 3:54
    what happens when there
    comes a time in your life
  • 3:54 - 3:56
    where that's not enough?
  • 3:56 - 3:59
    You need to actually run
    a program on the server.
  • 3:59 - 4:00
    Now, Google clearly does this.
  • 4:00 - 4:04
    Because when you ask for
    search for rainbows--
  • 4:04 - 4:07
    you want to find out on
    Google about rainbows--
  • 4:07 - 4:09
    a little server program runs.
  • 4:09 - 4:12
    And it connects to their
    massive, crazy database
  • 4:12 - 4:16
    of the entire universe of
    thought and information.
  • 4:16 - 4:17
    And it processes
    it, and figures out.
  • 4:17 - 4:19
    And it figures out what
    to give back to you.
  • 4:19 - 4:21
    That's a little
    server program, that's
  • 4:21 - 4:22
    running on Google's servers.
  • 4:22 - 4:24
    If you log into
    your online banking,
  • 4:24 - 4:26
    and put in your
    username and password,
  • 4:26 - 4:28
    a little server program has
    to read that, figure out
  • 4:28 - 4:30
    if it's the right
    username and password,
  • 4:30 - 4:32
    and give you back your account
    balance-- that sort of thing.
  • 4:32 - 4:39
    So Node is a framework,
    a JavaScript framework,
  • 4:39 - 4:44
    for writing custom--
    I would say-- servers.
  • 4:44 - 4:46
  • 4:46 - 4:49
    Now the thing is, even
    though-- so there's
  • 4:49 - 4:53
    a lot that you could
    do here, in terms
  • 4:53 - 4:55
    of writing your own web server.
  • 4:55 - 5:01
    But the reason why I am
    doing these videos about Node
  • 5:01 - 5:04
    is one of the things
    that you might
  • 5:04 - 5:06
    want to do in your
    life, making projects
  • 5:06 - 5:08
    with code and
    information and things,
  • 5:08 - 5:12
    is make use of the Twitter API.
  • 5:12 - 5:17
    So I have a whole set of
    videos, all about various APIs
  • 5:17 - 5:21
    you could connect to--
    Wordnik, New York Times, Giphy.
  • 5:21 - 5:24
    All these are APIs
    that in previous videos
  • 5:24 - 5:29
    I've shown you how to connect
    to, from p5.js itself.
  • 5:29 - 5:33
    But Twitter is a special
    sort of API, that requires
  • 5:33 - 5:36
    something called OAuth.
  • 5:36 - 5:39
    I think of that as like, oh my
    God, I have to authenticate.
  • 5:39 - 5:42
    When I see OAuth,
    I think oh, auth.
  • 5:42 - 5:43
    Because it's a pain.
  • 5:43 - 5:43
    And I don't know.
  • 5:43 - 5:45
    I need this extra thing.
  • 5:45 - 5:46
    And I can't do it
    in the client side.
  • 5:46 - 5:49
    And then I need a secret,
    and a key, and a token,
  • 5:49 - 5:51
    and start to feel very stressed
    out, and want to lie down,
  • 5:51 - 5:56
    and need a cold compress on
    my head-- all of this stuff.
  • 5:56 - 5:58
    I'm feeling flushed already,
    just thinking about it.
  • 5:58 - 6:02
    But ultimately, an answer
    to dealing with OAuth
  • 6:02 - 6:07
    is writing your own
    Node application.
  • 6:07 - 6:10
    You must authenticate with
    your account information.
  • 6:10 - 6:14
    You cannot do that from
    just the browser directly.
  • 6:14 - 6:17
    A server can do that,
    however, for you.
  • 6:17 - 6:20
    So if you wanted to connect to
    Twitter from-- that camera's
  • 6:20 - 6:21
    shut off.
  • 6:21 - 6:24
    If you want to connect
    to Twitter from-- I
  • 6:24 - 6:25
    lost my train of thought.
  • 6:25 - 6:28
    If you want to connect
    to Twitter from p5.js,
  • 6:28 - 6:30
    what you need is
    a server program
  • 6:30 - 6:33
    that's serving up
    your p5.js code,
  • 6:33 - 6:35
    and also talking
    to Twitter for you.
  • 6:35 - 6:38
    And there's some communication
    thing that's going on there.
  • 6:38 - 6:39
    I'm going to get to
    all of this eventually.
  • 6:39 - 6:40
    There's a lot of steps involved.
  • 6:40 - 6:42
    But I'm trying to give
    you the background of what
  • 6:42 - 6:43
    you might need Node for.
  • 6:43 - 6:45
    Other things you might
    need a server for-- you
  • 6:45 - 6:48
    want to save stuff to
    a database on a server.
  • 6:48 - 6:52
    If you want to process a
    massive amount of data,
  • 6:52 - 6:55
    and pass that back
    to your p5.js sketch,
  • 6:55 - 6:58
    you might want to
    write your own server.
  • 6:58 - 7:00
    If you want to build your
    own API-- an example that I
  • 7:00 - 7:02
    was thinking about
    the other day is
  • 7:02 - 7:05
    if you wanted to make an expose
    information about yourself,
  • 7:05 - 7:06
    through an API.
  • 7:06 - 7:08
    And you put all these
    sensors on your body,
  • 7:08 - 7:11
    all day, that are tracking
    all sorts of things.
  • 7:11 - 7:13
    Then, on your own server,
    you could expose that.
  • 7:13 - 7:14
    We could read a
    server that spits out
  • 7:14 - 7:16
    all that information, via JSON.
  • 7:16 - 7:17
    Other people could query it.
  • 7:17 - 7:19
    So there's lots
    of creative things
  • 7:19 - 7:20
    you could do with a server.
  • 7:20 - 7:24
    And I feel like this is the new
    playlist-- creative servers,
  • 7:24 - 7:25
    and working with Node.
  • 7:25 - 7:28
    But in this section, the
    reason why you need Node
  • 7:28 - 7:30
    is the Twitter API.
  • 7:30 - 7:33
    So that was a lot of
    background there, and thinking
  • 7:33 - 7:34
    about this picture.
  • 7:34 - 7:36
    Let's come back
    over here, to where
  • 7:36 - 7:41
    I am invisible, because this
    camera shut off by accident.
  • 7:41 - 7:42
    And now what I
    would like to do is
  • 7:42 - 7:47
    start looking at how you
    actually use Node, locally
  • 7:47 - 7:48
    on your own laptop.
  • 7:48 - 7:50
    So the first thing that
    you're going to need to do
  • 7:50 - 7:52
    is download and install Node.
  • 7:52 - 7:54
    And I guess I've
    already done that.
  • 7:54 - 7:57
    But I'm going to
    just Google Node.
  • 7:57 - 7:59
    And I'm going to grab
    the Node website.
  • 7:59 - 8:01
    You can see it's node.js.org.
  • 8:01 - 8:04
    The website is detecting
    that I'm on OS X.
  • 8:04 - 8:06
    So it's offering me
    the OS X download.
  • 8:06 - 8:08
    But of course, if you're on
    Windows, just about everything
  • 8:08 - 8:10
    I'm showing you here
    will be the same.
  • 8:10 - 8:13
    But please, if you're lost or
    confused, ask in the comments.
  • 8:13 - 8:16
    And maybe I could
    ultimately make
  • 8:16 - 8:20
    a video that just covers looking
    at Node on a Windows machine,
  • 8:20 - 8:21
    if that would be
    helpful to some of you.
  • 8:21 - 8:25
    So once you've downloaded
    and installed Node,
  • 8:25 - 8:26
    another question opens up.
  • 8:26 - 8:27
    Well, what do you do?
  • 8:27 - 8:28
    How do you run Node?
  • 8:28 - 8:30
    How do you even make
    anything with Node?
  • 8:30 - 8:35
    So this involves using your
    computer's command line.
  • 8:35 - 8:39
    So on a Mac, there is
    something called terminal.
  • 8:39 - 8:41
    And I have it already here.
  • 8:41 - 8:43
    I'm inside the terminal.
  • 8:43 - 8:46
    I can't see where I am.
  • 8:46 - 8:49
    Am I touching the terminal?
  • 8:49 - 8:53
    Anyway, this over here--
    this is the terminal.
  • 8:53 - 8:56
    You guys don't need this weird
    physical thing that I'm doing.
  • 8:56 - 9:00
    And if I run it, you could see--
    woo, I don't need it that big.
  • 9:00 - 9:01
    Everything's going crazy.
  • 9:01 - 9:03
    The font size is giant.
  • 9:03 - 9:06
  • 9:06 - 9:08
    You can see here
    is now-- this is
  • 9:08 - 9:10
    a little window
    into the computer,
  • 9:10 - 9:12
    into the computer's
    soul, in a way.
  • 9:12 - 9:14
    So you need to find terminal.
  • 9:14 - 9:17
    Search for-- it's in your
    applications utilities.
  • 9:17 - 9:18
    Run terminal.
  • 9:18 - 9:21
    Suddenly, this is a place where
    you can start issuing commands
  • 9:21 - 9:23
    for the computer to follow.
  • 9:23 - 9:26
    For example, a command that
    you might want to know about
  • 9:26 - 9:27
    is pwd.
  • 9:27 - 9:32
    pwd stands for print
    working directory.
  • 9:32 - 9:34
    And you can see,
    that's where I am.
  • 9:34 - 9:36
    I'm in the
    Users/processing folder.
  • 9:36 - 9:40
    So you can browse your
    computer's folder and directory
  • 9:40 - 9:42
    structure from this
    terminal window.
  • 9:42 - 9:46
  • 9:46 - 9:50
    These are essentially
    Unix commands.
  • 9:50 - 9:52
    And so ultimately,
    you're learning
  • 9:52 - 9:54
    about how you might
    operate a computer that
  • 9:54 - 9:55
    doesn't have a GUI interface.
  • 9:55 - 9:59
    And this is how lots of
    computers used to work only.
  • 9:59 - 10:00
    I'm a little off track here.
  • 10:00 - 10:03
    So another command that
    might be really useful
  • 10:03 - 10:05
    is cd, for change directory.
  • 10:05 - 10:09
    So I happen to know that I want
    to change into the Documents
  • 10:09 - 10:09
    folder.
  • 10:09 - 10:11
    So I'm going to do that.
  • 10:11 - 10:13
    And now I can say pwd to c.
  • 10:13 - 10:16
    Yes, I'm in
    Users/processing Documents.
  • 10:16 - 10:18
    And then another
    thing you could do,
  • 10:18 - 10:20
    at least on a Mac, which is
    really nice, is I can take cd.
  • 10:20 - 10:22
    And then I can
    say, well, where's
  • 10:22 - 10:23
    the folder I really want to be?
  • 10:23 - 10:28
    Well, I really want to be in
    Desktop, YouTube code, which
  • 10:28 - 10:30
    is where I'm going to put
    the code for this video.
  • 10:30 - 10:32
    I can actually just
    drag that folder
  • 10:32 - 10:34
    into the terminal itself.
  • 10:34 - 10:38
    And it auto-populated
    the path for that folder.
  • 10:38 - 10:39
    I can hit Enter.
  • 10:39 - 10:42
    And then you can see
    I'm in that directory.
  • 10:42 - 10:43
    So why does any of this matter?
  • 10:43 - 10:47
    The reason why this matters
    is unlike other things
  • 10:47 - 10:51
    that you might have done before,
    like processing, or p5, where
  • 10:51 - 10:54
    you're writing code
    in a GUI interface,
  • 10:54 - 10:57
    hitting a Run button, seeing
    a result, everything that you
  • 10:57 - 11:00
    do through Node is all going
    to happen through this terminal
  • 11:00 - 11:00
    view.
  • 11:00 - 11:04
    So for example-- this only will
    work if you've installed Node--
  • 11:04 - 11:05
    but one of the
    first things you can
  • 11:05 - 11:08
    do with Node is just type node.
  • 11:08 - 11:12
    Now notice the icon
    changed from a dollar sign
  • 11:12 - 11:14
    to a greater than symbol.
  • 11:14 - 11:17
    So now that you have
    this greater than symbol,
  • 11:17 - 11:19
    it means oh, I'm
    in JavaScript land.
  • 11:19 - 11:21
    I could just start
    typing JavaScript code.
  • 11:21 - 11:25
    I could say var x equals 100.
  • 11:25 - 11:29
    Var y equals x times 2.
  • 11:29 - 11:30
    And then I could say
    oh, let me type x.
  • 11:30 - 11:31
    And look, it's the value 100.
  • 11:31 - 11:32
    Let me type y.
  • 11:32 - 11:33
    It's the value 200.
  • 11:33 - 11:35
    So one of the things
    you can do with Node
  • 11:35 - 11:37
    is just start playing
    around with experimenting
  • 11:37 - 11:38
    with JavaScript.
  • 11:38 - 11:40
    It's like a real
    time console, where
  • 11:40 - 11:43
    this is very similar
    to the browser console,
  • 11:43 - 11:45
    if you've ever used that
    to debug, and figure out
  • 11:45 - 11:46
    what's going in the browser.
  • 11:46 - 11:50
    So first of all, Node just let's
    you run arbitrary JavaScript,
  • 11:50 - 11:51
    through the command line.
  • 11:51 - 11:53
    If I want to get
    out of this, I need
  • 11:53 - 11:55
    to type control-c, control-c.
  • 11:55 - 11:59
    The other thing Node lets you
    do is run a small JavaScript
  • 11:59 - 12:01
    programs through
    the command line.
  • 12:01 - 12:04
    So for example, I'm going
    to use this editor subline.
  • 12:04 - 12:07
    And I'm going to save this file.
  • 12:07 - 12:10
    And I'm going to go to
    that folder, YouTube code.
  • 12:10 - 12:13
    And I'm going to
    name it as node1.
  • 12:13 - 12:15
    I'm going to make a folder.
  • 12:15 - 12:18
    And then I'm going
    to say hello.js.
  • 12:18 - 12:23
    So I've now saved a file in
    that directory called hello.js.
  • 12:23 - 12:25
    Here, by the way,
    is another command
  • 12:25 - 12:29
    that that you might
    find useful-- ls.
  • 12:29 - 12:31
    You can say, oh, list?
  • 12:31 - 12:33
    I have a directory called node1.
  • 12:33 - 12:36
    Let's change into that
    directory and type ls again.
  • 12:36 - 12:39
    And I can see hello.Js is there.
  • 12:39 - 12:44
    Now, what if I were to put
    into this program console.log
  • 12:44 - 12:47
    Hello YouTube.
  • 12:47 - 12:47
    I don't know.
  • 12:47 - 12:49
    I might upload this to Vimeo.
  • 12:49 - 12:55
    Hello arbitrary video website.
  • 12:55 - 12:57
    So I wrote some
    code in this file.
  • 12:57 - 12:59
    How do I execute that code?
  • 12:59 - 13:03
    Node-- I want to run something
    with Node-- hello.js.
  • 13:03 - 13:06
  • 13:06 - 13:07
    And you can see hello.
  • 13:07 - 13:09
    And spelled abitrary aribitrary.
  • 13:09 - 13:10
    I spelled it wrong.
  • 13:10 - 13:13
    But you can see that
    that code has run.
  • 13:13 - 13:13
    And it's come out.
  • 13:13 - 13:16
    So little processes,
    little scripts,
  • 13:16 - 13:18
    little things that I might want
    to make happen on my computer,
  • 13:18 - 13:20
    that just-- I can program
    them in JavaScript,
  • 13:20 - 13:22
    and run them from
    the command line.
  • 13:22 - 13:25
    Number one, that's
    what Node is for.
  • 13:25 - 13:28
    Now if this were the
    part of the videos
  • 13:28 - 13:31
    that I intend to ultimately
    make, were all I'm doing
  • 13:31 - 13:37
    is looking at web servers,
    then ultimately, I
  • 13:37 - 13:40
    might show you then now how
    to open a listening port,
  • 13:40 - 13:42
    how to accept
    connections to that port,
  • 13:42 - 13:44
    how to send things
    back to the browsers
  • 13:44 - 13:46
    that are connected to that
    port-- all of those things.
  • 13:46 - 13:48
    But instead of that,
    what I want to do
  • 13:48 - 13:52
    is take another direction,
    which is just look at how can I,
  • 13:52 - 13:54
    from here, from
    the command line,
  • 13:54 - 13:57
    connect and authenticate
    to the Twitter API,
  • 13:57 - 14:01
    and get information
    from-- search Twitter,
  • 14:01 - 14:04
    look for people who have
    replied to me on Twitter,
  • 14:04 - 14:07
    and then also post tweets
    myself, back to Twitter.
  • 14:07 - 14:10
    How can I do all of that, just
    right here from the command
  • 14:10 - 14:12
    line, without ever opening
    up a browser, without ever
  • 14:12 - 14:14
    touching any other Twitter app?
  • 14:14 - 14:16
    Because if I can
    do that, then I can
  • 14:16 - 14:18
    make a program that
    will run and say,
  • 14:18 - 14:21
    every 5 minutes, or 15
    minutes, or one hour,
  • 14:21 - 14:23
    post a tweet
    automatically, based
  • 14:23 - 14:26
    on some other type of
    algorithmic set of rules
  • 14:26 - 14:27
    that I might want to make.
  • 14:27 - 14:29
    So I think this wraps
    up this first video.
  • 14:29 - 14:32
    If you're watching
    it, download Node.
  • 14:32 - 14:32
    Get it installed.
  • 14:32 - 14:35
    Figure out how to get to the
    command line of your computer.
  • 14:35 - 14:36
    Figure out that
    you can type Node,
  • 14:36 - 14:38
    that you can enter
    some JavaScript,
  • 14:38 - 14:39
    that you can run a
    short little JavaScript
  • 14:39 - 14:41
    program that just
    spits something
  • 14:41 - 14:42
    back out to the console.
  • 14:42 - 14:44
    And in the next video,
    what I'm going to look at
  • 14:44 - 14:48
    is how to use something called
    NPM-- Node Package Manager--
  • 14:48 - 14:52
    to spin up a larger
    Node project,
  • 14:52 - 14:56
    and use a Twitter package to
    connect to the Twitter API.
  • 14:56 - 14:57
    OK, so thanks for watching.
  • 14:57 - 15:02
    And see you in the next video.
  • 15:02 - 15:05
    That was me making
    a rainbow symbol.
  • 15:05 - 15:07
    You know how people
    have a sign out thing?
  • 15:07 - 15:10
    That's never going to work
    for me, I know, sorry.
  • 15:10 - 15:11
    I'm embarrassed for myself too.
  • 15:11 - 15:13
    OK.
Title:
15.1: What is Node.js? - Twitter Bot Tutorial
Description:

more » « less
Video Language:
English
Duration:
15:13

English subtitles

Revisions