Download from SourceForge/fr: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 1 | + | Vous pouvez utiliser la bibliothèque de communication réseau [[Synapse]] pour télécharger les fichiers depuis SourceForge. Parce que SourceForge stocke les fichiers sur de multiples mirroirs, vous devez traiter les redirections. | |
| 2 | + | ``` | |
| 6 | 3 | ... | |
| 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 | - | ||
| 4 | + | uses httpsend {a Synapse unit} | |
| 5 | + | ... | |
| 6 | + | function DownloadHTTPStream(URL: string; Buffer: TStream): boolean; | |
| 7 | + | // Download file; retry if necessary. | |
| 8 | + | const | |
| 9 | + | MaxRetries = 3; | |
| 10 | + | var | |
| 11 | + | RetryAttempt: integer; | |
| 12 | + | HTTPGetResult: boolean; | |
| 55 | 13 | begin | |
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 14 | + | Result:=false; | |
| 15 | + | RetryAttempt := 1; | |
| 16 | + | HTTPGetResult := False; | |
| 17 | + | while ((HTTPGetResult = False) and (RetryAttempt < MaxRetries)) do | |
| 59 | 18 | begin | |
| 60 | - | ||
| 61 | - | ||
| 19 | + | HTTPGetResult := HttpGetBinary(URL, Buffer); | |
| 20 | + | //Application.ProcessMessages; | |
| 21 | + | Sleep(100 * RetryAttempt); | |
| 22 | + | RetryAttempt := RetryAttempt + 1; | |
| 23 | + | end; | |
| 24 | + | if HTTPGetResult = False then | |
| 25 | + | raise Exception.Create('Cannot load document from remote server'); | |
| 26 | + | Buffer.Position := 0; | |
| 27 | + | if Buffer.Size = 0 then | |
| 28 | + | raise Exception.Create('Downloaded document is empty.'); | |
| 29 | + | Result := True; | |
| 30 | + | end; | |
| 31 | + | ||
| 32 | + | function SFDirectLinkURL(URL: string; Document: TMemoryStream): string; | |
| 33 | + | { | |
| 34 | + | Transform this part of the body: | |
| 35 | + | <noscript> | |
| 36 | + | <meta http-equiv="refresh" content="5; url=http://downloads.sourceforge.net/project/base64decoder/base64decoder/version%202.0/b64util.zip?r=&ts=1329648745&use_mirror=kent"> | |
| 37 | + | </noscript> | |
| 38 | + | into a valid URL: | |
| 39 | + | http://downloads.sourceforge.net/project/base64decoder/base64decoder/version%202.0/b64util.zip?r=&ts=1329648745&use_mirror=kent | |
| 40 | + | } | |
| 41 | + | const | |
| 42 | + | Refresh='<meta http-equiv="refresh"'; | |
| 43 | + | URLMarker='url='; | |
| 44 | + | var | |
| 45 | + | Counter: integer; | |
| 46 | + | HTMLBody: TStringList; | |
| 47 | + | RefreshStart: integer; | |
| 48 | + | URLStart: integer; | |
| 49 | + | begin | |
| 50 | + | HTMLBody:=TStringList.Create; | |
| 51 | + | try | |
| 52 | + | HTMLBody.LoadFromStream(Document); | |
| 53 | + | for Counter:=0 to HTMLBody.Count-1 do | |
| 62 | 54 | begin | |
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 55 | + | // This line should be between noscript tags and give the direct download locations: | |
| 56 | + | RefreshStart:=Ansipos(Refresh, HTMLBody[Counter]); | |
| 57 | + | if RefreshStart>0 then | |
| 58 | + | begin | |
| 59 | + | URLStart:=AnsiPos(URLMarker, HTMLBody[Counter])+Length(URLMarker); | |
| 60 | + | if URLStart>RefreshStart then | |
| 61 | + | begin | |
| 62 | + | // Look for closing " | |
| 63 | + | URL:=Copy(HTMLBody[Counter], | |
| 64 | + | URLStart, | |
| 65 | + | PosEx('"',HTMLBody[Counter],URLStart+1)-URLStart); | |
| 66 | + | infoln('debug: new url after sf noscript:'); | |
| 67 | + | infoln(URL); | |
| 68 | + | break; | |
| 69 | + | end; | |
| 70 | + | end; | |
| 70 | 71 | end; | |
| 72 | + | finally | |
| 73 | + | HTMLBody.Free; | |
| 71 | 74 | end; | |
| 75 | + | result:=URL; | |
| 72 | 76 | end; | |
| 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 | - | ||
| 77 | + | ||
| 78 | + | function SourceForgeURL(URL: string): string; | |
| 79 | + | // Detects sourceforge download and tries to deal with | |
| 80 | + | // redirection, and extracting direct download link. | |
| 81 | + | // Thanks to | |
| 82 | + | // Ocye: http://lazarus.freepascal.org/index.php/topic,13425.msg70575.html#msg70575 | |
| 83 | + | const | |
| 84 | + | SFProjectPart = '//sourceforge.net/projects/'; | |
| 85 | + | SFFilesPart = '/files/'; | |
| 86 | + | SFDownloadPart ='/download'; | |
| 87 | + | var | |
| 88 | + | HTTPSender: THTTPSend; | |
| 89 | + | i, j: integer; | |
| 90 | + | FoundCorrectURL: boolean; | |
| 91 | + | SFDirectory: string; //Sourceforge directory | |
| 92 | + | SFDirectoryBegin: integer; | |
| 93 | + | SFFileBegin: integer; | |
| 94 | + | SFFilename: string; //Sourceforge name of file | |
| 95 | + | SFProject: string; | |
| 96 | + | SFProjectBegin: integer; | |
| 112 | 97 | begin | |
| 113 | - | ||
| 114 | - | ||
| 115 | - | ||
| 98 | + | // Detect SourceForge download; e.g. from URL | |
| 99 | + | // 1 2 3 4 5 6 7 8 9 | |
| 100 | + | // 1234557890123456789012345578901234567890123455789012345678901234557890123456789012345578901234567890 | |
| 101 | + | // http://sourceforge.net/projects/base64decoder/files/base64decoder/version%202.0/b64util.zip/download | |
| 102 | + | // ^^^project^^^ ^^^directory............^^^ ^^^file^^^ | |
| 103 | + | FoundCorrectURL:=true; //Assume not a SF download | |
| 104 | + | i:=Pos(SFProjectPart, URL); | |
| 105 | + | if i>0 then | |
| 116 | 106 | begin | |
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 123 | - | ||
| 124 | - | ||
| 125 | - | ||
| 126 | - | ||
| 107 | + | // Possibly found project; now extract project, directory and filename parts. | |
| 108 | + | SFProjectBegin:=i+Length(SFProjectPart); | |
| 109 | + | j := PosEx(SFFilesPart, URL, SFProjectBegin); | |
| 110 | + | if (j>0) then | |
| 127 | 111 | begin | |
| 128 | - | ||
| 129 | - | ||
| 130 | - | ||
| 131 | - | ||
| 112 | + | SFProject:=Copy(URL, SFProjectBegin, j-SFProjectBegin); | |
| 113 | + | SFDirectoryBegin:=PosEx(SFFilesPart, URL, SFProjectBegin)+Length(SFFilesPart); | |
| 114 | + | if SFDirectoryBegin>0 then | |
| 115 | + | begin | |
| 116 | + | // Find file | |
| 117 | + | // URL might have trailing arguments... so: search for first | |
| 118 | + | // /download coming up from the right, but it should be after | |
| 119 | + | // /files/ | |
| 120 | + | i:=RPos(SFDownloadPart, URL); | |
| 121 | + | // Now look for previous / so we can make out the file | |
| 122 | + | // This might perhaps be the trailing / in /files/ | |
| 123 | + | SFFileBegin:=RPosEx('/',URL,i-1)+1; | |
| 124 | + | ||
| 125 | + | if SFFileBegin>0 then | |
| 126 | + | begin | |
| 127 | + | SFFilename:=Copy(URL,SFFileBegin, i-SFFileBegin); | |
| 128 | + | //Include trailing / | |
| 129 | + | SFDirectory:=Copy(URL, SFDirectoryBegin, SFFileBegin-SFDirectoryBegin); | |
| 130 | + | FoundCorrectURL:=false; | |
| 131 | + | end; | |
| 132 | + | end; | |
| 132 | 133 | end; | |
| 133 | 134 | end; | |
| 134 | - | ||
| 135 | - | ||
| 136 | - | ||
| 137 | - | ||
| 138 | - | ||
| 139 | - | ||
| 140 | - | ||
| 141 | - | ||
| 142 | - | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - | ||
| 135 | + | ||
| 136 | + | if not FoundCorrectURL then | |
| 146 | 137 | begin | |
| 147 | - | ||
| 148 | - | ||
| 149 | - | ||
| 150 | - | ||
| 151 | - | ||
| 152 | - | ||
| 153 | - | ||
| 154 | - | ||
| 155 | - | ||
| 138 | + | try | |
| 139 | + | // Rewrite URL if needed for Sourceforge download redirection | |
| 140 | + | // Detect direct link in HTML body and get URL from that | |
| 141 | + | HTTPSender := THTTPSend.Create; | |
| 142 | + | //Who knows, this might help: | |
| 143 | + | HTTPSender.UserAgent:='curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18'; | |
| 144 | + | while not FoundCorrectURL do | |
| 145 | + | begin | |
| 146 | + | HTTPSender.HTTPMethod('GET', URL); | |
| 147 | + | infoln('debug: headers:'); | |
| 148 | + | infoln(HTTPSender.Headers.Text); | |
| 149 | + | case HTTPSender.Resultcode of | |
| 150 | + | 301, 302, 307: | |
| 156 | 151 | begin | |
| 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 | - | ||
| 152 | + | for i := 0 to HTTPSender.Headers.Count - 1 do | |
| 153 | + | if (Pos('Location: ', HTTPSender.Headers.Strings[i]) > 0) or | |
| 154 | + | (Pos('location: ', HTTPSender.Headers.Strings[i]) > 0) then | |
| 155 | + | begin | |
| 156 | + | j := Pos('use_mirror=', HTTPSender.Headers.Strings[i]); | |
| 157 | + | if j > 0 then | |
| 158 | + | URL := | |
| 159 | + | 'http://' + RightStr(HTTPSender.Headers.Strings[i], | |
| 160 | + | length(HTTPSender.Headers.Strings[i]) - j - 10) + | |
| 161 | + | '.dl.sourceforge.net/project/' + | |
| 162 | + | SFProject + '/' + SFDirectory + SFFilename | |
| 163 | + | else | |
| 164 | + | URL:=StringReplace( | |
| 165 | + | HTTPSender.Headers.Strings[i], 'Location: ', '', []); | |
| 166 | + | HTTPSender.Clear;//httpsend | |
| 167 | + | FoundCorrectURL:=true; | |
| 168 | + | break; //out of rewriting loop | |
| 169 | + | end; | |
| 170 | + | end; | |
| 171 | + | 100..200: | |
| 172 | + | begin | |
| 173 | + | //Assume a sourceforge timer/direct link page | |
| 174 | + | URL:=SFDirectLinkURL(URL, HTTPSender.Document); //Find out | |
| 175 | + | FoundCorrectURL:=true; //We're done by now | |
| 176 | + | end; | |
| 177 | + | 500: raise Exception.Create('No internet connection available'); | |
| 178 | + | //Internal Server Error ('+aURL+')'); | |
| 179 | + | else | |
| 180 | + | raise Exception.Create('Download failed with error code ' + | |
| 181 | + | IntToStr(HTTPSender.ResultCode) + ' (' + HTTPSender.ResultString + ')'); | |
| 182 | + | end;//case | |
| 183 | + | end;//while | |
| 184 | + | infoln('debug: resulting url after sf redir: *' + URL + '*'); | |
| 185 | + | finally | |
| 186 | + | HTTPSender.Free; | |
| 187 | + | end; | |
| 188 | + | end; | |
| 189 | + | result:=URL; | |
| 188 | 190 | end; | |
| 189 | - | ||
| 190 | - | ||
| 191 | - | ||
| 192 | 191 | ``` | |
| 193 | 192 | ||
| 194 | - | ||
| 195 | - | ||
| 196 | - | ||
| 193 | + | Then you can download file using standard method. | |
| 194 | + | ``` | |
| 197 | 195 | ... | |
| 198 | - | ||
| 199 | - | ||
| 200 | - | ||
| 201 | - | ||
| 202 | - | ||
| 203 | - | ||
| 204 | - | ||
| 205 | - | ||
| 206 | - | ||
| 207 | - | ||
| 208 | - | ||
| 209 | - | ||
| 210 | - | ||
| 211 | - | ||
| 212 | - | ||
| 213 | - | ||
| 214 | - | ||
| 196 | + | function DownloadHTTP(URL, TargetFile: string): boolean; | |
| 197 | + | // Download file; retry if necessary. | |
| 198 | + | // Deals with SourceForge download links | |
| 199 | + | var | |
| 200 | + | Buffer: TMemoryStream; | |
| 201 | + | begin | |
| 202 | + | result:=false; | |
| 203 | + | URL:=SourceForgeURL(URL); //Deal with sourceforge URLs | |
| 204 | + | try | |
| 205 | + | Buffer := TMemoryStream.Create; | |
| 206 | + | DownloadHTTPStream(URL, Buffer); | |
| 207 | + | Buffer.SaveToFile(TargetFile); | |
| 208 | + | result:=true; | |
| 209 | + | finally | |
| 210 | + | FreeAndNil(Buffer); | |
| 211 | + | end; | |
| 212 | + | end; | |
| 215 | 213 | ``` | |
| 216 | 214 | ||
| 217 | 215 | ## See also | |
| 218 | 216 | ||
| 219 | - | ||
| 217 | + | * [[Synapse]] | |
| 220 | 218 | ||
| 221 | 219 | --- | |
| 222 | - | ||
| 220 | + | *From the Free Pascal wiki, page "Download from SourceForge/fr", as the Internet Archive kept it on 2022-08-19. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
