Since I don’t make any new posts for quite a while now, I decided to reward you all with a new tutorial, “How to make a Rapid-Share up-loader”, with this tutorial you will learn how to code a application capable of uploading, automatically, files to, the well known file-hosts, “rapid-share” and “easy-share” and it will return the “download” and “delectation” links.
(The code is made in Delphi)
To start with, we need to analyze the form which rapid-share uses to submit the files to their server, this can be done by seeing the page source code:
Let’s take a look at the form code:
<form action="http://rs241l3.rapidshare.com/cgi-bin/upload.cgi?rsuploadid=149017462993654077" enctype="multipart/form-data" method="post"> <div id="progbar" style="display:none;"> <div style="font-size:8pt;">You are uploading:</div> .</div> <div id="dateiwahl"> <table style="font-size:8pt;color:#002760;" border="0"> <tbody> <tr> <td align="left"> <div id="files"> <a style="font-size:8pt;color:#002760;" href="javascript:switchfiles()">Do you want to upload several files? Please click here</a> | Maximum upload size 200 MB | Split archives allowed! <input id="dateiname" name="filecontent" size="65" type="file" /></div></td> </tr> </tbody></table> <div id="infotextfeld" style="display: block; width: 400px; text-align: left;">Share your files for FREE via RapidShare.com! <a href="http://rapidshare.com/howto_upload.html" target="_blank">Upload assistance</a> <ol style="color:#002760;"> <li><strong>Select file and click the upload button</strong></li> <li>Share download link</li> </ol> Delete your files any time you want.</div> <input id="btnupload" style="display:none;" name="u" src="/img2/upload_file.jpg" type="image" /></div> </form>
Let’s remove everything we don’t need:
<form action=”http://rs241l3.rapidshare.com/cgi-bin/upload.cgi?rsuploadid=149017462993654077″ enctype=”multipart/form-data” method=”post”>
<input id=”dateiname” name=”filecontent” size=”65″ type=”file” />
</form>
Finally we can analyze the data:
action=”http://rs241l3 .rapidshare.com/cgi-bin/upload.cgi -> This is the URL we will use to make the post. In Brown you can find the rapid-share server, there’s more then one, our application will use them all.
rsuploadid=149017462993654077 -> This isn’t really necessary, though since we want to make a pretty uploader we will use it.
enctype=”multipart/form-data” -> Obviously the form enctype is a “multipart/form-data”
name=”filecontent” ->This is the variable which will “store” the file and then be sent to the server.
Now that we have all the information we can start coding our app
In delphi, start a new project:
Once you’ve done this, make your form look like this:
Now drop a TidHTTP component on the form:
and also drop a OpenDialog component:
Let’s configure the indy events:
Make it like this:
Now that our “IdHTTP” component is configured let’s proceed.
Like you saw on the first step, we will need to extract text from inside HTML tags, I created a easy-to-understand function that we will use in this tutorial:
add the following in your uses “StrUtils”
function TfmMain.GetInnerHTML(FullText: WideString; TagBegin: string; TagEnd: string): string;
var
position: integer;
middle: string;
begin
position := AnsiPos(TagBegin, FullText) + Length(TagBegin); { Get to know where the dam tag is }
middle := AnsiMidStr(FullText, position, Length(FullText)); { Cut the text behind the tag }
position := AnsiPos(TagEnd, middle)-1; { Get to know where's the ending tag }
result := AnsiLeftStr(middle, position); { Cut the text in front of the ending tag }
end;
Now we need to get the uploading link, let’s use our function:
procedure TfmMain.btnUploadFileClick(Sender: TObject);
var
UploadLink: String;
HTMLtoCheck : WideString;
begin
{ Get the upload link }
HTMLtoCheck := IdHTTP1.Get('http://www.rapidshare.com');
UploadLink := GetInnerHTML(HTMLtoCheck,'action="','"');
{ Clear the whole HTML Text to free up some memory }
HTMLtoCheck := '';
end;
the link is on our hands so let’s get the file dialog working, so that we can make the uploading code:
procedure TfmMain.btnBrowseFileClick(Sender: TObject); begin OpenDialog1.Filter := 'All Format Files | *.*'; if OpenDialog1.Execute then begin txtFile.Text := OpenDialog1.FileName; end; end;
the button done, let’s continue:
add the following in your uses “IdMultipartFormData”
procedure TfmMain.btnUploadFileClick(Sender: TObject);
var
UploadLink: String;
HTMLtoCheck : WideString;
FormStuff : TIdMultiPartFormDataStream; {multi-part-form post data}
begin
{ Get the upload link }
HTMLtoCheck := IdHTTP1.Get('http://www.rapidshare.com');
UploadLink := GetInnerHTML(HTMLtoCheck,'action="','"');
HTMLtoCheck := '';
FormStuff := TIdMultiPartFormDataStream.Create; {Create the multi-part-form class}
try
FormStuff.AddFile('filecontent',txtFile.Text,'multipart/form-data'); {add the file}
HTMLtoCheck := IdHTTP1.Post(UploadLink,FormStuff); {make the post}
finally
FreeAndNil(FormStuff); {Free and nil the class}
end;
UploadLink := ''; {free some memory}
end;
all we need now is to get the download and delete link:
procedure TfmMain.btnUploadFileClick(Sender: TObject);
var
UploadLink: String;
HTMLtoCheck : WideString;
FormStuff : TIdMultiPartFormDataStream; {multi-form post data}
begin
{ Get the upload link }
HTMLtoCheck := IdHTTP1.Get('http://www.rapidshare.com');
UploadLink := GetInnerHTML(HTMLtoCheck,'action="','"');
HTMLtoCheck := '';
{ MAKE THE UPLOAD }
FormStuff := TIdMultiPartFormDataStream.Create; {Create the MultiPartForm class}
try
FormStuff.AddFile('filecontent',txtFile.Text,'multipart/form-data'); {add the file}
HTMLtoCheck := IdHTTP1.Post(UploadLink,FormStuff); {make the post}
finally
FreeAndNil(FormStuff); {Free and nil the class}
end;
UploadLink := ''; {free some memory}
{ GET THE LINKS }
txtDownloadLink.Text := GetInnerHTML(HTMLtoCheck,'<div>','</div>');
txtDeleteLink.Text := GetInnerHTML(HTMLtoCheck,'<div>','</div>');
end;
And voilà! You got yourself a rapid-share fully automatic uploader!
I’ve attached the full project (exe + source + some modifications) :