SHARE
TWEET

C# MediaFire Uploader

LexManos Jan 26th, 2012 2,288 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Xml;
  8. using System.Text.RegularExpressions;
  9.  
  10. // ReSharper disable PossibleNullReferenceException
  11. namespace Uploader
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             if (args.Length < 2)
  18.             {
  19.                 Console.WriteLine("Proper Usage: Uploader <FolderName> <File1> <File2> ...");
  20.                 return;
  21.             }
  22.             var folder = args[0];
  23.             var files = new String[args.Length - 1];
  24.             Array.Copy(args, 1, files, 0, args.Length - 1);
  25.  
  26.             var web = new CookieWebClient();
  27.             //Must get the ukey by loading the root first
  28.             web.DownloadString("http://www.mediafire.com/");
  29.  
  30.             /*
  31.              * Now we do a login so we can get session, user, etc..
  32.              * Content Type must be "application/x-www-form-urlencoded"
  33.              */
  34.             web.Headers.Set(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
  35.             web.UploadString("http://www.mediafire.com/dynamic/login.php", "POST", String.Format("login_email={0}&login_pass={1}&login_remember=on", "EMAIL ADDRESS", "PASSWORD"));
  36.  
  37.             /*
  38.              * Need to get the 'session_token', Sadly this is in the javascript that is on the main page.
  39.              * It is a 72-byte long hash of some kind. Lucally, its the only 72-byte long hash on the page,
  40.              * so a simple regex can snag it.
  41.              *
  42.              * Also, we MUST set the user agent, else the javascript won't be sent.
  43.              */
  44.             web.Headers.Set(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2");
  45.             var ret = web.DownloadString("http://www.mediafire.com/myfiles.php");
  46.             var reg = Regex.Match(ret, "[0-9a-fA-F]{144}");
  47.  
  48.             /*
  49.              * Now, we need to create the folder, and get its key.
  50.              */
  51.             ret = web.DownloadString(String.Format("http://www.mediafire.com/api/folder/create.php?foldername={0}&session_token={1}&version=1", folder, reg.Value));
  52.             var xml = new XmlDocument();
  53.             xml.LoadXml(ret);
  54.             var folderKey = xml.SelectSingleNode("/response/upload_key").InnerText;
  55.  
  56.             var rets = files.Select(file => Upload(web, folderKey, file)).ToList();
  57.             while (rets.Count > 0)
  58.             {
  59.                 foreach (var info in rets.ToArray())
  60.                 {
  61.                     ret = web.DownloadString(String.Format("http://www.mediafire.com/basicapi/pollupload.php?key={0}&MFULConfig={1}", info[1], info[2]));
  62.                     xml.LoadXml(ret);
  63.                     /*
  64.                     var builder = new StringBuilder();
  65.                     using (var writer = new StringWriter(builder))
  66.                     {
  67.                         using (var xmlWriter = new XmlTextWriter(writer))
  68.                         {
  69.                             xmlWriter.Formatting = Formatting.Indented;
  70.                             xml.Save(xmlWriter);
  71.                         }
  72.                     }
  73.                     Console.WriteLine(builder);
  74.                     */
  75.                     var key = xml.SelectSingleNode("/response/doupload/quickkey").InnerText;
  76.                     if (key.Length <= 0) continue;
  77.                     Console.WriteLine("Download URL {0} http://www.mediafire.com/download.php?{1}", Path.GetFileName(info[0]), key);
  78.                     rets.Remove(info);
  79.                 }
  80.             }
  81.             Console.ReadLine();
  82.         }
  83.  
  84.         private static String[] Upload(CookieWebClient web, String folder, String file)
  85.         {
  86.             var xml = new XmlDocument();
  87.             var keys = new Dictionary<String, String>();
  88.             xml.LoadXml(web.DownloadString("http://www.mediafire.com/basicapi/uploaderconfiguration.php?"));
  89.  
  90.             keys.Add("user",           xml.SelectSingleNode("/mediafire/config/user").InnerText);
  91.             keys.Add("ukey",           xml.SelectSingleNode("/mediafire/config/ukey").InnerText);
  92.             keys.Add("upload_session", xml.SelectSingleNode("/mediafire/config/upload_session").InnerText);
  93.             keys.Add("trackkey",       xml.SelectSingleNode("/mediafire/config/trackkey").InnerText);
  94.             keys.Add("folderkey",     (folder ?? xml.SelectSingleNode("/mediafire/config/folderkey").InnerText));
  95.             keys.Add("mful_config",    xml.SelectSingleNode("/mediafire/MFULConfig").InnerText);
  96.  
  97.             String[] query = {
  98.                                "type=basic",
  99.                                "ukey=" + keys["ukey"],
  100.                                "user=" + keys["user"],
  101.                                "uploadkey=" + keys["folderkey"],
  102.                                "filenum=0",
  103.                                "uploader=0",
  104.                                "MFULConfig=" + keys["mful_config"]
  105.                            };
  106.             web.Headers.Set(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2");
  107.             var ret = Encoding.ASCII.GetString(web.UploadFile("http://www.mediafire.com/douploadtoapi?" + String.Join("&", query), file));
  108.             xml.LoadXml(ret);
  109.             return new[]{ file, xml.SelectSingleNode("/response/doupload/key").InnerText, keys["mful_config"] };
  110.         }
  111.  
  112.         public class CookieWebClient : WebClient
  113.         {
  114.             public CookieContainer Cookies = new CookieContainer();
  115.  
  116.             protected override WebRequest GetWebRequest(Uri address)
  117.             {
  118.                 var request = (HttpWebRequest)base.GetWebRequest(address);
  119.                 if (request != null)
  120.                 {
  121.                     request.CookieContainer = Cookies;
  122.                     request.KeepAlive = true;
  123.                     ServicePointManager.Expect100Continue = false;
  124.                 }
  125.                 return request;
  126.             }
  127.         }
  128.     }
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top