Page 1 of 1

Script to get Pictures from Google

Posted: 2013-01-06 22:41:48
by Tadeu64
Hi. I was looking for a script to get only the cover for my movies and other files (no more information needed) but could not find one that was suitable. Remarkably, I found that just copying the title from AMC and searching in Google pictures worked like a charm!

So, I decided to create a script just for that: get the file name and look for images in google.

This script shows various options that can be viewed by the "View" buttom in the PickList

I hope it is usefull to other people.

Here it goes:

GooglePics.ifs

Code: Select all

(***************************************************

Ant Movie Catalog importation script
www.antp.be/software/moviecatalog/

[Infos]
Authors=Tadeu Cunha
Title=Google Picture Import
Description=Get picture from google.com images
Site=http://www.google.com
Language=EN
Version=1.00 - 01.06.2013
Requires=3.5.0
Comments=See code comments. Thanks to the authors of IMDB.ifs and IAFD.ifs
License=GPL
GetInfo=1
RequiresMovies=1

[Options]
AutoSelect=0|1|0=Always show movie selection dialog|1=Auto-select movie if there is only one
GetOnlyFirstPicture=0|0|0=Show available pictures|1=Get Only the first picture found

[Parameters]

***************************************************)

program GooglePics;

// no mail; comments only in web

uses
  StringUtils1;
var
  MovieName: string;
const
  BaseURL  = 'http://www.google.com/search?safe=off&tbo=d&source=lnms&tbm=isch&sa=X&q=';
  MaxItems = 50;

// ---
function RemoveBrackets(wholetext: string) : string;
var
  str1: String;
  i: Integer;
begin
  str1 := Trim(TextBefore(wholetext, '[', ''));
  if str1 <> '' then
  begin
    if Pos(']', RemainingText) > 0 then
      wholetext := str1+' '+Trim(TextAfter(RemainingText, ']'));  // + end of text or ''
  end;
  result := Trim(wholetext);
end;
// ---
function RemovePar(wholetext: string) : string;
var
  str1: String;
  i: Integer;
begin
  str1 := Trim(TextBefore(wholetext, '(', ''));
  if str1 <> '' then
  begin
    if Pos(')', RemainingText) > 0 then
      wholetext := str1+' '+Trim(TextAfter(RemainingText, ')'));  // + end of text or ''
  end;
  result := Trim(wholetext);
end;

// ---
procedure AnalyzePage(Address: string);
var
  LineCnt: Integer;
  PageText,strRef, strImg: string;
begin
  // Read the whole page
	PageText := GetPage(Address);
  
  // Looks for the first images
  LineCnt:= MaxItems;
  PickTreeClear;

  if CheckVersion(4,1,2) = True then 
    PickTreeTitle(MovieName);

  while LineCnt > 0 do
  begin
    // Looks for tag imgrefurl
    if pos('imgrefurl=', PageText) = 0 then
    begin
      if LineCnt = MaxItems then
      begin
        ShowError('Error: imgrefurl tag not found! Probably something changed at Google :(');
        exit;
      end;
      break;
    end;
    
    // Get the img reference
    strRef := TextBetween(PageText,'imgrefurl=', '&');
    PageText := RemainingText;
    
    // Get the img url
    if pos('imgurl=', PageText) = 0 then
    begin
      if LineCnt = MaxItems then
      begin
        ShowError('Error: imgurl tag not found! Probably something changed at Google :(');
        exit;
      end;
      break;
    end;  
    strImg := TextBetween(PageText,'imgurl=', '&');
    PageText := RemainingText;
    
    if (GetOption('GetOnlyFirstPicture') = 1) then
    begin
      // That's it, we have the picture!
      GetPicture (strImg);
      exit;
    end
    
    // Add this option to the tree
    PickTreeAdd(strRef, strImg);
    LineCnt := LineCnt - 1;
  end
  
  // User choose the image, get it!
  if PickTreeExec(strImg) then
    GetPicture (strImg);
end;
//------------------------------------------------------------------------------

begin
  //SetField(fieldChecked, '');

  MovieName := GetField(fieldOriginalTitle);
  if MovieName = '' then MovieName := GetField(fieldTranslatedTitle);
  if (GetOption('AutoSelect') = 0) or (MovieName = '') then
	if Input('Google Picture Import', 'Input title:', MovieName) = False then exit;
  MovieName := RemovePar(RemoveBrackets(MovieName));
  AnalyzePage(BaseURL + UrlEncode(MovieName));
end.
FIXED: Checking version before using PicTreeTitle, that only works since version 4.1.2 (Thks, SoulSnake !)

Posted: 2013-01-07 11:23:19
by antp
Thanks ;)

Script error in "GOOGLEPICS"

Posted: 2013-01-10 20:34:13
by Tetsujin
Script error in "GOOGLEPICS":unknown identifier:PICKTREETITLE at line 54.
[url]http://dl.dropbox.com/u/6574173/ScriptError.jpg[/url]

Posted: 2013-01-10 21:44:32
by soulsnake
The function PickTreeTitle only exists since version 4.1.2.

You can replace line

Code: Select all

  PickTreeTitle(MovieName);
by

Code: Select all

  if CheckVersion(4,1,2) = True then
    PickTreeTitle(MovieName);
or upgrade AMC to version 4.1.2.

Soulsnake.

Posted: 2013-01-12 08:49:56
by Tetsujin
Upgrade to latest version worked fine..
Thanks

Posted: 2013-01-28 09:01:07
by fulvio53s03
Very useful & nice script! :grinking:
Should it be possible to limitate searched image dimension (or to show dimensions in picklist) and/or using only one language page's origin?
(i.e.: only medium images coming from Italian language Sites)

Thanks in advance
:)

Google Picture Import does not work ---Please help me

Posted: 2014-03-06 19:45:26
by fogrob
Unfortunately, the script does not work, every time I do a search on an image I get an error message "Error: imgrefurl tag not found! Probably something changed at Google :(".
Please help me because I should. Thank you in advance for your help.
:??: :??: :cry:

Re: Script to get Pictures from Google

Posted: 2019-11-17 21:54:53
by otreux
is possible update the script? thanks :)