C# MediaFire Uploader


SUBMITTED BY: Guest

DATE: Nov. 11, 2013, 9:02 p.m.

FORMAT: Text only

SIZE: 6.6 kB

HITS: 834

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

comments powered by Disqus