using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Xml; using System.Text.RegularExpressions; // ReSharper disable PossibleNullReferenceException namespace Uploader { class Program { static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Proper Usage: Uploader ..."); return; } var folder = args[0]; var files = new String[args.Length - 1]; Array.Copy(args, 1, files, 0, args.Length - 1); var web = new CookieWebClient(); //Must get the ukey by loading the root first web.DownloadString("http://www.mediafire.com/"); /* * Now we do a login so we can get session, user, etc.. * Content Type must be "application/x-www-form-urlencoded" */ web.Headers.Set(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded"); web.UploadString("http://www.mediafire.com/dynamic/login.php", "POST", String.Format("login_email={0}&login_pass={1}&login_remember=on", "EMAIL ADDRESS", "PASSWORD")); /* * Need to get the 'session_token', Sadly this is in the javascript that is on the main page. * It is a 72-byte long hash of some kind. Lucally, its the only 72-byte long hash on the page, * so a simple regex can snag it. * * Also, we MUST set the user agent, else the javascript won't be sent. */ 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"); var ret = web.DownloadString("http://www.mediafire.com/myfiles.php"); var reg = Regex.Match(ret, "[0-9a-fA-F]{144}"); /* * Now, we need to create the folder, and get its key. */ ret = web.DownloadString(String.Format("http://www.mediafire.com/api/folder/create.php?foldername={0}&session_token={1}&version=1", folder, reg.Value)); var xml = new XmlDocument(); xml.LoadXml(ret); var folderKey = xml.SelectSingleNode("/response/upload_key").InnerText; var rets = files.Select(file => Upload(web, folderKey, file)).ToList(); while (rets.Count > 0) { foreach (var info in rets.ToArray()) { ret = web.DownloadString(String.Format("http://www.mediafire.com/basicapi/pollupload.php?key={0}&MFULConfig={1}", info[1], info[2])); xml.LoadXml(ret); /* var builder = new StringBuilder(); using (var writer = new StringWriter(builder)) { using (var xmlWriter = new XmlTextWriter(writer)) { xmlWriter.Formatting = Formatting.Indented; xml.Save(xmlWriter); } } Console.WriteLine(builder); */ var key = xml.SelectSingleNode("/response/doupload/quickkey").InnerText; if (key.Length <= 0) continue; Console.WriteLine("Download URL {0} http://www.mediafire.com/download.php?{1}", Path.GetFileName(info[0]), key); rets.Remove(info); } } Console.ReadLine(); } private static String[] Upload(CookieWebClient web, String folder, String file) { var xml = new XmlDocument(); var keys = new Dictionary(); xml.LoadXml(web.DownloadString("http://www.mediafire.com/basicapi/uploaderconfiguration.php?")); keys.Add("user", xml.SelectSingleNode("/mediafire/config/user").InnerText); keys.Add("ukey", xml.SelectSingleNode("/mediafire/config/ukey").InnerText); keys.Add("upload_session", xml.SelectSingleNode("/mediafire/config/upload_session").InnerText); keys.Add("trackkey", xml.SelectSingleNode("/mediafire/config/trackkey").InnerText); keys.Add("folderkey", (folder ?? xml.SelectSingleNode("/mediafire/config/folderkey").InnerText)); keys.Add("mful_config", xml.SelectSingleNode("/mediafire/MFULConfig").InnerText); String[] query = { "type=basic", "ukey=" + keys["ukey"], "user=" + keys["user"], "uploadkey=" + keys["folderkey"], "filenum=0", "uploader=0", "MFULConfig=" + keys["mful_config"] }; 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"); var ret = Encoding.ASCII.GetString(web.UploadFile("http://www.mediafire.com/douploadtoapi?" + String.Join("&", query), file)); xml.LoadXml(ret); return new[]{ file, xml.SelectSingleNode("/response/doupload/key").InnerText, keys["mful_config"] }; } public class CookieWebClient : WebClient { public CookieContainer Cookies = new CookieContainer(); protected override WebRequest GetWebRequest(Uri address) { var request = (HttpWebRequest)base.GetWebRequest(address); if (request != null) { request.CookieContainer = Cookies; request.KeepAlive = true; ServicePointManager.Expect100Continue = false; } return request; } } } }