%PDF- %PDF-
Direktori : /www/old2/_music/hotfile/ |
Current File : /www/old2/_music/hotfile/hotfileDownloader.cs |
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; using System.Text.RegularExpressions; using System.ComponentModel; namespace hotfile { class hotfileDownloader { private const int threads = 6; private int id; private string user; private string pass; CookieCollection cookies = new CookieCollection(); Queue<string> queue = new Queue<string>(); System.ComponentModel.BackgroundWorker[] bw = new System.ComponentModel.BackgroundWorker[threads]; public hotfileDownloader(int id, string user, string pass) { this.id = id; this.user = user; this.pass = pass; } public void addUrl(string addr) { this.queue.Enqueue(addr); } public void doDownloads() { for (int i = 0; i < threads; i++) { this.bw[i] = new BackgroundWorker(); this.bw[i].DoWork += new DoWorkEventHandler(this.bw_DoWork); this.bw[i].RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); this.bw[i].WorkerReportsProgress = false; this.bw[i].WorkerSupportsCancellation = false; } this.assignJobs(); while (this.queue.Count != 0) { System.Threading.Thread.Sleep(3 * 1000); } for (int i = 0; i < threads; i++) { if (this.bw[i].IsBusy) { i--; System.Threading.Thread.Sleep(5 * 1000); } } } private void bw_DoWork(object sender, DoWorkEventArgs e) { if (this.queue.Count > 0) { string addr = this.queue.Dequeue(); this.download(addr); } } private void assignJobs() { for (int i = 0; i < threads; i++) { if (!this.bw[i].IsBusy) { this.bw[i].RunWorkerAsync(); } } } private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (this.queue.Count == 0) { return; } this.assignJobs(); } public void login() { CookieContainer cc = new CookieContainer(); System.Net.ServicePointManager.Expect100Continue = false; byte[] buff = System.Text.ASCIIEncoding.ASCII.GetBytes(String.Format("user={0}&pass={1}&Login", this.user, this.pass)); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://hotfile.com/login.php"); req.CookieContainer = cc; req.Method = "POST"; req.KeepAlive = false; req.UserAgent = "curl/7.18.2(x86_64-pc-linux-gnu)libcurl/7.18.2OpenSSL/0.9.8gzlib/1.2.3.3libidn/1.8libssh2/0.18"; req.ContentLength = buff.Length; req.ContentType = "application/x-www-form-urlencoded"; req.Referer = "http://hotfile.com"; req.GetRequestStream().Write(buff, 0, buff.Length); req.AllowAutoRedirect = false; HttpWebResponse res = (HttpWebResponse)req.GetResponse(); if (res.Cookies.Count > 0) { this.cookies = res.Cookies; } } private void download(string addr) { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(addr); req.KeepAlive = false; req.Headers.Add(HttpRequestHeader.Cookie, this.cookies[0].ToString()); req.UserAgent = req.UserAgent = "curl/7.18.2(x86_64-pc-linux-gnu)libcurl/7.18.2OpenSSL/0.9.8gzlib/1.2.3.3libidn/1.8libssh2/0.18"; bool isDone = false; HttpWebResponse res = null; while (!isDone) { try { res = (HttpWebResponse)req.GetResponse(); isDone = true; } catch { Console.WriteLine("[{0}] Error getting address {1}, waiting 120 seconds", DateTime.Now.ToLongTimeString(), addr); System.Threading.Thread.Sleep(120 * 1000); } } StreamReader sr = new StreamReader(res.GetResponseStream()); string txt = sr.ReadToEnd(); sr.Close(); Regex reg = new System.Text.RegularExpressions.Regex("http://hotfile.com/get/[0-9]+/[0-9a-f]+/[0-9a-f]+/([0-9a-z\\.\\-_]+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Compiled); Match m = reg.Match(txt); res.Close(); Console.WriteLine("[{1}] Starting download of {0} ...", m.Groups[1].ToString(), DateTime.Now.ToLongTimeString()); // Get the file req = (HttpWebRequest)HttpWebRequest.Create(m.ToString()); req.KeepAlive = false; req.Headers.Add(HttpRequestHeader.Cookie, this.cookies[0].ToString()); req.UserAgent = req.UserAgent = "curl/7.18.2(x86_64-pc-linux-gnu)libcurl/7.18.2OpenSSL/0.9.8gzlib/1.2.3.3libidn/1.8libssh2/0.18"; req.Referer = addr; res = (HttpWebResponse)req.GetResponse(); this.ReadFully(res.GetResponseStream(), m.Groups[1].ToString()); Console.WriteLine("[{1}] Download of {0} completed", m.Groups[1].ToString(), DateTime.Now.ToLongTimeString()); } private void ReadFully(Stream stream, string outfile) { int initialLength = 0; int num = 1; // If we've been passed an unhelpful initial length, just // use 64K. if (initialLength < 1) { initialLength = 512*1024; } // File init if (File.Exists(outfile)) { while (File.Exists(String.Format("{0}.{1}", num))) num++; outfile = String.Format("{0}.{1}", num); } FileStream os = File.OpenWrite(outfile); byte[] buffer = new byte[initialLength]; int read = 0; try { int chunk; while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0) { read += chunk; // If we've reached the end of our buffer, check to see if there's // any more information if (read == buffer.Length) { int nextByte = stream.ReadByte(); // End of stream? If so, we're done if (nextByte == -1) { // Write data to outfile os.Write(buffer, 0, read); os.Close(); return; } // Nope. Write read data to output os.Write(buffer, 0, read); os.WriteByte((byte)nextByte); read = 0; } } } catch { Console.Error.WriteLine("Error reading from the socket"); } os.Write(buffer, 0, read); os.Close(); } } }