But I need some help to improve the script because I was trying to:
- Show a thumbnail instead of a title (actualy, the title is the URL, but it does not help me to choose the right image)
- Show messages each time I find one image, but not asking for "OK" as the ShowMessage does.
Any ideas?
Code: Select all
(***************************************************
Ant Movie Catalog importation script
www.antp.be/software/moviecatalog/
[Infos]
Authors=Tadeu Cunha
Title=Bing Picture Import
Description=Get picture from bing.com images
Site=http://www.bing.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|1|0=Show available pictures|1=Get Only the first picture found
IncludeTheWordCover=1|1|0=Normal search|1=Include COVER as text string
[Parameters]
***************************************************)
program BingPics;
// no mail; comments only in web
uses
  StringUtils1;
var
  MovieName: string;
const
  BaseURL  = 'http://www.bing.com/images/search?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
  ImgCnt: Integer;
  PageText,strRef,strImg,strTest: string;
begin
  // Read the whole page
  PageText := GetPage(Address);
 
  // Looks for the first images
  ImgCnt:= MaxItems;
  PickTreeClear;
  PickTreeTitle(MovieName);
  while ImgCnt > 0 do
  begin
    // Looks for all tags href
    strTest := TextBetween(PageText,'<a href','</a>');
    if (strTest = '') then
      break;
    PageText := RemainingText;
    // Check if there is a class with the url inside this href
    if pos('class="thumb"', strTest) > 0 then
    begin
      // Get the img reference
      strImg := TextBetween(strTest,'="','" class="thumb"');
      if (GetOption('GetOnlyFirstPicture') = 1) then
      begin
        // That's it, we have the picture!
        GetPicture (strImg);
        exit;
      end;
      // Add this option to the tree
      // ShowMessage(strImg);
      PickTreeAdd(strImg, strImg);
      ImgCnt := ImgCnt - 1;
    end;
  end
  
  if (ImgCnt = MaxItems) then
  begin
    ShowError('Error: Images not found! Probably something changed at Bing :(');
  end else
  begin
    // User choose the image, get it!
    if PickTreeExec(strImg) then
      GetPicture (strImg);
  end;
end;
//------------------------------------------------------------------------------
begin
  //SetField(fieldChecked, '');
  MovieName := GetField(fieldOriginalTitle);
  if MovieName = '' then MovieName := GetField(fieldTranslatedTitle);
  
  if (GetOption('AutoSelect') = 0) or (MovieName = '') then
   if Input('Bing Picture Search', 'Input title:', MovieName) = False then exit;
  
  MovieName := RemovePar(RemoveBrackets(MovieName));
  if(GetOption('IncludeTheWordCover') = 1) then
    MovieName := 'COVER ' + MovieName;
  AnalyzePage(BaseURL + UrlEncode(MovieName));
end. 

