Page 1 of 1

[REL/WIP] Youtube search (movie trailers or any)

Posted: 2010-05-07 09:57:44
by ElMauro
_____________________YouTube
_______________________search script

This is a REALLY SIMPLE and fast (one of the fastest you will ever see) scripts that I simply did to add trailers/promos/preview youtube video links to my movie catalog, wich is a custom modified version of ant's original one that have a custom field and functions (and other custom things) to manage movie trailers.
Took just a few minutes of batchmode search+add trailer links to more than 700 movies in my catalog, and you can see the result
(and why I designed a field for that) here: http://ronin.netii.net/ (ITS IN SPANISH !! sorry) **

THE SITE TEMPLATE IS FROM HERE

Also there you can download the "offline" (with custom antviewer) version to check out how embed trailers looks in there.

** That site will be less than a month online. The hosting site is known, totally free and with full features if you wanna know to host your online catalog: www.000webhost.com.

This script uses the youtube search feed intestead of the main site search and thats why is that fast. I can add another one using the normal site search if someone need it, but is no sense.

Sorry for the spanish (images and site) and thats why I cant share everything I did, its a mess. Unless nobody is interested, later I will share the diff patch with the changes made to ant catalog and antviewer. Need to look back and order all the mess first :D

______________***** WARNING *****

This script is using the Video Format field to store the youtube link
and the Video Bitrate field to mark the movie as With Trailer or Without (1 or 0, just for later manual searchs)
Im using a custom Movie catalog with fields designed for this, but as sharing
here I just used a random string and integer fields wich you should change to
whatever field you want to use or wait for an antmovie version with extra links fields.

Look for the lines with the "PLEASE CORRECT WICH FIELD YOU WANT TO USE!" comments and change them to your need.

You can totally remove the "With Trailer or Without" marking (SetField(fieldVideoBitrate, ..)
as you can always order by the field used to store the link.
I do it this way because extra purposes and easier ordering.



----------------------------------------
Finally, the code:
----------------------------------------

Code: Select all

program youtubefeed;

uses
  StringUtils1;

	// ***** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING *****
	
	// This script is using the Video Format field to store the youtube link
	// and the Video Bitrate field to mark the movie as With Trailer or Without (1 or 0, just for later manual searchs)
	// Im using a custom Movie catalog with fields designed for this, but as sharing
	// here I just used a random string and integer fields wich you should change to
	// whatever field you want to use or wait for an antmovie version with extra links fields.
	
	// Look for the lines with the "PLEASE CORRECT WICH FIELD YOU WANT TO USE!" comments and change them to your need.
	
	// You can totally remove the "With Trailer or Without" marking (SetField(fieldVideoBitrate, ..)
	// as you can always order by the field used to store the link.
	// I do it this way because extra purposes and easier ordering.
	
	// ***** WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING *****
  
  
const
  BaseURL_YT = 'http://gdata.youtube.com/feeds/videos?vq=';
  Base_YT_V = 'http://www.youtube.com/v/';
  NumResults = IntToStr(GetOption('MaxResults'));
  
var
  MovieName, MovieTitle, MovieYear, SetLink: string;

// the search
procedure SearchYT(Address: string);
var
   Page: TStringList;
   Count: Integer;
   ResultsData, CurrentData, MovieAddress, VideoTitle: string;

begin
   Count := 0;
   Page  := TStringList.Create;
   MovieAddress := '';


   Page.Text := GetPage(Address);
   PickTreeClear;
   PickTreeAdd('None', 'SINENCONTRAR');

   ResultsData := TextAfter(Page.Text, 'title":{"$t":"YouTube Videos matching query');
   
   // add the results to the picktree if batchmode is off
   if GetOption('BatchMode') = 0 then
   begin
     repeat
      VideoTitle := TextBetween(ResultsData, '"title":{"$t":"', '",');
  		MovieAddress := TextBetween(ResultsData, 'media$content":[{"url":"', '?f');
   		ResultsData := RemainingText; //keep the rest..
      if (MovieAddress <> '') AND (VideoTitle <> '') then
          PickTreeAdd(VideoTitle , MovieAddress);

      Count := ++Count;
      
     until (VideoTitle = '') OR (Count = GetOption('MaxResults')); // this count is just in case but not needed
     
  if PickTreeExec(MovieAddress) then
     if (MovieAddress <> 'SINENCONTRAR') then // user selected a valid result. Store
        begin
          SetField(fieldVideoFormat, MovieAddress); // PLEASE CORRECT WICH FIELD YOU WANT TO USE!
          SetField(fieldVideoBitrate, '1'); // PLEASE CORRECT WICH FIELD YOU WANT TO USE!
        end;
   end
   else //batchmode. using only 1 result based on relevance
   begin
     MovieAddress := TextBetween(ResultsData, 'media$content":[{"url":"', '?f');
     if (MovieAddress <> '') then
     begin
      SetField(fieldVideoFormat, MovieAddress);  // PLEASE CORRECT WICH FIELD YOU WANT TO USE!
      SetField(fieldVideoBitrate, '1'); // PLEASE CORRECT WICH FIELD YOU WANT TO USE!
     end
     else //no results? no way ! but if... mark the movie as needing a trailer.
     begin
      SetField(fieldVideoBitrate, '0'); // PLEASE CORRECT WICH FIELD YOU WANT TO USE!
     end;
   end;

   Page.Free
   Exit;
end;
// end

// ***** beginning of the program *****

begin
  // Check for current AMC version
  if CheckVersion(3,5,0) then
  begin
    MovieTitle := '';
    MovieTitle := GetField(fieldOriginalTitle);
    MovieYear := GetField(fieldYear);

    // titulo original para filmaffinity
    MovieTitle := Cp1252ToASCII(MovieTitle);  //clear non standar characters
    MovieTitle := StringReplace(MovieTitle, '"', ''); //clear quotation marks used by imdb series names
    MovieTitle := StringReplace(MovieTitle, '&','and'); // that ;)

    MovieName := MovieTitle + ' trailer';
    // before continue, let me explain what those extra paramns are, the not so obvious ones:
    //  &alt=json-in-script  << means the video must have the embed code (avoid embed-disabled videos)
    //  &format=5 << call it "double check for the &alt=json-in-script". The results CAN be embeded into your site.
    //  &fmt=18  << this is currently not working (neither for search or play parameter) since 1 week ago (5/2010)
    //              too much 320x240 1024HD thumbnails (damn people) but should be working again soon..
    //              With this we specify(filter) that results must be at least 480p quality videos.
    // an option can be added to the script to select the desired minimun quality of the results.
    // the default is fmt=5 (360p) and is what you should use on 320x240 embed videos -.-
    
    If GetOption('BatchMode') = 1 then
    begin
      SearchYT(BaseURL_YT + UrlEncode(MovieName) + '&max-results=1&alt=json-in-script&orderby=relevance&sortorder=descending&format=5&fmt=18');
    end
    else
    begin
      if Input(MovieTitle + ' (' + MovieYear + ')', 'Enter the search query or a youtube link here:', MovieName) then
      begin
        if (TextAfter(MovieName, 'http://www.youtube.com/watch?v=') <> '') then //user has used a link, so thats it..
        begin
          if (TextAfter(MovieName, '&') <> '') then //only save the clear link, without player parameters.
              MovieName := TextBefore(MovieName, '&', '');

          SetLink := Base_YT_V + TextAfter(MovieName, 'watch?v=');
          SetField(fieldVideoFormat, SetLink);  // PLEASE CORRECT WICH FIELD YOU WANT TO USE!
        end
        else  //search on youtube
        begin
           SearchYT(BaseURL_YT + UrlEncode(MovieName) + '&max-results=' + NumResults + '&alt=json-in-script&orderby=relevance&sortorder=descending&format=5&fmt=18');
        end;
      end;
    end;
  end
  else
    ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.5.0)');
end.

Posted: 2010-05-07 23:26:07
by darkemi20
Que buen trabajo man.... Me pasas tu template... Me re gusto eso de poner trailer y demas... Muchas gracias.-