Jay Hickey

Technology, life, and fascinating web encounters.

Second Crack Bookmarklet For Embedding Videos



So far, Second Crack has been really nice for blogging. The included bookmarklets have been indispensable for drafting quick links from my iPhone or iPad, but there's no easy way to post videos. I watch tons of videos on the web, and would love to be able to share them here. So I created a bookmarklet that makes it simple to do that.

This is the first time I've ever worked with PHP, so I'm definitely out of my comfort zone. But I'm pleased with my implementation. The bulk of the functionality relies on two functions:

get_html uses cURL to easily return the HTML of the current page as a string:

   
    function get_html($url) {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

Pretty straightforward. By setting CURLOPT_RETURNTRANSFER to TRUE, curl_exec is returned as a string instead of being output directly.

video_embed finds the ID of the video and returns an HTML iframe for embedding:



   function video_embed($html) {
        $vimeo = array('#player.vimeo.com/video/([0-9]*)#i');
        $youtube = array('#/embed/([0-9a-z_-]+)#i', '#youtu.be/([0-9a-z_-]+)#i',                 '#/v/([0-9a-z_-]+)#i', '#v=([0-9a-z_-]+)#i');

        for ($i = 0; $i < sizeof($vimeo); $i++) {
            preg_match($vimeo[$i], $html, $id);
            if ($id[1] != NULL) {
                return '<iframe src="http://player.vimeo.com/video/' . $id[1] . '?title=0&amp;byline=0&amp;portrait=0&amp;color=ff0000&amp;fullscreen=1&amp;autoplay=0" frameborder="0"></iframe>';         
            }       
        }

        for ($i = 0; $i < sizeof($youtube); $i++) {
            preg_match($youtube[$i], $html, $id);
            if ($id[1] != NULL) {
            return '<iframe src="http://www.youtube.com/embed/' . $id[1] . '?autohide=1&amp;fs=1&amp;autoplay=0&amp;iv_load_policy=3&amp;rel=0&amp;modestbranding=1&amp;showinfo=0&amp;hd=1" frameborder="0" allowfullscreen></iframe>';    
            }
        }   
        return NULL;
    }


The first two lines create arrays of regex patterns for video IDs of both embedded and non-embedded videos.1

preg_match then searches for a match in the HTML and returns an array called $id. $id[0] has the full text of the regex match and $id[1] has the text that matches the first captured parenthesized subpattern (i.e., just the video ID). Upon a match, embed code is returned with $id[1]'s value in place.

Notes:

  • This bookmarklet only works for YouTube and Vimeo videos. Pretty much all of my online video viewing is from these two services, so I'm fine with this limitation.

  • Any selected text will also be included in the draft as a blockquote. If no video is found on the current page, the bookmarklet functions just like Second Crack's included "Draft Link".

Download the source for the bookmarklet and install it the same way as Marco instructs. If you already have the other bookmarklets setup, just place add-video.php in the same directory and navigate to, e.g.:

http://admin.myblog.com/add-video.php

and install the bookmarklet from there.

Alternatively, I've added this to the existing "Draft Article" and "Draft Link" bookmarklet code. This conveniently keeps all the bookmarklets together and in one file. See the full source at my fork on GitHub.

I'm open to all ideas on improving the existing regex, expanding the functionality to other video services, or anything that will make this better. Get in touch.


  1. These are mixes of patterns I threw together and ones found on the web. They've worked against everything I've tried—mobile and non-mobile sites, embedded and non-embedded videos—but there may be some edge cases I've missed. 

ℐℋ
ℐℋ
ℐℋ
ℐℋ
ℐℋ

My New Blog!



I've finally gotten around to making my dream blog! I now have a snappy little site that is simple and allows me to post from any computer or iOS device I have in front of me.

Thanks to Marco Arment's awesome Second Crack, all I do is to save a markdown draft to my dropbox. From there, Second Crack instantly creates a nice static preview html file that allows me to view my post exactly as it will appear when I commit it. When the post's ready to be published, I just move it into the _publish-now/ directory or add a header that says "publish-now" in the markdown file. And it's done!

Marco has posted an awesome video demo of this greatness in action.1

Installing Second Crack wasn't the easiest thing in the world, but thanks to Nick Wynja's awesome deploysecondcrack and through instructions, it's definitely doable. Anyone with moderate SSH and command line knowledge can figure it out.

Because of it's simplicity, I'm hoping it encourages me to blog more often. Also, be prepared for drastic changes to the look and functionality of this blog in the coming weeks. Due to my amateur web development knowledge, some stuff will probably be broken too. But hey, move fast and break things, right?


  1. There's some nifty bookmarklet action in the video — I haven't gotten around to messing with these yet. However, there's instructions for doing so on Marco's github page. 

ℐℋ

Loomi: My Favorite Christmas Gift



I love the Loomi light that my sister got me for christmas. It looks so cool, and the DIY aspect of putting it together just adds to the fun. Here's how the creators describe the Loomi on their Kickstarter page:

Loomi is a modular, makeable, paintable, recyclable light that looks beautiful just about anywhere. Loomi is made up of interlinking quadrilaterals that easily connect to create wonderful lights of all shapes and sizes.

I was gifted the kickstarter single light kit with electrical fixture. You get a pack of thirty three quadrilateral cutouts that you can use to build whatever you want. They feel identical to high quality posterboard. I wouldn't be surprised if that what it's cut from.

Loomi Kit

I chose to make the Loomi Globe, mainly because that was the only creation they had posted instructions for. I used all but three of the cutouts to build the globe. It was a little challenging to make, and at times I was afraid I might rip an edge. Regardless, I had a great time hooking the pieces together.

My Loomi

The Loomi looks fantastic once you slip the light fixture in and turn on the light. I thought the photos on Loomi's website looked too good to be true, but it really looks that awesome.

Everyone in my family wants one now. I've never seen a light that gave off such a unique, relaxing hue. I can't wait to hang the Loomi up when I get back to school.

ℐℋ