Einzelnen Beitrag anzeigen

venice2
(Gast)

n/a Beiträge
 
#12

AW: JSON einlesen - Hilfe für Anfänger

  Alt 21. Dez 2021, 15:36
Delphi-Quellcode:
function TZattoo.FormatJSONStr(AString: string): string;
var
  Obj: TJsonObject;
begin

  Obj := Obj.Parse(PWideChar(AString))as TJsonObject;
  result := Obj.ToJSON(False);
end;
Delphi-Quellcode:
procedure TZattoo.GetEPG;
var
  iRun, nRun: Integer;
  doc: TDocVariantData;
  docChannels: PDocVariantData;
  docPrograms: PDocVariantData;

  TimeEnd : Int64;
  TimeSTart : Int64;
  SendStr : string;
  Data : string;
  TxtFile: string;
  dwBytes: DWORD;
  lstream: TFileStream;
  lutf8: UTF8String;
  List : TStringList;
begin

  SetLength(gEPG, 0);
  SetLength(gEpgChannels, 0);

  try
    TimeStart := DateTimeToUnix(IncHour(Now, 0), false);
    TimeEnd := DateTimeToUnix(IncDay(Now, 1), false);
    SendStr := FProvRec.URL +
      cPathEPG +
      FProvRec.PWRHASH +
      '?end=' + inttostr(TimeEnd) +
      '&start=' + inttostr(TimeStart) +
      '&format=json' + '&details=True';

    Log('GetEPG:', 'SendStr: ' + Sendstr);
    data := FHTTP.Get(SendStr);
  except
    Log('GetEPG:', 'Failed !');
    exit;
  end;

  TxtFile := ExtractFilePath(ParamStr(0)) + 'Json\EPG_data.json';
  if FileExists(TxtFile) then
    DeleteFile(TxtFile);

  List := TStringList.Create;
  try
    List.Clear;
    List.Append(Data);
    List.SaveToFile(TxtFile);
  finally
    List.Free;
  end;

  lstream := TFileStream.create(TxtFile, fmOpenReadWrite);
  try
    dwBytes := lstream.size;
    Setlength(lutf8, dwBytes);
    lstream.ReadBuffer(lutf8[1], dwBytes);
    // Format Json
    lutf8 := UTF8String(FormatJSONStr(string(lutf8)));
    lstream.Position := 0;
    lstream.WriteBuffer(lutf8[1], length(lutf8));
  finally
    lstream.free;
  end;

  nRun := 0;

  doc.InitJSONFromFile(TxtFile, JSON_OPTIONS_FAST_EXTENDED);
  if not doc.B['success'] then
    Exit;
  try
  for iRun := 0 to doc.A['channels'].Count - 1 do
  begin
    docChannels := doc.A['channels']._[iRun];
    setLength(gEpgChannels, iRun + 1);
    gEpgChannels[iRun] := docChannels.S['cid'];

    docPrograms := docChannels.A['programs']._[nRun];
    setLength(gEpg, iRun + 1);
    setLength(sStart, iRun + 1);
    setLength(SEnd, iRun + 1);

    Log('GetEPG: Current Channel = ', IntToStr(iRun) + ' ' + gEpgChannels[iRun]);
    // Image Url
    gEpg[iRun].i_url := docPrograms.S['i_url'];
    Log('GetEPG: Image Url = ', gEpg[iRun].i_url);
    // Stop Zeit
    gEpg[iRun].e := docPrograms.I['e'];
    if gEpg[iRun].e <> 0 then
      SEnd[iRun] := UnixToDateTime(gEpg[iRun].e, false)
    else
    SEnd[iRun] := 0;
    Log('GetEPG: Stop Time = ', IntToStr(gEpg[iRun].e));

    // Genre
    gEpg[iRun].g := StringReplace(docPrograms.S['g'], '[', '', [rfReplaceAll]);
    gEpg[iRun].g := StringReplace(gEpg[iRun].g, ']', '', [rfReplaceAll]);
    gEpg[iRun].g := StringReplace(gEpg[iRun].g, '"', '', [rfReplaceAll]);
    if gEpg[iRun].g = 'then
      gEpg[iRun].g := 'N/A';
    Log('GetEPG: Genre = ', gEpg[iRun].g);

    gEpg[iRun].ry_u := docPrograms.I['ry_u'];
    gEpg[iRun].i := docPrograms.S['i'];
    gEpg[iRun].sr_u := docPrograms.I['sr_u'];
    gEpg[iRun].c := docPrograms.S['c'];
    gEpg[iRun].r_e:= docPrograms.B['r_e'];
    gEpg[iRun].e_no:= docPrograms.S['e_no'];
    // Start Zeit
    gEpg[iRun].s:= docPrograms.I['s'];
    if gEpg[iRun].s <> 0 then
      sStart[iRun] := UnixToDateTime(gEpg[iRun].s, false)
    else
    sStart[iRun] := 0;
    Log('GetEPG: Start Time = ', IntToStr(gEpg[iRun].s));

    // Titel
    gEpg[iRun].t:= docPrograms.S['t'];
    Log('GetEPG: Title = ', gEpg[iRun].t);
    gEpg[iRun].ser_e:= docPrograms.B['ser_e'];
    gEpg[iRun].c_ids := docPrograms.I['c_ids'];
    // Unter Titel
    gEpg[iRun].et := docPrograms.S['et'];
    if gEpg[iRun].et = 'then
      gEpg[iRun].et := 'N/A';
    gEpg[iRun].i_t := docPrograms.S['i_t'];
    gEpg[iRun].ts_id := docPrograms.I['ts_id'];
    gEpg[iRun].id := docPrograms.I['id'];
    gEpg[iRun].tms_id := docPrograms.s['tms_id'];
    gEpg[iRun].s_no := docPrograms.I['s_no'];
  end;
  except
    on e: exception do
      Log('GetEPG:', 'Failed! with Message ' + e.Message);
  end;
end;
Wie @mytbo schon sagt verwende dazu mORMot
Ich selber mache mit Zattoo nichts mehr es nervt wenn alle paar Tage die API wieder geändert wird.
Zudem gibt es diese ja auch nicht wirklich.

Geändert von venice2 (21. Dez 2021 um 15:42 Uhr)
  Mit Zitat antworten Zitat