...

View Full Version : Collecting YouTube links..



Adee
09-28-2010, 10:52 PM
So, I use Last.fm. I have like 260 loved tracks that I want to download. I'm obviously not going to download them, but they're on YouTube, so it's fair game.

Anyway, I was wondering if it's possible to make a script in php that would basically search youtube and return the first result. i know it wouldn't be right 100% of the time. then, after I have a list of all the songs I want, I can put them in a youtube->mp3 converter, and i have my songs. i'm capable of doing the coding myself, i just need to know if this is at all possible, and how i might go about doing it.

DJCMBear
09-29-2010, 01:24 AM
This code works if you can use full http urls on your server I built it to run on the current YouTube search results.



<?php # YouTube Video Collector - YouTube.class.php

#####################################################
# #
# YouTube Video Collector 2010 #
# #
# Author: CMBSystems (Chris Bearcroft) #
# Copyright: CMBSystems 2010, All Rights Reserved #
# Last Modded: 29-09-2010, 12:24 AM (GMT) #
# #
#####################################################

class YouTube {
# @Function: getURL :: Public
# Calling the URL of the first video in the search results
public function getURL($Search){
if(!empty($Search)){
$Search=urlencode($Search);
$Search="http://www.youtube.com/results?search_category=10&search_type=videos&search_query={$Search}";
preg_match('/\/watch\?v=(.*?)\"/',file_get_contents($Search),$URL);
if(strpos($URL[1],'&')){$URL=split('&',$URL[1]);$URL=$URL[0];}else{$URL=$URL[1];}
return "http://www.youtube.com/watch?v={$URL}";
}
}

# @Function: printForm :: Public
# Creating the form for searching YouTube
public function printForm($Search){
$Search=htmlspecialchars(stripslashes($Search));
return "<form method=\"get\" action=\"\">\n <label for=\"search_query\">Search:</label> \n".
" <input type=\"text\" id=\"search_query\" name=\"search_query\" value=\"{$Search}\" />\n".
" <input type=\"submit\" value=\"Search YouTube\" />\n".
"</form>\n";
}
}
?>


USAGE:


// To Print the search form write this
YouTube::printForm($_GET['search_query']);

// To obtain the URL of the first video result write this
YouTube::getURL($_GET['search_query']);


So something like this would work.


<?php # YouTube Video Collector - YouTube.php

#####################################################
# #
# YouTube Video Collector 2010 #
# #
# Author: CMBSystems (Chris Bearcroft) #
# Copyright: CMBSystems 2010, All Rights Reserved #
# Last Modded: 29-09-2010, 12:24 AM (GMT) #
# #
#####################################################

# Require the YouTube class
require 'YouTube.class.php';

# Set the PHP vars
$Form = YouTube::printForm($_GET['search_query']);
$Result = YouTube::getURL($_GET['search_query']);

print <<<YTVSC
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>YouTube Collector</title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
{$Form}
HTTP URL: {$Result}
</body>
</html>
YTVSC;
?>

Adee
09-29-2010, 01:48 AM
This code works if you can use full http urls on your server I built it to run on the current YouTube search results.



Woww awesome, thanks a lot! Is there any way I could get this to work with multiple URLs?

DJCMBear
09-29-2010, 02:01 AM
As in how ???

Like you write a list and it searches through them collecting each item in the list and calling each url?

Adee
09-29-2010, 02:05 AM
As in how ???

Like you write a list and it searches through them collecting each item in the list and calling each url?

Yep that's exactly it. My friend is a web developer and he's trying to guide me through it, but I'm not getting it.

I mean if the input box was a textarea, then by using the explode function to make the inputted list into an array.. then returning the url for each item.

DJCMBear
09-29-2010, 02:26 AM
If you look at my profile your see I know well over 30 programming languages http://www.codingforums.com/member.php?u=93400 and I to am a web developer and from what you said that is a good solution but if we do it as an array to start with.

In YouTube.php the $Result var is now an array of all lines in the text area, each line is a new YouTube search.

YouTube.class.php


<?php # YouTube Video Collector - YouTube.class.php

#####################################################
# #
# YouTube Video Collector 2010 #
# #
# Author: CMBSystems (Chris Bearcroft) #
# Copyright: CMBSystems 2010, All Rights Reserved #
# Last Modded: 29-09-2010, 1:26 AM (GMT) #
# #
#####################################################

class YouTube {
public function getURL($Search){
if(!empty($Search)){
$Search=split("\n",$Search);
for($i=0;$i<count($Search);$i++){
$SearchURL=urlencode($Search[$i]);
$SearchURL="http://www.youtube.com/results?search_category=10&search_type=videos&search_query={$SearchURL}";
preg_match('/\/watch\?v=(.*?)\"/',file_get_contents($SearchURL),$URL);
if(strpos($URL[1],'&')){$URL=split('&',$URL[1]);$URL=$URL[0];}else{$URL=$URL[1];}
$array[]="http://www.youtube.com/watch?v={$URL}";
}
return $array;
}
}
public function printForm($Search){
$Search=htmlspecialchars(stripslashes($Search));
return "<form method=\"get\" action=\"\">\n <table>\n <tr>\n".
" <td><textarea cols=\"60\" rows=\"15\" name=\"search_query\">{$Search}</textarea></td>\n".
" </tr>\n <tr>\n <td align=\"right\"><input type=\"submit\" value=\"Search YouTube\" /></td>\n".
" </tr>\n </table>\n</form>\n";
}
}
?>


YouTube.php


<?php # YouTube Video Collector - YouTube.php

#####################################################
# #
# YouTube Video Collector 2010 #
# #
# Author: CMBSystems (Chris Bearcroft) #
# Copyright: CMBSystems 2010, All Rights Reserved #
# Last Modded: 29-09-2010, 1:26 AM (GMT) #
# #
#####################################################

# Require the YouTube class
require 'YouTube.class.php';

# Set the PHP vars
$Form = YouTube::printForm($_GET['search_query']);
$Result = YouTube::getURL($_GET['search_query']);

print <<<YTVSC
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>YouTube Collector</title>
</head>
<body>
{$Form}
YTVSC;
print_r($Result);
print <<<YTVSC
</body>
</html>
YTVSC;
?>

Adee
09-29-2010, 02:39 AM
intense, thanks a lot, yet again. could you explain to me what the print <<<YTVSC is and the YTVSC;?

DJCMBear
09-29-2010, 02:48 AM
basicly having <<<[Any word you like] and then ending it with the same word but with the semi-colon at the end allow html to be written within php without the need to use backslashes on quotes it also allows PHP vars to be used by writing {$var} into the html codes.

Adee
09-29-2010, 02:55 AM
basicly having <<<[Any word you like] and then ending it with the same word but with the semi-colon at the end allow html to be written within php without the need to use backslashes on quotes it also allows PHP vars to be used by writing {$var} into the html codes.


One last question..for real :P It's working perfectly, but is there a way to make it so it doesn't print all the information like Array ( [0] => blah blah ) etc?

DJCMBear
09-29-2010, 03:02 AM
just remove the code print_r($Result); from the code where the html is in YouTube.php and then use $Result as your array for what ever you need it for.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum