-
Notifications
You must be signed in to change notification settings - Fork 15
/
ASR.pas
626 lines (596 loc) · 23.3 KB
/
ASR.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
{ ############################################################################ }
{ # # }
{ # MSpeech v1.5.10 # }
{ # # }
{ # Copyright (с) 2012-2020, Mikhail Grigorev. All rights reserved. # }
{ # # }
{ # License: http://opensource.org/licenses/GPL-3.0 # }
{ # # }
{ # Contact: Mikhail Grigorev (email: [email protected]) # }
{ # # }
{ ############################################################################ }
unit ASR;
interface
uses Windows, Classes, SysUtils, Global, HTTPSend, synautil;
type
//JSONError = class(Exception);
// Статусы
TRecognizeStatus = (rsRecognizeDone, rsRecordingNotRecognized,
rsErrorJsonParse, rsErrorResponse,
rsInfo, rsAbort, rsErrorGetAPIKey,
rsFileSizeNull, rsErrorConnectionTimedOut,
rsErrorNoRouteToHost, rsErrorHostNotFound,
rsErrorCommunication, rsErrorPermissionDenied);
// Структура с информацией о статусе распознавания (передается в callback процедуру)
TRecognizeInfo = record
FStatus : TRecognizeStatus; // Статус
FMessage : String; // Сообщение
FConfidence : Real; // Достоверность распознавания в %
FTranscript : String; // Распознанная фраза
end;
// callback процедура, вызываемая после распознавания
TRecognizeResultEvent = procedure (Sender: TObject; pInfo: TRecognizeInfo) of object;
// Запуск распознавания
procedure StartRecognize(pGoogleAPIKey, pFileName, pRecognizeLang: String; pUseProxy: Boolean; pProxyAddress, pProxyPort: String; pProxyAuth: Boolean; pProxyAuthUserName, pProxyAuthPassword: String; pRecognizeInfoCallBack: TRecognizeResultEvent);
// Остановка распознавания
procedure StopRecognize;
implementation
uses
SSL_OpenSSL, blcksock, uJSON, TypInfo;
type
RecognizeError = class(Exception);
TGoogleRecognizer = class(TThread)
private
FInputFileName: String;
FResultList: TStringList;
FInputStream: TFileStream;
FRecognizeLang: String;
FUseProxy: Boolean;
FProxyAddress: String;
FProxyPort: String;
FProxyAuth: Boolean;
FProxyAuthUserName: String;
FProxyAuthPassword: String;
FRecognizeInfoCallBack: TRecognizeResultEvent;
FRecognizeInfo: TRecognizeInfo;
FTerminated: Boolean;
FGoogleAPIKey: String;
FHTTP: THTTPSend;
FReadCount: Integer;
procedure RecognizeEvent;
function RecognizeResult(E: Boolean; eType: TRecognizeStatus; eText: String): Boolean;
function GetFileSize(FileName: String): Integer;
function HTTPPostFile(Const URL, FieldName, FileName: String; Const Data: TStream; Const ResultData: TStrings): Boolean;
procedure HTTPStatus(Sender: TObject; Reason: THookSocketReason; const Value: String);
function SendRecognizeRequest(AudioFile: String): TRecognizeInfo;
function ParseJSONv2(JStringList: TStringList): TRecognizeInfo;
function HTTPGetSize(var HTTP: THTTPSend; URL: String): int64; overload;
function HTTPGetSize(URL: String): int64; overload;
protected
procedure Execute; override;
public
constructor Create(pGoogleAPIKey, pFileName, pRecognizeLang: String; pUseProxy: Boolean; pProxyAddress, pProxyPort: String; pProxyAuth: Boolean; pProxyAuthUserName, pProxyAuthPassword: String; pRecognizeInfoCallBack: TRecognizeResultEvent);
destructor Destroy; override;
property Terminated;
procedure Terminate(NewPriority: TThreadPriority = tpIdle); reintroduce;
property OnRecognize: TRecognizeResultEvent read FRecognizeInfoCallBack;
end;
var
GoogleRecognize: TGoogleRecognizer;
const
// Ваш ключ Google Speech API
GoogleRecognizePublicAPIKey = 'Enter-Your-Google-Speech-API-Key';
GoogleRecognizeURLv2Public = 'https://www.google.com/speech-api/v2/recognize?client=chrome-hotword&output=json&lang=%s&key=%s&app=web-hotword-0.1.1.5018_0';
GoogleRecognizeURLv2MSpeech = 'https://www.google.com/speech-api/v2/recognize?output=json&lang=%s&key=%s&app=%s';
PCMInSampleRate = '16000';
FLACInSampleRate = '44100';
HTTPTimeout = 4000;
procedure StartRecognize(pGoogleAPIKey, pFileName, pRecognizeLang: String; pUseProxy: Boolean; pProxyAddress, pProxyPort: String; pProxyAuth: Boolean; pProxyAuthUserName, pProxyAuthPassword: String; pRecognizeInfoCallBack: TRecognizeResultEvent);
begin
GoogleRecognize := TGoogleRecognizer.Create(pGoogleAPIKey, pFileName, pRecognizeLang, pUseProxy, pProxyAddress, pProxyPort, pProxyAuth, pProxyAuthUserName, pProxyAuthPassword, pRecognizeInfoCallBack);
end;
procedure StopRecognize;
begin
if Assigned(GoogleRecognize) then
begin
if EnableLogs then WriteInLog(WorkPath, FormatDateTime('dd.mm.yy hh:mm:ss', Now) + ': StopRecognize');
GoogleRecognize.Terminate;
end;
end;
procedure DeallocRecognizerThread;
begin
if Assigned(GoogleRecognize) then
begin
if EnableLogs then
begin
WriteInLog(WorkPath, FormatDateTime('dd.mm.yy hh:mm:ss', Now) + ': DeallocRecognizerThread');
CloseLogFile;
end;
GoogleRecognize.Terminate;
GoogleRecognize.WaitFor;
GoogleRecognize.Free;
GoogleRecognize := nil;
end;
end;
constructor TGoogleRecognizer.Create(pGoogleAPIKey, pFileName, pRecognizeLang: String; pUseProxy: Boolean; pProxyAddress, pProxyPort: String; pProxyAuth: Boolean; pProxyAuthUserName, pProxyAuthPassword: String; pRecognizeInfoCallBack: TRecognizeResultEvent);
begin
inherited Create(True);
FreeOnTerminate := True;
FInputFileName := pFileName;
FRecognizeLang := pRecognizeLang;
FUseProxy := pUseProxy;
FProxyAddress := pProxyAddress;
FProxyPort := pProxyPort;
FProxyAuth := pProxyAuth;
FProxyAuthUserName := pProxyAuthUserName;
FProxyAuthPassword := pProxyAuthPassword;
FRecognizeInfoCallBack := pRecognizeInfoCallBack;
FResultList := TStringList.Create;
FHTTP := THTTPSend.Create;
FRecognizeInfo.FStatus := rsRecordingNotRecognized;
FRecognizeInfo.FConfidence := 0;
FRecognizeInfo.FTranscript := '';
FReadCount := 0;
if pGoogleAPIKey = '' then
FGoogleAPIKey := GoogleRecognizePublicAPIKey
else
FGoogleAPIKey := pGoogleAPIKey;
FTerminated := False;
if EnableLogs then WriteInLog(WorkPath, FormatDateTime('dd.mm.yy hh:mm:ss', Now) + ': TGoogleRecognizer.Create');
Resume;
end;
destructor TGoogleRecognizer.Destroy;
begin
FreeAndNil(FHTTP);
FreeAndNil(FResultList);
if EnableLogs then
begin
WriteInLog(WorkPath, FormatDateTime('dd.mm.yy hh:mm:ss', Now) + ': TGoogleRecognizer.Destroy');
CloseLogFile;
end;
inherited Destroy;
end;
procedure TGoogleRecognizer.Terminate(NewPriority: TThreadPriority = tpIdle);
begin
if (NewPriority <> tpIdle) and (NewPriority <> Priority) then
Priority := NewPriority;
inherited Terminate;
end;
procedure TGoogleRecognizer.RecognizeEvent;
begin
if Assigned(FRecognizeInfoCallBack) then
FRecognizeInfoCallBack(Self, FRecognizeInfo);
end;
function TGoogleRecognizer.RecognizeResult(E: Boolean; eType: TRecognizeStatus; eText: String): Boolean;
begin
Result := E;
if E then
begin
FRecognizeInfo.FStatus := eType;
FRecognizeInfo.FMessage := eText;
FRecognizeInfo.FConfidence := 0;
FRecognizeInfo.FTranscript := '';
RecognizeEvent;
end;
end;
procedure TGoogleRecognizer.Execute;
begin
if not FileExists(FInputFileName) then
begin
raise RecognizeError.Create(SysErrorMessage(GetLastError));
Terminate;
end
else
begin
FRecognizeInfo := SendRecognizeRequest(FInputFileName);
if FRecognizeInfo.FStatus = rsErrorJsonParse then // Ошибка при парсинге ответа
begin
FRecognizeInfo.FStatus := rsErrorGetAPIKey;
FRecognizeInfo.FMessage := 'Validity of Key Speech API, obtained from the server MSpeech, already ended.';
Synchronize(RecognizeEvent);
end
else if FRecognizeInfo.FStatus = rsRecordingNotRecognized then // Запись не распознана
begin
Synchronize(RecognizeEvent);
end
else if FRecognizeInfo.FStatus = rsRecognizeDone then // Запись распознана
begin
Synchronize(RecognizeEvent);
end;
end
end;
function TGoogleRecognizer.SendRecognizeRequest(AudioFile: String): TRecognizeInfo;
var
FResultListCnt: Integer;
FURL, FResultStr, FReplaceStr: String;
FResultStrV2: String;
FJSON, FJo, FJoV2: TJSONobject;
JArray: TJSONArray;
begin
if RecognizeResult(GetFileSize(AudioFile) <= 0, rsFileSizeNull, 'File size ' + FInputFileName + ' ' + IntToStr(GetFileSize(AudioFile))) then
Exit
else
begin
FResultList.Clear;
try
FInputStream := TFileStream.Create(AudioFile, fmOpenRead or fmShareDenyWrite);
except
on e: Exception do
begin
raise RecognizeError.Create(SysErrorMessage(GetLastError));
Terminate;
end;
end;
try
if GoogleRecognizePublicAPIKey = FGoogleAPIKey then
FURL := Format(GoogleRecognizeURLv2Public, [FRecognizeLang, FGoogleAPIKey])
else
FURL := Format(GoogleRecognizeURLv2MSpeech, [FRecognizeLang, FGoogleAPIKey, ProgramsName+'-'+ProgramsVer]);
if not HTTPPostFile(FURL, 'userfile', FInputFileName, FInputStream, FResultList) then
begin
Result.FStatus := rsErrorCommunication;
Result.FTranscript := 'Failed to get a response from the Google ASR';
Exit;
end;
finally
FInputStream.Free;
end;
{if EnableLogs then
begin
WriteInLog(WorkPath, FormatDateTime('dd.mm.yy hh:mm:ss', Now) + ': FResultList.Count = ' + IntToStr(FResultList.Count));
WriteInLog(WorkPath, FormatDateTime('dd.mm.yy hh:mm:ss', Now) + ': FResultList.Text = ' + UTF8ToString(FResultList.Text));
end;}
Result := ParseJSONv2(FResultList);
end;
end;
function TGoogleRecognizer.GetFileSize(FileName: String): Integer;
var
FS: TFileStream;
begin
Result := 0;
try
FS := TFileStream.Create(Filename, fmOpenRead);
Result := FS.Size;
except
Result := -1;
FS.Free;
Exit;
end;
FS.Free;
end;
function TGoogleRecognizer.HTTPPostFile(const URL, FieldName, FileName: String; const Data: TStream; const ResultData: TStrings): Boolean;
const
CRLF = #$0D + #$0A;
var
Bound, Str: String;
begin
Result := False;
Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary';
try
if FUseProxy then
begin
FHTTP.ProxyHost := FProxyAddress;
if FProxyPort <> '' then
FHTTP.ProxyPort := FProxyPort
else
FHTTP.ProxyPort := '3128';
if FProxyAuth then
begin
FHTTP.ProxyUser := FProxyAuthUserName;
FHTTP.ProxyPass := FProxyAuthPassword;
end;
end;
Str := '--' + Bound + CRLF;
Str := Str + 'content-disposition: form-data; name="' + FieldName + '";';
Str := Str + ' filename="' + FileName + '"' + CRLF;
if ExtractFileExt(FileName) = '.wav' then
Str := Str + 'Content-Type: audio/l16; rate='+PCMInSampleRate+'' + CRLF + CRLF
else
Str := Str + 'Content-Type: audio/x-flac; rate='+FLACInSampleRate+'' + CRLF + CRLF;
FHTTP.Timeout := HTTPTimeout;
FHTTP.Sock.OnStatus := HTTPStatus;
FHTTP.Document.Clear;
FHTTP.Headers.Clear;
FHTTP.Document.Write(Pointer(Str)^, Length(Str));
FHTTP.Document.CopyFrom(Data, 0);
Str := CRLF + '--' + Bound + '--' + CRLF;
FHTTP.Document.Write(Pointer(Str)^, Length(Str));
if ExtractFileExt(FileName) = '.wav' then
FHTTP.MimeType := 'audio/l16; rate='+PCMInSampleRate
else
FHTTP.MimeType := 'audio/x-flac; rate='+FLACInSampleRate;
FHTTP.UserAgent := 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36';
Result := FHTTP.HTTPMethod('POST', URL);
ResultData.LoadFromStream(FHTTP.Document);
except
on e: Exception do
begin
raise RecognizeError.Create(SysErrorMessage(GetLastError));
Terminate;
end;
end;
end;
procedure TGoogleRecognizer.HTTPStatus(Sender: TObject; Reason: THookSocketReason; const Value: String);
var
Status: String;
begin
{$IFDEF DEBUG}
if EnableLogs then WriteInLog(WorkPath, FormatDateTime('dd.mm.yy hh:mm:ss', Now) + ': ' + GetEnumName(TypeInfo(THookSocketReason),ord(Reason))+': '+Value);
{$ENDIF DEBUG}
if (Terminated) and (not FTerminated) then
begin
FTerminated := True;
if Sender is TTCPBlockSocket then
TTCPBlockSocket(Sender).StopFlag := True;
FRecognizeInfo.FStatus := rsAbort;
FRecognizeInfo.FMessage := 'Requested stop sending data';
FRecognizeInfo.FConfidence := 0;
FRecognizeInfo.FTranscript := '';
Synchronize(RecognizeEvent);
end;
if not FTerminated then
begin
if Value <> '' then
begin
case Reason of
HR_ResolvingBegin: Status := 'Start resolving: ' + Value;
HR_ResolvingEnd: Status := 'End resolving: ' + Value;
HR_SocketCreate: Status := 'Socket created: ' + Value;
HR_SocketClose: Status := 'Socket closed: ' + Value;
HR_Bind: Status := 'Socket binded: ' + Value;
HR_Connect: Status := 'Socket connected: ' + Value;
HR_CanRead: Status := 'Read: ' + Value;
HR_CanWrite: Status := 'Write: ' + Value;
HR_Listen: Status := 'Listen: ' + Value;
HR_Accept: Status := 'Accept: ' + Value;
HR_ReadCount:
begin
// Считаем количество принятых байт, потом их выведем суммарно и отдельно
// В это количество не входит размер заголовка и т.п.
FReadCount := FReadCount+StrToInt(Value);
Status := '';
end;
HR_WriteCount: Status := 'WriteCount: ' + Value;
HR_Wait: Status := 'Wait: ' + Value;
HR_Error:
begin
if RecognizeResult(Value = '10065,No route to host', rsErrorNoRouteToHost, Value) then
Exit
else if RecognizeResult(Value = '11001,Host not found', rsErrorHostNotFound, Value) then
Exit
else if RecognizeResult(Value = '10060,Connection timed out', rsErrorConnectionTimedOut, Value) then
Exit
else if Value = '10054,Connection reset by peer' then
Exit
else if RecognizeResult(Value = '11002,Non authoritative - host not found', rsErrorHostNotFound, Value) then
Exit
else if RecognizeResult(Value = '10013,Permission denied', rsErrorPermissionDenied, Value) then
Exit
else
begin
Status := 'Error: ' + Value;
FRecognizeInfo.FStatus := rsErrorCommunication;
FRecognizeInfo.FMessage := Status;
FRecognizeInfo.FConfidence := 0;
FRecognizeInfo.FTranscript := '';
Synchronize(RecognizeEvent);
Exit;
end;
end;
end;
if Status <> '' then
begin
FRecognizeInfo.FStatus := rsInfo;
FRecognizeInfo.FMessage := Status;
FRecognizeInfo.FConfidence := 0;
FRecognizeInfo.FTranscript := '';
Synchronize(RecognizeEvent);
end;
end;
end;
end;
function TGoogleRecognizer.ParseJSONv2(JStringList: TStringList): TRecognizeInfo;
var
JStringListCnt: Integer;
FJSON, FJo, FJo2: TJSONobject;
JArray, JArrayA: TJSONArray;
begin
Result.FStatus := rsRecordingNotRecognized;
Result.FMessage := '';
Result.FConfidence := 0;
Result.FTranscript := '';
for JStringListCnt := 0 to JStringList.Count-1 do
begin
try
if EnableLogs then WriteInLog(WorkPath, Format('%s: Parsing string = %s', [FormatDateTime('dd.mm.yy hh:mm:ss', Now), UTF8ToString(JStringList[JStringListCnt])]));
FJSON := TJSONObject.Create(UTF8ToString(JStringList[JStringListCnt]));
except
on e: Exception do
begin
//raise JSONError.Create(SysErrorMessage(GetLastError));
FJSON.Free;
Result.FStatus := rsErrorJsonParse;
Result.FMessage := e.Message;
Exit;
end;
end;
try
if not FJSON.isNull('result_index') then
begin
try
JArray := TJSONArray.create(FJSON.optString('result'));
except
on e: Exception do
begin
//raise JSONError.Create(SysErrorMessage(GetLastError));
Result.FStatus := rsErrorJsonParse;
Result.FMessage := e.Message;
JArray.Free;
FJSON.Free;
Exit;
end;
end;
try
if EnableLogs then WriteInLog(WorkPath, Format('%s: Lenght of array [result] = %d', [FormatDateTime('dd.mm.yy hh:mm:ss', Now), JArray.length]));
if FJSON.optInt('result_index') < JArray.length then
begin
if EnableLogs then WriteInLog(WorkPath, Format('%s: Array [result] = %s', [FormatDateTime('dd.mm.yy hh:mm:ss', Now), JArray.get(FJSON.optInt('result_index')).toString]));
try
FJo := TJSONObject.Create(JArray.get(FJSON.optInt('result_index')).toString);
except
on e: Exception do
begin
//raise JSONError.Create(SysErrorMessage(GetLastError));
Result.FStatus := rsErrorJsonParse;
Result.FMessage := e.Message;
FJo.Free;
JArray.Free;
FJSON.Free;
Exit;
end;
end;
try
if FJo.optString('final') = 'true' then
begin
{$IFDEF DEBUG}
if EnableLogs then WriteInLog(WorkPath, Format('%s: String alternative = %s', [FormatDateTime('dd.mm.yy hh:mm:ss', Now), FJo.optString('alternative')]));
{$ENDIF}
try
JArrayA := TJSONArray.create(FJo.optString('alternative'));
except
on e: Exception do
begin
//raise JSONError.Create(SysErrorMessage(GetLastError));
Result.FStatus := rsErrorJsonParse;
Result.FMessage := e.Message;
JArrayA.Free;
FJo.Free;
JArray.Free;
FJSON.Free;
Exit;
end;
end;
try
{$IFDEF DEBUG}
if EnableLogs then WriteInLog(WorkPath, Format('%s: Lenght of array [alternative] = %d', [FormatDateTime('dd.mm.yy hh:mm:ss', Now), JArrayA.length]));
{$ENDIF}
if JArrayA.length > 0 then
begin
if EnableLogs then WriteInLog(WorkPath, Format('%s: Array alternative[%d] = %s', [FormatDateTime('dd.mm.yy hh:mm:ss', Now), 0, JArrayA.get(0).toString]));
try
FJo2 := TJSONObject.Create(JArrayA.get(0).toString);
except
on e: Exception do
begin
//raise JsonError.Create(SysErrorMessage(GetLastError));
Result.FStatus := rsErrorJsonParse;
Result.FMessage := e.Message;
FJo2.Free;
JArrayA.Free;
FJo.Free;
JArray.Free;
FJSON.Free;
Exit;
end;
end;
try
Result.FStatus := rsRecognizeDone;
Result.FMessage := '';
if not FJo2.isNull('transcript') then
begin
Result.FTranscript := FJo2.optString('transcript');
if EnableLogs then WriteInLog(WorkPath, Format('%s: Array alternative[%d] transcript = %s', [FormatDateTime('dd.mm.yy hh:mm:ss', Now), 0, FJo2.optString('transcript')]));
end;
if not FJo2.isNull('confidence') then
begin
FormatSettings.DecimalSeparator := '.';
Result.FConfidence := FJo2.optDouble('confidence')*100;
if EnableLogs then WriteInLog(WorkPath, Format('%s: Array alternative[%d] confidence = %s', [FormatDateTime('dd.mm.yy hh:mm:ss', Now), 0, FJo2.optString('confidence')]));
end;
finally
FJo2.Free;
end;
end;
finally
JArrayA.Free;
end;
end;
finally
FJo.Free;
end;
end;
finally
JArray.Free;
end;
end;
finally
FJSON.Free;
end;
end;
end;
function TGoogleRecognizer.HTTPGetSize(var HTTP: THTTPSend; URL: String): int64;
var
I: Integer;
Size: String;
Ch: Char;
begin
Result := -1;
HTTP.Document.Clear;
HTTP.Headers.Clear;
HTTP.UserAgent := 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36';
if HTTP.HTTPMethod('HEAD',URL) then
begin
for I := 0 to HTTP.Headers.Count - 1 do
begin
if Pos('content-length', lowercase(HTTP.Headers[I])) > 0 then
begin
Size := '';
for Ch in HTTP.Headers[i]do
if Ch in ['0'..'9'] then
Size := Size + Ch;
Result := StrToInt(Size) + Length(HTTP.Headers.Text);
Break;
end;
end;
end;
end;
function TGoogleRecognizer.HTTPGetSize(URL: String): int64;
const
CRLF = #$0D + #$0A;
var
Size: String;
HTTP: THTTPSend;
begin
Result := -1;
HTTP := THTTPSend.Create;
try
if FUseProxy then
begin
HTTP.ProxyHost := FProxyAddress;
if FProxyPort <> '' then
HTTP.ProxyPort := FProxyPort
else
HTTP.ProxyPort := '3128';
if FProxyAuth then
begin
HTTP.ProxyUser := FProxyAuthUserName;
HTTP.ProxyPass := FProxyAuthPassword;
end;
end;
HTTP.Document.Clear;
HTTP.Headers.Clear;
HTTP.MimeType := 'application/x-www-form-urlencoded';
HTTP.UserAgent := 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36';
if HTTP.HTTPMethod('HEAD', URL) then
begin
HeadersToList(HTTP.Headers);
Size := HTTP.Headers.Values['Content-Length'];
Result := StrToIntDef(Size, -1);
if Result > -1 then
Result := Result + Length(HTTP.Headers.Text);
end;
finally
HTTP.Free;
end;
end;
end.