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
| unit WebImageDownloader_Processor;
interface
uses
System.Classes, System.Contnrs;
type
TWebImageDownloaderProcessorClass = class of TWebImageDownloaderProcessor;
TWebImageDownloaderProcessor = class(TObject)
private
class var FClassList: TClassList;
class var FWorkDir: string;
class var FProcesssLinkRecursive: Boolean;
protected
class procedure RegisterMe(); virtual; abstract;
class procedure RegisterClass(AClass: TWebImageDownloaderProcessorClass);
class procedure ProcessContent(const AURL: string; AContent: TStringStream; AProcessImage: Boolean; AProcessLink: Boolean; const ALinkSectionStart, ALinkSectionEnd: string);
class procedure ProcessImage(const AContent: string; IndexStart, IndexEnd: Integer; AImageURLs: TStringList); virtual;
class procedure ProcessLink(const AContent: string; IndexStart, IndexEnd: Integer; ALinks: TStringList); virtual;
class procedure DownloadFile(const AWebDir: string; const AURL: string);
class procedure GetInternetFile(const AServerName: string; const AURL: string; AStream: TStream); overload;
class procedure GetInternetFile(const AURL: string; AStream: TStream); overload;
public
class function FindClass(const AClassSuffix: string): TWebImageDownloaderProcessorClass;
class procedure ProcessURL(const AURL: string; AProcessImage: Boolean; AProcessLink: Boolean; const ALinkSectionStart, ALinkSectionEnd: string);
class property WorkDir: string read FWorkDir write FWorkDir;
class property ProcesssLinkRecursive: Boolean read FProcesssLinkRecursive write FProcesssLinkRecursive;
public
class destructor Destroy();
end;
implementation
uses
System.SysUtils, System.StrUtils,
IdHTTP, IdSSLOpenSSL, IdSSLOpenSSLHeaders,
Winapi.WinInet, Winapi.Windows;
class destructor TWebImageDownloaderProcessor.Destroy();
begin
FreeAndNil(FClassList);
end;
class procedure TWebImageDownloaderProcessor.DownloadFile(const AWebDir: string; const AURL: string);
function ExtractFileNameFromURL(const FileName: string): string;
var
I: Integer;
begin
I := FileName.LastDelimiter('/');
Result := FileName.SubString(I + 1);
Result := StringReplace(Result, '>', '_', [rfReplaceAll]);
Result := StringReplace(Result, '<', '_', [rfReplaceAll]);
Result := StringReplace(Result, ':', '_', [rfReplaceAll]);
Result := StringReplace(Result, '"', '_', [rfReplaceAll]);
Result := StringReplace(Result, '/', '_', [rfReplaceAll]);
Result := StringReplace(Result, '\', '_', [rfReplaceAll]);
Result := StringReplace(Result, '|', '_', [rfReplaceAll]);
Result := StringReplace(Result, '?', '_', [rfReplaceAll]);
Result := StringReplace(Result, '*', '_', [rfReplaceAll]);
Result := StringReplace(Result, '.jpg_c=2', '.jpg', [rfIgnoreCase]);
end;
function ExcludeTrailingURL(const S: string): string;
begin
Result := S;
if (Length(Result) >= 1) and (Result[Length(Result)] = '/') then
SetLength(Result, Length(Result)-1);
end;
var
LocalDir, LocalName: string;
DownloadStream: TFileStream;
Downloader: TIdHTTP;
begin
LocalDir := IncludeTrailingPathDelimiter(FWorkDir) + ExtractFileNameFromURL(ExcludeTrailingURL(AWebDir));
ForceDirectories(LocalDir);
LocalName := IncludeTrailingPathDelimiter(LocalDir) + ExtractFileNameFromURL(AURL);
DownloadStream := TFileStream.Create(LocalName, fmCreate);
try
Downloader := TIdHTTP.Create(nil);
try
if StartsText('https:', AURL) then
Downloader.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(Downloader);
try
Downloader.Get(AURL, DownloadStream);
except
on E: EIdOSSLUnderlyingCryptoError do
GetInternetFile(AURL, DownloadStream);
end;
finally
Downloader.Free();
end;
finally
DownloadStream.Free();
end;
end;
class function TWebImageDownloaderProcessor.FindClass(const AClassSuffix: string): TWebImageDownloaderProcessorClass;
var
I: Integer;
begin
Result := nil;
if Assigned(FClassList) then
for I := 0 to FClassList.Count - 1 do
if SameText(FClassList[I].ClassName(), TWebImageDownloaderProcessor.ClassName() + AClassSuffix) then
Exit(TWebImageDownloaderProcessorClass(FClassList[I]));
end;
class procedure TWebImageDownloaderProcessor.GetInternetFile(const AURL: string; AStream: TStream);
var
I1, I2: Integer;
ServerName: string;
URI: string;
begin
I1 := Pos('//', AURL);
Inc(I1, 2);
I2 := Pos('/', AURL, I1);
if (I1 > 0) and (I2 > I1) then
begin
ServerName := Copy(AURL, I1, I2 - I1);
URI := Copy(AURL, I2 + 1);
GetInternetFile(ServerName, URI, AStream);
end;
end;
class procedure TWebImageDownloaderProcessor.GetInternetFile(const AServerName: string; const AURL: string; AStream: TStream);
const
BufferSize = 1024;
accept: packed array[0..1] of LPWSTR = (PChar('text/*'), nil); // PCTSTR rgpszAcceptTypes[] = {_T(text/*), NULL};
var
hSession, hHTTP, hReq : HINTERNET;
Buffer: array[1..BufferSize] of Byte;
BufferLen: DWORD;
sAppName: string;
begin
AStream.Size := 0;
sAppName := ExtractFileName(ParamStr(0));
hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
hHTTP := InternetConnect(hSession, PChar(AServerName), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 1);
try
hReq := HttpOpenRequest(hHTTP, PChar('GET'), PChar(AURL), nil, nil, @accept, 0, 1);
try
if HttpSendRequest(hReq, nil, 0, nil, 0) then
begin
BufferLen := 0;
repeat
if InternetReadFile(hReq, @Buffer, BufferSize, BufferLen) then
AStream.WriteBuffer(Buffer, BufferLen);
until BufferLen = 0;
end
else
raise Exception.Create('HttpOpenRequest failed. ' + SysErrorMessage(GetLastError));
finally
InternetCloseHandle(hReq);
end;
finally
InternetCloseHandle(hHTTP);
end;
finally
InternetCloseHandle(hSession);
end;
end;
class procedure TWebImageDownloaderProcessor.ProcessContent(const AURL: string; AContent: TStringStream; AProcessImage: Boolean; AProcessLink: Boolean; const ALinkSectionStart, ALinkSectionEnd: string);
var
Content: string;
iStart, iEnd: Integer;
Images, Links: TStringList;
Link: string;
begin
Content := AContent.DataString;
if AProcessLink then
begin
iStart := Pos(ALinkSectionStart, Content);
if iStart > 0 then
begin
Inc(iStart, Length(ALinkSectionStart));
iEnd := Pos(ALinkSectionEnd, Content, iStart);
if iEnd > 0 then
begin
Links := TStringList.Create();
try
Links.Duplicates := dupIgnore;
Links.Sorted := True;
ProcessLink(Content, iStart, iEnd, Links);
Links.SaveToFile(IncludeTrailingPathDelimiter(FWorkDir) + 'Links.txt');
for Link in Links do
ProcessURL(Link, True, FProcesssLinkRecursive, '', '');
finally
Links.Free();
end;
end
else
raise Exception.CreateFmt('End of Section "%s" not found', [ALinkSectionEnd]);
end
else
raise Exception.CreateFmt('Start of Section "%s" not found', [ALinkSectionStart]);
end;
if AProcessImage then
begin
Images := TStringList.Create();
try
ProcessImage(Content, 1, Length(Content), Images);
Images.SaveToFile(IncludeTrailingPathDelimiter(FWorkDir) + 'Images.txt');
for Link in Images do
DownloadFile(AURL, Link);
finally
Images.Free();
end;
end;
end;
class procedure TWebImageDownloaderProcessor.ProcessImage(const AContent: string; IndexStart, IndexEnd: Integer; AImageURLs: TStringList);
const
IMG_START = '<img src="';
IMG_END = '/>';
IMG_SUB_END = '" ';
var
iStart, iEnd, iSub: Integer;
URL: string;
begin
iStart := IndexStart;
while iStart < IndexEnd do
begin
iStart := Pos(IMG_START, AContent, iStart);
if (iStart > 0) and (iStart <= IndexEnd) then
begin
Inc(iStart, Length(IMG_START));
iEnd := Pos(IMG_END, AContent, iStart);
if (iEnd > iStart) and (iEnd <= IndexEnd) then
begin
URL := Copy(AContent, iStart, iEnd - iStart);
iSub := Pos(IMG_SUB_END, URL);
if iSub > 0 then
URL := Copy(URL, 1, iSub - 1);
AImageURLs.Add(URL);
end
else
Exit;
end
else
Exit;
iStart := iEnd + Length(IMG_END);
end;
end;
class procedure TWebImageDownloaderProcessor.ProcessLink(const AContent: string; IndexStart, IndexEnd: Integer; ALinks: TStringList);
const
REF_START = '<a href="';
REF_END = '">';
REF_SUB_END = '" ';
var
iStart, iEnd, iSub: Integer;
URL: string;
begin
iStart := IndexStart;
while iStart < IndexEnd do
begin
iStart := Pos(REF_START, AContent, iStart);
if (iStart > 0) and (iStart <= IndexEnd) then
begin
Inc(iStart, Length(REF_START));
iEnd := Pos(REF_END, AContent, iStart);
if (iEnd > iStart) and (iEnd <= IndexEnd) then
begin
URL := Copy(AContent, iStart, iEnd - iStart);
iSub := Pos(REF_SUB_END, URL);
if iSub > 0 then
URL := Copy(URL, 1, iSub - 1);
ALinks.Add(URL);
end
else
Exit;
end
else
Exit;
iStart := iEnd + Length(REF_END);
end;
end;
class procedure TWebImageDownloaderProcessor.ProcessURL(const AURL: string; AProcessImage: Boolean; AProcessLink: Boolean; const ALinkSectionStart, ALinkSectionEnd: string);
var
WebPage: TIdHTTP;
WebContent: TStringStream;
begin
WebPage := TIdHTTP.Create(nil);
try
if StartsText('https:', AURL) then
WebPage.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(WebPage);
WebContent := TStringStream.Create();
try
try
WebPage.Get(AURL, WebContent);
except
on E: EIdOSSLUnderlyingCryptoError do
GetInternetFile(AURL, WebContent);
end;
ProcessContent(AURL, WebContent, AProcessImage, AProcessLink, ALinkSectionStart, ALinkSectionEnd);
finally
WebContent.Free();
end;
finally
WebPage.Free();
end;
end;
class procedure TWebImageDownloaderProcessor.RegisterClass(AClass: TWebImageDownloaderProcessorClass);
begin
// Not Thread-Safe !
if not Assigned(FClassList) then
FClassList := TClassList.Create();
FClassList.Add(AClass);
end;
end. |
Partager