diff --git a/downloader.pas b/downloader.pas index 65e3821..b72b7aa 100644 --- a/downloader.pas +++ b/downloader.pas @@ -1,13 +1,13 @@ program downloader; -uses fphttpclient, base64, opensslsockets, fpjson, jsonparser, SysUtils, process, Classes, libtar; +uses fphttpclient, base64, opensslsockets, fpjson, jsonparser, SysUtils, process, Classes, libtar, CustApp; var - authString: String; gameId: Integer; gameData: TJSONData; client: TFPHTTPClient; - tarFile: String; + authString, tarFile, errorMsg: String; + app: TCustomApplication; procedure createFileDir(filePath: AnsiString); var @@ -111,10 +111,24 @@ var username, password: String; begin - Write('Enter username: '); - ReadLn(username); - Write('Enter password: '); - ReadLn(password); + if app.HasOption('u', 'user') then + begin + username := app.GetOptionValue('u', 'username'); + end + else + begin + Write('Enter username: '); + ReadLn(username); + end; + if app.HasOption('p', 'password') then + begin + password := app.GetOptionValue('p', 'password'); + end + else + begin + Write('Enter password: '); + ReadLn(password); + end; authString := username + ':' + password; client.AddHeader('Authorization', 'Basic '+base64.EncodeStringBase64(authString)); client.AllowRedirect := true; @@ -127,32 +141,41 @@ begin end; begin - if ParamCount > 0 then + app := TCustomApplication.Create(nil); + errorMsg := app.checkOptions('g:u:p:', 'gameid: username: password:'); + if (errorMsg = '') then begin - gameId := StrToInt(ParamStr(1)); - client := TFPHTTPClient.Create(nil); - if authenticate(client) then + if app.HasOption('g', 'gameid') then begin - if getGameData(client, gameId, gameData) then + gameId := StrToInt(app.GetOptionValue('g', 'gameid')); + client := TFPHTTPClient.Create(nil); + if authenticate(client) then begin - tarFile := downloadGameTar(client, gameId); - extractTar(tarFile); - DeleteFile(tarFile); - WriteLn('Download finished'); + if getGameData(client, gameId, gameData) then + begin + tarFile := downloadGameTar(client, gameId); + extractTar(tarFile); + DeleteFile(tarFile); + WriteLn('Download finished'); + end + else + begin + WriteLn('Invalid game id given'); + end; end else begin - WriteLn('Invalid game id given'); + WriteLn('Invalid username or password'); end; end else begin - WriteLn('Invalid username or password'); + WriteLn('No game id was given'); end; end else begin - WriteLn('No game id was given'); + WriteLn(errorMsg); end; end.