My video blog post generator (http://v.leau.co/) does this but not in wp context.
You supply a query e.g. "superman" (then wait (without notice that it is doing something) (since im the only user)), then click the vids you like to post, click generate code and you have the code where the thumbs are hosted on my site because it downloaded these in the meanwhile. This code can be copy and pasted in a post.
In other words if you would put the code in a function call it would return the piece of code e.g. a href with a link to the video that gets added to the content or e.g. the link to the featured image that is downloaded locally.
Is that the piece of code you are looking for ? I think the core is:
A function to retrieve more results (ergo if you just want to display more than 1 video in the resulting code instead of 1 specific) :
function retrieveMoreResults($key, $q, $start, $cache) {
$url = "http://ajax.googleapis.com/ajax/services/search/video?v=1.0&q=" . $q . "&rsz=large&start=" . $start. "&key=" . $key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$body = curl_exec($ch);
curl_close($ch);
$responseData = json_decode($body, true);
$tempOutputString .= display($responseData, $cache, $q);
return $tempOutputString;
}
A function to get the initial results page:
function retrieveResults($key, $q, $cache) {
# first call
$url = "http://ajax.googleapis.com/ajax/services/search/video?v=1.0&q=" . $q . "&rsz=large&key=" . $key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
$body = curl_exec($ch);
curl_close($ch);
$responseData = json_decode($body, true);
$tempOutputString = "";
$tempOutputString .= display($responseData, $cache, $q);
$boolFirstRequest = true;
foreach ($responseData["responseData"]["cursor"]["pages"] as $GsearchResultClass) {
$start = $GsearchResultClass["start"];
if ($boolFirstRequest) {
$boolFirstRequest = false;
} else {
$tempOutputString .= retrieveMoreResults($key, $q, $start, $cache);
}
}
return $tempOutputString;
}
A function to display download the thumbs in a certain directory (variable) and to return a piece of code to put in the post:
function display($responseData, $cache, $tag) {
$strBuffer="";
foreach ($responseData["responseData"]["results"] as $GsearchResultClass) {
#
# there are YouTube urls and also Google Video urls they are both different
# the one from Google video has the word "ThumbnailServer" in it
# example:
# youtube: http://1.gvt0.com/vi/6jKzr143K8U/default.jpg
# video.google: http://3.gvt0.com/ThumbnailServer2?app=vss&contentid=7efbd69963e4cc67&offsetms=30000&itag=w160&hl=en&sigh=J6N1fv_It6H5jJWX51fKt-eYqNk
#
$thumbId="";
$imageThumb=$GsearchResultClass["tbUrl"];
if (strstr($imageThumb, 'ThumbnailServer')) {
$imgStringBits = explode('&',$imageThumb);
$parsedImgStr=strstr($imgStringBits[1],'=');
$parsedImgStr = substr($parsedImgStr,1);
$thumbId = $parsedImgStr;
} else {
$imgStringBits = explode('/',$imageThumb);
$thumbId = $imgStringBits[4];
}
$imgFile=$cache . "/" . $thumbId . ".jpg";
#
# Now that we have the imageFile Name check if we already have it in the cache:
# - if we have it NEVER delete it (why should we?)
# - if we dont well... get it via curl
#
if (!file_exists($imgFile)) {
$ch = curl_init ();
$timeout = 5;
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0');
curl_setopt ($ch, CURLOPT_AUTOREFERER, true);
curl_setopt ($ch, CURLOPT_URL, $imageThumb);
curl_setopt ($ch, CURLOPT_HEADER, false);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt ($ch, CURLOPT_VERBOSE, 1);
$rawdata = curl_exec ($ch);
curl_close ($ch);
if($rawdata) {
$fp = fopen($imgFile, 'w');
fwrite($fp, $rawdata);
fclose($fp);
} else {
#
# If our Curl action retrieves nothing then use the default image
#
$imgfile="images/default.jpg";
}
}
#
# Now that we have the image url create a div (hmm.. might drop that
# styling later on in a seperate csss) containg the video thumb, link
# and the description. When you like it you can add all the other
# parameters that Google returns like length etc...
#
$strBuffer .= "<div style=\"float:left;width:125px;height:130px;font-size:8px;font-family:arial;\" class=\"thumb\"><div>";
$strBuffer .= "<a href=\"#\" class=\"del\" id=\"$thumbId\"> ADD</a><br />";
$strBuffer .= "<a href=\"" . $GsearchResultClass["playUrl"] . "\" target=\"_blank\">";
$strBuffer .= "<img src=\"" . $imgFile . "\" alt=\"" . $GsearchResultClass["title"] . "\" border=\"0\" width=\"120\">";
$strBuffer .= "</a><br />\n";
#
# Note that we also add a delete option, for now that only removes it from the page
# but in the next version it should do an AJAX call to write the id somewhere so
# that we never see it again.
#
$strBuffer .= $GsearchResultClass["titleNoFormatting"] . "<br />";
$strBuffer .= "</div></div>\n";
}
return $strBuffer;
}
A function to call the above:
function moviePage($tag, $cacheName, $cacheTime, $key) {
$cache = $cacheName . "/" . $tag;
checkCache($cache);
cleanCacheHTML($cache);
$filename = $tag . ".html";
$spFile=$cache . "/" . $filename;
if (file_exists($spFile) && filemtime($spFile) > $cacheTime ) {
$strBuffer = file_get_contents($spFile) . "<!-- " . $spFile . " from cache -->";
} else {
$strBuffer = retrieveResults($key, $tag, $cache);
}
$strBuffer .= "<br clear=\"all\">";
$fp = fopen($spFile, 'w');
fwrite($fp, $strBuffer);
fclose($fp);
return $strBuffer;
}
(the $key is you Google API key) (http://code.google.com/intl/nl-NL/more/)
IMHO I think the rest is more just "getting the returned stuff and adding it to the content of a post + setting the downloaded cached thumb as featured?
P.S. IMHO it is always better to post a thumbnail in a video when referring to YouTube videos since often older videos are removed by YouTube and they leave ugly postings as a result. With your own thumbs at least the image stays there forever so that afterwards you do have a clue what you posted there originally.