MySpace,MyVideo,Youtube auf der Dbox2

Über weitere Skripte ist es möglich auf der D-Box2 auch bekannte Videoseiten einzubinden. Youtube bietet für solche Aktionen eine sehr gute API. Hiermit können sowohl statische Seiten (RSS-Feeds) angezeigt werden als auch *Suchbegriffe*. eingetragen werden, die gewünschten Begriffe müssen in der CSV-Datei angelegt werden. Im Moviebrowser kann für jeden Begriff die gewünschte Suchoptionen *(Relevanz,Datum,..)* und die Seitenanzahl ausgewählt werden.
Die Längen der Videos werden in Sekunden neben den Dateinamen angezeigt. Da dieses Feld allerdings im Moviebrowser eigentlich ein Bytefeld ist wird ab 1024 in k umgerechnet.

Beschreibung

Alle gewünschten Suchbegriffe können in die CSV-Datei eingetragen werden. Dazu muss in der Spalte url ein Suchbegriff oder eine Suchkombination eingetragen werden, der in nameshow eingetragen Wert repräsentiert dann die Suche im Moviebrowser. In der Spalte type muss der Wert search eingetragen werden, somit die Suchfunktion für diesen Begriff genutzt wird.

Ebenfalls sind Kombinationen mit AND,OR und (-) NOT im Suchbegriff möglich. Es ist aber hilfreich diese Einträge durch Klammer zu Begrenzen und vorher auch direkt auf der Youtubeseite zu testen. Der "Suchbegriff" wird 1zu1 an Youtube gereicht. Die Suchkombination ist übrigens keine Besonderheiten des Scripts, sondern sind erweiterte Suchfunktionen von Youtube.

Beispiele

Zum Eintragen der gewünschten Suchbegriffe ist die CSV-Datei zu benutzen. Für die Beispiele oben wäre das:

"youtube","Testsuche","Suchbegriffe oder Funktion",,
"youtube","kombinierte Suche","(dortmund OR bvb) AND (bayern OR muenchen) -schalke","search",

RSS-Feeds

Ebenso können die RSS-Feeds von Youtube eingebunden werden. Anstatt des Suchbegriff wird dann einfach die URL zum Feed angeben. z.B. für die Top10:http://gdata.youtube.com/feeds/api/standardfeeds/top_rated. Die vorhanden Untermenü im Moviebrowser für die Suchbegriffe fallen hier logischerweise weg.

MySpace, MyVideo

Ebenso können *MySpace* (Video) und *MyVideo* Suchbegriffe genutzt werden. Hier ein paar Beispiele:

"myspace","suche nach test","test","search",
"myspace,youtube,myvideo","suche nach test ueberall","test","search",
"myvideo","suche nach test bei myspace","test","search",

SVN Files

Youtube

scripts/inc/youtube.php

<?php
if (!isset($_GET['v'])) return '';
 
$url=isset($_GET['fmt']) ?  YoutubeVideoUrl($_GET['v'],$_GET['fmt']) : YoutubeVideoUrl($_GET['v']);
 
header("Location: ".$url);
 
#works very good!
/**
 * Get download URL of Youtube video
 * http://stackoverflow.com/questions/3311795/youtube-video-download-url (since 2010/07)
 * http://www.longtailvideo.com/support/forum/General-Chat/18570/-Solution-Youtube-Get-Video (old)
 *
 * @param youtube video id $videoid
 * @param videotype $fmt
 * @return download url
 */
function YoutubeVideoUrl($videoid,$fmt=5) {
 
	$content = file_get_contents("http://www.youtube.com/get_video_info?video_id={$videoid}");
	preg_match('|&token\=(.*?)&|is', $content, $matches);
	$token = $matches[1];
	$url = "http://www.youtube.com/get_video?video_id={$videoid}&t={$token}&fmt={$fmt}&asv=2";
	$headers = get_headers($url,1);
	if (is_array($headers['Location'])) {
		return $headers['Location'][0];
	} else {
		return $headers['Location'];
	}
 
 
	parse_str(file_get_contents("http://youtube.com/get_video_info?video_id={$videoid}"),$i);
	if($i['status'] == 'fail' && $i['errorcode'] == '150') {
		$content = file_get_contents("http://www.youtube.com/watch?v={$videoid}");
 
		preg_match_all ("/(\\{.*?\\})/is", $content, $matches);
		$obj = json_decode($matches[0][1]);
 
		$token = $obj->{'t'};
		$fmt_url_map = $obj->{'fmt_url_map'};
	} elseif ($i['status'] == 'fail' && $i['errorcode'] != '150') {
		return '';
		#die("Fail, Errorcode: {$i['errorcode']} , Reason: {$i['reason']}");
	} else {
		$token = $i['token'];
		$fmt_url_map = $i['fmt_url_map'];
	}
	$url = "http://www.youtube.com/get_video.php?video_id={$videoid}&vq=2&fmt={$fmt}&t={$token}";
	$headers = get_headers($url,1);
	$video = $headers['Location'];
	if(!isset($video)) {
		preg_match ("/((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s\"]*))/is", $fmt_url_map, $matches);
		$video = explode(',', $matches[0]); $video = $video[0];
	}
	#some times array?
	if (is_array($video)) return $video[0];
	return $video;
}
 
#----------------------------------------------------------
#----------------------------------------------------------
#----------------------------------------------------------

#functions that we dont need anymore; here for info
	#old function
	function dlflv_old($url) {
		$page = cacheurl($url,false);
		preg_match('|video_id": "(.*?)"(?:.*?)"t": "(.*?)"|i', $page, $matches);
		$video ="http://www.youtube.com/get_video?video_id=".$matches[1]."&t=".$matches[2];
		return  $video;
	}
 
	/**
	 *  YOUTUBE GRABBER UTIL by Centreonet Solutions
	 *  Support :: greenscripts@gmail.com
	 *  Donate from paypal to same address.
	 * 
	 *  http://www.longtailvideo.com/support/forum/General-Chat/16851/Youtube-blocked-http-youtube-com-get-video
	 */
	function googleCache($key,$token,$fmt){
		$ytURL = "http://www.youtube.com/get_video.php?video_id=" . $key . "&t=" . $token. "&fmt=" . $fmt;
		$headers = get_headers($ytURL,1);
		$status = explode(" ",$headers['1']);
		if($status[1] == "200"){
		if(!is_array($headers['Location'])) {
		$videoURL = $headers['Location'];
		}else{
		foreach($headers['Location'] as $header){
		if(strpos($header,"googlevideo.com") != false){
		$videoURL = $header;
		break;
		}
		}
		}
		return $videoURL;
		}else{
		return "";
		}
	}
 
	function youtubeData($url){
		$key = explode("v=",$url);
		$key = $key[1];
		$content = file_get_contents("http://youtube.com/get_video_info?video_id=".$key);
		parse_str($content);
		if($token != ""){
		$videoHD = googleCache($key,$token,22);
		if($videoHD != ""){
		$videoFile = $videoHD;
		}else{
		$videoFile = googleCache($key,$token,18);
		}
		}
		return $videoFile;
	}
 
#dont work on all links
	function YoutubeVideoUrl_notgood($url) {
		$page = @file_get_contents('http://www.youtube.com/get_video_info?&video_id='.$id);
		preg_match('/token=(.*?)&thumbnail_url=/', $page, $token);
		$token = urldecode($token[1]);
 
		$get = $title->video_details;
		$url_array = array (
			"http://youtube.com/get_video?video_id=".$id."&t=".$token."&fmt=5",
			"http://youtube.com/get_video?video_id=".$id."&t=".$token,
			"http://youtube.com/get_video?video_id=".$id."&t=".$token."&fmt=18",
			"http://youtube.com/get_video?video_id=".$id."&t=".$token."&fmt=34",
 
			);
 
		foreach($url_array as $flv_url) {
			if(url_exists($flv_url) === true) { return $flv_url; }
		}
		return '';
	}
 
	/**
	 * checks if an url exits
	 *
	 * @param string $url
	 * @return bool
	 */
	function url_exists($url) {
	 if(@file_get_contents($url, FALSE, NULL, 0, 0) === false) return false;
	 return true;
	}
 
?>

MyVideo

scripts/MyVideo.php

<?php
/*
<changelog>fixed: searching dec 09</changelog>
<info>use csv to add searchterms</info>
*/
 
$search='http://www.myvideo.de/Videos_A-Z?lpage=%page%&searchWord=%string%&searchOrder=%orderby%';
 
for ($i=0;$i<13;$i++) $seite['Seite '.($i + 1)]['nr']=$i+1;
 
$orderby['Relevanz']['api']="0";
$orderby['Datum']['api']="1";
$orderby['Zugriffe']['api']="5";
$orderby['Bewertung']['api']="2";
 
 
function getdir() {
	global $links,$orderby,$seite;
	$r=explode("/",trim($_GET['dir'],"/"));
 
	if (count($r)==2) {
		return gennavi($links);
	}
 
	if (count($r)==3) {
		$ar=$links[$r[2]];
		if (isset($ar['suche'])) {
			return gennavi($orderby);
		} else {
			return gennavi(myvideo_rss_feed_input($ar['url']));
		}
	}
 
	if (count($r)==4) {
		return gennavi($seite);
	}
 
	if (count($r)==5) {
		$page=$seite[$r[4]]['nr'];
		$ar=$links[$r[2]];
		$ar=myvideo_search_input(urlencode($ar['suche']),$page,$orderby[$r[3]]['api']);
		return gennavi($ar);
	}
}
 
function geturl($pfad) {
	global $links,$orderby;
	$r=explode("/",trim($pfad,"/"));
 
	#check if we are searching
	if(count($r)==6) {
		$page=$seite[$r[4]]['nr'];
		$ar=myvideo_search_input(urlencode($links[$r[2]]['suche']),$page,$orderby[$r[3]]['api']);
		$key=$r[5]; 
		return myvideo_flv_download($ar[$key]['url']);
	}
 
 
	#no searchobject, use rss
	$in=myvideo_rss_feed_input($links[$r[2]]['url']);
	$t=stripslashes($r[3]);
	return myvideo_flv_download($in[$t]['url']);
}
 
/**
 * Parse the video url of a given myvideo video link site
 * and returns the http url of the video
 * * 
 * @param string $url
 *   url of video must contain watch/*
 *   http://www.myvideo.de/watch/7125794
 * @return string
 *   the url of the video as http url; should the flv file
 */
function myvideo_flv_download($url) {
	preg_match('|watch/(\d+)/|i', $url, $match);
	$objekt=new Browser($browser);
	if ($cookies!="") $objekt->cookies_set($cookies);
	$objekt->url="http://www.myvideo.de/movie/".$match[1];
	$objekt->read();
	$head=$objekt->returnHeader();
	preg_match('/V\=(.*?).flv/', $head['Location'], $matches);
	return urldecode($matches[1]).".flv";
}
 
 
/**
 * parse the myvideo feeds
 *
 * @param string $url
 *   feed url
 * @return array
 *   videos as array
 */
function myvideo_rss_feed_input($url) {
	$t_html = cacheurl($url);
	preg_match_all('/<item>(.*?)<\/item>/si',$t_html,$row);
 
	foreach ($row[1] as $mov) {
	         preg_match("/<title>(.*?)CDATA\[(.*?)\](.*?)<\/title>/is", $mov, $matches); $tmp_array['title'] = reducehtml($matches[2]);
	         preg_match("/<link>(.*?)<\/link>/is", $mov, $matches); $tmp_array['url'] = $matches[1];
 
	         $tmp_array['type']="file";
	         $out[$tmp_array['title']]=$tmp_array;
	}
	return $out;
}
 
/**
 * peforms a search on myvideo and returns the videos as array
 *
 * @param string $string
 *   the searchstring
 * @param string $page
 *   the page of the results to display
 * @param string $orderby
 *   order the results: relevance=0,date=1,views=5
 * @return array
 *   returns videopages as array
 */
function myvideo_search_input($string,$page=0,$orderby=0) {
	global $search;	
	$url= str_replace("%string%",urlencode($string),$search);
	$url= str_replace("%orderby%",$orderby,$url);
	$url= str_replace("%page%",$page,$url);
	$t_html = cacheurl($url);
	$out=array();
	preg_match_all("|<span class='title'>(.*?)</span>|si",$t_html,$row);
	foreach ($row[0] as $mov) {
	         preg_match("/title='(.*?)'/is", $mov, $matches); $tmp_array['title'] = reducehtml($matches[1]);
	         preg_match("/href='(.*?)'/is", $mov, $matches); $tmp_array['url'] = "http://www.myvideo.de/".$matches[1];
	         $tmp_array['type']="file";
	         $out[$tmp_array['title']]=$tmp_array;
	}
	return $out;
}
 
?>