Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts

    Collecting YouTube links..

    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.

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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 Code:
    <?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:
    PHP Code:
    // 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 Code:
    <?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;
    ?>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • Users who have thanked DJCMBear for this post:

    Adee (09-29-2010)

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    Quote Originally Posted by DJCMBear View Post
    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?

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    As in how ???

    Like you write a list and it searches through them collecting each item in the list and calling each url?
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #5
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    Quote Originally Posted by DJCMBear View Post
    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.

  • #6
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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 Code:
    <?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 Code:
    <?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;
    ?>
    Last edited by DJCMBear; 09-29-2010 at 01:50 AM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • Users who have thanked DJCMBear for this post:

    Adee (09-29-2010)

  • #7
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    intense, thanks a lot, yet again. could you explain to me what the print <<<YTVSC is and the YTVSC;?

  • #8
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #9
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    Quote Originally Posted by DJCMBear View Post
    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?

  • #10
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    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.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •