New Script for www.startrek.com

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
trekkie

New Script for www.startrek.com

Post by trekkie »

Well, nobody answered my previous request , so i took the challenge and wrote the script myself.

some notes:

1. You should choose only one template for the Episode Title . Uncomment
The one that suites you at the begining of the script (see
TitleTemplate )

2. Some Info does not have a corresponding field so i've put it in the
comments field. Use ImportComments constant to control
using it.

3. If your episode title is unspecific ( for example : DS9 - Emmisary)
The scripts will present you with "Movie selection window" to choose
The specific episode. ( It will eventually correct the originalTitle field
with your choice )

Here it Is

Code: Select all

// GETINFO SCRIPTING 
// Paramount StarTrek.Com import Episode Info With picture

(*************************************************** 
*  Episode Information importation script for:     *
*      StarTrek(US), http://www.startrek.Com       *
*                                                  * 
*  (c) 2002 Antoine Potten    antoine@buypin.com   * 
*  Improvements made by Trekkie asimov@hotmail.com * 
*                                                  *
*  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 StarTrek;


const
   ImportLongDescription = True;   // True - All Episode Description. 
                                   // False - Only Short Synopsis
   ImportLargePicture  =   True;   // True - A Larger Pictures of the Episodes Will Be Imported
                                   // False - A Small Thumbnail Picture Will be Imported
   StorePictureInDatabase = True;  // False - Will Store the Picture Externally

   ImportComments = True;          // Comment Will Contain : Writers & Teleplay Info,Production
                                   // Number & Stardate

   ImportGuestCast = True;         // Add Info on Guest Cast to Actors field

  // Use Only ONE of the following Episode Titles Templetes:  

   TitleTemplate = 'Series - Episode Nr -  Title';   // Example: Star Trek TOS - 1x01 - The Cage
// TitleTemplate = 'Title -  Episode Nr -  Series';  // Example: The Cage - 1x01 - Original Series
// TitleTemplate = 'Title -  Series';                // Example: The Cage - TOS
// TitleTemplate = 'Series - Title';                 // Example: DS9 - Dax

var 
  EpisodeTitle: string; 
  TheEpisodesListing: string; 
  PickTreeSelected:Boolean;

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 
  begin
    if Pos(Pattern, List.GetString(i)) <> 0 then 
    begin 
      result := i; 
      Break; 
    end; 
  end;
end; 

Function GetEpisodeAddress(Line:String):String;
var
  BeginPos,EndPos:Integer;
Begin
   Result:='';
   BeginPos:= Pos('a href=',Line);
   BeginPos:= BeginPos +8;
   EndPos:= Pos('">',Line);
   Result:=Copy(Line,BeginPos, EndPos-BeginPos);
End;

procedure AnalyzePage(Address: string); 
var 
  Page: TStringList; 
  LineNr,TitleLineNr: Integer; 
  BeginPos,EndPos:Integer;
  Line:String;
  Title,EpisodeAddress:String;

begin 
  Page := TStringList.Create; 
  Page.Text := GetPage(Address); 

  // Find The Selected Episode in The Whole Episode Listings
  LineNr := FindLine('<TABLE STYLE=',Page,0);
  if LineNr > 0 Then
  begin
    Line :=Page.GetString(LineNr);
    TitleLineNr := FindLine(EpisodeTitle, Page, LineNr);
    if TitlelineNr > -1 then
    begin

       // Title    
       Line :=Page.GetString(TitleLineNr);
       EndPos:=Pos('</A>',Line);
       Title:=Copy(Line,1,Length(Line) - EndPos);

       // Address
       Line :=Page.GetString(TitleLineNr-1);
       EpisodeAddress:=GetEpisodeAddress(Line);


       Address:='http://www.startrek.com' + EpisodeAddress;

       // Find Next Matching Titles
       LineNr:= FindLine(EpisodeTitle,Page,TitleLineNr+1);
       if LineNr > -1 then
       begin
          PickTreeClear;
          PickTreeAdd('Matching Episode Titles', ''); 
          PickTreeAdd( Title, 'http://www.startrek.com' + EpisodeAddress);

          AddEpisodesTitles(Page,LineNr);
  
          PickTreeSelected:=PickTreeExec(Address);

       End;

      
       HTMLDecode(Address);
       SetField(fieldURL, Address); 

       Page.Free;
       Page:=TStringList.Create();
       Page.Text := GetPage(Address); 

       // Handle Page redirection
       LineNr:= FindLine('window.location.replace',Page,0);
       if LineNr > -1 then
       begin
         Line:=Page.GetString(LineNr);
         BeginPos:= Pos('replace',Line) + 9;
         EndPos := Pos(')',Line) -1;
         Address:=Copy(Line,BeginPos, EndPos-BeginPos);
         Address:='http://www.startrek.com' + Address;

         Page.Free;
         Page:=TStringList.Create();
         Page.Text := GetPage(Address); 
       
       
       End;
      
       AnalyzeEpisodePage(Page);
    end
    else
      Showmessage(' Episode ' + EpisodeTitle + ' Not Found');
  end 
  else
      ShowMessage('Error In Page for ' + EpisodeTitle);

  Page.Free; 
end; 

procedure AddEpisodesTitles(Page: TStringList; var LineNr: Integer); 
var 
  Line,Title,Address: string; 
  EndPos: Integer; 
begin 
  While (LineNr > -1) do
  begin
    Line := Page.GetString(LineNr);

   // Title    
    Line :=Page.GetString(LineNr);
    EndPos:=Pos('</A>',Line);
    Title:=Copy(Line,1,EndPos-1);

    // Address
    Line :=Page.GetString(LineNr-1);
    Address:=GetEpisodeAddress(Line);
  
    PickTreeAdd(Title, 'http://www.startrek.com' + Address); 

    LineNr:= FindLine(EpisodeTitle,Page,LineNr+1);

  End;
 
end; 



function FindValue(BeginTag,EndTag:String;                                       Page:TStringList; var LineNr:Integer):String;
var
 BeginPos,EndPos:Integer;
 Line,Value:String;
 A:Char;
begin
  Value:='';
  Line := Page.GetString(LineNr); 

  BeginPos := pos(BeginTag, Line); 
  if BeginPos > 0 then 
       BeginPos := BeginPos + Length(BeginTag); 

  if BeginTag = EndTag then
  begin
     Delete(Line,1,BeginPos);
     BeginPos:=1;
  End;

  EndPos := pos(EndTag, Line); 

  While (EndPos = 0) do    
  begin
       Value := Value + copy(Line, BeginPos, Length(Line) - BeginPos); 
       // Next Lines
       BeginPos:= 0;
       LineNr:= LineNr+1;
       Line:= Page.GetString(LineNr);
       EndPos := Pos(EndTag,Line);
     
  End;
  Value:= Value + copy(Line, BeginPos, EndPos - BeginPos);

  Result:=Value;
End;



procedure AnalyzeEpisodePage(Page: TStringList); 
var 
  Line, Value, Value2, FullValue: string; 
  Production,StarDate,Year,PictureAddress:String;
  LineNr,DirectorLine,StartLine: Integer; 
  BeginPos, EndPos: Integer; 
  PicturePage:TStringList;
begin

  // Original Title & Year , Production Nr & Star Date
  StartLine := FindLine('Begin Episodes List', Page, 0);
  LineNr := FindLine(EpisodeTitle, Page, StartLine + 1);
  Line := Page.GetString(LineNr); 
  
  if LineNr > -1 then
  begin 
    Value:=FindValue('<B>','</B>',Page,LineNr);

    HTMLDecode(Value); 
    SetField(fieldTranslatedTitle, Value); 

    if PickTreeSelected Then
    Begin
      Value2:=Getfield(fieldOriginalTitle);
      FullValue:= StringReplace(Value2,EpisodeTitle,Value);

      SetField(fieldOriginalTitle,FullValue);

    End;

    Line := Page.GetString(LineNr); 

    Year:='';

    // Concatenate Next 2 Lines
    BeginPos := Pos(EpisodeTitle,Line);
    Delete(Line,1,BeginPos);
    Value:=Line;
    Line:=Page.GetString(LineNr+1);
    Value:=Value + Line;
    HTMLRemoveTags(Value);
  
    BeginPos:= Pos('Production',Value);
    if BeginPos > 0 then
    Begin
     Production:= Copy(Value,BeginPos,14);

     EndPos :=  Pos('Stardate',Value);
     StarDate := copy(Value,EndPos,Length(Value) - EndPos);
 
     Year:=Copy(Value,BeginPos + 14,EndPos -BeginPos -14);

     EndPos:=Length(Year);
     Year:=Copy(Year,EndPos-3,4);
  
    End;
    
    SetField(fieldYear, Year); 
  end
  else
    ShowMessage('Could Not Find Episode Title');

 
  // Picture 
  if not ImportLargePicture then
  begin
     LineNr := FindLine('<IMG SRC=/content/Photo/', Page, StartLine); 
     if LineNr > -1 then 
     begin 
       Line := Page.GetString(LineNr); 
       BeginPos := pos('Photo', Line) + 5; 
       Delete(Line, 1, BeginPos); 
       EndPos := pos('ALIGN', Line) -1;
       Value := copy(Line, 1, EndPos); 

       PictureAddress:='http://www.startrek.com/content/Photo/'+ Value;
       GetPicture(PictureAddress, not StorePictureInDatabase); // False = do not store picture  
          // externally ; store it in the catalog file 

     end; 
   end
   else
   Begin
     LineNr := FindLine('/art/tinyicons/PHOTO.gif', Page, StartLine);
     if LineNr > -1 then
     begin 
         Line:=Page.GetString(LineNr-2);


         BeginPos:=Pos('href="',Line) + 6;
         EndPos:=Pos('">',Line);
         Value:= Copy(Line,BeginPos,EndPos-BeginPos);
         PictureAddress:='http://www.startrek.com'+ Value;

         PicturePage:=TStringList.Create();
         PicturePage.Text := GetPage(PictureAddress);
         LineNr:=FindLine('/content/PHOTO/',PicturePage,0);
         if LineNr > -1 then
         Begin
             Line:=PicturePage.GetString(LineNr);
             BeginPos:=Pos('PHOTO',Line) + 5;
             EndPos:= Pos('">',Line);
             Value:= Copy(Line,BeginPos,EndPos-BeginPos);
             PictureAddress:='http://www.startrek.com/content/PHOTO'+ Value;
       
             GetPicture(PictureAddress, not StorePictureInDatabase); // False = do not store 
                                                // externally ; store it in the catalog file

         End
         PicturePage.Free;
     
     End;

   End;

  // Director 
  DirectorLine:=0;
  LineNr := FindLine('Director:', Page, StartLine); 
  if LineNr > -1 then 
  begin 
    DirectorLine:=LineNr;
    Value:=FindValue('<B>','</B>',Page,LineNr);
    HTMLRemoveTags(Value);

    HTMLDecode(Value); 
    SetField(fieldDirector, Value); 
  end
  else
    ShowMessage('Could Not Find Director Paragraph'); 



  // Actors 
  LineNr := FindLine('Cast:', Page, StartLine); 
  if LineNr > -1 then 
  begin 
    FullValue := ''; 
    LineNr:=LineNr+1;
    Line := Page.GetString(LineNr);  

    Value2:='Guest Cast';
    if ImportGuestCast then
       Value2:='Creative staff';

    While (Pos(Value2,Line) = 0) do
    begin 

      HTMLRemoveTags(Line);
      if (FullValue <> '') and (Length(Line) > 1 ) then 
            FullValue := FullValue + ', '; 
      FullValue := FullValue + Line; 

      LineNr:=LineNr+1;   
      Line := Page.GetString(LineNr);  
          
    end;
    
  
    HTMLDecode(FullValue); 
    SetField(fieldActors, FullValue); 
  end
  else
    ShowMessage('Could Not Find Cast Paragraph'); 

 

  //Country 
  Value := 'United States';
  SetField(fieldCountry, Value); 

  //Category 
  Value := 'Science Fiction';
  SetField(fieldCategory, Value); 

  // Language 
  Value := 'English';
  SetField(fieldLanguages, Value); 

  // Producer
  Value := 'Paramount Pictures';
  SetField(fieldProducer, Value); 

  //Description 
  LineNr := FindLine('Synopsis:', Page, StartLine); 
  if LineNr > -1 then 
  begin 
    LineNr:=LineNr +1;
    
    if ImportLongDescription Then
    begin
       FullValue := ''; 
       Line := Page.GetString(LineNr); 
       While ( Pos('Related Links',Line) = 0 ) do
       begin

          HTMLRemoveTags(Line);
          Line:=StringReplace(Line,#11,' ');
          Line:=StringReplace(Line,'&#151',' ');

          FullValue := FullValue + Line;
          LineNr:=LineNr +1;
          Line := Page.GetString(LineNr); 

       End;
     
     
    end
    else
       FullValue:=FindValue('<B>','</B>',Page,LineNr);

    HTMLRemoveTags(FullValue);

    SetField(fieldDescription, FullValue); 
  end; 


  // Comments 
  if ImportComments then
  begin
        FullValue := ''; 
        LineNr :=DirectorLine +1;
        Line:= Page.GetString(LineNr);
        While(Pos('p class',Line) = 0 ) do
        begin
            
            HTMLRemoveTags(Line);

            if FullValue <> '' then 
                FullValue := FullValue + #13#10; 

            FullValue := FullValue + Line;
            LineNr := LineNr + 1; 
            Line := Page.GetString(LineNr); 
         
         End;   
      
         FullValue:=FullValue + #13#10 + Production + ' ' + Stardate;

         SetField(fieldComments, FullValue); 
    
   End;

  
   DisplayResults; 
end; 

var
  BeginPos,EndPos,Len:Integer;
  Template:String;
begin

  if CheckVersion(3,4,0) then 
  begin 
    TheEpisodesListing:=''; 
    EpisodeTitle := GetField(fieldOriginalTitle); 
    if EpisodeTitle = '' then 
      EpisodeTitle := GetField(fieldTranslatedTitle); 
      
    if (Pos('TOS',EpisodeTitle) > 0) or (Pos('Original Series',EpisodeTitle) > 0 ) then
      TheEpisodesListing:= 'http://www.startrek.com/library/episodes_TOS.asp';

    if (Pos('DS9',EpisodeTitle) > 0) or (Pos('Deep Space',EpisodeTitle) > 0 ) then
      TheEpisodesListing:= 'http://www.startrek.com/library/episodes_DS9.asp';

    if (Pos('TNG',EpisodeTitle) > 0) or ( Pos('Next Generation',EpisodeTitle) > 0 ) then
      TheEpisodesListing:= 'http://www.startrek.com/library/episodes_TNG.asp';

    if ( Pos('VOY',EpisodeTitle) > 0) or (Pos('Voyager',EpisodeTitle) > 0 ) then
      TheEpisodesListing:= 'http://www.startrek.com/library/episodes_voy.asp';

    
    // Extract Episode Title

    Template:= TitleTemplate;

    BeginPos:=Pos('-',EpisodeTitle);
    Len:= Length(EpisodeTitle);
    if Template = 'Series - Episode Nr -  Title' then
    Begin
   
       Delete(EpisodeTitle, 1, BeginPos);
       BeginPos:=Pos('-',EpisodeTitle);
       Delete(EpisodeTitle, 1, BeginPos);
    End
    Else if Template = 'Title -  Series' Then
    Begin
        BeginPos:=Pos('-',EpisodeTitle);
        Delete(EpisodeTitle, BeginPos, Len);
    End
    Else if Template = 'Title -  Episode Nr -  Series' Then
    Begin
       BeginPos:=Pos('-',EpisodeTitle);
       Delete(EpisodeTitle, BeginPos, Len);

    End
    Else if Template = 'Series - Title' then
    Begin
        Delete(EpisodeTitle, 1, BeginPos);
    End
    Else
    Begin
       ShowMessage('Episode Title does not match the template: ' + Template);
       Input('Star Trek Episode Guide Import', 'Enter Episode Title', EpisodeTitle);
    End;

    EpisodeTitle := Trim(EpisodeTitle);
    EpisodeTitle := AnsiMixedCase(EpisodeTitle,' ');

    if TheEpisodesListing = '' then
    begin
       Input('Star Trek Episode Guide Import', 'Enter the Address of the Episodes Listing:', TheEpisodesListing);
    end;
    AnalyzePage(TheEpisodesListing);

  end
  else 
     ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');

end. 
[/list]
antp
Site Admin
Posts: 9651
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Nice, thanks, I'll add it to the scripts included with the program if you want :)
But you could remove my name from the "(C)" since you wrote the script alone ;)
trekkie

New Script for www.startrek.com

Post by trekkie »

Sure you can add it to the program scripts :cool:
I thought you wrote the original script so you can credit whoever wrote it :)

I made some improvements to the previous script as follows:

1. Added better handling of the episode title ( handling character case)

2. Added option of a verbose mode ( display/ignore error windows).
see Verbosemode constant.

3. In case you completely mis-spelled the episode title a window
displaying the WHOLE series episode titles can appear allowing you
to choose the correct title . see OnErrorWholeList constant.

BTW
I wish you could allow changing the window title in calling
PickTreeExec

4. Added option to use a local cache file in order to offload the server.
see CacheMode constant.

This allows a kind of "Batch mode" operation on several catalog entries

NOTE:
In order for this option to work you MUST (at least at FIRST use
of this option ) have an EMPTY file called "StarTrekScript_Cache.txt'
residing in the \Scripts Sub-Directory

BTW
Is there a way to check file existanse within the Script ?

Here is The Code:

Code: Select all

// GETINFO SCRIPTING 
// Paramount StarTrek.Com import Episode Info With picture

(*************************************************** 
*  Episode Information importation script for:     *
*      StarTrek(US), http://www.startrek.Com       *
*                                                  * 
*  (c) 2002 Antoine Potten    antoine@buypin.com   * 
*        Written by Trekkie   asimov@hotmail.com   * 
*                                                  *
*  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 StarTrek;


const
   ImportLongDescription = True;   // True - All Episode Description. 
                                   // False - Only Short Synopsis
   ImportLargePicture  =   True;   // True - A Larger Pictures of the Episodes Will Be Imported
                                   // False - A Small Thumbnail Picture Will be Imported
   StorePictureInDatabase = True;  // False - Will Store the Picture Externally

   ImportComments = True;          // Comment Will Contain : Writers & Teleplay Info,Production
                                   // Number & Stardate

   ImportGuestCast = True;         // Add Info on Guest Cast to Actors field

   OnErrorWholeList = True;       // If Episode Title not found
                                  // display a list of ALL episodes to choose from

   VerboseMode = True;            // True - Will Display Error messages 



  // Use Only ONE of the following Episode Titles Templetes:  


   TitleTemplate = 'Series - Episode Nr -  Title';   // Example: Star Trek TOS - 1x01 - The Cage
// TitleTemplate = 'Title -  Episode Nr -  Series';  // Example: The Cage - 1x01 - Original Series
// TitleTemplate = 'Title -  Series';                // Example: The Cage - TOS
// TitleTemplate = 'Series - Title';                 // Example: DS9 - Dax


  // If CacheMode is TRUE you should verify a file 'StarTrekScript_Cache.txt'
  // Exists in the Scripts Directory Under Ant Movie Catalog Install Directory
  // If not you MUST (at First Run ) create an EMPTY File and put it in that directory.

   CacheMode   = True;            // True - Will Store A Series Listing in File and
                                  // Use it to offload server queries
                                  // False - Will Query Series Episode listing for every entry

var 
  EpisodeTitle,OriginalTitle: string; 
  TheEpisodesListing: string; 
  PickTreeSelected,TitleCorrected,CacheExists:Boolean;
  CacheFile:TStringList;


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 
  begin
    if Pos(Pattern, List.GetString(i)) <> 0 then 
    begin 
      result := i; 
      Break; 
    end; 
  end;
end; 

Function GetEpisodeAddress(Line:String):String;
var
  BeginPos,EndPos:Integer;
Begin
   Result:='';
   BeginPos:= Pos('a href=',Line);
   BeginPos:= BeginPos +8;
   EndPos:= Pos('">',Line);
   Result:=Copy(Line,BeginPos, EndPos-BeginPos);
End;

procedure AnalyzePage(Address: string); 
var 
  Page: TStringList; 
  LineNr,TitleLineNr: Integer; 
  BeginPos,EndPos:Integer;
  Line:String;
  Title,EpisodeAddress:String;

begin 
  Page := TStringList.Create; 
  if CacheMode Then
  begin
    if not CacheExists then
    begin
       Page.Text := GetPage(Address);
       CacheFile.Free;
       CacheFile:=TStringList.Create;
       CacheFile.Add(Address);
       For LineNr:=0 to Page.Count -1 do
       Begin
          CacheFile.Add(Page.GetString(LineNr));
       End;
    end
    else
       For LineNr:=0 to CacheFile.Count -1 do
       Begin
          Page.Add(CacheFile.GetString(LineNr));
       End;

  end
  else
      Page.Text := GetPage(Address);

  // Find The Selected Episode in The Whole Episode Listings
  LineNr := FindLine('<TABLE STYLE=',Page,0);
  if LineNr > 0 Then
  begin
    Line :=Page.GetString(LineNr);
    TitleLineNr := FindLine(EpisodeTitle, Page, LineNr);
    if TitlelineNr > -1 then
    begin

       // Title    
       Line :=Page.GetString(TitleLineNr);
       EndPos:=Pos('</A>',Line);
       Title:=Copy(Line,1,EndPos-1);

       // Address
       Line :=Page.GetString(TitleLineNr-1);
       EpisodeAddress:=GetEpisodeAddress(Line);


       Address:='http://www.startrek.com' + EpisodeAddress;

       EndPos:=Pos('Part I',EpisodeTitle);
       if (EndPos <> 0) and (Length(EpisodeTitle) > 7) then
          Title:=Title + ' ';

       // Find Next Matching Titles
       LineNr:= FindLine(Title,Page,TitleLineNr+1);
       if LineNr > -1 then
       begin
          PickTreeClear;
          PickTreeAdd('Matching Episode Titles for: ' + EpisodeTitle, '');
          PickTreeAdd( Title, 'http://www.startrek.com' + EpisodeAddress);
          AddEpisodesTitles(Page,LineNr);
          PickTreeSelected:=PickTreeExec(Address);
          if Not PickTreeSelected Then
              Exit;

       End;

    End
    Else
    Begin
          if OnErrorWholeList  then
          Begin
            PickTreeClear;
            PickTreeAdd('Select A Title Closest to: '+ EpisodeTitle, '');
            DisplayAllTitles(Page,LineNr);
            PickTreeSelected:=PickTreeExec(Address);
            if Not PickTreeSelected Then
              Exit;
          End
          else
          begin
              if VerboseMode then
                 Showmessage(' Episode ' + EpisodeTitle + ' Not Found');
              Exit;
          End;


    End;

      
    HTMLDecode(Address);
    SetField(fieldURL, Address); 

    
    Page.Free;
    Page:=TStringList.Create();
    Page.Text := GetPage(Address); 

    // Handle Page redirection
    LineNr:= FindLine('window.location.replace',Page,0);
    if LineNr > -1 then
    begin
      Line:=Page.GetString(LineNr);
      BeginPos:= Pos('replace',Line) + 9;
      EndPos := Pos(')',Line) -1;
      Address:=Copy(Line,BeginPos, EndPos-BeginPos);
      Address:='http://www.startrek.com' + Address;

      Page.Free;
      Page:=TStringList.Create();
      Page.Text := GetPage(Address); 
         
         
    End;
      
    AnalyzeEpisodePage(Page);
    
  
  end 
  else
     if VerboseMode then
       ShowMessage('Error In Page for ' + EpisodeTitle);

  Page.Free; 
end; 


procedure DisplayAllTitles(Page: TStringList; var LineNr: Integer);
var 
  Line,Title,Address,Season: string; 
  BeginPos,EndPos,EndLine: Integer; 
begin 

  LineNr:=LineNr +1; 
  EndLine :=  FindLine('</HTML>',Page,LineNr);

  Line := Page.GetString(LineNr);  

  While (LineNr < EndLine) do
  begin

         BeginPos:=Pos('SEASON',Line);
         if BeginPos <> 0 Then
         Begin
               EndPos:=Pos('</b>',Line);
               Season:=Copy(Line,BeginPos,EndPos-BeginPos);
               PickTreeAdd(Season,'');
         End;

         EndPos:= Pos('</A></TD>',Line);

         if EndPos <> 0 then
         Begin
              	 // Title    
         	 Title:=Copy(Line,1,EndPos-1);

          	 // Address
        	 Line :=Page.GetString(LineNr-1);
	         Address:=GetEpisodeAddress(Line);
  
         	 PickTreeAdd(Title, 'http://www.startrek.com' + Address); 
               
         End;

	
         LineNr:=LineNr+1;

         Line := Page.GetString(LineNr);

  End; //while
 
end; 


procedure AddEpisodesTitles(Page: TStringList; var LineNr: Integer); 
var 
  Line,Title,Address: string; 
  EndPos: Integer; 
begin 
  While (LineNr > -1) do
  begin
    Line := Page.GetString(LineNr);

   // Title    
    Line :=Page.GetString(LineNr);
    EndPos:=Pos('</A>',Line);
    Title:=Copy(Line,1,EndPos-1);

    // Address
    Line :=Page.GetString(LineNr-1);
    Address:=GetEpisodeAddress(Line);
  
    PickTreeAdd(Title, 'http://www.startrek.com' + Address); 

    LineNr:= FindLine(EpisodeTitle,Page,LineNr+1);

  End;
 
end; 

function FindValue(BeginTag,EndTag:String;                                       Page:TStringList; var LineNr:Integer):String;
var
 BeginPos,EndPos:Integer;
 Line,Value:String;
 A:Char;
begin
  Value:='';
  BeginPos:=0;
 
  Repeat

     Line := Page.GetString(LineNr); 
     BeginPos := pos(BeginTag, Line); 
     LineNr:=LineNr+1;
    
  Until ((BeginPos <> 0) or (LineNr > Page.Count-1 ));

  if BeginPos > 0 then 
  begin
       BeginPos := BeginPos + Length(BeginTag); 
       LineNr:=LineNr - 1;
  end;

 
  if BeginTag = EndTag then
  begin
     Delete(Line,1,BeginPos);
     BeginPos:=1;
  End;

  EndPos := pos(EndTag, Line); 

  While ((EndPos = 0) and (LineNr < Page.Count-1 ) ) do    
  begin
       Value := Value + copy(Line, BeginPos, Length(Line) - BeginPos); 
       // Next Lines
       BeginPos:= 0;
       LineNr:= LineNr+1;
       Line:= Page.GetString(LineNr);
       EndPos := Pos(EndTag,Line);
     
  End;
  Value:= Value + copy(Line, BeginPos, EndPos - BeginPos);

  Result:=Value;
End;






procedure AnalyzeEpisodePage(Page: TStringList); 
var 
  Line, Value, Value2, FullValue: string; 
  Production,StarDate,Year,PictureAddress:String;
  LineNr,DirectorLine,StartLine: Integer; 
  BeginPos, EndPos: Integer; 
  PicturePage:TStringList;
begin


// Original Title & Year , Production Nr & Star Date
  StartLine := FindLine('Begin Episodes List', Page, 0);

  LineNr:=StartLine+1;
  Line := Page.GetString(LineNr); 
  
  Value:=FindValue('<B>','</B>',Page,LineNr);

  if Value <> '' Then
  begin

    HTMLDecode(Value); 
   
    SetField(fieldTranslatedTitle, Value); 

    if PickTreeSelected or TitleCorrected Then
    Begin

      Value2:=Getfield(fieldOriginalTitle);
      FullValue:= StringReplace(Value2,OriginalTitle,Value);
      SetField(fieldOriginalTitle,FullValue);
     
    End;

    Line := Page.GetString(LineNr); 

    Year:='';

    // Concatenate Next 2 Lines
    BeginPos := Pos(EpisodeTitle,Line);
    Delete(Line,1,BeginPos);
    Value:=Line;
    Line:=Page.GetString(LineNr+1);
    Value:=Value + Line;
    HTMLRemoveTags(Value);
  
    BeginPos:= Pos('Production',Value);
    if BeginPos > 0 then
    Begin
       Production:= Copy(Value,BeginPos,14);
   
       EndPos :=  Pos('Stardate',Value);
       StarDate := copy(Value,EndPos,Length(Value) - EndPos);
  
       Year:=Copy(Value,BeginPos + 14,EndPos -BeginPos -14);
    
       EndPos:=Length(Year);
       Year:=Copy(Year,EndPos-3,4);
    
    End;
    
    SetField(fieldYear, Year); 
  end
  else
  begin
      if VerboseMode then
         ShowMessage('Could Not Find Episode Title')
      exit;
  end;

 
 
  // Picture 
  if not ImportLargePicture then
  begin
       LineNr := FindLine('<IMG SRC=/content/Photo/', Page, StartLine); 
       if LineNr > -1 then 
       begin 
         Line := Page.GetString(LineNr); 
         BeginPos := pos('Photo', Line) + 5; 
         Delete(Line, 1, BeginPos); 
         EndPos := pos('ALIGN', Line) -1;
         Value := copy(Line, 1, EndPos); 

         PictureAddress:='http://www.startrek.com/content/Photo/'+ Value;
         GetPicture(PictureAddress, not StorePictureInDatabase); // False = do not store picture          // externally ; store it in the catalog file 

       end; 
   end
   else
   Begin
        LineNr := FindLine('/art/tinyicons/PHOTO.gif', Page, StartLine);
        if LineNr > -1 then
        begin 
          Line:=Page.GetString(LineNr-2);


          BeginPos:=Pos('href="',Line) + 6;
          EndPos:=Pos('">',Line);
          Value:= Copy(Line,BeginPos,EndPos-BeginPos);
          PictureAddress:='http://www.startrek.com'+ Value;

          PicturePage:=TStringList.Create();
          PicturePage.Text := GetPage(PictureAddress);
          LineNr:=FindLine('/content/PHOTO/',PicturePage,0);
          if LineNr > -1 then
          Begin
             Line:=PicturePage.GetString(LineNr);
             BeginPos:=Pos('PHOTO',Line) + 5;
             EndPos:= Pos('">',Line);
             Value:= Copy(Line,BeginPos,EndPos-BeginPos);
             PictureAddress:='http://www.startrek.com/content/PHOTO'+ Value;
       
             GetPicture(PictureAddress, not StorePictureInDatabase); // False = do not store 
                                                // externally ; store it in the catalog file

          End
          PicturePage.Free;
     
        End;
  End;

  // Director 
  DirectorLine:=0;
  LineNr := FindLine('Director:', Page, StartLine); 
  if LineNr > -1 then 
  begin 
    DirectorLine:=LineNr;
    Value:=FindValue('<B>','</B>',Page,LineNr);
    HTMLRemoveTags(Value);

    HTMLDecode(Value); 
    SetField(fieldDirector, Value); 
  end
  else
    if VerboseMode then
       ShowMessage('Could Not Find Director Paragraph'); 



  // Actors 
  LineNr := FindLine('Cast:', Page, StartLine); 
  if LineNr > -1 then 
  begin 
    FullValue := ''; 
    LineNr:=LineNr+1;
    Line := Page.GetString(LineNr);  

    Value2:='Guest Cast';
    if ImportGuestCast then
       Value2:='Creative staff';

    While (Pos(Value2,Line) = 0) do
    begin 

      HTMLRemoveTags(Line);
      if (FullValue <> '') and (Length(Line) > 1 ) then 
            FullValue := FullValue + ', '; 
      FullValue := FullValue + Line; 

      LineNr:=LineNr+1;   
      Line := Page.GetString(LineNr);  
          
    end;
    
  
    HTMLDecode(FullValue); 
    SetField(fieldActors, FullValue); 
  end
  else
    if VerboseMode then
        ShowMessage('Could Not Find Cast Paragraph'); 

 

  //Country 
  Value := 'United States';
  SetField(fieldCountry, Value); 

  //Category 
  Value := 'Science Fiction';
  SetField(fieldCategory, Value); 

  // Language 
  Value := 'English';
  SetField(fieldLanguages, Value); 

  // Producer
  Value := 'Paramount Pictures';
  SetField(fieldProducer, Value); 

  //Description 
  LineNr := FindLine('Synopsis:', Page, StartLine); 
  if LineNr > -1 then 
  begin 
    LineNr:=LineNr +1;
    
    if ImportLongDescription Then
    begin
       FullValue := ''; 
       Line := Page.GetString(LineNr); 
       While ( Pos('Related Links',Line) = 0 ) do
       begin

          HTMLRemoveTags(Line);
          Line:=StringReplace(Line,#11,' ');
          Line:=StringReplace(Line,'&#151',' ');

          FullValue := FullValue + Line;
          LineNr:=LineNr +1;
          Line := Page.GetString(LineNr); 

       End;
     
     
    end
    else
       FullValue:=FindValue('<B>','</B>',Page,LineNr);

    HTMLRemoveTags(FullValue);

    SetField(fieldDescription, FullValue); 
  end; 


  // Comments 
  if ImportComments then
  begin
        FullValue := ''; 
        LineNr :=DirectorLine +1;
        Line:= Page.GetString(LineNr);
        While(Pos('p class',Line) = 0 ) do
        begin
            
            HTMLRemoveTags(Line);

            if FullValue <> '' then 
                FullValue := FullValue + #13#10; 

            FullValue := FullValue + Line;
            LineNr := LineNr + 1; 
            Line := Page.GetString(LineNr); 
         
         End;   
      
         FullValue:=FullValue + #13#10 + Production + ' ' + Stardate;

         SetField(fieldComments, FullValue); 
    
   End;

  
   DisplayResults; 
end; 

function CorrectEpisodeTitle (Title:string):string;
var Words:Array of string;
    Replace,Original:String;
    Index:Integer;
begin
   Title := AnsiMixedCase(Title,' ');
   TitleCorrected:=False;

   Setarraylength(Words,10);
   Words[0]:=' In ';
   Words[1]:=' On ';
   Words[2]:=' Of ';
   Words[3]:=' As ';
   Words[4]:=' The ';
   Words[5]:=' At ';
   Words[6]:=' And ';
   Words[7]:=' A ';
   Words[8]:=' An ';
   Words[9]:=' To ';
   for Index:=0 to 9 do
   begin
     if Pos(Words[Index],Title) <> 0 then
     begin
       Original:=Words[Index];
       Replace:=AnsiLowerCase(Original);
       Title:=StringReplace(Title,Original,Replace);
       TitleCorrected:=True;
     End;
   End;
   
   Result:=Title;
End;

var
  BeginPos,EndPos,Len:Integer;
  Template:String;
begin

  if CheckVersion(3,4,0) then 
  begin 
    TheEpisodesListing:=''; 

    if CacheMode Then
    begin
       CacheFile:=TStringList.Create;
       CacheFile.LoadFromFile('StarTrekScript_Cache.txt')
    End;

    EpisodeTitle := GetField(fieldOriginalTitle); 
    if EpisodeTitle = '' then 
       EpisodeTitle := GetField(fieldTranslatedTitle); 

    
      
    if (Pos('TOS',EpisodeTitle) > 0) or (Pos('Original Series',EpisodeTitle) > 0 ) then
      TheEpisodesListing:= 'http://www.startrek.com/library/episodes_TOS.asp';

    if (Pos('DS9',EpisodeTitle) > 0) or (Pos('Deep Space',EpisodeTitle) > 0 ) then
      TheEpisodesListing:= 'http://www.startrek.com/library/episodes_DS9.asp';

    if (Pos('TNG',EpisodeTitle) > 0) or ( Pos('Next Generation',EpisodeTitle) > 0 ) then
      TheEpisodesListing:= 'http://www.startrek.com/library/episodes_TNG.asp';

    if ( Pos('VOY',EpisodeTitle) > 0) or (Pos('Voyager',EpisodeTitle) > 0 ) then
      TheEpisodesListing:= 'http://www.startrek.com/library/episodes_voy.asp';

    if TheEpisodesListing = '' then
    begin
       Input('Star Trek Episode Guide Import', 'Enter the Internet Address of the Episodes               Listing:',TheEpisodesListing);
       if Pos('www.startrek.com',TheEpisodesListing) = 0 then
       begin
          ShowMessage('Error in Entered Address');
          Exit; 
       end;
    end;

    CacheExists:=False;
    if CacheMode Then
    Begin
      if CacheFile.Count > 0 Then
      Begin
        // verify the Cache is for the same star trek series
        CacheExists:= (FindLine(TheEpisodesListing,CacheFile,0) > -1 );
      End;
    End;

 // Extract Episode Title from template

    Template:= TitleTemplate;

    BeginPos:=Pos('-',EpisodeTitle);
    Len:= Length(EpisodeTitle);
    case Template of
    'Series - Episode Nr -  Title':
       Begin
   
          Delete(EpisodeTitle, 1, BeginPos);
          BeginPos:=Pos('-',EpisodeTitle);
          Delete(EpisodeTitle, 1, BeginPos);
       End
    'Title -  Series':
       Begin
          BeginPos:=Pos('-',EpisodeTitle);
          Delete(EpisodeTitle, BeginPos, Len);
       End
    'Title -  Episode Nr -  Series':
       Begin
          BeginPos:=Pos('-',EpisodeTitle);
          Delete(EpisodeTitle, BeginPos, Len);
       End
    'Series - Title':
       Begin
          Delete(EpisodeTitle, 1, BeginPos);
       End
    Else
       Begin
          if VerboseMode then
             ShowMessage('Episode Title does not match the template: ' + Template);
          Input('Star Trek Episode Guide Import', 'Enter Episode Title', EpisodeTitle);
       End;
    End; // Case 

    EpisodeTitle := Trim(EpisodeTitle);
    OriginalTitle:= EpisodeTitle;
    EpisodeTitle:=CorrectEpisodeTitle(EpisodeTitle);
  
 
    AnalyzePage(TheEpisodesListing);

  end
  else 
     ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the                 version 3.4.0)');

  if CacheMode Then
  begin
       CacheFile.SaveToFile('StarTrekScript_Cache.Txt');
       CacheFile.Free;
  end;
end. 
antp
Site Admin
Posts: 9651
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Re: New Script for www.startrek.com

Post by antp »

trekkie wrote: Is there a way to check file existanse within the Script
You can load the file in a TStringList, and if it is empty (count=0) save this empty TStringList, this should create the file
antp
Site Admin
Posts: 9651
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Re: New Script for www.startrek.com

Post by antp »

trekkie wrote:I thought you wrote the original script so you can credit whoever wrote it :)
I wrote IMDB script but I guess that you nearly rewrote the whole script for the other site ;)
trekkie

New Script for www.startrek.com

Post by trekkie »

antp wrote
You can load the file in a TStringList, and if it is empty (count=0) save this empty TStringList, this should create the file
That's not good because if the cache file does not exist ,The LoadFromfile
fails with an error message that causes the script to stop executing.
I was aiming to verify file existanse before LoadFromFile and
warn the user to create one
antp
Site Admin
Posts: 9651
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Actually I never tryed. I was thinking that it could be caugth with a try/except, but it does not seem to work.
So I'll have to add a link to the FileExists function in the next version.
Also few constants (ApplicationFolder, ScriptsFolder, ...) because if you use current folder it may be something else that the scripts folder, if the user loaded a script from somewhere else.
trekkie

New Script for www.startrek.com

Post by trekkie »

1. antp wrote
.. because if you use current folder it may be something else that the scripts folder, if the user loaded a script from somewhere else.
Even When i tried to load the script from a different diretory than "Scripts"
( using the Scripts - > Editor -> Open file option ) , the LoadfromFile
Used the "Scripts" directory as currentdirectory :/

Can you explain ?

2. I also tried try/except in the script but it doesn't work .Maybe a bug
( that sould be reported to ) with the Innerfuse Pascal Script compiler ?

2. Good idea to add file support in the next version. :grinking:
antp
Site Admin
Posts: 9651
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Re: New Script for www.startrek.com

Post by antp »

trekkie wrote: Can you explain ?
It's Windows' current directory, so it is nearly a random, and sometimes depends on the Windows version I think.
But if you use this current directory it should work fine in most of the cases, so do not worry ;)
antp
Site Admin
Posts: 9651
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Re: New Script for www.startrek.com

Post by antp »

trekkie wrote: 2. I also tried try/except in the script but it doesn't work .Maybe a bug
( that sould be reported to ) with the Innerfuse Pascal Script compiler ?
I use version 2, and they are now at version 3.
Lots of things changed, I guess that this may have been corrected too ;)
But since there are lots of changes to do in the program, I'll update that later, since it works not too bad like it is for the moment.
Post Reply