1

I'm looking for the way to get the direct link from mediafire. By default, when a user visits a download link, he will be presented with a download page where he has to wait for the download to be processed and then a link will appear.

I googled and found a VB.NET 2008 solution to this using WebBrowser WB

http://www.vbforums.com/showthread.php?t=556681

It works pretty well but I'm tired of pop-up windows and the loading speed. So, I wonder if there is a solution to this problem? (a non WB solution ^^)

Any help is greatly appreciated.

Johan
  • 74,508
  • 24
  • 191
  • 319
ByulTaeng
  • 1,269
  • 1
  • 21
  • 40

3 Answers3

3

I'll be posting back when I've worked the regular expression into the code, not sure this is going to work though as I think the actual link is obtained through AJAX. I'm still playing with this.

AJAX concerns discussed: StackOverflow related question


Based on the php code provided in the comments:

  • 1st Response->fetch the value passed to a function called "cG(var1,var2,var3)" I don't think mediafire still uses that function, it seems it's called "cu(var1,var2,var3)" now, not sure if the php you gave will still work. anyway, we can do the same thing get the values from the cu function & post our request to http://www.mediafire.com/dynamic/download.php? with the cookie we retrieved from our first response.
  • The 2nd response creates this huge list of random generated variables, then generates the download url concatenating some of those variables, the only way to get the url out if this is by using the Microsoft.JScript engine to evaluate this code. I'll be posting my code asap

Code (warning this code is ugly & needs to be cleaned up):

  string sURL = "http://www.mediafire.com/?syzjuytmdkn";

  HttpWebRequest wrGETURL = (HttpWebRequest)WebRequest.Create(sURL);
  wrGETURL.CookieContainer = new CookieContainer();
  wrGETURL.Referer = "http://www.mediafire.com";
  wrGETURL.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

  HttpWebResponse wrResponse = (HttpWebResponse)wrGETURL.GetResponse();
  CookieCollection cookies = wrResponse.Cookies;

Here we send the first request & Store the cookies received. Next we want to parse the page to find out the keys for the 2nd request:

  StreamReader objReader = new StreamReader(wrResponse.GetResponseStream());

  string[] parameters = {};//will contain the parameters fetched
  string html = objReader.ReadToEnd();
  int cupos1 = html.IndexOf("cu(");
  int cupos2 = html.IndexOf("')",cupos1);
  string[] separators = { "','"};

  parameters = html.Substring(cupos1 + 4, cupos2 - cupos1 - 4)
                   .Split(separators, StringSplitOptions.None);

Fetch the 2nd page which will contain the encoded download url:

  string sURL2 = String.Format("http://www.mediafire.com/dynamic/download.php?qk={0}&pk={1}&r={2}",
                       parameters[0],parameters[1],parameters[2]);

  HttpWebRequest wrGETURL2 = (HttpWebRequest)WebRequest.Create(sURL2);
  wrGETURL2.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
  wrGETURL2.Referer = "http://www.mediafire.com";

  wrGETURL2.CookieContainer = new CookieContainer();
  wrGETURL2.CookieContainer.Add(cookies);
  wrResponse = (HttpWebResponse)wrGETURL2.GetResponse();
  objReader = new StreamReader(wrResponse.GetResponseStream());
  html = objReader.ReadToEnd();

This html contains the Javascript that will generate the download url, here we extract it, then evaluate it & finaly write it to the console:

  int varpos1 = html.IndexOf("<script language=\"Javascript\">")+35;
  //The variables are declared just before the 'function'
  int varpos2 = html.IndexOf("function",varpos1);
  string vardata = html.Substring(varpos1, varpos2 - varpos1);

  int hrefpos1 = html.IndexOf("href=\\\"http://", varpos2)+6 ;
  int hrefpos2 = html.IndexOf(">", hrefpos1);
  string hrefdata = String.Format("var url = {0};", html.Substring(hrefpos1, hrefpos2 - hrefpos1-5));
  object Result = EvalJScript(vardata + "\n" + hrefdata);
  Console.WriteLine(Result.ToString());

This stuff worked for me, but needs to be rewritten, I also leave the EvalJScript function for you to work as the one I'm using (from Evaluating JScript in c#) is deprecated

Community
  • 1
  • 1
Vincent De Smet
  • 4,859
  • 2
  • 34
  • 41
  • There is a PHP solution but I don't know PHP, so I posting here in case someone need it. http://www.mysteryzillion.org/forums/showthread.php?t=4381 – ByulTaeng Jul 24 '09 at 06:39
  • 1
    the PHP is very useful, apparently the mediafire code sets a cookie (which needs to be included when moving towards http://www.mediafire.com/dynamic/download.php) also, a cG function initializes qk, pk & r variables that need to be included in the URL Request. this will return a javascript that needs to be evaluated to generate the final url... – Vincent De Smet Jul 24 '09 at 07:09
  • Thanks, with your suggestion, I finally found the way to get the direct link using WebRequest :D. Thanks you again. – ByulTaeng Jul 24 '09 at 10:25
  • 1
    you're welcome, I just finished figuring it out as well. My code is ugle, do you mind posting some of your code? – Vincent De Smet Jul 24 '09 at 10:58
1
Dim req As HttpWebRequest, res As HttpWebResponse
Dim cok As New CookieContainer, str As String, match As Match
req = WebRequest.Create("http://www.mediafire.com/?65d1dftjwml")
req.CookieContainer = cok
res = req.GetResponse
str = New StreamReader(res.GetResponseStream).ReadToEnd
match = Regex.Match(str, "cu\('(.+)','(.+)','(.+)'\);")
Dim qk As String = match.Groups(1).Value
Dim pk As String = match.Groups(2).Value
Dim r As String = match.Groups(3).Value
Dim t As String = "http://www.mediafire.com/dynamic/download.php?qk=" & qk & "&pk=" & pk & "&r=" & r & "&ukey=" & res.Cookies.Item("ukey").Value

req = WebRequest.Create(t)
res = req.GetResponse
txtcode.Text = New StreamReader(res.GetResponseStream).ReadToEnd
ByulTaeng
  • 1,269
  • 1
  • 21
  • 40
  • Here is my code to retrieve the huge list of of random generated variables. The following step is just like your code. Thanks you very much for helping me ^^ – ByulTaeng Jul 24 '09 at 13:29
  • By accident, I found a little trick to get the final variables by using RegEX :D. var\s[a-z0-9]{6}='([a-z0-9]{11})'; – ByulTaeng Jul 24 '09 at 14:09
0

the final block of code on my original answer is cut off - it shows the C# code on evaluating jscript:

  int varpos1 = html.IndexOf("<script language=\"Javascript\">")+35;
  //The variables are declared just before the 'function'
  int varpos2 = html.IndexOf("function",varpos1);
  string vardata = html.Substring(varpos1, varpos2 - varpos1);

  int hrefpos1 = html.IndexOf("href=\\\"http://", varpos2)+6 ;
  int hrefpos2 = html.IndexOf(">", hrefpos1);
  string hrefdata = String.Format("var url = {0};", html.Substring(hrefpos1, hrefpos2 - hrefpos1-5));
  object Result = EvalJScript(vardata + "\n" + hrefdata);
  Console.WriteLine(Result.ToString());
Vincent De Smet
  • 4,859
  • 2
  • 34
  • 41