const
  ts_key = 'ytrf_ts';
  lts_key = 'ytrf_lts';
    
procedure InitScriptBuffer;
begin
  if ScriptBuffer = '' then begin
    ScriptBuffer := '<' + ts_key + '=0>_<' + lts_key + '=0>';
  end;  
end;

procedure SaveIntValue(const key: string; const value: Integer);
var
  val_expr, val_repl: string;
begin
  val_expr := '<' + key + '=(\d+)>';
  val_repl := '<' + key + '=' + IntToStr(value) + '>';
  ScriptBuffer := RegExprReplaceMatchSubStr(ScriptBuffer, val_expr, val_repl);
end;

function GetIntValue(const key: string): Integer;
var
  val_expr, val_str: string;
begin
  val_expr := '<' + key + '=(\d+)>';
  val_str := RegExprGetMatchSubStr(ScriptBuffer, val_expr, 1);  
  if (val_str <> '') then begin
    Result := StrToInt(val_str);
  end else begin
    Result := 0;
  end; 
end;

function DownloadUrl(const AUrl: AnsiString): string;
var
  VResponseCode: Cardinal;
  VResponseHeader, VResponseData: AnsiString;
  VRequestUrl, VRequestHeader, VPostData: AnsiString;
begin
  Result := '';
  if Assigned(Downloader) then begin
    VRequestUrl := AUrl;
    VRequestHeader := '';
    VPostData := '';
    VResponseHeader := '';
    VResponseData := ''; 
    VResponseCode := Downloader.DoHttpRequest(VRequestUrl, VRequestHeader, VPostData, VResponseHeader, VResponseData);      
    if VResponseCode = 200 then begin
      Result := VResponseData;
    end;
  end;
end;

function IsNeedUpdateTimeStamp(const ADelta: Integer): Boolean;
var
  ts, lts: Integer;
begin
  ts := GetUnixTime;
  lts := GetIntValue(lts_key);    
  if (ts > lts + ADelta) then begin
    SaveIntValue(lts_key, ts);
    Result := True;
  end else begin
    Result := False;
  end;
end;

function GetTimeStamp(const ADelta: Integer): Integer;
var
  ts: Integer;
  ts_val: string;
  stat_js: string;  
begin
  ts := 0;
  if IsNeedUpdateTimeStamp(ADelta) then begin
    stat_js := DownloadUrl('https://api-maps.yandex.ru/services/coverage/v2/layers_stamps?l=trf');
    if stat_js <> '' then begin
      ts_val := RegExprGetMatchSubStr(stat_js, '"version":"(\d+)"', 1); 
      if ts_val <> '' then begin
        ts := StrToInt(ts_val);
        SaveIntValue(ts_key, ts);
      end;
    end;   
  end else begin
    ts := GetIntValue(ts_key);
  end;
  Result := ts;
end;

begin
  InitScriptBuffer;  
  if GetZ <> 0 then begin 
    ResultURL := GetURLBase + IntToStr(GetX) + '&y=' + IntToStr(GetY) + 
      '&z=' + IntToStr(GetZ-1) + '&tm=' + IntToStr(GetTimeStamp(60));
  end else begin
    ResultURL := '';
  end;
end.
