[fix] imdb socket error #10020

If you made a script you can offer it to the others here, or ask help to improve it. You can also report here bugs & problems with existing scripts.
Post Reply
mrobama
Posts: 85
Joined: 2009-04-03 12:34:34

[fix] imdb socket error #10020

Post by mrobama »

there is a problem in imdb dns configuration as you can check using online dns tools.

I worked at a quick fix, using ip instead of domain. I added also a retry for getPage, so if you get a timeout script will retry up to 4 times ... (you can skip this setting "Count = 2" in the line where "Count = 5").

Code: Select all

program IMDB;

uses
//  Debug,
  StringUtils1, StringUtils7552;

const
  PopularTitleSearchURL =  'https://52.94.225.248/find?s=tt&q=';   // use https://www.imdb.com/ when imdb fixes its dns problems
  FullTitleSearchURL = 'https://52.94.225.248/find?s=tt&exact=true&q=';
  EpisodeTitleSearchURL = 'https://52.94.225.248/find?s=ep&q=';
  ImdbUrl = 'https://52.94.225.248';

var
  MovieName: string;
  MovieURL: string;
  MovieNumber: string;
  MovieYear: string;
  UpdateFile: TStringList;
  
function RetryGetPage(Url: string): string;
var
  Count: Integer;
  Html: string;
begin
  Count := 1;
  repeat
    Count := Count + 1;
    Html := GetPage(Url);
    //if (Html = '') then ShowWarning('Fails accessing: [' + Url + ']');
  until ((Html <> '') OR (Count = 5));
  Result := Html;
end;
 
function ConvertToASCII(AText: string): string;
begin
  Result := UTF8Decode(AText);
  if Result = '' then
    Result := AText; // in case of a UTF8 decoding error
  if GetOption('ConvertToASCII') = 1 then
    Result := Cp1252ToASCII(Result);
end;

function AKACountry: string;
var
  UserCountry: string;
begin
  UserCountry := GetParam('UserCountry');
  if UserCountry = 'United States' then
    Result := 'USA'
  else if UserCountry = 'United Kingdom' then
    Result := 'UK'
  else
    Result := UserCountry;
end;

// ***** analyzes IMDB's results page that asks to select a movie from a list *****

procedure AnalyzeResultsPage(Address: string);
var
  PageText: string;
  Value: string;
begin
  PageText := ConvertToASCII(RetryGetPage(Address));
  if pos('<title>Find - IMDb', PageText) = 0 then
    begin
      if (PageText <> '') then begin
         AnalyzeMoviePage(PageText)
      end;
    end else
    begin
      if Pos('<b>No Matches.</b>', PageText) > 0 then
        begin
          if GetOption('BatchMode') = 0 then
            ShowMessage('No movie found for this search.');
          Exit;
        end;
      if GetOption('BatchMode') = 0 then
        begin
          PickTreeClear;
          Value := TextBetween(PageText, '<h1 class="findHeader">Displaying', '</h1>');
          if Value <> '' then
          begin
            HTMLRemoveTags(Value);
            HTMLDecode(Value);
            PickTreeAdd(Value, '');
          end;
          Value := TextBetween(PageText, '<table class="findList">', '</table>');
          PageText := RemainingText;
          AddMovieTitles(Value);
          {
          Value := TextBefore(PageText, '"><b>more titles</b></a>', '<a href="');
          if Value <> '' then
            PickTreeMoreLink(ImdbUrl + Value);
          if GetOption('EpisodeTitleSearch') > 0 then
            PickTreeMoreLink(EpisodeTitleSearchURL + UrlEncode(MovieName));
          }
          if PickTreeExec(Address) then
            AnalyzeResultsPage(Address);
        end
      else
        if (MovieYear <> '') then
          begin
            Value := GetUrlBefore(PageText, '(' + MovieYear + ')', '');
            if Value = '' then
              begin
                Value := GetUrlBefore(PageText, '(' + IntToStr(StrToInt(MovieYear, 0) - 1) + ')', '');
                if Value = '' then
                  Value := GetUrlBefore(PageText, '(' + IntToStr(StrToInt(MovieYear, 0) + 1) + ')', '');
              end;
            if Value <> '' then
              AnalyzeResultsPage(ImdbUrl + Value);
          end
        else 
          begin
            Value := TextBetween(PageText, '<td class="primary_photo">', '<img src');
            if Value <> '' then
              AnalyzeResultsPage(ImdbUrl + TextBetween(Value, '<a href="', '"'));
          end;
    end;
end;

// ***** adds the movie titles found on IMDB's results page *****

function AddMovieTitles(List: string): Boolean;
var
  Value: string;
  Address: string;
begin
  Result := False;
  Value := TextBetween(List, '<td class="result_text">', '</td>');
  if GetOption('HideAkaTitles') = 1 then
    Value := StringReplace(Value, TextAfter(Value, '<br/>aka'), '')
  else
    Value := StringReplace(Value, 'aka', ' | aka');
  List := RemainingText;
  while Value <> '' do
  begin
    Address := TextBetween(Value, '<a href="/title/tt', '/');
    Address := Address + '/reference';
    HTMLRemoveTags(Value);
    HTMLDecode(Value);
    PickTreeAdd(Value, ImdbUrl + '/title/tt' + Address);
    Result := True;
    Value := TextBetween(List, '<td class="result_text">', '</td>');
    if GetOption('HideAkaTitles') = 1 then
      Value := StringReplace(Value, TextAfter(Value, '<br/>aka'), '')
    else
      Value := StringReplace(Value, 'aka', ' | aka');
     List := RemainingText;
  end;
end;

// ***** analyzes the page containing movie information *****

procedure AnalyzeMoviePage(PageText: string);
var
  Value, Value2, FullValue, originalTitle: string;
  p, Count: Integer;
begin
  MovieNumber := TextBetween(PageText, '<input type="hidden" name="auto" value="legacy/title/tt', '/');
  if MovieNumber = '' then
    MovieNumber := TextBetween(PageText, '<input type="hidden" name="auto" value="legacy/title/tt', '/reference"');
  if MovieNumber = '' then
    MovieNumber := TextBetween(PageText, '<link rel="canonical" href="https://www.imdb.com/title/tt', '/');
  if Pos('/reference"', TextBetween(PageText, '<link rel="canonical"', '/>')) = 0 then
    PageText := ConvertToASCII(RetryGetPage(ImdbUrl + '/title/tt' + MovieNumber + '/reference'));
  MovieURL := ImdbUrl + '/title/tt' + MovieNumber;
  // URL
  if CanSetField(fieldURL) then
    SetField(fieldURL, StringReplace(MovieURL, ImdbUrl, 'https://www.imdb.com'));
  // OriginalTitle & Year
  {
  if CanSetField(fieldOriginalTitle) or CanSetField(fieldYear) then
  begin
    originalTitle := TextBefore(PageText, '(original title)', '</h1>');
    HTMLRemoveTags(originalTitle);
    HTMLDecode(originalTitle);
    originalTitle := RemoveSpaces(originalTitle, true);
    if (originalTitle <> '') and CanSetField(fieldOriginalTitle) then
    begin
      SetField(fieldOriginalTitle, FullTrim(originalTitle));
    end;
    Value := TextBetween(PageText, '<title>', '</title>');
    p := Pos(' (1', Value);
    if p = 0 then
      p := Pos(' (2', Value);
    if p = 0 then
      p := Pos(' (TV', Value);
    if p > 0 then
    begin
      Value2 := Copy(Value, 0, p-1);
      Value := Copy(Value, p+2, Length(Value));
      HTMLDecode(Value2);
      if (originalTitle = '') and CanSetField(fieldOriginalTitle) then
        SetField(fieldOriginalTitle, FullTrim(Value2));
      if CanSetField(fieldYear) then
      begin
        if Pos('/', Value) > 0 then
        begin
          Value := TextBefore(Value, '/', '');
        end
        else
        begin
          Value := TextBefore(Value, ')', '');
        end;
        if Pos('–', Value2) > 0 then
        begin
          Value := TextBefore(Value, '–', '');
        end;
        SetField(fieldYear, Value);
      end;
    end;
  end;
  }
  if CanSetField(fieldOriginalTitle) or CanSetField(fieldYear) then
  begin
    originalTitle := TextBefore(PageText, '(original title)', '</h3>');
    if originalTitle ='' then originalTitle := TextBetween (PageText,'<h3 itemprop="name">','<span class="titlereference-title-year">');
    Value:= TextBetween (Pagetext, '<span class="titlereference-title-year">', '<ul class="ipl-inline-list">');
    Value:= TextBetween (Value, 'url', '</a>)');
    Value:= TextAfter (Value, '>');
    HTMLRemoveTags(originalTitle);
    HTMLDecode(originalTitle);
    originalTitle := RemoveSpaces(originalTitle, true);
    if (originalTitle <> '') and CanSetField(fieldOriginalTitle) then
      SetField(fieldOriginalTitle, FullTrim(originalTitle));
   end;
  if CanSetField(fieldYear) then SetField(fieldYear, Value);
  // Picture
  if CanSetPicture then
  begin
    case GetOption('ImageKind') of
      2:  if not ImportSmallPicture(PageText) then
              ImportPictureNotAvailable(PageText);
      3:  ImportLargePicture(PageText);
      4:  if not ImportMerchandisingPicture then
              if not ImportDvdDetailsPicture then
                ImportSmallPicture(PageText);
      5:  if not ImportDvdDetailsPicture then
              if not ImportMerchandisingPicture then
                ImportSmallPicture(PageText);
      3:  if not ImportLargePicture(PageText) then
              if not ImportSmallPicture(PageText) then
                ImportPictureNotAvailable(PageText);
    else
      ImportSmallPicture(PageText);
    end;
  end;

  // Director
  if CanSetField(fieldDirector) then
  begin
    Value := TextBetween(PageText, '<h4 name="directors"', '</table>');
    FullValue := '';
    Value2 := TextBetween(Value, '<a href="/name/', '</a>');
    while Value2 <> '' do
    begin
      Value := RemainingText;
      Value2 := TextAfter(Value2, '>');
      if FullValue <> '' then
        FullValue := FullValue + ', ';
      FullValue := FullValue + Value2;
      Value2 := TextBetween(Value, '<a href="/name/', '</a>');
    end;
    HTMLDecode(FullValue);
    FullValue := RemoveSpaces(FullValue, true);
    SetField(fieldDirector, FullValue);
  end;

  // Actors
  if CanSetField(fieldActors) then
  begin
    if GetOption('AllActors') = 0 then
    begin
      FullValue := TextBetween(PageText, ' Stars:', '</li>');
      HTMLRemoveTags(FullValue);
      HTMLDecode(FullValue);
      FullValue := RemoveSpaces(FullValue, true);
    end
    else
    begin
      FullValue := '';
      Value := FullTrim(TextBetween(PageText, '<h4 name="cast"', '</table>'));
      if Value <> '' then
      begin
        Count := 0;
        p := StrToInt(GetParam('MaxActors'), 10);
        case GetOption('ActorsLayout') of
          0, 1:
            while Pos('<tr class', Value) > 0 do
            begin
              Value2 := TextBetween(Value, '<tr class', '</tr>');
              Value := RemainingText;
              if Pos('Rest of cast', Value2) > 0 then
                Continue;
              if FullValue <> '' then
                FullValue := FullValue + #13#10;
              TextBefore(Value2, '</td>', '');
              Value2 := FullTrim(TextBetween(Value2, '<td class="itemprop" itemprop="actor" itemscope itemtype="http://schema.org/Person">', '</td>'));
              HTMLRemoveTags(Value2);
              Value2 := RemoveSpaces(Value2, true);
              if Value2 <> '' then
              begin
                FullValue := FullValue + Value2;
                Count := Count + 1;
              end;
              if (Count = p) and (GetOption('AllActors') = 2) then
              begin
                Break;
              end;
            end;
          2, 3, 4:
            while Pos('<tr', Value) > 0 do
            begin
              Value2 := TextBetween(Value, '<tr class', '</tr>');
              Value := RemainingText;
              if Pos('Rest of cast', Value2) > 0 then
                Continue;
              if FullValue <> '' then
                FullValue := FullValue + #13#10;
              TextBefore(Value2, '</td>', '');
              Value2 := FullTrim(TextBetween(Value2, '<td class="itemprop" itemprop="actor" itemscope itemtype="http://schema.org/Person">', '</td>'));
              HTMLRemoveTags(Value2);
              Value2 := RemoveSpaces(Value2, true);
              if Value2 <> '' then
              begin
                FullValue := FullValue + Value2;
                Value2 := FullTrim(TextBetween(RemainingText, '<td class="character">', '</td>'));
                HTMLRemoveTags(Value2);
                Value2 := RemoveSpaces(Value2, true);
                if Value2 <> '' then
                begin
                  if GetOption('ActorsLayout') = 4 then
                  begin
                    FullValue := FullValue + ' ... ' + Value2;
                  end
                  else
                  begin
                    FullValue := FullValue + ' (as ' + Value2 + ')';
                  end;
                end;
                Count := Count + 1;
                if (Count = p) and (GetOption('AllActors') = 2) then
                begin
                  Break;
                end;
              end;
            end;
        end;
        HTMLRemoveTags(FullValue);
        HTMLDecode(FullValue);
        case GetOption('ActorsLayout') of
          0, 2:
            begin
              FullValue := StringReplace(FullValue, #13#10, ', ');
            end;
        end;
      end;
    end;
    SetField(fieldActors, FullValue);
  end;

  // Composer
  if CanSetField(fieldComposer) then
  begin   
    Value := TextBetween(PageText, '<h4 name="composers"', '</table>');
    FullValue := '';
    Value2 := TextBetween(Value, '<a href="/name/', '</a>');
    while Value2 <> '' do
    begin
      Value := RemainingText;
      Value2 := TextAfter(Value2, '>');
      if FullValue <> '' then
        FullValue := FullValue + ', ';
      FullValue := FullValue + Value2;
      Value2 := TextBetween(Value, '<a href="/name/', '</a>');
    end;
    HTMLDecode(FullValue);
    FullValue := RemoveSpaces(FullValue, true);
    SetField(fieldComposer, FullValue);
  end;

  //Country
  if CanSetField(fieldCountry) then
  begin
    SetField(fieldCountry, ImportList(PageText, GetOption('MultipleValuesCountry'), '<td class="ipl-zebra-list__label">Country</td>'));
  end;
  //Category
  if CanSetField(fieldCategory) then
  begin
    SetField(fieldCategory, ImportList(PageText, GetOption('MultipleValuesCategory'), '<td class="ipl-zebra-list__label">Genres</td>'));
  end;
  // Language
  if CanSetField(fieldLanguages) then
  begin
    SetField(fieldLanguages, ImportList(PageText, GetOption('MultipleValuesLanguages'), '<td class="ipl-zebra-list__label">Language</td>'));
  end;
  // Audio Format
  if CanSetField(fieldAudioFormat) then
  begin
    SetField(fieldAudioFormat, ImportList(PageText, GetOption('MultipleValuesAudioFormat'), '<td class="ipl-zebra-list__label">Sound Mix</td>'));
  end;
  // Aspect Ratio
  begin
    Value := '';
    Value := TextBetween(PageText, '<td class="ipl-zebra-list__label">Aspect Ratio</td>', '</li>');
    HTMLRemoveTags(Value);
    HTMLDecode(Value);
    Value := RemoveSpaces(Value, true);
    if (CanSetField(fieldVideoFormat)) and (GetOption('AspectRatio') = 1) then
      SetField(fieldVideoFormat, Value);
    if (CanSetField(fieldResolution)) and (GetOption('AspectRatio') = 2) then
      SetField(fieldResolution, Value);
  end;
  // Description
  if CanSetField(fieldDescription) then
  begin
    if (GetOption('DescriptionSelection') = 0) then
    begin
      Value := TextBetween(PageText, '<section class="titlereference-section-overview">', '<div class="titlereference-overview-section">');
      //Value := TextAfter(Value, '<hr>');
    end
    else
    begin
      Value := TextBetween(PageText, '<td class="ipl-zebra-list__label">Plot Summary</td>', '</td>');
      Value := TextBefore(Value, '</p>', '');
      if Pos('<em', Value) > 0 then
      begin
        Value := TextBefore(Value, '<em', '');
      end;
      if Value = '' then // no long description on the page, taking the short one
      begin
        Value := TextBetween(PageText, '<section class="titlereference-section-overview">', '<div class="titlereference-overview-section">');
      end;
    end;
    HTMLRemoveTags(Value);
    HTMLDecode(Value);
    Value := StringReplace(Value, ConvertToAscii('See more »'), '');
    Value := RemoveSpaces(Value, true);
    SetField(fieldDescription, Value);
  end;
  // Length
  if CanSetField(fieldLength) then
  begin
    Value := TextBetween(PageText, '<td class="ipl-zebra-list__label">Runtime</td>', '</li>');
    Value := TextBefore(Value, ' min', '');
    HTMLRemoveTags(Value);
    Value := RemoveSpaces(Value, true);
    if Value <> '' then
    begin
      SetField(fieldLength, Value);
    end;
  end;

  // Producer
  if CanSetField(fieldProducer) then
  begin
    Value := TextBetween(PageText, '<h4 name="producers"', '</table>');
    FullValue := '';
    Value2 := TextBetween(Value, '<a href="/name/', '</a>');
    while Value2 <> '' do
    begin
      Value := RemainingText;
      Value2 := TextAfter(Value2, '>');
      if FullValue <> '' then
        FullValue := FullValue + ', ';
      HTMLRemoveTags(Value2);
      FullValue := FullValue + RemoveSpaces(Value2, true);
      Value2 := TextBetween(Value, '<a href="/name/', '</a>');
    end;
    HTMLDecode(FullValue);
    SetField(fieldProducer, FullValue);
  end;

  // Writer
  if CanSetField(fieldWriter) then
  begin   
    Value := TextBetween(PageText, '<h4 name="writers"', '</table>');
    FullValue := '';
    Value2 := TextBetween(Value, '<a href="/name/', '</a>');
    while Value2 <> '' do
    begin
      Value := RemainingText;
      Value2 := TextAfter(Value2, '>');
      if FullValue <> '' then
        FullValue := FullValue + ', ';
      HTMLRemoveTags(Value2);
      FullValue := FullValue + RemoveSpaces(Value2, true);
      Value2 := TextBetween(Value, '<a href="/name/', '</a>');
    end;
    HTMLDecode(FullValue);
    SetField(fieldWriter, FullValue);
  end;

  // AKA Name
  if CanSetField(fieldTranslatedTitle) then
  begin
    FullValue := ConvertToASCII(GetPage(MovieURL+'/releaseinfo#akas'));
//    FullValue := ConvertToASCII(GetLocalPage(MovieURL+'/releaseinfo#akas'));
    FullValue := TextBetween(FullValue, '<a id="akas" name="akas">', '</table>');
    FullValue := RemoveSpaces(FullValue, True);
    FullValue := StringReplace(FullValue, #13, '');
    FullValue := StringReplace(FullValue, #10, '');
    FullValue := StringReplace(FullValue, '> <', '><');
    FullValue := StringReplace(FullValue, '<tr class="even">', '<tr>');
    FullValue := StringReplace(FullValue, '<tr class="odd">', '<tr>');
    if RegExprSetExec('</td>\s*<tr>', FullValue) then
      // removing the original title
      FullValue := '<tr>'+TextAfter(FullValue, '</td><tr>');

    while fullvalue <> '' do
    begin
    // each line is a pair of  translated title and country
    // format: <tr><td>country</td><td>Translated title</td><tr>
      value2 := TextBetween(FullValue, '<tr>', '</tr>');
    // showMessage('Line: '+value2);
      value := TextBetween(Value2, '<td>', '</td>');
      value2 := TextAfter(Value2, '</td>');
      value2 := TextBetween(Value2, '<td>', '</td>');
      HTMLDecode(value2);
   //  showMessage('Translated: '+value+' into '+value2);
      if pos(AKACountry(), value) > 0 then
      begin
        HTMLDecode(Value2);
        SetField(fieldTranslatedTitle, FullTrim(Value2));
        FullValue := '';
      end
      else  FullValue := TextAfter(FullValue, '</tr>');
    end;
 {
    if Value <> '' then
    begin
      Value := StringReplace(Value, 'See more', '');
      Value := StringReplace(Value, '&raquo;', '');
      Value := FullTrim(StringReplace(Value, '<br>', ', '));
      HTMLRemoveTags(Value);
      HTMLDecode(Value);
      Value := FullTrim(Value);
      if Value <> '' then
        if StrGet(Value, Length(Value)) = ',' then
          Delete(Value, Length(Value), 1);
      SetField(fieldTranslatedTitle, Value)
   end;
   }
  end;
  // Comments
  if CanSetField(fieldComments) then
  begin
    case GetOption('CommentType') of
      0, 1:
        begin
          Value2 := '';
          p := 0;
          FullValue := ConvertToASCII(GetPage(MovieURL+'/reviews'));
          FullValue := TextAfter(FullValue, '<div class="lister-item mode-detail imdb-user-review');
          while FullValue <> '' do
          begin
            Value := TextBetween(FullValue, '<div class="review-container">','<div class="actions text-muted">');
            if GetOption('CommentType') = 1 then
            begin
              p := p + 1;
              Value2 := Value2 + inttostr(p) + '. ';
            end;
            Value2 := Value2 + TextBetween(Value, '<div class="title">','</div>');
            Value2 := Value2 + ' (by ' + TextAfter (TextBetween(Value, '<span class="display-name-link">','</a>'),'>');
            Value2 := Value2 + #32 + 'on ' + TextBetween(Value, '<span class="review-date">', '</span>') + ')' ;
            Value := RemainingText;
            Value := TextBetween(Value, '<div class="text show-more__control">','</div>');
            Value := StringReplace(Value, #13#10, ' ');
            Value := StringReplace(Value, '<br/><br/>', #13#10);
            Value := StringReplace(Value, '<br/>', #13#10);
            HtmlRemoveTags(Value);
            Value2 := Value2 + #13#10 + #13#10 + FullTrim(Value);
            if GetOption('CommentType') = 0 then
            begin
              break;
            end;
            FullValue := TextAfter(FullValue, '<div class="lister-item mode-detail imdb-user-review');
            if FullValue <> '' then
            begin
              Value2 := Value2 +  #13#10 + #13#10;
            end;
          end;
          HTMLRemoveTags(Value2);
          HTMLDecode(Value2);
          SetField(fieldComments, Value2);
        end;
      2:
        begin
          SetField(fieldComments, '');
        end;
    end;
  end;
  // TagLine
  if GetOption('GetTagline') > 0 then
  begin
    Value := TextBetween(PageText, '<td class="ipl-zebra-list__label">Taglines</td>', '</td>');
    HTMLRemoveTags(Value);
    HTMLDecode(Value);
    Value := StringReplace(Value, ConvertToAscii('See more »'), '');
    Value := RemoveSpaces(Value, true);
    if Value <> '' then
    begin
      if StrGet(Value, 1) <> '"' then
        Value := '"' + Value + '"';
      case GetOption('GetTagline') of
        1:
          begin
            if GetField(fieldDescription) <> '' then
              Value := Value + #13#10 + #13#10 + GetField(fieldDescription);
            SetField(fieldDescription, Value);
          end;
        2:
          begin
            if GetField(fieldComments) <> '' then
              Value := Value + #13#10 + #13#10 + GetField(fieldComments);
            SetField(fieldComments, Value);
          end;
      end;
    end;
  end;
  // Trivia
  if GetOption('Trivia') > 0 then
  begin
    sleep(50);
    Value := MovieUrl;
    FullValue := ConvertToASCII(GetPage(Value+'/trivia'));
    case GetOption('Trivia') of
      1,2:
        Value := TextBetween(FullValue, '<div class="sodatext">', '</div>');
      3,4:
        Value := TextBetween(FullValue, '<div class="list">', '<div id="sidebar">');
    end;
    if Value <> '' then
    begin
      Value := StringReplace(Value, #13#10, '');
      while Pos('  ', Value) > 0 do
        Value := StringReplace(Value, '  ', '');
      while Pos('<span class="linksoda">', Value) > 0 do
      begin
        Value := StringReplace(Value, TextBetween(Value, '<span class="linksoda">', '</div>'), '');
        Value := StringReplace(Value, '<span class="linksoda"></div>', '');
      end;
      Value := StringReplace(Value, 'Link this trivia', '');
      Value := StringReplace(Value, '<div class="sodatext">', #13#10 + '- ');
      Value := StringReplace(Value, TextBetween(Value, '<script type="text/javascript">', '</script>'), '');
      HTMLRemoveTags(Value);
      HTMLDecode(Value);
      Value := RegExprSetReplace('\s[\d,]+\s+of\s+[\d,]+\sfound\sthis\sinterestingInteresting\?YesNo\|', Value, '', false);
      Value := RegExprSetReplace('\s*Is\sthis\sinteresting\?Interesting\?YesNo\|', Value, '', false);
      Value := RegExprSetReplace('Spoilers\sThe\strivia\sitems?\sbelow\smay\sgive\saway\simportant\splot\spoints\.\s*', Value, #13#10 + 'Spoilers:' + #13#10, false);
      Value := RegExprSetReplace('\s*See\salsoGoofs.*', Value, '', false);     

      if (GetOption('Trivia') = 1) or (GetOption('Trivia') = 2) then
        Value := ' ' + Value;

      case GetOption('Trivia') of
        1,3:
          begin
            if GetField(fieldDescription) <> '' then
              Value := GetField(fieldDescription) + #13#10 + #13#10 + 'IMDB TRIVIA:' + Trim(Value)
            else
              Value :=  'IMDB TRIVIA: ' + Value;
            SetField(fieldDescription, Value);
          end;
        2,4:
          begin
            if GetField(fieldComments) <> '' then
              Value := GetField(fieldComments) + #13#10 + #13#10 + 'IMDB TRIVIA:' + Trim(Value)
            else
              Value :=  'IMDB TRIVIA: ' + Value;
            SetField(fieldComments, Value);
          end;
      end;
    end;
  end;
  // Awards
  if (GetOption('Awards') > 0) then
  begin
    ImportAwards();
  end;
  // Rating
  if CanSetField(fieldRating) then
  begin
//   (Remove the "//" of the next two lines beginning with "Value" and add "//" to the following two lines beginning with Value
//   if you would like to import arithmetic ratings instead of IMDB's user ratings)
//    Value := ConvertToASCII(GetPage(MovieURL + '/ratings'));
//    Value := TextBetween(Value, 'Arithmetic mean = ', '. ');
// *** not sure that changing that still works, to be confirmed
    Value := TextBetween(PageText, '<span class="ipl-rating-star__rating">', '</span>');
    if Value <> '' then
      if (GetOption('RoundRating') = 1) then
        SetField(fieldRating, IntToStr(Round(StrToFloat(Value))))
      else
        SetField(fieldRating, Value);
  end;
  if GetOption('UserRatings') > 0 then
    begin
      Value := TextBetween(PageText, '<a href="ratings" class="tn15more">', '</a>');
      if Value <> '' then
        Value := 'User Rating: ' + GetField(fieldRating) + ' out of 10  (with ' + Value + ')';
      if (GetOption('UserRatings') = 1) and (Value <> '') and (CanSetField(fieldMediaType)) then
        SetField(fieldMediaType, Value);
      if (GetOption('UserRatings') = 2) and (Value <> '') then
        SetField(fieldComments, GetField(fieldComments) + #13#10 + #13#10 + Value);
    end;
  // Classification
  if CanSetField(fieldCertification) then
  begin
    Value := TextBetween(PageText, '<a href="/search/title?certificates=', '</div>');
    Value := TextBetween(Value, GetParam('UserCountry') + ':', '</a>');
    if Value <> '' then
      SetField(fieldCertification, Value);
  end;
  // MPAA rating
  if (GetOption('MPAA') > 0) then
  begin
     Value := TextBetween(PageText, '<a href="/search/title?certificates=US', '</a>');
     Value := Trim(TextAfter(Value, ':'));
     if Value <> '' then
     begin
      if GetOption('MPAA') = 1 then
        SetField(fieldMediaType, Value)
      else
        SetField(fieldComments, GetField(fieldComments) + #13#10 + #13#10 + 'Rated ' + Value);
     end;
  end;
end;

// ***** Imports lists like Genre, Country, etc. depending of the selected option *****

function ImportList(PageText: string; MultipleValues: Integer; StartTag: string): string;
var
  Value, Value2: string;
begin
  if MultipleValues = 0 then
  begin
    Value := TextBetween(PageText, StartTag, '</a>');
    Value2 := TextAfter(Value, '<li class="ipl-inline-list__item">');
  end
  else if MultipleValues = 3 then
  begin
    Exit;
  end
  else
  begin
    Value := TextBetween(PageText, StartTag, '</ul>');
    Value2 := TextBefore(Value, 'See more &raquo;</a>', '');
    if Value2 = '' then
      Value2 := Value;
    Value2 := TextAfter(Value2, '">');
    Value2 := StringReplace(Value2, '<li class="ipl-inline-list__item">', ' / ');
  end;
  HTMLRemoveTags(Value2);
  HTMLDecode(Value2);
  Value2 := RemoveSpaces(Value2, true);
  if Pos('/ ', Value2) = 1 then
  begin
    Delete(Value2, 1, 2);
  end;
  if MultipleValues = 1 then
  begin
    Value2 := StringReplace(Value2, ' / ', ', ');
  end;
  Result := Value2;
end;

// ***** functions to import the different pictures kinds, depending of the option selected by user *****

//Import small picture from IMDB movie main page
function ImportSmallPicture(PageText: string): Boolean;
var
  Value: string;
begin
  Result := False;
  Value := TextBetween(PageText, 'alt="Poster"', '/>');
  if (Value <> '') then
  begin
    Value := TextBetween(Value, 'src="', '"');
    if Value <> '' then
    begin
      //Value := StringReplace(Value, 'https://', 'http://');
      GetPicture(Value);
      Result := True;
    end;
  end;
end;

//Import large image, building link from the small image of the movie page
function ImportLargePicture(PageText: string): Boolean;
var
  Value: string;
begin
  Result := False;
  Value := TextBetween(PageText, 'alt="Poster"', '/>');
  if (Value <> '') then
  begin
    Value := TextBetween(Value, 'src="', '"');
    if Value <> '' then
    begin
      //Value := StringReplace(Value, 'https://', 'http://');
      Value := TextBefore(Value, '._', '') + '._V1_SY' + GetParam('LargePictureHeight') + '_AL_.jpg';
      GetPicture(Value);
      Result := True;
    end;
  end;
end;

//Image not available, import "No Poster Available" icon
function ImportPictureNotAvailable(PageText: string): Boolean;
var
  Value: string;
begin
  Result := False;
  Value := TextBetween(PageText, '<div class="photo">', '</div>');
  if (Value <> '') and (Pos('"Poster Not Submitted"', Value) > 0) then
  begin
    Value := TextBetween(Value, 'src="', '"');
    if Value <> '' then
    begin
      GetPicture(Value);
      Result := True;
    end;
  end;
end;

//Image from DVD Details Page
function ImportDvdDetailsPicture: Boolean;
var
  Value, PageText: string;
begin
  Result := False;
  PageText := ConvertToASCII(GetPage(MovieUrl+'/dvd'));
  Value := TextBefore(PageText, '.jpg alt="', '<table class="dvd_section" cellpadding="10"><tr>');
  if Value <> '' then
  begin
    Value := TextBetween(Value, '<a href="', '">');
    if Value <> '' then
      Result := ImportFromAmazonRedirect(ImdbUrl + Value);
  end;
end;

//Image from Merchandising Links (/sales) Page
function ImportMerchandisingPicture: Boolean;
var
  Value, PageText: string;
begin
  Result := False;
  PageText := ConvertToASCII(GetPage(MovieUrl+'/sales'));
  Value := TextBefore(PageText, '.jpg" width=', '<td class=w_rowtable_colcover><a href="');
  if Value <> '' then
  begin
    Value := TextBefore(Value, '"><img', '');
    if Value <> '' then
      Result := ImportFromAmazonRedirect(ImdbUrl + Value);
  end;
end;

function ImportFromAmazonRedirect(Url: string): Boolean;
var
  Value, PageText: string;
begin
  Result := False;
  PageText := ConvertToASCII(GetPage(Url));
  Value := TextBetween(PageText, '<td id="prodImageCell"', '" id="prodImage"');
//  Value := StringReplace(TextAfter(Value, ' src="'), '_AA240', '');
  Value := StringReplace(Value, TextBetween(Value, '_AA', '_'), '');
  Value := StringReplace(TextAfter(Value, ' src="'), '_AA', '');
  if Value <> '' then
  begin
    GetPicture(Value);
    Result := True;
  end;
end;

// ***** Gets summaries for the movie, based on the plot outline given in parameter (that contains the URL to more summaries) *****

function ImportSummary(PlotText: string): string;
var
  Address, Value, PageText, Longest: string;
begin
  Address := TextBetween(PlotText, '<a class="tn15more inline" href="', '" ');
  if (Address = '') or (GetOption('DescriptionSelection') = 0) then
  begin
    Result := FullTrim(TextBefore(PlotText, '<a class="tn15more inline"', ''));
    if Result = '' then
      Result := FullTrim(PlotText);
    HTMLRemoveTags(Result);
    HTMLDecode(Result);
  end else
  begin
    PageText := ConvertToASCII(GetPage(ImdbUrl + Address));
    PickListClear;
    Longest := '';
    PageText := TextBetween(PageText, '<ul class="ipl-zebra-list" id="plot-summaries-content">', '</ul>');
    Value := TextBetween(PageText, '<p>', '</p>');
    PageText := RemainingText;
    while Value <> '' do
    begin
      Value := FullTrim(StringReplace(Value, #13#10, ' '));
      Value := StringReplace(Value, '<br>', #13#10);
      HTMLRemoveTags(Value);
      HTMLDecode(Value);
      while Pos('  ', Value) > 0 do
        Value := StringReplace(Value, '  ', ' ');
      if Length(Value) > Length(Longest) then
        Longest := Value;
      PickListAdd(FullTrim(Value));
      Value := TextBetween(PageText, '<p>', '</p>');
      PageText := RemainingText;
    end;
    if (GetOption('BatchMode') > 0) or (GetOption('DescriptionSelection') = 2) then
      Result := Longest
    else
    begin
      if not PickListExec('Select a description for "' + GetField(fieldOriginalTitle) + '"', Result) then
        Result := '';
    end;
  end;
end;

// Import awards
procedure ImportAwards;
var
  IndexPage: TStringList;
  PageText, FullValue, Block, Value, Row, Outcome, Details, AwardShow, PageText1: string;
  Year, Result, Award, Category: string;
begin
  sleep(50);
  Value := MovieUrl;
  PageText := ConvertToASCII(GetPage(Value+'/awards'));
 
  repeat

    AwardShow := TextBetween(PageText, '<h3>', '</h3>');

    if Pos('User Lists', AwardShow) > 0 then             
      break;
      HTMLRemoveTags(AwardShow);
      HTMLDecode(AwardShow);
      AwardShow := FullTrim(AwardShow);
      AwardShow := StringReplace(AwardShow, #13, '');
      AwardShow := StringReplace(AwardShow, #10, '');
      FullValue := FullValue + #32 + AwardShow + #13#10;
      PageText1 := TextBetween(PageText, '<table class="awards"', '</table>');
      PageText := RemainingText;
      Block := PageText1;

      repeat
         
        Row := TextBetween(Block, '<tr>', '</tr>');
        Block := RemainingText;
 
        Outcome := TextBetween(Row, '<b>', '</b>');
        Category := TextBetween(Row, '<span class="award_category">', '</span>');
        Award := TextBetween(Row, '<td class="award_description">', '</td>');
        Details := TextBetween(Row, '<div class="award_detail_notes">', '</div>');
           
        If Outcome <> '' then
          FullValue := FullValue + #32 + Outcome + #13#10;
           
        If Category <> '' then
          FullValue := FullValue + #32 + Category;
           
        If Award <> '' then
          Award := StringReplace(Award, '<br />', #32);
         
          HTMLRemoveTags(Award);
          HTMLDecode(Award);
          Award := StringReplace(Award, #13, '');
          Award := StringReplace(Award, #10, '');                         
          Award := FullTrim(Award);       
          Award := StringReplace(Award, '  ', '');
          FullValue := FullValue + #32 + Award;
             
        If Details <> '' then
          HTMLRemoveTags(Details);
          HTMLDecode(Details);
          Award := StringReplace(Details, #13, '');
          Award := StringReplace(Details, #10, '');                         
          Award := FullTrim(Details);

          FullValue := FullValue + #32 + Details + #13#10;

     until (Row = ''); 
  until (PageText1 = '');
 
 // ShowInformation(FullValue);
 
  if FullValue <> '' then
    case GetOption('Awards') of
      1:
        begin
          if GetField(fieldDescription) <> '' then
            Value := GetField(fieldDescription) + #13#10 + #13#10 + 'AWARDS: ' + #13#10 + FullValue
          else
            Value := 'AWARDS: ' + FullValue;
          SetField(fieldDescription, Value);
        end;
      2:
        begin
          if GetField(fieldComments) <> '' then
            Value := GetField(fieldComments) + #13#10 + #13#10 + 'AWARDS: ' + #13#10 + FullValue
          else
            Value := 'AWARDS: ' + FullValue;
          SetField(fieldComments, Value);
        end;
    end;
end;

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

begin
  // Check StringUtils1 version and update if < version 7
  if StringUtils1_Version < 7 then
    begin
      if ShowWarning('Old version of file "Stringutils1.pas" detected: v'+IntToStr(StringUtils1_Version)+#13#10+'The script requires at least version 7.'+#13#10+'Download and install latest version now ?') = True then
        begin
          UpdateFile := TStringList.Create;
          UpdateFile.Text := GetPage('http://update.antp.be/amc/scripts/StringUtils1.pas');
          UpdateFile.SaveToFile(dirScripts + 'StringUtils1.pas');
          UpdateFile.Free;
          ShowInformation('StringUtils1 has been updated. Please restart IMDB script now. Thank you.');
          Exit;
        end
      else
        begin
          ShowInformation('You can download latest version of "StringUtils1.pas" using script "update scripts" or via http://update.antp.be/amc/scripts');
          Exit;
        end;
    end;
  // Check for current AMC version
  if CheckVersion(4,2,2) then
  begin
    // bug with default values it seems ?
    if GetParam('MaxActors') = '' then
    begin
      SetParam('MaxActors', '10');
    end;
    if GetParam('UserCountry') = '' then
    begin
      SetParam('UserCountry', 'United States');
    end;
    if GetParam('LargePictureHeight') = '' then
    begin
      SetParam('LargePictureHeight', '720');
    end;
    MovieYear := GetField(fieldYear);
    MovieName := '';
    if GetOption('BatchMode') = 2 then
    begin
      MovieName := GetField(fieldURL);
      if Pos('imdb.com', MovieName) = 0 then
        MovieName := '';
    end;
    if MovieName = '' then
      MovieName := GetField(fieldOriginalTitle);
    if MovieName = '' then
      MovieName := GetField(fieldTranslatedTitle);
    if GetOption('BatchMode') = 0 then
    begin
      if not Input('IMDB Import', 'Enter the title or the IMDB URL of the movie:', MovieName) then
        Exit;
    end
    else
      Sleep(500);
    if MovieName <> '' then
    begin
      if Pos('imdb.com', MovieName) > 0 then
      begin
        MovieName := StringReplace(MovieName, '//imdb.com', '//www.imdb.com');
        MovieName := StringReplace(MovieName, 'http://', 'https://');
        MovieName := StringReplace(MovieName, 'https://www.imdb.com', ImdbUrl);
        AnalyzeResultsPage(MovieName)
      end else
      if RegExprSetExec('tt[0-9]+', moviename) then
        AnalyzeResultsPage('https://www.imdb.com/title/' + MovieName + '/reference')
      else
      begin
        MovieName := UTF8Encode(StringReplace(MovieName, '&', 'and'));
        if (GetOption('BatchMode') > 0) or (GetOption('PopularSearches') = 1) then
          AnalyzeResultsPage(PopularTitleSearchURL + UrlEncode(MovieName))
        else
          AnalyzeResultsPage(FullTitleSearchURL + UrlEncode(MovieName));
      end;
    end;
  end
  else if ShowWarning('This script requires a newer version of Ant Movie Catalog, at least the version 4.2.2, currently available as beta - do you wish to open the link to download it?)') = true then
  begin
    Launch('https://forum.antp.be/phpbb3/viewtopic.php?f=6&t=6862', '');
  end;
end. 
antp
Site Admin
Posts: 9651
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Re: [fix] imdb socket error #10020

Post by antp »

It seems they already fixed the problem (at least for me it works currently)
For such big site, I imagine that this kind of problem is quickly solved :)
mrobama
Posts: 85
Joined: 2009-04-03 12:34:34

Re: [fix] imdb socket error #10020

Post by mrobama »

Problem is still alive: "I have asked your DNS server for www.imdb.com. but i did not receive an IP address (maybe i received a CNAME...), however these are the records i received:
www.imdb.com. = CNAME us.dd.imdb.com."

https://www.dnsqueries.com/en/domain_check.php

This are some topic about this problem:
https://airvpn.org/topic/29835-problems-with-imdb-dns/
https://airvpn.org/topic/29838-imdbcom/

Maybe it's a problem only in some countries
Post Reply