Advertisement
Guest User

Untitled

a guest
May 18th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  7. Vcl.Graphics, vcl.Controls, Vcl.Forms, Vcl.Dialogs, Winapi.WinInet;
  8.  
  9. type
  10. TEventWorkStart = procedure (Sender : TObject; iFileSize : UInt64) of object;
  11. TEventWork = procedure (Sender : TObject; iBytesTransfered : Int64) of object;
  12. TEventWorkEnd = procedure (Sender : TObject; iBytesTransfered : Int64;
  13. ErrorCode : Integer) of object;
  14.  
  15. TWinApiDownload = class(TObject)
  16. private
  17. fEventWorkStart : TEventWorkStart;
  18. fEventWork : TEventWork;
  19. fEventWorkEnd : TEventWorkEnd;
  20. public
  21. FileNameOutput : string;
  22. URL : string;
  23. fUserAgent : string;
  24. fStop : Boolean;
  25. fProgressUpdateInterval : Cardinal;
  26. constructor Create;
  27. destructor Destroy; override;
  28. function Download(Stream : TStream) : Integer;
  29. procedure Stop;
  30. procedure Clear;
  31. property UserAgent : string read fUserAgent write fUserAgent;
  32. property OnWorkStart : TEventWorkStart read fEventWorkStart write fEventWorkStart;
  33. property OnWork : TEventWork read fEventWork write fEventWork;
  34. property OnWorkEnd : TEventWorkEnd read fEventWorkEnd write fEventWorkEnd;
  35. end;
  36.  
  37. TForm1 = class(TForm)
  38. procedure FormCreate(Sender: TObject);
  39. private
  40. { Private declarations }
  41. procedure DownloadWorkStart(Sender : TObject; iFileSize : UInt64);
  42. procedure DownloadWork(Sender : TObject; iBytesTransfered : Int64);
  43. procedure DownloadWorkEnd(Sender : TObject; iBytesTransfered : Int64;
  44. ErrorCode : Integer);
  45. public
  46. { Public declarations }
  47. end;
  48.  
  49. const
  50. DOWNLOAD_ERROR_UNKNOWN = -1;
  51. DOWNLOAD_ABORTED_BY_USER = -2;
  52.  
  53. var
  54. Form1: TForm1;
  55.  
  56. implementation
  57.  
  58. {$R *.dfm}
  59.  
  60. constructor TWinApiDownload.Create;
  61. begin
  62. inherited;
  63. fUserAgent := 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101';
  64. fProgressUpdateInterval := 100;
  65. end;
  66.  
  67. destructor TWinApiDownload.Destroy;
  68. begin
  69. Stop;
  70. inherited;
  71. end;
  72.  
  73. function TWinApiDownload.Download(Stream : TStream) : Integer;
  74. var
  75. hInet : HINTERNET;
  76. hUrl : HINTERNET;
  77. buf : array [0..1023 * 3] of Byte;
  78. lpdwNumberOfBytesAvailable : DWORD;
  79. dwBufferLen, dwIndex : DWORD;
  80. pSize, pErrorCode : array [0..255] of Char;
  81. b, iter : Cardinal;
  82. transfered : Int64;
  83. begin
  84. Result := DOWNLOAD_ERROR_UNKNOWN;
  85. fStop := False;
  86.  
  87. hInet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_PRECONFIG,
  88. nil, nil, 0);
  89. if Assigned(hInet) then
  90. begin
  91. hUrl := InternetOpenUrl(hInet, PChar(URL), nil, 0,0,0);
  92. if Assigned(hUrl) then
  93. begin
  94. dwIndex := 0;
  95. dwBufferLen := 20;
  96. HttpQueryInfo(hUrl, HTTP_QUERY_STATUS_CODE, @pErrorCode, dwBufferLen, dwIndex);
  97. Result := StrToInt(pErrorCode);
  98. if Result <> 200 then
  99. begin
  100. InternetCloseHandle(hUrl);
  101. InternetCloseHandle(hInet);
  102. Exit;
  103. end;
  104. dwIndex := 0;
  105. dwBufferLen := 20;
  106. if HttpQueryInfo(hUrl, HTTP_QUERY_CONTENT_LENGTH, @pSize,
  107. dwBufferLen, dwIndex) then
  108. // begin
  109. if Assigned(OnWorkStart) then
  110. OnWorkStart(Self, StrToInt(pSize));
  111. // end;
  112. iter := 0;
  113. transfered := 0;
  114. repeat
  115. if InternetQueryDataAvailable(hUrl,
  116. lpdwNumberOfBytesAvailable, 0, 0) then
  117. begin
  118. if lpdwNumberOfBytesAvailable > 0 then
  119. begin
  120. if InternetReadFile(hUrl, @buf, SizeOf(buf), b) then
  121. begin
  122. transfered := transfered + b;
  123. Stream.WriteBuffer(buf, b);
  124. if Assigned(OnWork) then
  125. begin
  126. inc(iter);
  127. if iter >= fProgressUpdateInterval then
  128. begin
  129. OnWork(Self, transfered);
  130. iter := 0;
  131. end;
  132. end;
  133. end;
  134. end;
  135. end;
  136. until (lpdwNumberOfBytesAvailable = 0) or (b = 0) or (fStop);
  137. if fStop then
  138. Result := DOWNLOAD_ABORTED_BY_USER;
  139. if Assigned(fEventWorkEnd) then
  140. OnWorkEnd(Self, transfered, Result);
  141. InternetCloseHandle(hUrl);
  142. end;
  143. InternetCloseHandle(hInet);
  144. end;
  145. end;
  146.  
  147. procedure TWinApiDownload.Stop;
  148. begin
  149. fStop := True;
  150. end;
  151.  
  152. procedure TForm1.FormCreate(Sender: TObject);
  153. const
  154. MyURL = 'https://vod-secure.twitch.tv/35062d653e0f40c2b455_miramisu_33375945456_1154634538/chunked/';
  155. var
  156. i, c : Integer;
  157. d : TWinApiDownload;
  158. stream : TStream; //эта переменная должна быть глобальной, но для примера не важно
  159. begin
  160. stream := TFileStream.Create('D:\test.ts', fmCreate or fmOpenReadWrite);
  161. d := TWinApiDownload.Create;
  162. d.OnWorkStart := DownloadWorkStart;
  163. d.OnWork := DownloadWork;
  164. d.OnWorkEnd := DownloadWorkEnd;
  165. for I := 50 to 300 do
  166. begin
  167. d.URL := MyURL + IntToStr(i) + '.ts';
  168. c := d.Download(stream);
  169. if c <> 200 then
  170. Break;
  171. end;
  172. stream.Free;
  173. d.Free;
  174. end;
  175.  
  176. procedure TForm1.DownloadWorkStart(Sender: TObject; iFileSize: UInt64);
  177. begin
  178. //скачивание начато
  179. end;
  180.  
  181. procedure TForm1.DownloadWork(Sender: TObject; iBytesTransfered: Int64);
  182. begin
  183. {
  184. .....
  185. обновляем индикаторы скачивания
  186. iBytesTransfered - количество скачанных байт текущего файла
  187. .....
  188. }
  189. Application.ProcessMessages;
  190. end;
  191.  
  192. procedure TForm1.DownloadWorkEnd(Sender: TObject;
  193. iBytesTransfered: Int64; ErrorCode: Integer);
  194. begin
  195. //скачивание завершено
  196. end;
  197.  
  198. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement