{ ' UNIT uZatEPG.pas
'--------------------------- uZatEPG -----------------------------
' Copyright © 2020 BrewIdeas@Emil Weiss, All Rights Reserved
'
' Author(s) of this Unit: Emil Weiss
'---------------------------------------------------------------------------- }
unit uZatEPG;
interface
{$REGION 'uses'}
uses
Windows, Messages, SysUtils, StrUtils,
VCL.Dialogs, Classes, UrlMon,
ActiveX,
uZatJson, uGlobal, CommCtrl, JsonDataObjects, Generics.Collections;
{$ENDREGION}
{$REGION 'Const/Type'}
type
TEpgEntries = (i_url, e, g, ry_u, i, sr_u, c, r_e, rg_u, e_no, s, t,
ser_e, c_ids, et, i_t, ts_id, id, tms_id, s_no, success);
TEpgEntryNames =
array [TEpgEntries]
of string;
type
TEpgData =
record
i_url:
string;
e: LongInt;
g:
string;
ry_u: LongInt;
i:
string;
sr_u: LongInt;
c:
string;
r_e: BOOL;
e_no:
string;
s: LongInt;
t:
string;
ser_e: BOOL;
c_ids: LongInt;
et:
string;
i_t:
string;
ts_id: LongInt;
id: LongInt;
tms_id:
string;
s_no: LongInt;
success: BOOL;
end;
const
EntryEpgNames: TEpgEntryNames = ('
i_url', '
e', '
g', '
ry_u', '
i', '
sr_u', '
c',
'
r_e', '
rg_u', '
e_no', '
s', '
t', '
ser_e', '
c_ids', '
et', '
i_t','
ts_id',
'
id', '
tms_id', '
s_no', '
success');
{$ENDREGION}
{$REGION 'Class TEpgDataList'}
type
TEpgDataList =
class
private
EpgData: TList<TEpgData>;
procedure addEpgData(aZatJson: TZATJSON);
procedure fillfromJson(
const aJsonString:
string);
procedure FillList(aZatJson: TZATJSON);
function GetItems(
index: integer): TEpgData;
public
constructor create(
const aJsonString:
string);
destructor Destroy;
override;
function Count: integer;
property Items[
index: integer]: TEpgData
read GetItems;
default;
end;
{$ENDREGION}
{$REGION 'Class TZatEpg'}
type
TZatEpg =
class
private
EpgDataList: TEpgDataList;
function GetEpgInfoString(Path:
string; Data:
string;
var Info:
string): boolean;
function FormatJSONStr(AString :
string):
string;
public
constructor create;
destructor Destroy;
override;
procedure Filltitles(aitems: Tstrings);
function GetEpgInfo(Path:
string; Data:
string): boolean;
function Item(
index: integer): TEpgData;
function ValidData: boolean;
end;
{$ENDREGION}
implementation
{$REGION 'TZatEpg'}
{ TZatEpg }
function TZatEpg.GetEpgInfo(Path:
string; Data:
string): boolean;
var
lWideFileData:
string;
begin
Result := False;
try
if GetEpgInfoString(Path, Data, lWideFileData)
then
begin
EpgDataList := TEpgDataList.create(lWideFileData);
end;
Result := ValidData;
except
on e:
exception do
ShowMessage(e.
Message);
end;
end;
function TZatEpg.GetEpgInfoString(Path:
string; Data:
string;
var Info:
string): boolean;
var
TxtFile:
string;
dwBytes: DWORD;
lstream: TFileStream;
lutf8: UTF8String;
List : TStringList;
begin
Result := False;
Info := '
';
if Path = '
'
then
exit;
TxtFile := Path + '
EPG_data.json';
// get new Data from now
if FileExists(TxtFile)
then
DeleteFile(TxtFile);
List := TStringList.Create;
try
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);
Info :=
string(lutf8);
// Format Json
lutf8 := UTF8String(FormatJSONStr(
string(lutf8)));
lstream.Position := 0;
lstream.WriteBuffer(lutf8[1], length(lutf8));
Result := True;
finally
lstream.free;
end;
end;
constructor TZatEpg.create;
begin
inherited;
end;
destructor TZatEpg.Destroy;
begin
if Assigned(EpgDataList)
then
FreeAndNil(EpgDataList);
inherited;
end;
procedure TZatEpg.Filltitles(aitems: Tstrings);
//var
// I: integer;
begin
aitems.clear;
// for I := 0 to EpgDataList.Count - 1 do
// aitems.add(EpgDataList[I].title);
end;
function TZatEpg.FormatJSONStr(AString:
string):
string;
var
Obj: TJsonObject;
begin
Obj := Obj.Parse(PWideChar(AString))
as TJsonObject;
result := Obj.ToJSON(False);
end;
function TZatEpg.Item(
index: integer): TEpgData;
begin
if ValidData
then
Result := EpgDataList[
index];
end;
function TZatEpg.ValidData: boolean;
begin
Result := assigned(EpgDataList)
and (EpgDataList.Count > 0);
end;
{$ENDREGION}
{$REGION 'TEpgDataList'}
{ TEpgDataList }
constructor TEpgDataList.create(
const aJsonString:
string);
begin
inherited create;
EpgData := TList<TEpgData>.create;
fillfromJson(aJsonString);
end;
destructor TEpgDataList.Destroy;
begin
FreeAndNil(EpgData);
inherited;
end;
procedure TEpgDataList.addEpgData(aZatJson: TZATJSON);
var
entry: TEpgData;
Epgloop: tEpgEntries;
ltemp: TZATJSON;
lEntrienames: tEpgEntryNames;
begin
if assigned(aZatJson)
then
begin
// Set up temporary Data
entry := System.
default(TEpgData);
lEntrienames := EntryEpgNames;
begin
for Epgloop := Low(TEpgEntries)
to High(TEpgEntries)
do
// Check if there is a Name.......
if lEntrienames[Epgloop] <> '
'
then
begin
ltemp := aZatJson[lEntrienames[Epgloop]];
if ltemp <>
nil then
case Epgloop
of
i_url: entry.i_url := ltemp.AsString;
e: entry.e := ltemp.AsInteger;
g: entry.g := ltemp.TArray;
ry_u: entry.ry_u := ltemp.AsInteger;
i: entry.i := ltemp.AsString;
sr_u: entry.sr_u := ltemp.AsInteger;
c: entry.c := ltemp.AsString;
r_e: entry.r_e := ltemp.AsBoolean;
e_no: entry.e_no := ltemp.AsString;
s: entry.s := ltemp.AsInteger;
t: entry.t := ltemp.AsString;
ser_e: entry.ser_e := ltemp.AsBoolean;
c_ids: entry.c_ids := ltemp.AsInteger;
et: entry.et := ltemp.AsString;
i_t: entry.i_t := ltemp.AsString;
ts_id: entry.ts_id := ltemp.AsInteger;
id: entry.id := ltemp.AsInteger;
tms_id: entry.tms_id := ltemp.AsString;
s_no: entry.s_no := ltemp.AsInteger;
end;
end;
end;
EpgData.add(entry);
end;
end;
function TEpgDataList.Count: integer;
begin
Result := EpgData.Count;
end;
procedure TEpgDataList.fillfromJson(
const aJsonString:
string);
Var
lZatJson: TZATJSON;
begin
lZatJson := TZATJSON.Parse(aJsonString);
try
if assigned(lZatJson)
then
FillList(lZatJson);
finally
lZatJson.free;
end;
end;
procedure TEpgDataList.FillList(aZatJson: TZATJSON);
var
// I: integer;
lZatJson: TZATJSON;
begin
if assigned(aZatJson)
then
begin
lZatJson := aZatJson.JSONByNameOrIndex['
channels'];
if assigned(lZatJson)
and lZatJson.IsList
then
begin
if lZatJson.ListItems.Count > 0
then
addEpgData(lZatJson.ListItems[1]);
end;
end;
end;
function TEpgDataList.GetItems(
index: integer): TEpgData;
begin
Result := EpgData[
index];
end;
{$ENDREGION}
end.