void Routine() { this.work = true; this.Save(Application.StartupPath + '\\' + "amongstyle.(^_^)"); DateTime maint = DateTime.Now; for (; localindex < actlist.Count; localindex++) { try { if (actlist[localindex].Path.Text.Length > 0) { DateTime t = DateTime.Now; actlist[localindex].EnsureVisible(); actlist.prototype.Proc((actpair = actlist[localindex])); actlist.SetName("В отстойнике : " + actlist.prototype.OtstoyCount()); if ((DateTime.Now - t).TotalSeconds > 60) Save(Application.StartupPath + "\\conv.(O_o)"); Application.DoEvents(); } } catch (Exception e) { MessageBox.Show(e.Message,"Routine"); } //catch { Save(Application.StartupPath + "\\crash.(O_o)"); } } if (actlist.OnOffState == CheckState.Indeterminate) actlist.OnOffState = CheckState.Unchecked; localindex = 0; actpair = null; if (++this.globalindex >= styles.Count) this.globalindex = 0; if ((DateTime.Now - maint).TotalSeconds > 60) Save(Application.StartupPath + "\\conv.(O_o)"); this.work = false; }
/// <summary>Цикл поиска по страницам</summary> /// <returns>Массив найденых ссылок</returns> void PageSearch(StylePair obj,int min,int max) { if (max == 0) return; string link = masklink.Replace("*", obj.stylecode); for (serindex = min; serindex <= max; serindex++) { obj.Info.Text = "Поиск ( " + serindex + " из " + max + " ) Найдено : ~" + _findedresources.Count; string req = link.Replace("#", "" + serindex); if (!PagePatternSearch(obj.lastlink, req, _findedresources)) break; } serindex = -1; }
/// <summary>Поиск ссылок</summary> void Search(StylePair obj) { obj.Info.Text = "Поиск"; ptype = ProcType.SEARCH; if (pages) { int min, max; InitPages(obj.stylecode, out min, out max); //if (max < 1) max = 100; PageSearch(obj, serindex == -1 ? min : serindex, max); } else OffsetSearch(obj, offserindex.Length == 0 ? masklink : offserindex); for (int loop1 = 0; loop1 < _findedresources.Count; loop1++) for (int loop2 = loop1+1; loop2 < _findedresources.Count; loop2++) if(_findedresources[loop2]==_findedresources[loop1]) _findedresources.RemoveAt(loop2--); obj.Info.Text = ""; }
/// <summary>Цикл поиска со смещением на следующую страницу</summary> /// <returns>Массив найденых ссылок</returns> void OffsetSearch(StylePair obj,string lnk) { string offserindex = lnk; int page = 0; while (true) { obj.Info.Text = "Поиск ( Страниц обработано : " + (++page)+" ) Найдено : ~"+_findedresources.Count; offserindex = OffsetPatternSearch(obj.lastlink, offserindex, _findedresources); if (offserindex.Length == 0) break; } offserindex = ""; }
/// <summary>Закачка файлов</summary> void Download(StylePair obj) { Down.DownloadFlag res = Down.DownloadFlag.Sucess; string title; for (; index >=0; index--) { if (_findedresources[index] == "") continue; obj.Info.Text = "Осталось : " + (index+1) + " ( ошибок : " + (_findedresources.Count - (index + 1)) + " )"; try { if (obj.Path.Text.Length == 0) { _findedresources.Clear(); index = 0; return; } string lnk = GetLink(_findedresources[index]); string reff = lnk; for (int tr = 0; tr < maxtry; tr++) { obj.SubInfo.Text = "Попытка получения ссылки № " + (tr + 1) + " из " + maxtry; System.Windows.Forms.Application.DoEvents(); //string link = lnk == "Error" ? lnk : ((tr % 4 == 0) ? Down.__GetZippyShareLink4(lnk) : (tr % 4 == 1) ? Down.__GetZippyShareLink2(lnk) : (tr % 4 == 2) ? Down.__GetZippyShareLink1(lnk) : Down.__GetZippyShareLink3(lnk)); string link = lnk == "Error" ? lnk : Down.__GetZippyShareLinkUpdated(lnk); res = Down.DownloadFlag.None; if (link == "Deleted") { res = Down.DownloadFlag.Sucess; logs.Text = "\t[" + link + "] " + lnk + "\r\n" + logs.Text; break; } if (link == "Not exist") continue; if (link == "Error") { logs.Text = "\t\t[" + link + "] " + lnk + "\r\n" + logs.Text; break; } res = Down._Downloadfile(300, reff, link, obj.Path.Text, obj, logs, out title); obj.SubInfo.Text = ""; if (res == Down.DownloadFlag.Sucess) { logs.Text = "[Downloaded] " + title + "\r\n" + logs.Text; break; } else if (res == Down.DownloadFlag.ConnectionClosed) { logs.Text = "[Server fault] " + title + "\r\n" + logs.Text; break; } else logs.Text = "\t\t[" + link + "] " + lnk + "\r\n" + logs.Text; #region Out of empty space if (res == Down.DownloadFlag.EmptySpaceNeed) { //TODO: учесть возможность перехода на другой таск DateTime date = DateTime.Now; DriveInfo di = new DriveInfo(obj.Path.Text[0] + ":\\"); long tempfree = di.AvailableFreeSpace; obj.SubInfo.Text = "Места нету. Ожидаю пока появится..."; while (true) { System.Threading.Thread.Sleep(10000); di = new DriveInfo(obj.Path.Text[0] + ":\\"); if (tempfree != di.AvailableFreeSpace) break; if ((DateTime.Now - date).TotalMinutes > 30) System.Windows.Forms.Application.Exit(); } } #endregion if (tr + 1 == maxtry) if (Pause != 0) System.Threading.Thread.Sleep(Pause); } if (res == Down.DownloadFlag.Sucess || res == Down.DownloadFlag.ConnectionClosed) _findedresources.RemoveAt(index); } catch (Exception e) { /*if (e.Message == "Invalid URI: The URI is empty.") { if (_findedresources[0] == "") { obj.lastlink = (index + 1 < _findedresources.Count) ? _findedresources[index + 1] : _findedresources[index]; _findedresources.RemoveRange(0, index+1); index = -1; } else break; } else System.Windows.Forms.MessageBox.Show(e.Message);*/ } } obj.Info.Text = obj.SubInfo.Text = ""; int ind = GetO(obj.Name.Text); if(ind!=-1) ots[ind].links = _findedresources.ToArray(); else ots.Add(new Otstoy(_findedresources, obj)); _findedresources.Clear(); }
/// <summary>Запуск новой закачки файлов</summary> public void Proc(StylePair obj) { if (logs == null) logs = (System.Windows.Forms.TextBox)Form1.logs; if (ptype != ProcType.DOWNLOAD) { currstyle = obj.Name.Text; Search(obj); if (_findedresources.Count > 0) { obj.lastlink = _findedresources[0]; _findedresources.Insert(0, ""); } AddO(GetO(currstyle)); index = _findedresources.Count - 1; } else { //откат на несколько позиций в списке ссылок для избежания потерь if (index + 2 < _findedresources.Count) index += 2; else index = _findedresources.Count - 1; } obj.Pro2.Visible = true; ptype = ProcType.DOWNLOAD; Download(obj); ptype = ProcType.NONE; obj.Pro2.Visible = false; currstyle=obj.Info.Text = ""; }
public Otstoy(List<string> links, StylePair pair, int cycleready = 0) { this.links = links.ToArray(); objname = pair.Name.Text; cycles = cycleready; }
public static DownloadFlag _Downloadfile(int pingvalue, string referer, string URL, string path, StylePair style, System.Windows.Forms.TextBox logs, out string clear) { HttpWebRequest req = null; HttpWebResponse res = null; Uri url = new Uri(URL); Stream stream = null; FileStream file = null; clear = CLEAR(url.LocalPath); clear = (clear == "") ? CLEAR(URL) : clear; clear = clear.Replace("+", " ").Replace(":", "").Replace("?", "").Replace("<", "").Replace(">", "").Replace("*", ""); path = path + (path[path.Length - 1] != '\\' ? "\\" : "") + clear; byte[] buf; long temp = 12000, summ = 0, len = -1; int read = 0, tr = 0; style.Pro2.Clear(); try { req = (HttpWebRequest)WebRequest.Create(url); req.Timeout = timeout; req.UserAgent = agent; //req.UserAgent = "Mozila/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; MyIE2;"; req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; req.Headers.Add("Accept-Language", "ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3"); req.Headers.Add("Accept-Encoding", "gzip,deflate"); req.Headers.Add("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.7"); req.KeepAlive = true; //req.ReadWriteTimeout = pingvalue;//200;// 500;//1000; req.Referer = referer; if (Cookies != null && Cookies.Count > 0) (req.CookieContainer = new CookieContainer()).Add(Cookies); res = (HttpWebResponse)req.GetResponse(); len = res.ContentLength; if (len < 0) return DownloadFlag.WrongLink; style.Pro2.Max = (int)len; string inf = FileSizeView(len) + " * " + clear; style.SubInfo.Text = inf; System.Windows.Forms.Application.DoEvents(); if (File.Exists(path)) { FileInfo f = new FileInfo(path); if (f.Length != len) File.Delete(path); else { res.Close(); return DownloadFlag.Sucess; } } ////////////////////////////////////////////////// DriveInfo di = new DriveInfo(path[0]+":\\"); if (di.AvailableFreeSpace <= len) { while (true) { if (System.Windows.Forms.MessageBox.Show(style.Path, "На колотушке нету места однако.\r\n Выбрать другую папку ?\r\n В случае отказа ваше домашнее животное\r\n будет подвергнуто жестоким пыткам...", "Интимное предложение...", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { style.SetPathDialog(); di = new DriveInfo(style.Path.Text[0] + ":\\"); if (di.AvailableFreeSpace > len) { path = style.Path.Text + '\\' + clear; break; } } else { //фикс на случай если пользователь очистит место на диске пока высвечено окно запроса di = new DriveInfo(style.Path.Text[0] + ":\\"); if (di.AvailableFreeSpace < len) return DownloadFlag.EmptySpaceNeed; else break; } } } ////////////////////////////////////////////////// file = new FileStream(path, FileMode.Create); if (file == null) return DownloadFlag.WriteStreamOpenError; stream = res.GetResponseStream(); if (stream == null) return DownloadFlag.ReadStreamOpenError; DateTime d = DateTime.Now; double dod = 1; int alread = 0, uy = 1; double timelowlevel = 0.5d; while (true) { System.Threading.Thread.Sleep(40); //25 if (temp < 6000) temp = 6000; buf = new byte[temp]; try { if (dod >= timelowlevel) { d = DateTime.Now; alread = 0; } alread+=(read = stream.Read(buf, 0, buf.Length)); file.Write(buf, 0, read); //file.Flush(); dod = (DateTime.Now - d).TotalSeconds; if (dod >= timelowlevel) { uy = alread == 0 ? 0 : (int)(alread / dod); style.SubInfo.Text = inf + " ~ " + (uy == 0 ? "0" : FileSizeView(uy, ',', -1)) + "/сек"; System.Windows.Forms.Application.DoEvents(); } if ((summ += read) >= len) break; try { style.Pro2.SetValue((int)summ, uy); } catch { } temp = (int)(temp * ((read == temp) ? 1.1f : 0.98f)); tr = 0; } catch (Exception e) { //System.Windows.Forms.MessageBox.Show(e.Message); tr++; logs.Text = " try(" + tr + "/40) " + e.Message + "\r\n" + logs.Text; Boolean cc = e.Message == "Не удается прочитать данные из транспортного соединения: Соединение разорвано."; if (tr > 40 || cc) { if (file.SafeFileHandle != null) file.Close(); stream.Close(); File.Delete(path); return cc ? DownloadFlag.ConnectionClosed : DownloadFlag.ReadWriteError; } } } file.Flush(); if (file.SafeFileHandle != null) file.Close(); stream.Close(); res.Close(); style.Pro2.Clear(); } catch (Exception e) { //System.Windows.Forms.MessageBox.Show(e.Message); try { if (file != null) file.Close(); if (stream != null) stream.Close(); } catch { } return DownloadFlag.ReadWriteError; } if (len > 0 && summ < len) return DownloadFlag.OfflineError; if (summ < 8 * 1024) return DownloadFlag.ContentToSmall; return DownloadFlag.Sucess; }