An important note: These lessons are now updated and built into Hackety Hack. Don't bother reading them here on the wiki. Just run HH and click on the lightbulb icon in the toolbar. Much obliged! - _why

Lesson Five / Hackety Fun with YouTube?

Approx. Time Needed: 30 mins.

Due Date: Wed, 2007 Mar 28

Part A: Why's YouTube? Module

While you've been writing your blog and fetching web pages and stuff, I've been off in Greenland working on my own program that lets me keep copies of my favorite YouTube? videos. It's turning out so good that I totally want you to try it out!!

In this lesson, I'm going to share my program with you and it's up to you to put it to good use. This is going to be a super-challenging lesson, but you'll grow a lot as a hacker, so it's very much worth it, don't you think??

Getting Why's Program

Start up a new program. Here's how you'll start it out:

  # Playing with Why's YouTube program.
  Hacker('why').load_program('youtube.rb')
  youtube_vid = ask("Enter the link to a YouTube video:")
  save_as = ask("Your name for this video:")
  YouTube.download(youtube_vid, save_as + ".flv")

Save this program as "YouTube?". We'll continue to improve this over the course of the lesson. Go ahead and run this script.

It asks you for a YouTube? link. Let's download a cartoon, huh?

  http://youtube.com/watch?v=LNVYWJOEy9A

When it asks you for a name, type in "Adventure Time" and click OK.

So, did it start downloading the video?? Let it go, it may take awhile.

In the first line, you load a program from my batch of shared programs. The phrase "Hacker('why')" creates a Hacker object. You'll be able to use this object to send messages to other hackers and to share programs between yourselves. For instance, the Hacker object has a method called "load_program" which is used to fetch my YouTube? program.

Did you notice how Hackety Hack downloaded my program before it ran the rest of your program? Where did my program come from? How did Hackety Hack know where to find youtube.rb??

  http://why.hacketyhack.net/program/youtube.rb

Every Hackety Hack user, if they like, gets their own spot to share programs at HacketyHack?.net. Mine is at http://why.hacketyhack.net. So, as you see, programs are simply shared over the web. This is neat because you can go ahead and read the code to my program right on hacketyhack.net before you download it.

Back to Adventure Time

So, the video will finish downloading. But what can you do with it? We save it as "Adventure Time.flv" but what is a .flv file?

Here's one way to watch the video:

  puts Video("Downloads/Adventure Time.flv")

You can put that in a new, blank program and run it.

Extra credit: For extra credit, see if you can get the Video to appear in a popup.

Part B: Listing Your Videos

Okay, so we're kind of at the point we were with your blog. Your blog had one program for adding blog entries. And another program for reading your blog. That was fun and all, but wouldn't it be nice to have just one program called "YouTube?" that was for downloading and watching and sorting your videos??

Let's start with a simple popup. You can just make this the new "YouTube?" program.

  # Keep a catalog of YouTube videos.
  vid = Web.popup() {
    title("My Downloaded Videos")
    bullets() do
      Dir["Downloads/*.flv"].each() do |file|
        link_to(file)
      end
      link_to("Download a New Video")
    end
  }
  puts "Want to watch #{vid}?"

Okay, so a lot of familiar things here. You know about "Web.popup" and its "title". But a bunch of new things "bullets" and "link_to" and "Dir".

Run this program a few times and get the hang of it. Obviously, we're not finished. But, ask yourself, what happens when you click on a link? Read over the program and see if you can figure out how it works yourself.

Okay, good. Let's talk about the three new parts of this program:

  1. 'bullets' is a method which creates a bulleted list. Whenever you make a new link or paragraph or title inside the list, it becomes a new bullet.
  1. 'link_to' simply adds a link. When you click the link, the popup closes and the name of the link is saved in 'vid'. Do you see where 'vid' gets set?
  1. 'Dir[]' is a special command for searching folders. We want to find all .flv files in the Downloads directory. So we use:
            Dir["Downloads/*.flv"]
    
    If we wanted to list all files in the Downloads directory, not just .flv files, then we'd use:
            Dir["Downloads/*"]
    
    The asterisk is a wildcard character. It matches any set of words, such as "Adventure Time" in the name "Adventure Time.flv".

This popup is going to be our main menu for our program. We'll be able to watch videos or download new videos from here. And the vid variable will tell us what's next.

Extra credit: try adding a title in the bullets. Or try changing the bullets to numbers with the list method.

The Main Menu

Okay, so let's wrap up the main menu in its own new command.

  # Keep a catalog of YouTube videos
  def main_menu()
    # ... MOVE JUST THE POPUP INTO HERE ...
  end

  vid = main_menu()

And when I say "MOVE JUST THE POPUP INTO HERE", I'm serious. Put the Web.popup code right in there where that line is.

Run it, make sure it still works and we'll move along.

Part C: The Other Two Popups

Since this is a really tough lesson and you're moving along so nicely, I'm going to just cheat and give you the other two popups. You can stick these right under the main_menu() popup.

  def download_video_popup()
    Web.popup() {
      title("Download a Video")
      p("(Works with YouTube, Google Video, Revver, etc.)")
      editline("URL")
      editline("Video Name")
      buttons() { cancel(); ok() }
    }
  end
  
  def show_video_popup(video_title)
    Web.popup {
      title video_title
      puts Video(video_title)
    }
  end

Extra credit: Before I show you anything else, see if you can get these two popups to appear by calling them instead of main_menu() on the last line of the program.

Look over these two popups. What do they do??

The first popup looks a lot like the window you created for your blog! But it's not attached to the Blog Table. Or any Table, for that matter.

The second popup plays videos! Too easy!

Bringing it All Together

Here's the final piece of the puzzle. Put this under the three popup commands. You should have the three commands and then this.

  vid = main_menu()
  if vid == "Download a New Video"
    new_vid = download_video_popup()
    Hacker('why').load_program('youtube.rb')
    YouTube.download(new_vid["URL"], new_vid["Video Name"] + ".flv")
  else
    show_video_popup(vid)
  end

This is becoming a serious program. Much more intense than your other programs. I would spend some time looking over everything to get a handle on what's going on.

In this code, we're using an 'if'. The 'if' will check to see what was picked from the main_menu. 'if' we picked "Download a New Video", the program will go ahead and use Why's YouTube? module to download a new video.

Otherwise, we go to 'else', which tells Hackety Hack what to do if we picked one of the other links. Since the other links are all video file names, we can send that video name straight the the show_video_popup.

Try running it. Download a few more YouTube? videos, watch a few more, take a break, etc. You'll get the hang of it if you run it a few times and read through it over and over.

Extra credit: Okay, now that you've been exposed to making popups into commands, I'm going to really challenge you!! Take your blog and turn it into a single program. With a popup for reading your blog and an "Add a New Entry" link at the top!!

Appendix: The Complete YouTuber?

Here's the complete code for the YouTube? popup, if you got tripped up in any way at all:

# Keep a catalog of YouTube videos.
def main_menu()
  Web.popup() {
    title("My Downloaded Videos")
    bullets() do
      Dir["Downloads/*.flv"].each() do |file|
        link_to(file)
      end
      link_to("Download a New Video")
    end
  }
end

def download_video_popup()
  Web.popup() {
    title("Download a Video")
    p("(Works with YouTube, Google Video, Revver, etc.)")
    editline("URL")
    editline("Video Name")
    buttons() { cancel(); ok() }
  }
end

def show_video_popup(video_title)
  Web.popup {
    title video_title
    puts Video(video_title)
  }
end

video_title = main_menu()
if video_title == "Download a New Video"
  new_vid = download_video_popup()
  Hacker('why').load_program('youtube.rb')
  YouTube.download(new_vid["URL"], new_vid["Video Name"] + ".flv")
else
  show_video_popup(video_title)
end

===

Good work, Hacketeers!

_why