Archive for 七月 29th, 2007

Downloading YouTube Videos with a Groovy one-liner, kind of

Inspired by Downloading YouTube videos with a Perl one-liner, I’ve put together a piece of code to do the same thing with Groovy. Not as succinct as Perl. But figure out how much noise there will be in plain Java.

HttpURLConnection.setFollowRedirects(false)
def vid = (args[0] =~ (/(? new URL("http://www.youtube.com/get_video?video_id=$vid&t=${(uri =~ (/(? os.write(it)}}

Let’s take a closer look at the code. To capture the redirect location, we must temporarily disallow HttpURLConnection to follow redirects.

HttpURLConnection.setFollowRedirects(false)

Then we extract the v id from the original YouTube URL and compose the URI for the embedded player. Open a connection to the video URI and extract the redirect location from the HTTP header. The input parameter arg[0] should be something like http://www.youtube.com/watch?v=5C0I7Ef4gQI

def vid = (args[0] =~ (/(?<=v=).*$/)).getAt(0)
def uri = ((HttpURLConnection)new URL("http://www.youtube.com/v/$vid").openConnection()).getHeaderField('Location')

Now we must enable http redirects again, otherwise the last line of code won’t work.

HttpURLConnection.setFollowRedirects(true)

Finally, go ahead to compose the video data download URI, open a connection and download the data stream.

new File("${vid}.flv").withOutputStream{os -> new URL("http://www.youtube.com/get_video?video_id=$vid&t=${(uri =~ (/(? os.write(it)}}

Save the script to a groovy file like “YoutubeSaver.groovy”. Run the script from command line and give the video URL as the parameter. For example,

groovy youtubesaver http://www.youtube.com/watch?v=5C0I7Ef4gQI

Hello world, again (Part II, how I moved from JRoller to Wordpress)

This is my summary about the migration of my blog from JRoller to here.

I’m a Java engineer working in China. So when I was looking for a free blogging service, JRoller was my natural choice, because

1. In China, Blogger, Wordpress, LiveJournal, and all other mainstream blogging sites are blocked. So there are sadly not many choices left for me.

2. Chinese local blogging sites are crap, specially from a developer’s perspective.

3. JRoller is the best free blogging service implemented in Java today. It’s hosted by JavaLobby which is one of my favorites. There are a lot of brilliant folks hosting their blogs there. And since it’s not so mainstream, it’s not blocked in China. Hurray!

But after more than 3 months living with JRoller, I have to say I’m an unhappy user. Roller’s (JRoller’s, actually) lack of attractive features and looks, defective Chinese support, mysterious API support, make it fairly uncompetitive compared to today’s top ones, and make my blogging experience rather unexciting.

So I decided to move to Wordpress, setting up on a server space I’m sharing with my friend, to get rid of all the problems. And I have to say Wordpress is simply great.

So I did some Google search about the migration of my posts from JRoller to Wordpress. I found a post from zeusville very helpful. And Jesus Rodriguez, the blogger, was kind enough to send me the custom JRoller template file which I wasn’t able to download from his blog anymore. Thanks again, Jesus.

Now I’m already very comfortable with Wordpress. BTW. This post is written with Windows Live Writer. I have to say that this is the best free offline blogging software I’ve used so far. Unfortunately it comes from Microsoft so it only runs on Windows. But maybe it’ll have a chance running on Mono on Linux. I’ll give it a try later.