Page 1 of 1

TV Tome Script v0.1

Posted: 2003-08-19 20:45:57
by Apollyon
Hi!

First of all, thank you antp for a great program! Can't wait for the new version! By the way, any updates on its status?

Anyway, I've put together a little script for TV Tome. Right now, its hardcoded for Futurama (best show ever!! :)), but by uncommenting a line, you can choose your own show. It has also been tried with The Simpsons and it seems to work right.

There may be bugs, so don't get too upset. Right now, it does what I need, but if there is interest in the comunity, I'll keep developing it. Ideas and bug reports are welcome!

Code: Select all

// SCRIPTING
// Futurama TV Tome import
// Created by Alex Iribarren
// http://alex.iribarren.com

(***************************************************
 *  Episode importation script for:                *
 *      TV Tome, http://www.tvtome.com             *
 *                                                 *
 *  (c) 2003 Alex Iribarren     alexiri@terra.es   *
 *                                                 *
 *  Modified version of IMDB batch import by:      *
 *    Antoine Potten    antoine@buypin.com         *
 *      Contributors :                             *
 *       Danny Falkov                              *
 *       Kai Blankenhorn                           *
 *                                                 *
 *  For use with Ant Movie Catalog 3.4.0           *
 *  www.ant.be.tf/moviecatalog ··· www.buypin.com  *
 *                                                 *
 *  The source code of the script can be used in   *
 *  another program only if full credits to        *
 *  script author and a link to Ant Movie Catalog  *
 *  website are given in the About box or in       *
 *  the documentation of the program               *
 ***************************************************)

program TvTome;

var
  EpName, EpNumber: string;
  MovieName: string;

function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
var
  i: Integer;
begin
  result := -1;
  if StartAt < 0 then
    StartAt := 0;
  for i := StartAt to List.Count-1 do
    if Pos(Pattern, List.GetString(i)) <> 0 then
    begin
      result := i;
      Break;
    end;
end;

procedure AnalyzePage(Address: string);
var
  Page: TStringList;
  LineNr, StartPos: Integer;
  Line, Season: string;
begin
  Page := TStringList.Create;
  Page.Text := GetPage(Address);

  PickTreeClear;
  PickTreeAdd('Searching for ' + EpName, '');
  LineNr := 0;
  Address := '';
  
  repeat
    LineNr := FindLine('<tr><td colspan="6"><', Page, LineNr);
    if LineNr > -1 then
    begin
      Line := Page.GetString(LineNr);
      StartPos := pos('<b>', Line) + 3;
      Season := copy(Line, StartPos, pos('</b>', Line) - StartPos);
      PickTreeAdd(Season + ':', '');
      Address := AddEpisodeTitles(Page, LineNr);
      if (Address <> '') then Break;
      LineNr := LineNr + 1;
    end;
  until (LineNr < 0);

  if (Address <> '') then
  begin
    AnalyzeEpisodePage(Address);
  end else begin
    if PickTreeExec(Address) then
      AnalyzeEpisodePage(Address);
  end;
  Page.Free;
end;

procedure AnalyzeEpisodePage(Address: string);
var
  Line, TempStr: string;
  BeginPos: Integer;
  Page: TStringList;
  LineNr: Integer;
begin
  Page := TStringList.Create;
  Page.Text := GetPage(Address);
  
  // URL
  SetField(fieldURL, Address);
  
  // Episode Number
  if EpNumber <> '' then
    SetField(fieldTranslatedTitle, EpNumber);

  // Original Title
  LineNr := FindLine('<H1><CENTER>', Page, 0);
  Line := Page.GetString(LineNr);
  CutAfter(Line,'<H1><CENTER>');
  SetField(fieldOriginalTitle, copy(Line, 1, pos('<CENTER></H1>',Line)-1));

  // First Aired
  LineNr := FindLine('<tr><td>First Aired', Page, 0);
  Line := Page.GetString(LineNr);
  CutAfter(Line,'</td><td>');
  SetField(fieldComments, 'First aired: ' + copy(Line, 1, pos('</td></tr>',Line)-1));
  SetField(fieldYear, copy(Line, pos(', ',Line)+2, 4));

  // Rating
  LineNr := FindLine('nowrap>Average Rating:</td>', Page, 0);
  Line := Page.GetString(LineNr);
  TempStr := Line + #13#10;
  CutAfter(Line,'</td><td>');
  SetField(fieldRating,copy(Line, 1, pos('.',Line)-1));
  LineNr := FindLine('nowrap>Votes Cast:</td>', Page, 0);
  Line := Page.GetString(LineNr);
  TempStr := TempStr + Line + #13#10;
  Line := Page.GetString(LineNr + 1);
  TempStr := TempStr + Line + #13#10;
  TempStr := StringReplaceAll(TempStr, '</td><td>', ' ');
  HTMLRemoveTags(TempStr);
  SetField(fieldComments, GetField(fieldComments)
      + #13#10'-------------------------------------------------------------------------------'#13#10
      +'RATINGS:' + #13#10 + TempStr);

  // Production Code
  LineNr := FindLine('<tr><td>Production Code', Page, 0);
  Line := Page.GetString(LineNr);
  CutAfter(Line,'<tr><td>Production Code</td><td>');
  SetField(fieldMedia,'Production Code: ' + copy(Line, 1, pos('</td></tr>',Line)-1));

  // Writer
  LineNr := FindLine('<tr><td>Writer', Page, 0);
  Line := Page.GetString(LineNr);
  CutAfter(Line,'<nobr>');
  TempStr := copy(Line, 1, pos('</nobr><br></td></tr>',Line)-1);
  HTMLRemoveTags(TempStr);
  SetField(fieldProducer, 'Writer: ' + TempStr);

  // Director
  LineNr := FindLine('<tr><td>Director', Page, 0);
  Line := Page.GetString(LineNr);
  CutAfter(Line,'<nobr>');
  TempStr := copy(Line, 1, pos('</nobr><br></td></tr>',Line)-1);
  HTMLRemoveTags(TempStr);
  SetField(fieldDirector, TempStr);

  // Guest Stars
  LineNr := FindLine('<b>Guest Stars:</b>', Page, 0) + 1;
  TempStr := '';
  repeat
    Line := Page.GetString(LineNr);
    TempStr := TempStr + copy(Line, pos('">',Line) + 2, pos('</nobr>',Line)-7) + #13#10;
    LineNr := LineNr + 1;
  until (pos('</td></tr></table>', Line) > 0);
  HTMLRemoveTags(TempStr);
  SetField(fieldActors, TempStr);

  // Description
  LineNr := FindLine('<td class="moduletitle">Synopsis</td>', Page, 0);
  LineNr := FindLine('<table cellpadding=0 class="mainbody"><tr><td>', Page, LineNr) + 1;
  TempStr := '';
  repeat
    Line := Page.GetString(LineNr);
    TempStr := TempStr + Line;
    LineNr := LineNr + 1;
  until (pos('</td></tr></table>', Line) > 0);
  TempStr := StringReplaceAll(TempStr, '<br>', #13#10);
  HTMLRemoveTags(TempStr);
  SetField(fieldDescription, TempStr);

  Page.Free;
  DisplayResults;
end;



function AddEpisodeTitles(Page: TStringList; var LineNr: Integer): string;
var
  Line: string;
  EpTitle, EpAddress, TempNum: string;
  StartPos: Integer;
begin
  Result := '';
  repeat
    LineNr := LineNr + 1;
    Line := Page.GetString(LineNr);
    StartPos := pos('</td><td nowrap class="small"> ', Line);
    if StartPos > 0 then
    begin
      Startpos := Startpos + 31;
      TempNum := copy(Line, StartPos, pos('-', Line) - StartPos);
      EpNumber := 'S';
      If Length(TempNum) < 2 then EpNumber := EpNumber + '0';
      EpNumber := EpNumber + TempNum;
      
      CutAfter(Line,'-');
      TempNum := copy(Line, 1, pos('<', Line) - 1);
      EpNumber := EpNumber + 'E';
      If Length(TempNum) < 2 then EpNumber := EpNumber + '0';
      EpNumber := EpNumber + TempNum;

      CutAfter(Line,'href="');
      EpAddress := copy(Line, 1, pos('">', Line) - 1);
      StartPos := pos('">', Line) + 2;
      EpTitle := copy(Line, StartPos, pos('</a>', Line) - StartPos);
      HTMLDecode(EpTitle);
      if (pos(EpName,EpTitle) > 0) then
      begin
        Result := 'http://www.tvtome.com' + EpAddress;
        Break;
      end;
      PickTreeAdd(EpNumber + ': ' + EpTitle, 'http://www.tvtome.com' + EpAddress);
    end;
    if Result <> '' then Break;
  until ((pos('<tr><td colspan="6"> </td></tr>', Line) > 0) or (pos('</div>', Line) > 0));
end;

procedure CutAfter(var Str: string; Pattern: string);
begin
  Str := Copy(str, Pos(Pattern, Str) + Length(Pattern), Length(Str));
end;

function StringReplaceAll(S, Old, New: string): string;
begin
  while Pos(Old, S) > 0 do
    S := StringReplace(S, Old, New);
  Result := S;
end;

begin
  if CheckVersion(3,4,0) then
  begin
    if (GetField(fieldURL) <> '') then
    begin
      AnalyzeEpisodePage(GetField(fieldURL));
    end else begin
      EpName := GetField(fieldOriginalTitle);
      if (EpName = '') then
        EpName := GetField(fieldTranslatedTitle);
      if (EpName = '') then
        if Input('TV Tome Import', 'Enter the title of the episode:', EpName) then
          AnalyzePage('http://www.tvtome.com/tvtome/servlet/EpisodeGuideServlet/showid-249/'); // Futurama
          //AnalyzePage('http://www.tvtome.com/Simpsons/eplist.html'); // The Simpsons
          //AnalyzePage('http://www.tvtome.com/tvtome/servlet/EpisodeGuideServlet/showid-5382/'); // Garfield
    end;
  end else
    ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
end.
By the way, when the program executes "EpName := Input('TV Tome Import', 'Enter the title of the episode:', EpName);" it gives me a Type Mismatch error, but EpName is a string, like it's supposed to be. Any ideas? (This only applies if you run the script on an episode with no titles or URL) Fixed!

Posted: 2003-08-19 23:08:41
by antp
Thanks
And you are right, Futurama is the best ;)

The error you get is because Input returns a boolean, and it modifies the contents of the string variable that is passed in parameters.
So you have to do :
if Input(.....) then
begin
....
end;

Posted: 2003-08-20 11:07:00
by Apollyon
Thanks, that solves it. I've corrected the script already.

By the way, do you know of a good resource (preferably a reference book) on programing Object Pascal? I've been looking arround, but I can't seem to find anything good, and I'm kind of programming in the dark. If I had such a reference, I would have caught that stupid bug. By the way, you'll want to correct the IMDB Batch import script, that's where I got that line of code.

Also, this batch script is very inefficient. For every futurama episode, I download the episode list to find the URL, and then proccess the episode's page. I'd rather download the episode list once, and then look for the URLs for each episode. Is this possible?

Posted: 2003-08-20 12:51:30
by antp
I do not know where to find reference, I learned that at school and then by myself later :/

If you want to put one episode per "movie" item, it is not yet possible to download the list only one, unless you use a temp "cache" file like the Startrek script does.

Posted: 2003-09-02 19:28:34
by Apollyon
I just noticed that the version of my script distributed in the latest update is buggy. The one at the begining of this thread is slightly less so. :)

Posted: 2003-09-02 19:46:59
by antp
ok