Problem exporting to UTF-8

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
ihelfe
Posts: 5
Joined: 2009-02-01 23:35:10

Problem exporting to UTF-8

Post by ihelfe »

I am using a slightly modified version of XMBC.NFO for use with YAMJ, currently running AMC 4.2.0.2

Although I have

Code: Select all

OutToFile.Append(UTF8Encode(FormatedData + MovieDirector + MovieGenre + '</movie>'));
the resultant NFO file is not UTF-8. What have I missed, do I need to fix?

Full Script

Code: Select all

program XBMC2Nfo;
  uses StringUtils1;
  var
  MovieGenre: string;
  MovieDirector: string;
  SavefileName: string;
  DirSelected: string;
  FormatedData: string;
  OutToFile: TStringList;

 
   function GenreToXML(genre: string): string;
      var
      lstGenre: TStringList;
      iGenre: Integer;
      sGenre: string;
      begin
      lstGenre := TStringList.Create;
      lstGenre.Text := StringReplace(genre, ', ', #13#10);
      for iGenre := 0 to lstGenre.Count -iGenre do
        begin
        sGenre :=  '   <genre>' + lstGenre.GetString(iGenre) + '</genre>';
        lstGenre.SetString(iGenre, sGenre);
        end
        Result := lstGenre.Text;
        lstGenre.Free;
      end;

   function DirectorToXML(director: string): string;
      var
      lstDirector: TStringList;
      iDirector: Integer;
      sDirector: string;
      begin
      lstDirector := TStringList.Create;
      lstDirector.Text := StringReplace(director, ', ', #13#10);
      for iDirector := 0 to lstDirector.Count -iDirector do
        begin
        sDirector :=  '   <director>' + lstDirector.GetString(iDirector) + '</director>';
        lstDirector.SetString(iDirector, sDirector);
        end
        Result := lstDirector.Text;
        lstDirector.Free;
      end;

   function ChooseTitle(Original: string; Translated: string): string;
     begin
      if Translated <> '' then Result := Translated else Result := Original
     end;

  begin
    if DirSelected ='' then
      begin
      Input('Folder Select','Select Folder to save NFO files',DirSelected)
      end;
      begin
      OutToFile := TStringList.Create;
      SaveFileName := IncludeTrailingPathDelimiter(DirSelected) + GetField(fieldOriginalTitle) + '.NFO';
      FormatedData := FormatedData +'<movie>'+#13#10;
      FormatedData := FormatedData +'   <title>'+ ChooseTitle(GetField(fieldOriginalTitle), GetField(fieldTranslatedTitle)) + '</title>'+#13#10;
      FormatedData := FormatedData +'   <originaltitle>'+GetField(fieldOriginalTitle)+'</originaltitle>'+#13#10;
      FormatedData := FormatedData +'   <rating></rating>'+#13#10;
      FormatedData := FormatedData +'   <year>'+GetField(fieldYear)+'</year>'+#13#10;
      FormatedData := FormatedData +'   <top250></top250>'+#13#10;
      FormatedData := FormatedData +'   <votes></votes>'+#13#10;
      FormatedData := FormatedData +'   <outline></outline>'+#13#10;
      FormatedData := FormatedData +'   <plot>'+GetField(fieldDescription)+'</plot>'+#13#10;
      FormatedData := FormatedData +'   <tagline></tagline>'+#13#10;
      FormatedData := FormatedData +'   <runtime></runtime>'+#13#10;
      FormatedData := FormatedData +'   <thumb></thumb>'+#13#10;
      FormatedData := FormatedData +'   <mpaa></mpaa>'+#13#10;
      FormatedData := FormatedData +'   <certification>UK:'+GetField(fieldMediaType)+'</certification>'+#13#10;
      FormatedData := FormatedData +'   <playcount></playcount>'+#13#10;
      FormatedData := FormatedData +'   <watched>'+GetField(fieldSource)+'</watched>'+#13#10;
      FormatedData := FormatedData +'   <id>' + TextAfter(GetField(fieldURL), '/title/') + '</id>'+#13#10;
      FormatedData := FormatedData +'   <id moviedb="allocine"></id>'+#13#10;
      FormatedData := FormatedData +'   <sets>'+#13#10;
      FormatedData := FormatedData +'     <set>'+GetField(fieldBorrower)+'</set>'+#13#10;
      FormatedData := FormatedData +'   </sets>'+#13#10;
      FormatedData := FormatedData +'   <id moviedb="filmweb"></id>'+#13#10;
      FormatedData := FormatedData +'   <filenameandpath></filenameandpath>'+#13#10;
      FormatedData := FormatedData +'   <trailer></trailer>'+#13#10;
      FormatedData := FormatedData +'   <studio></studio>' + #13#10;
      FormatedData := FormatedData +'   <actor>' + #13#10;
      FormatedData := FormatedData +'     <name></name>' + #13#10;
      FormatedData := FormatedData +'     <role></role>' + #13#10;
      FormatedData := FormatedData +'   </actor>' + #13#10;
(* Run Actor Script here*)
      MovieGenre  := GenreToXML(GetField(fieldCategory));
      MovieDirector  := DirectorToXML(GetField(fieldDirector));
      OutToFile.Append(UTF8Encode(FormatedData + MovieDirector + MovieGenre + '</movie>'));
      OutToFile.SaveToFile(SaveFileName);
      OutToFile.Free;
      FormatedData := ''
     end;
end.
antp
Site Admin
Posts: 9639
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

It seems to work fine to me.
But it is only the content which is UTF8-Encoded, not the file itself.
i.e. it does not have a UTF8 header.
Try adding the following string at the beginning of the file:

Code: Select all


assuming you have a western-european character set defined in Windows; if you use another one the raw UTF8 header would appear different.
One way to find the right header: make a UTF8 file with notepad, then open it with AMC's script editor: the first three characters are the UTF8 header.
ihelfe
Posts: 5
Joined: 2009-02-01 23:35:10

Post by ihelfe »

The code characters are correct, but can you explain how and where in the script I would add them to the header without them becoming part of the visible text.
antp
Site Admin
Posts: 9639
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Just after OutToFile := TStringList.Create;
you could try to add

Code: Select all

OutToFile.Text := '';
ihelfe
Posts: 5
Joined: 2009-02-01 23:35:10

Post by ihelfe »

Works perfectly, thank you
Post Reply