ItalianSharedPas.pas updated

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
fulvio53s03
Posts: 745
Joined: 2007-04-28 05:46:43
Location: Italy

ItalianSharedPas.pas updated

Post by fulvio53s03 »

Conversion of two special characters is added in SharedUTF8ToCP1252Decode function:
Nella funzione SharedUTF8ToCP1252Decode si aggiunge la conversione di due caratteri speciali:

’-------- in---------'
é--------- in---------é

Code: Select all

unit ItalianSharedPas;

{
  Shared var, constants and functions for all italian units
}

uses
  StringUtils7552;

const
  SH_UnitVersion = 3;

  SH_DebugMode = false;
  SH_DebugFileFullPath = 'D:\antMovieCatalog.debug.txt';
  SH_TitDel = ' || ';

var
  SH_ForceExit, SH_StandAloneMode: boolean;
  SH_Source, SH_MovieUrl, SH_MovieName, SH_OriginalStr, SH_TranslatedStr, SH_DirectorName, SH_YearDate: string;
  SH_LatestPageHtml, SH_LatestPageUrl: string;
  SH_CoverUrl, SH_CoverReferral: string;  
  SH_LatestMovieUrlInPickTree: string;
  SH_OverwriteOriginalTitle: integer;
  SH_MoviesFound: integer;

  
// --------------------------------------------------------------------------------------------------------------------------------------
//                                               "public" functions e procedures
// --------------------------------------------------------------------------------------------------------------------------------------

function SharedGetDebugMode: boolean;
begin
  result := SH_DebugMode;
end;  
  
function SharedGetStandAloneMode: boolean;
begin
  result := SH_StandAloneMode;
end;

procedure SharedSetStandAloneMode(value: boolean);
begin
  SH_StandAloneMode := value;
end;
  
function SharedGetForceExit: boolean;
begin
  result := SH_ForceExit;
end;  

procedure SharedSetForceExit(value: boolean);
begin
  SH_ForceExit := value;
end; 

function SharedGetMoviesFound: integer;
begin
  result := SH_MoviesFound;
end;  

procedure SharedSetMoviesFound(value: integer);
begin
  SH_MoviesFound := value;
end; 
  
function SharedGetSource: string;
begin
  result := SH_Source;
end;

procedure SharedSetSource(value: string);
begin
  SH_Source := value;
end;

function SharedGetMovieUrl: string;
begin
  result := SH_MovieUrl;
end;

procedure SharedSetMovieUrl(value: string);
begin
  SH_MovieUrl := value;
end;

function SharedGetMovieName: string;
begin
  result := SH_MovieName;
end;

function SharedGetOriginalStr: string;
begin
  result := SH_OriginalStr;
end;

procedure SharedSetOriginalStr(title: string);
begin
  SH_OriginalStr := title;
end;

function SharedGetTranslatedStr: string;
begin
  result := SH_TranslatedStr;
end;

procedure SharedSetTranslatedStr(title: string);
begin
  SH_TranslatedStr := title;
end;

function SharedGetDirectorName: string;
begin
  result := SH_DirectorName;
end;

procedure SharedSetDirectorName(director: string);
begin
  SH_DirectorName := director;
end;

function SharedGetYearDate: string;
begin
  result := SH_YearDate;
end;

procedure SharedSetYearDate(yeardate: string);
begin
  SH_YearDate := yeardate;
end;

function SharedGetOverwriteOriginalTitle: integer;
begin
  result := SH_OverwriteOriginalTitle;
end;

function SharedGetLatestPageHtml: string;
begin
  result := SH_LatestPageHtml;
end;

function SharedGetLatestPageUrl: string;
begin
  result := SH_LatestPageUrl;
end;

// -----------------------
// Import image
// -----------------------
procedure SharedGetPicture;
begin
  if (CanSetPicture AND (SH_CoverUrl <> '')) then
  begin  
    SharedDebugAppendToFile(SH_CoverUrl, '[ItalianSharedPas][SharedGetPicture] Referral: ' + SH_CoverReferral);
    GetPicture2(SH_CoverUrl, SH_CoverReferral);
  end;  
end;

// -------------------------------
// Save Image info
// IN:  image url and url referral
// -------------------------------
procedure SharedSavePictureInfo(Url, Referral: string);
begin
  SH_CoverUrl := Url;
  SH_CoverReferral := Referral;
end;

// -------------------------------
// Show Message
// IN:  UTF8 valid string
// -------------------------------
procedure SharedShowMessage(utf8s: string);
begin
  showMessage(StringReplace(utf8s, '&', '&&'));
end;


function SharedSerializeMe: string;
begin
  // example of a serialized object : O:5:"myobj":3:{s:1:"b";b:0;s:1:"s";s:5:"prova";s:1:"i";i:5;}
  result := 'O:' + IntToStr(Length('ItalianSharedPas')) + ':"ItalianSharedPas":15:{';
  result := result + 's:' + IntToStr(Length('SH_ForceExit')) + ':"SH_ForceExit";b:';
  if (SH_ForceExit) then
    result := result + '1;'
  else
    result := result + '0;';
  result := result + 's:' + IntToStr(Length('SH_StandAloneMode')) + ':"SH_StandAloneMode";b:';
  if (SH_StandAloneMode) then
    result := result + '1;'
  else
    result := result + '0;';  
  result := result + 's:' + IntToStr(Length('SH_Source')) + ':"SH_Source";s:' + IntToStr(Length(SH_Source)) + ':"' + SH_Source + ';';
  result := result + 's:' + IntToStr(Length('SH_MovieUrl')) + ':"SH_MovieUrl";s:' + IntToStr(Length(SH_MovieUrl)) + ':"' + SH_MovieUrl + ';';
  result := result + 's:' + IntToStr(Length('SH_MovieName')) + ':"SH_MovieName";s:' + IntToStr(Length(SH_MovieName)) + ':"' + SH_MovieName + ';';
  result := result + 's:' + IntToStr(Length('SH_OriginalStr')) + ':"SH_OriginalStr";s:' + IntToStr(Length(SH_OriginalStr)) + ':"' + SH_OriginalStr + ';';
  result := result + 's:' + IntToStr(Length('SH_TranslatedStr')) + ':"SH_TranslatedStr";s:' + IntToStr(Length(SH_TranslatedStr)) + ':"' + SH_TranslatedStr + ';';
  result := result + 's:' + IntToStr(Length('SH_DirectorName')) + ':"SH_DirectorName";s:' + IntToStr(Length(SH_DirectorName)) + ':"' + SH_DirectorName + ';';
  result := result + 's:' + IntToStr(Length('SH_YearDate')) + ':"SH_YearDate";s:' + IntToStr(Length(SH_YearDate)) + ':"' + SH_YearDate + ';';
  result := result + 's:' + IntToStr(Length('SH_LatestPageHtml')) + ':"SH_LatestPageHtml";s:' + IntToStr(Length(SH_LatestPageHtml)) + ':"' + SH_LatestPageHtml + ';';
  result := result + 's:' + IntToStr(Length('SH_LatestPageUrl')) + ':"SH_LatestPageUrl";s:' + IntToStr(Length(SH_LatestPageUrl)) + ':"' + SH_LatestPageUrl + ';';
  result := result + 's:' + IntToStr(Length('SH_CoverUrl')) + ':"SH_CoverUrl";s:' + IntToStr(Length(SH_CoverUrl)) + ':"' + SH_CoverUrl + ';';
  result := result + 's:' + IntToStr(Length('SH_CoverReferral')) + ':"SH_CoverReferral";s:' + IntToStr(Length(SH_CoverReferral)) + ':"' + SH_CoverReferral + ';';
  result := result + 's:' + IntToStr(Length('SH_LatestMovieUrlInPickTree')) + ':"SH_LatestMovieUrlInPickTree";s:' + IntToStr(Length(SH_LatestMovieUrlInPickTree)) + ':"' + SH_LatestMovieUrlInPickTree + ';'; 
  result := result + 's:' + IntToStr(Length('SH_OverwriteOriginalTitle')) + ':"SH_OverwriteOriginalTitle";i:' + IntToStr(SH_OverwriteOriginalTitle) + ';';
  result := result + 's:' + IntToStr(Length('SH_MoviesFound')) + ':"SH_MoviesFound";i:' + IntToStr(SH_MoviesFound) + ';';
  result := result + '}'; 
end;

function SharedUnserializeMe(serializedObj: string): string;
var
  str, propertyName, propertyType, propertyValueStr: string;
  propertyValueBool: boolean;
  propertyValueInt, propertyValueStrLength: integer;
begin
  // example of a serialized object : O:5:"myobj":3:{s:1:"b";b:0;s:1:"s";s:5:"prova";s:1:"i";i:5;}
  str := serializedObj;
  Delete(str, 1, pos('{', str));
  while (pos('"', str) > 0) do
  begin
    // str = s:1:"b";b:0;s:1:"s";s:5:"prova";s:1:"i";i:5;
    propertyName := textBetween(str, '"', '"'); // b
    propertyType := textBetween(str, ';', ':'); // b
    Delete(str, 1, pos(';', str)); // b:0;s:1:"s";s:5:"prova";s:1:"i";i:5;
    case propertyType of
    'b':
      if (textBetween(str, ':', ';') = '0') then
        propertyValueBool := false
      else
        propertyValueBool := true;      
    'i':    
      propertyValueInt := StrToInt(textBetween(str, ':', ';'), 0);
    's':
      begin
        propertyValueStrLength := StrToInt(textBetween(str, ':', ':'), 0);
        Delete(str, 1, pos('"', str));
        propertyValueStr := copy(str, 1, propertyValueStrLength);
        Delete(str, 1, propertyValueStrLength);
      end;
    end;
    Delete(str, 1, pos(';', str));
    case propertyName of
    'SH_ForceExit': SH_ForceExit := propertyValueBool;
    'SH_StandAloneMode': SH_StandAloneMode := propertyValueBool;
    'SH_Source': SH_Source := propertyValueStr;
    'SH_MovieUrl': SH_MovieUrl := propertyValueStr;
    'SH_MovieName': SH_MovieName := propertyValueStr;
    'SH_OriginalStr': SH_OriginalStr := propertyValueStr;
    'SH_TranslatedStr': SH_TranslatedStr := propertyValueStr;
    'SH_DirectorName': SH_DirectorName := propertyValueStr;
    'SH_YearDate': SH_YearDate := propertyValueStr;
    'SH_LatestPageHtml': SH_LatestPageHtml := propertyValueStr;
    'SH_LatestPageUrl': SH_LatestPageUrl := propertyValueStr;
    'SH_CoverUrl': SH_CoverUrl := propertyValueStr;
    'SH_CoverReferral': SH_CoverReferral := propertyValueStr;
    'SH_LatestMovieUrlInPickTree': SH_LatestMovieUrlInPickTree := propertyValueStr;
    'SH_OverwriteOriginalTitle': SH_OverwriteOriginalTitle := propertyValueInt;
    'SH_MoviesFound': SH_MoviesFound := propertyValueInt;
    end;
  end;
end;


// -----------------------
// SET MOVIE FIELDS
// IN:  field name and field value
// OUT: set Ant field
// -----------------------
procedure SharedSecureSetField(fieldname: integer; fieldvalue: string);
begin
  if ((GetOption('Sovrascrivi campi') = 1) or (FullTrim(GetField(fieldname)) = '')) then
    SetField(fieldname, fieldvalue);
end;


// -----------------------
// SET ALL MOVIE FIELDS
// IN:  all fields values
// OUT: set all Ant fields
// -----------------------
procedure SharedSecureSetAllFields(originalTitle, translatedTitle, rating, year, producers, writers, composers, directors, actors, duration, categories, certification, countries, description, comments, url, picture: string);
begin
//  SharedSecureSetField(fieldcertification, FS_certificazione);         //FS20220304 per debug salvo indirizzo pagina di ricerca
  SharedSecureSetField(fieldDate, DateToStr(Date));
  if (originalTitle = '') then
    originalTitle := translatedTitle;  
  SharedSecureSetField(fieldTranslatedTitle, translatedTitle);
  if (SharedGetOverwriteOriginalTitle() = 0) then
    // inserimento/aggiornamento "sicuro" 
    SharedSecureSetField(fieldOriginalTitle, originalTitle)
  else
    // forzo l'aggiornamento perche' il titolo originale l'ho usato e poi modificato in fase di input, presumibilmente contiene qualche carattere "sporco"
    SetField(fieldOriginalTitle, originalTitle);
  SharedSecureSetField(fieldDescription, description);
  if (comments = description) then
    SharedSecureSetField(fieldComments, '')
  else  
    SharedSecureSetField(fieldComments, comments);
  SharedSecureSetField(fieldRating, rating);
  SharedSecureSetField(fieldCertification, certification);
  SharedSecureSetField(fieldCountry, countries);
  SharedSecureSetField(fieldYear, year);
  SharedSecureSetField(fieldCategory, categories);
  if ((duration <> '') AND (getField(fieldLength) = '')) then 
    SharedSecureSetField(fieldLength, duration);
  SharedSecureSetField(FieldProducer, producers);
  SharedSecureSetField(FieldWriter, writers);
  SharedSecureSetField(FieldComposer, composers);
  SharedSecureSetField(FieldDirector, directors);
  SharedSecureSetField(fieldActors, actors);
  SharedSecureSetField(fieldURL, url);
  if picture <> '' then
  begin
    SharedSavePictureInfo(picture, url);
    if (SharedGetStandAloneMode()) then
      SharedGetPicture();
  end;
end;


procedure SharedPickTreeCreate;
var ExtendedTitle: string;
begin
  SharedSetMoviesFound(0);
  SH_LatestMovieUrlInPickTree := '';
  ExtendedTitle := SH_Source + ' - Risultati ricerca per "' + SH_MovieName;
  if ((SH_YearDate <> '') AND (SH_DirectorName <> '')) then
  begin
    if (SH_DirectorName <> '') AND (SH_YearDate <> '') then
      ExtendedTitle := ExtendedTitle + ' (' + SH_DirectorName + ', ' + SH_YearDate + ')'
    else
    if (SH_DirectorName <> '') then
      ExtendedTitle := ExtendedTitle + ' (' + SH_DirectorName + ')'
    else
    if (SH_YearDate <> '') then
      ExtendedTitle := ExtendedTitle + ' (' + SH_YearDate + ')';
  end;
  PickTreeClear;
  PickTreeAdd(SH_Source + ' - Risultati ricerca per "' + ExtendedTitle + '":', '');
  if (SH_StandAloneMode) then
    PickTreeAdd('>> interrompi la ricerca di questo titolo in questo sito --->>', 'about:blank')  
  else
  if (SharedGetSource() <> 'IMDb') then
    PickTreeAdd('>> continua la ricerca su un altro sito --->>', 'about:blank');
end;


procedure SharedPickTreeAdd(filmTranslatedTitle, filmOriginalTitle, filmDirectors, filmYear, filmUrl: string);
var
  ExtendedTitle: string;
begin
  if ((GetOption('Ricerca con Regista e Anno') = 1) AND (filmYear <> '') AND (SH_YearDate <> '')) then
    if ((StrToInt(filmYear, 0) < (StrToInt(SH_YearDate, 0) - 5)) OR (StrToInt(filmYear, 0) > (StrToInt(SH_YearDate, 0) + 5))) then
      exit;
  if (filmTranslatedTitle = '') AND (filmOriginalTitle = '') then
    exit;

  if (filmTranslatedTitle <> '') AND (filmOriginalTitle <> '') then
    ExtendedTitle := filmOriginalTitle + ' - ' + filmTranslatedTitle
  else
  if (filmTranslatedTitle <> '') then
    ExtendedTitle := filmTranslatedTitle
  else
    ExtendedTitle := filmOriginalTitle;
  
  if (filmDirectors <> '') AND (filmYear <> '') then
    ExtendedTitle := ExtendedTitle + ' (' + filmDirectors + ', ' + filmYear + ')'
  else
  if (filmDirectors <> '') then
    ExtendedTitle := ExtendedTitle + ' (' + filmDirectors + ')'
  else
  if (filmYear <> '') then
    ExtendedTitle := ExtendedTitle + ' (' + filmYear + ')';    

  SharedSetMoviesFound(SharedGetMoviesFound() + 1);
  SH_LatestMovieUrlInPickTree := filmUrl;
  PickTreeAdd(ExtendedTitle, filmUrl);
end;

procedure SharedPickTreeExec();
begin
  if (SharedGetMoviesFound() > 0) AND (SharedGetMovieUrl() = '') then
  begin
    SharedSetForceExit(not PickTreeExec(SH_MovieUrl));
    SH_MovieUrl := URLEncode(SharedUTF8ToCP1252Decode(SH_MovieUrl));
    if ((SharedGetForceExit()) or (SharedGetMovieUrl() = 'about:blank') or (copy(SharedGetMovieUrl(), 1, 5) = 'next:')) then 
      SharedSetMoviesFound(0);
  end;
end;


// c'è matching se almeno uno tra titolo originale e tradotto corrisponde esattamente
// E se il nome del regista somiglia al 95% o più
// E se la distanza tra l'anno trovato e quello voluto non è superiore a +/-1 anno
// Il matching viene cercato solo se l'opzione Ricerca con Regista e Anno è pari a 1 oppure se sto cercando in IMDB
function SharedPickTreeMatching(filmOriginalTitle, filmTranslatedTitle, filmYear, filmDirectors: string): boolean;
var
  title1Input, title2Input, title1Shared, title2Shared: string;
begin
  title1Shared := AnsiLowerCaseNoAccents(SharedGetOriginalStr());
  title2Shared := AnsiLowerCaseNoAccents(SharedGetTranslatedStr());
  if ((title1Shared = '') OR (title2Shared = '')) then
  begin
  // questo si verifica, ad esempio, quando il nome nel campo titolo tradotto è quello importato dal filename, esempio: Il tempo delle api (Anitori, 2017)---webrip.audio ita
    title1Shared := AnsiLowerCaseNoAccents(SharedGetMovieName());
    title2Shared := title1Shared;
  end;
  
  title1Input := AnsiLowerCaseNoAccents(filmOriginalTitle);
  title2Input := AnsiLowerCaseNoAccents(filmTranslatedTitle);  
  if (title2Input = '') then
    title2Input := title1Input
  else
  if (title1Input = '') then
    title1Input := title2Input;  
  // confronta titolo, anno e regista. Il confronto sul titolo si rende necessario perché talvolta lo stesso regista, dopo un anno, gira un sequel
  result := (((GetOption('Ricerca con Regista e Anno') = 1) or (SH_Source = 'IMDb')) and (SH_DirectorName <> '') AND (filmDirectors <> '') and (filmYear <> '') and
             ((title1Shared = title1Input) or (title2Shared = title2Input) or (title1Shared = title2Input) or (title2Shared = title1Input)) and
             ((filmYear = SH_YearDate) or (filmYear = IntToStr(StrToInt(SH_YearDate, 0) + 1)) or(filmYear = IntToStr(StrToInt(SH_YearDate, 0) - 1))) and
             (((pos(AnsiLowerCaseNoAccents(SH_DirectorName), AnsiLowerCaseNoAccents(filmDirectors)) <> 0) or (pos(AnsiLowerCaseNoAccents(filmDirectors), AnsiLowerCaseNoAccents(SH_DirectorName)) <> 0)) or (CompareWords(AnsiLowerCaseNoAccents(filmDirectors), AnsiLowerCaseNoAccents(SH_DirectorName)) >= 95)));           
  if (result) then
  begin
    PickTreeClear;
    SharedSetMoviesFound(1);  
  end;
end;


procedure SharedPickTreeClose(paramNextPageAvailable: boolean);
begin
  if (paramNextPageAvailable) then
    PickTreeAdd('>> continua la ricerca su questo sito --->>', 'next:')   
  else
  if (SharedGetStandAloneMode() AND (SharedGetMoviesFound() = 1) AND (SharedGetMovieUrl() = '')) then
    SharedSetMovieUrl(SH_LatestMovieUrlInPickTree); 
end;


procedure SharedHTTPPostPage(paramUrl, paramData: string);
begin
  SharedHTTPGetPostPage(paramUrl, 'POST', paramData);
end;


procedure SharedHTTPGetPage(paramUrl: string);
begin
  SharedHTTPGetPostPage(paramUrl, 'GET', '');
end;

procedure SharedHTTPGetPostPage(paramUrl, paramRequestMethod, paramData: string);
begin
  SH_LatestPageUrl := paramUrl;
  if (paramUrl = '') then
    SH_LatestPageHtml := ''
  else
  begin
    RaiseConnectionErrors(false);
    if (paramRequestMethod = 'GET') then
      SH_LatestPageHtml := GetPage(paramUrl)
    else
      SH_LatestPageHtml := PostPage(paramUrl, paramData);
    RaiseConnectionErrors(true);      
    SH_LatestPageHtml := SharedUTF8ToCP1252Decode(SH_LatestPageHtml);
  end;  
  SharedDebugAppendToFile(SH_LatestPageHtml, '[ItalianSharedPas][SharedHTTP' + paramRequestMethod + 'Page] ' + paramUrl + '   ' + paramData);
end;


// -----------------------------------
// APPEND TEXT COMMENTED TO DEBUG FILE
// IN: content text and comment text
// -----------------------------------
procedure SharedDebugAppendToFile(content, comment: string);
var
	page: TStringList;
begin
  if not SH_DebugMode then // debug
    exit;
  page := TStringList.Create;
  if (FileExists(SH_DebugFileFullPath)) then
    page.LoadFromFile(SH_DebugFileFullPath);
  page.Text := page.Text + '--- [' + SH_Source + ']' + comment + ' START -----------------------------------------' + crlf + content + crlf + '--- [' + SH_Source + ']' + comment + ' END -------------------------------------------' + crlf;
  page.SaveToFile(SH_DebugFileFullPath);
  page.Free;  
end;



// -----------------------------------
// RETRIEVE THE URL OF MOVIE TO SEARCH
// IN:  none
// -----------------------------------  
procedure SharedRetrieveMovieUrl;
var
  domain, mysource: string;
begin
  SH_MovieUrl := '';
  SH_MovieName := '';
  SH_TranslatedStr := '';
  SH_OriginalStr := '';
  SH_OverwriteOriginalTitle := 1;
  SH_DirectorName := '';
  SH_YearDate := '';  
  mysource := '';
  if (getOption('UseUrlField') = 1) then
  begin
    domain := AnsiLowerCase(textBetween(getField(FieldUrl), '://', '/'));
    case domain of
    'www.comingsoon.it', 'comingsoon.it': mysource := 'ComingSoon';
    'www.film.tv.it', 'film.tv.it': mysource := 'FilmTv';
    'www.filmscoop.it', 'filmscoop.it': mysource := 'FilmScoop'; 
    'www.kultvideo.com', 'kultvideo.com': mysource := 'KultVideo';
    'www.mymovies.it', 'mymovies.it': mysource := 'MyMovies';
    'www.movieplayer.it', 'movieplayer.it': mysource := 'MoviePlayer';
    'www.imdb.com', 'imdb.com': if (GetParam('IMDB Ricerca') <> '0') then mysource := 'IMDb';
    end;
    if ((SharedGetSource() = '') OR (SharedGetSource() = mysource)) then // if it's ItalianMultisite script or a StandAlone script ...
    begin
      if ((mysource = '') AND RegExprSetExec('tt[0-9]+', getField(FieldUrl))) then
      begin
        SharedSetMovieUrl('https://www.imdb.com/title/' + SH_MovieName + '/reference');
        SharedSetMoviesFound(1);
        SharedSetSource('IMDb');
      end
      else
      if (mysource <> '') then
      begin
        SharedSetMovieUrl(fulltrim(getField(FieldUrl)));
        SharedSetMoviesFound(1);
        SharedSetSource(mysource);
      end;
    end;
  end;
end;

// ------------------------------------
// RETRIEVE THE NAME OF MOVIE TO SEARCH
// IN:  none
// ------------------------------------  
procedure SharedRetrieveMovieName;
var parenthesisOpenedPos, parenthesisClosedPos, commaPos: integer;
begin
  SH_TranslatedStr := FullTrim(GetField(fieldTranslatedTitle));
  if (pos(SH_TitDel, SH_TranslatedStr) > 0) then
    SH_TranslatedStr := copy(SH_TranslatedStr, 1, pos(SH_TitDel, SH_TranslatedStr) - 1);  
  SH_OriginalStr := FullTrim(GetField(fieldOriginalTitle));
  SH_OverwriteOriginalTitle := 1;
  if (SH_TranslatedStr <> '') then
    SH_MovieName := SH_TranslatedStr
  else
  begin
    SH_MovieName := SH_OriginalStr;
    if (SH_OriginalStr <> '') then
      SH_OverwriteOriginalTitle := 0;		  
  end;
  
  SH_MovieName := StringReplace(SH_MovieName, '’', '''');

  // Retrieve director name and year from title. Accepted title example "nome film (director, year) other text". Example: "Junkyard dog (Bass, 2010) dvdrip ita ac3 eng aac"
  parenthesisOpenedPos := pos('(', SH_MovieName);
  if (parenthesisOpenedPos > 0) then
  begin
    commaPos := pos(',', TextAfter(SH_MovieName, '(')) + parenthesisOpenedPos;
    parenthesisClosedPos := pos(')', TextAfter(SH_MovieName, '(')) + parenthesisOpenedPos;
  end  
  else
  begin	  
    commaPos := 0;
    parenthesisClosedPos := 0;		  
  end;  
  SH_DirectorName := '';
  SH_YearDate := '';
  if (GetOption('Ricerca con Regista e Anno') = 1) then
    if ((parenthesisOpenedPos <> 0) AND (commaPos > parenthesisOpenedPos) AND (parenthesisClosedPos <> commaPos)) then
    begin
      SH_DirectorName := FullTrim(copy(SH_MovieName, parenthesisOpenedPos + 1, commaPos - parenthesisOpenedPos - 1)); 
      SH_YearDate := FullTrim(copy(SH_MovieName, commaPos + 1, parenthesisClosedPos - commaPos - 1));
      SH_MovieName := FullTrim(copy(SH_MovieName, 1, parenthesisOpenedPos - 1));  
      SH_OverwriteOriginalTitle := 1;
    end
    else
    begin
      SH_YearDate := getField(fieldYear);
      SH_DirectorName := getField(fieldDirector);
    end;
  SharedAskForTitle;
end;


// --------------------------------------------------------------------------------------------------
// DEFINE AND RETRIEVE WHICH ELEMENTS USE FOR SEARCHING: MOVIEURL/SOURCE OR MOVIE TITLE/YEAR/DIRECTOR
// IN:  none
// -------------------------------------------------------------------------------------------------- 
procedure SharedRetrieveSearchElements;
begin
  SharedRetrieveMovieUrl();
  if (SharedGetMovieUrl() = '') then
    SharedRetrieveMovieName();
end;


procedure SharedAskForTitle;
begin
  SharedSetForceExit(not Input(SH_Source, 'Inserire il titolo del film:', SH_MovieName));
  if SharedGetForceExit() then
    exit;

  if ((SH_OverwriteOriginalTitle = 0) and (SH_MovieName <> SH_OriginalStr)) then
    // Used 'Original title' field value but it was manually changed in input field, so field 'Original title' will be overwritten in any case
    SH_OverwriteOriginalTitle := 1;
end;



function SharedUTF8ToCP1252Decode(str: string): string;
begin
  result := UTF8Decode(str);
  // Sometimes UTF8Decode fails and return a void string, then I manually decode UTF8 to CP1252 
  // (test url: https://www.film.tv.it/film/26509/la-mala-educacion/ )
  if (result = '') then
  begin
    str := StringReplace(str,  '’'     , '''' );       
    str := StringReplace(str, 'é', 'é');
    str := StringReplace(str, 'æ', 'æ'); 
    str := StringReplace(str, '€', '€'); 
    str := StringReplace(str, '‚', '‚'); 
    str := StringReplace(str, 'Æ’', 'ƒ'); 
    str := StringReplace(str, '„', '„'); 
    str := StringReplace(str, '…', '…');
    str := StringReplace(str, '†', '†'); 
    str := StringReplace(str, '‡', '‡'); 
    str := StringReplace(str, 'ˆ', 'ˆ'); 
    str := StringReplace(str, '‰', '‰');
    str := StringReplace(str, 'Å ', 'Š'); 
    str := StringReplace(str, '‹', '‹');
    str := StringReplace(str, 'Å’', 'Œ'); 
    str := StringReplace(str, 'Ž', 'Ž'); 
    str := StringReplace(str, 'ð', 'ð'); 
    str := StringReplace(str, '‘', '‘'); 
    str := StringReplace(str, 'ñ', 'ñ'); 
    str := StringReplace(str, '´', '’'); 
    str := StringReplace(str, 'ò', 'ò'); 
    str := StringReplace(str, '“', '“');
    str := StringReplace(str, 'ó', 'ó'); 
    str := StringReplace(str, '”', '”'); 
    str := StringReplace(str, 'ô', 'ô'); 
    str := StringReplace(str, '•', '•'); 
    str := StringReplace(str, 'õ', 'õ'); 
    str := StringReplace(str, '–', '–');
    str := StringReplace(str, 'Æ', 'Æ'); 
    str := StringReplace(str, 'ö', 'ö'); 
    str := StringReplace(str, '—', '—');
    str := StringReplace(str, 'Ç', 'Ç'); 
    str := StringReplace(str, '÷', '÷'); 
    str := StringReplace(str, 'Ëœ', '˜ '); 
    str := StringReplace(str, 'È', 'È'); 
    str := StringReplace(str, 'ø', 'ø'); 
    str := StringReplace(str, 'â„¢', '™');
    str := StringReplace(str, 'É', 'É'); 
    str := StringReplace(str, 'ù', 'ù'); 
    str := StringReplace(str, 'Å¡', 'š'); 
    str := StringReplace(str, 'Ê', 'Ê'); 
    str := StringReplace(str, 'ú', 'ú'); 
    str := StringReplace(str, '›', '›');
    str := StringReplace(str, 'û', 'û'); 
    str := StringReplace(str, 'Å“', 'œ'); 
    str := StringReplace(str, 'Ì', 'Ì'); 
    str := StringReplace(str, 'ü', 'ü'); 
    str := StringReplace(str, 'Í', 'Í');
    str := StringReplace(str, 'ý', 'ý'); 
    str := StringReplace(str, 'ž', 'ž'); 
    str := StringReplace(str, 'Î', 'Î'); 
    str := StringReplace(str, 'þ', 'þ'); 
    str := StringReplace(str, 'Ÿ', 'Ÿ'); 
    str := StringReplace(str, 'Ï', 'Ï');
    str := StringReplace(str, 'ÿ', 'ÿ');
    str := StringReplace(str, 'Ð', 'Ð');
    str := StringReplace(str, '¡', '¡'); 
    str := StringReplace(str, 'Ñ', 'Ñ'); 
    str := StringReplace(str, '¢', '¢'); 
    str := StringReplace(str, 'Ò', 'Ò'); 
    str := StringReplace(str, '£', '£'); 
    str := StringReplace(str, 'Ó', 'Ó'); 
    str := StringReplace(str, 'Ô', 'Ô'); 
    str := StringReplace(str, '¥', '¥'); 
    str := StringReplace(str, 'Õ', 'Õ'); 
    str := StringReplace(str, 'Ö', 'Ö'); 
    str := StringReplace(str, '§', '§'); 
    str := StringReplace(str, '¨', '¨'); 
    str := StringReplace(str, 'Ø', 'Ø'); 
    str := StringReplace(str, '©', '©');
    str := StringReplace(str, 'Ù', 'Ù');
    str := StringReplace(str, 'ª', 'ª');
    str := StringReplace(str, 'Ú', 'Ú');
    str := StringReplace(str, '«', '«');
    str := StringReplace(str, 'Û', 'Û');
    str := StringReplace(str, '¬', '¬');
    str := StringReplace(str, 'Ü', 'Ü');
    str := StringReplace(str, 'Ý', 'Ý');
    str := StringReplace(str, '®', '®');
    str := StringReplace(str, 'Þ', 'Þ');
    str := StringReplace(str, '¯', '¯');
    str := StringReplace(str, 'ß', 'ß');
    str := StringReplace(str, '°', '°');
    str := StringReplace(str, 'à', 'à');
    str := StringReplace(str, '±', '±');
    str := StringReplace(str, 'á', 'á');
    str := StringReplace(str, '²', '²');
    str := StringReplace(str, '³', '³');
    str := StringReplace(str, 'ã', 'ã');
    str := StringReplace(str, '´', '´');
    str := StringReplace(str, 'ä', 'ä');
    str := StringReplace(str, 'µ', 'µ');
    str := StringReplace(str, 'å', 'å');
    str := StringReplace(str, '¶', '¶');
    str := StringReplace(str, 'æ', 'æ');
    str := StringReplace(str, '·', '·');
    str := StringReplace(str, 'ç', 'ç');
    str := StringReplace(str, '¸', '¸');
    str := StringReplace(str, 'è', 'è');
    str := StringReplace(str, '¹', '¹');
    str := StringReplace(str, 'é', 'é');
    str := StringReplace(str, 'º', 'º');
    str := StringReplace(str, 'ê', 'ê');
    str := StringReplace(str, '»', '»');
    str := StringReplace(str, 'ë', 'ë');
    str := StringReplace(str, '¼', '¼');
    str := StringReplace(str, 'ì', 'ì');
    str := StringReplace(str, '½', '½');
    str := StringReplace(str, 'í', 'í');
    str := StringReplace(str, '¾', '¾');
    str := StringReplace(str, 'î', 'î');
    str := StringReplace(str, '¿', '¿');
    str := StringReplace(str, 'ï', 'ï');

    str := StringReplace(str, 'â', 'â');  
    str := StringReplace(str, 'Â', 'Â');   
    str := StringReplace(str, 'Ã', 'Ã');   
    str := StringReplace(str, 'Ä', 'Ä');   
    str := StringReplace(str, 'À', 'À');   
    str := StringReplace(str, 'Á', 'Á');
    str := StringReplace(str, 'Å', 'Å'); 
    str := StringReplace(str, 'Ë', 'Ë');
    result := str;  
  end;  
end;


// --------------------------------------------------------------------------------------------------------
// UrlEncode working: does not encode new line, carrier feed, tab, backspace and other invisible characters
// IN: string to encode, raw url encode mode (see difference in php between urlencode and rawurlencode)
// OUT: string encoded
// --------------------------------------------------------------------------------------------------------
function SharedAdvancedUrlEncode(str: string; raw: boolean): string;
var
  oldchar, newchar: string;
begin
  result := '';
  while (Length(str) > 0) do
  begin
    oldchar := copy(str, 1, 1);
    Delete(str, 1, 1);
    newchar := oldchar;
    case oldchar of
      #10: newchar := '%0a';
      #13: newchar := '%0d';
      #160: newchar := '%a0';
      #127: newchar := '%7f';
      #129: newchar := '%81';
      #141: newchar := '%8d';
      #143: newchar := '%8f';
      #144: newchar := '%90';
      #157: newchar := '%9d';
      #173: newchar := '%ad';
      'æ': newchar := '%00';
      '`': newchar := '%60';
      ':': newchar := '%3a';
      ';': newchar := '%3b';
      '<': newchar := '%3c';
      '=': newchar := '%3d';
      '>': newchar := '%3e';
      '?': newchar := '%3f';
      '@': newchar := '%40';
      '{': newchar := '%7b';
      '|': newchar := '%7c';
      '}': newchar := '%7d';
      '€': newchar := '%80';
      '!': newchar := '%21';
      '"': newchar := '%22';
      '‚': newchar := '%82';
      '#': newchar := '%23';
      'ƒ': newchar := '%83';
      '$': newchar := '%24';
      '„': newchar := '%84';
      '%': newchar := '%25';
      '…': newchar := '%85';
      '&': newchar := '%26';
      '†': newchar := '%86';
      '''': newchar := '%27';
      '‡': newchar := '%87';
      '(': newchar := '%28';
      'ˆ': newchar := '%88';
      ')': newchar := '%29';
      '‰': newchar := '%89';
      '*': newchar := '%2a';
      'Š': newchar := '%8a';
      '+': newchar := '%2b';
      '[': newchar := '%5b';
      '‹': newchar := '%8b';
      ',': newchar := '%2c';
      '\': newchar := '%5c';
      'Œ': newchar := '%8c';
      ']': newchar := '%5d';
      '^': newchar := '%5e';
      'Ž': newchar := '%8e';
      '/': newchar := '%2f';
      'À': newchar := '%c0';
      'ð': newchar := '%f0';
      '‘': newchar := '%91';
      'Á': newchar := '%c1';
      'ñ': newchar := '%f1';
      '’': newchar := '%92';
      'Â': newchar := '%c2';
      'ò': newchar := '%f2';
      '“': newchar := '%93';
      'Ã': newchar := '%c3';
      'ó': newchar := '%f3';
      '”': newchar := '%94';
      'Ä': newchar := '%c4';
      'ô': newchar := '%f4';
      '•': newchar := '%95';
      'Å': newchar := '%c5';
      'õ': newchar := '%f5';
      '–': newchar := '%96';
      'Æ': newchar := '%c6';
      'ö': newchar := '%f6';
      '—': newchar := '%97';
      'Ç': newchar := '%c7';
      '÷': newchar := '%f7';
      '˜': newchar := '%98';
      'È': newchar := '%c8';
      'ø': newchar := '%f8';
      '™': newchar := '%99';
      'É': newchar := '%c9';
      'ù': newchar := '%f9';
      'š': newchar := '%9a';
      'Ê': newchar := '%ca';
      'ú': newchar := '%fa';
      '›': newchar := '%9b';
      'Ë': newchar := '%cb';
      'û': newchar := '%fb';
      'œ': newchar := '%9c';
      'Ì': newchar := '%cc';
      'ü': newchar := '%fc';
      'Í': newchar := '%cd';
      'ý': newchar := '%fd';
      'ž': newchar := '%9e';
      'Î': newchar := '%ce';
      'þ': newchar := '%fe';
      'Ÿ': newchar := '%9f';
      'Ï': newchar := '%cf';
      'ÿ': newchar := '%ff';
      'Ð': newchar := '%d0';
      '¡': newchar := '%a1';
      'Ñ': newchar := '%d1';
      '¢': newchar := '%a2';
      'Ò': newchar := '%d2';
      '£': newchar := '%a3';
      'Ó': newchar := '%d3';
      'Ô': newchar := '%d4';
      '¥': newchar := '%a5';
      'Õ': newchar := '%d5';
      '|': newchar := '%a6';
      'Ö': newchar := '%d6';
      '§': newchar := '%a7';
      '¨': newchar := '%a8';
      'Ø': newchar := '%d8';
      '©': newchar := '%a9';
      'Ù': newchar := '%d9';
      'ª': newchar := '%aa';
      'Ú': newchar := '%da';
      '«': newchar := '%ab';
      'Û': newchar := '%db';
      '¬': newchar := '%ac';
      'Ü': newchar := '%dc';
      'Ý': newchar := '%dd';
      '®': newchar := '%ae';
      'Þ': newchar := '%de';
      '¯': newchar := '%af';
      'ß': newchar := '%df';
      '°': newchar := '%b0';
      'à': newchar := '%e0';
      '±': newchar := '%b1';
      'á': newchar := '%e1';
      '²': newchar := '%b2';
      'â': newchar := '%e2';
      '³': newchar := '%b3';
      'ã': newchar := '%e3';
      '´': newchar := '%b4';
      'ä': newchar := '%e4';
      'µ': newchar := '%b5';
      'å': newchar := '%e5';
      '¶': newchar := '%b6';
      'æ': newchar := '%e6';
      '·': newchar := '%b7';
      'ç': newchar := '%e7';
      '¸': newchar := '%b8';
      'è': newchar := '%e8';
      '¹': newchar := '%b9';
      'é': newchar := '%e9';
      'º': newchar := '%ba';
      'ê': newchar := '%ea';
      '»': newchar := '%bb';
      'ë': newchar := '%eb';
      '¼': newchar := '%bc';
      'ì': newchar := '%ec';
      '½': newchar := '%bd';
      'í': newchar := '%ed';
      '¾': newchar := '%be';
      'î': newchar := '%ee';
      '¿': newchar := '%bf';
      'ï': newchar := '%ef';
      '~': 
        case raw of
        true: newchar := '~';
        false: newchar := '%7e';
        end;
      ' ':
        case raw of
        true: newchar := '%20'; // raw url encoding
        false: newchar := '+'; // encoding
        end;
    end;
    result := result + newchar;  
  end;
end;


// ------------------------------------
// INIT SETUP UNIT VARIABLES AND STATES
// IN:  Source, stand alone mode
// OUT: None
// ------------------------------------
procedure SharedInitCommon(SourceParam: string; isStandAloneParam: boolean);
begin
  SharedSetSource(SourceParam);
  SharedSetForceExit(false);
  SharedSetStandAloneMode(isStandAloneParam);
  SharedSetMovieUrl('');
  SharedSetMoviesFound(0); 
  SH_LatestMovieUrlInPickTree := '';  
  if (((SourceParam = '') AND (not isStandAloneParam)) OR isStandAloneParam) then // full init
  begin 
    SH_MovieName := '';
    SH_OriginalStr := '';
    SH_TranslatedStr := '';
    SH_DirectorName := '';
    SH_YearDate := '';
    SH_LatestPageHtml := '';
    SH_LatestPageUrl := '';
    SH_CoverUrl := '';
    SH_CoverReferral := '';
    SH_LatestMovieUrlInPickTree := '';    
    SH_OverwriteOriginalTitle := 0;
  end;
end;


// --------------------------------------------
// SETUP & MAIN CONTROLS FOR STAND ALONE SCRIPT
// IN:  Source
// OUT: None
// --------------------------------------------  
procedure SharedInitStandAlone(SourceParam: string);
begin
  if not CheckVersion(4, 2, 2) then
  begin
    SharedShowMessage('Questo script richiede una versione aggiornata di Ant Movie Catalog (versione 4.2.2 o successiva)');
    exit;
  end;

  if StringUtils1_Version < 7 then
  begin
    SharedShowMessage('Questo script richiede una versione aggiornata di StringUtils1.pas (versione 7 o successiva)');
    exit;
  end;

  SharedInitCommon(SourceParam, true);  
  SharedRetrieveSearchElements();
end;

// --------------------------------------------------------------------------------------------------------------------------------------
//                                               "private" functions e procedures
// --------------------------------------------------------------------------------------------------------------------------------------

end.
antp
Site Admin
Posts: 9639
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Re: ItalianSharedPas.pas updated

Post by antp »

Thanks, updated
MrObama2022
Posts: 58
Joined: 2022-02-02 00:03:55

Re: ItalianSharedPas.pas updated

Post by MrObama2022 »

Hi Fulvio, Hi antp

I just created a new account, my old account is dead and I can't recover it (I used a temporary email to register years ago, sorry ...).

Thank you fulvio for your work!
Post Reply