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