New Script for www.startrek.com
Posted: 2003-01-17 07:44:33
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[/list]
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,'—',' ');
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.