public static void Download(object handle) { //будем крутить цикл, пока не закончатся ULR в очереди while (true) { try { string URL; string titt; string desss; //блокируем очередь URL и достаем оттуда один адрес lock (URLlocker) { if (URLs.Count == 0) { break;//адресов больше нет, выходим из метода, завершаем поток } else { vidosi tmpvid = URLs.Dequeue(); URL = tmpvid.url; titt = tmpvid.title.Replace("\\", ""); desss = titt + " " + tmpvid.desc.Replace("\\", ""); } } Console.WriteLine(URL + " - start downloading ..."); //скачиваем страницу if (titt == "No name" || titt == "no name") { continue; } if (URL.IndexOf(".flv") != -1) { continue; } string filename = titt.Replace(@"\", "").Replace(@"/", "").Replace(@":", "").Replace(@"*", "").Replace(@"?", "").Replace("\"", "").Replace(@"<", "").Replace(@">", "").Replace(@"|", "").Replace(@".", ""); int bytesss = Downloadfile(URL, AppDomain.CurrentDomain.BaseDirectory + "video\\" + filename + ".mp4"); lock (HTMLlocker) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "video\\upload.csv", true, Encoding.UTF8)) { if (desss == "") { desss = titt; } //file.WriteLine(titt + "05452" + desss + "05452" + titt.Replace(@"\", " ").Replace(@"/", " ") + ".mp4"); file.WriteLine(titt + "05452" + desss + "05452" + titt + "05452" + "People" + "05452" + "TRUE" + "05452" + AppDomain.CurrentDomain.BaseDirectory + "video\\" + titt.Replace(@"\", "").Replace(@"/", "").Replace(@":", "").Replace(@"*", "").Replace(@"?", "").Replace("\"", "").Replace(@"<", "").Replace(@">", "").Replace(@"|", "").Replace(@".", "") + ".mp4"); } } Console.WriteLine(URL + " - downloaded (" + bytesss + " bytes)"); } catch (ThreadAbortException) { //это исключение возникает если главный поток хочет завершить приложение //просто выходим из цикла, и завершаем выполнение break; } }
static void Main(string[] args) { string post = "email=" + "*****@*****.**" + "&pass="******"Qwertyui1" + "&q=1&act=login&q=1&al_frame=1&expire=&captcha_sid=&captcha_key=&from_host=vk.com&from_protocol=http&ip_h=4e78766a2890ac1115&quick_expire=1"; string html = GetHtml(@"https://vk.com/", ""); html = GetHtml(@"https://login.vk.com/?act=login", post); Console.WriteLine(html); Regex rex4 = new Regex(@"parent\.onLoginDone\(\'(.*?)\'\)", RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); Match matc4 = rex4.Match(html); string userid = matc4.Groups[1].ToString().Replace("/id", ""); Console.WriteLine("Id: " + userid); //html = GetHtml(@"https://vk.com/id" + userid, ""); while (true) { URLs.Clear(); html = GetHtml(@"http://vk.com/feed?q=has%3Avideo§ion=search", ""); File.WriteAllText(@"WriteText.txt", html); // тайтлы <b class="fl_l video"></b><span class="a post_video_title">(.*?)</span> <span class="post_video_duration"> // видосы href="/video(.*?)\?list (убрать - и дубликаты из массива) int status = Testlogin(html); Console.WriteLine("Результат: " + status); //(.*?) Regex postReg = new Regex(@"return showVideo\(\'(?<video>.*?)\', \'(?<list>.*?)\', \{autoplay", RegexOptions.Singleline); MatchCollection matches = postReg.Matches(html); Console.WriteLine(matches.Count); if (matches.Count > 0) { foreach (Match mat in matches) { string link = "http://vk.com/video" + mat.Groups["video"].Value + "?list=" + mat.Groups["list"].Value; Console.WriteLine(link); using (System.IO.StreamWriter file = new StreamWriter("file.txt", true, Encoding.Default)) { file.WriteLine(link); } string HTML = GetHtml(link, ""); Regex mp4Reg = new Regex(@"http:\\\\\\\/\\\\\\\/(?<cs>.*?).vk.me\\\\\\\/(?<uu>.*?)\\\\\\\/videos\\\\\\\/(?<fb>.*?).mp4", RegexOptions.Singleline); MatchCollection mp4Matches = mp4Reg.Matches(HTML); if (mp4Matches.Count > 0) { //получение ссылки на видео string urlvideo = ""; foreach (Match mata in mp4Matches) { //urlvideo = "http" + mata.Groups["urlVideo"].Value.Replace(@"\\\", "") + ".mp4"; urlvideo = "http://" + mata.Groups["cs"].Value + ".vk.me/" + mata.Groups["uu"].Value + "/videos/" + mata.Groups["fb"].Value + ".mp4"; using (System.IO.StreamWriter file = new StreamWriter("videos.txt", true, Encoding.Default)) { file.WriteLine(urlvideo); } } Console.WriteLine(""); //получение тайтла видео string title = ""; Regex titleReg = new Regex("<title>(?<titleVideo>.*?)</title>", RegexOptions.Singleline); MatchCollection titleMatches = titleReg.Matches(HTML); if (titleMatches.Count > 0) { foreach (Match mata2 in titleMatches) { title = HttpUtility.HtmlDecode(mata2.Groups["titleVideo"].Value); } } Console.WriteLine("Title: " + title); //получение тайтла видео string descr = ""; Regex descrReg = new Regex("<meta name=\"description\" content=\"(?<descrVideo>.*?)\" />", RegexOptions.Singleline); MatchCollection descrMatches = descrReg.Matches(HTML); if (descrMatches.Count > 0) { foreach (Match mata3 in descrMatches) { descr = HttpUtility.HtmlDecode(mata3.Groups["descrVideo"].Value); } } Console.WriteLine("Description: " + descr); Console.WriteLine("UrlVideo: " + urlvideo); Console.WriteLine(""); vidosi vds = new vidosi(); vds.title = title.Replace("\\", ""); vds.desc = descr.Replace("\\", ""); vds.url = urlvideo; URLs.Enqueue(vds); //Download(urlvideo, AppDomain.CurrentDomain.BaseDirectory + "video\\" + title + ".mp4"); } Console.WriteLine(mp4Matches.Count + " m"); Thread.Sleep(1000); } } //создаем массив хендлеров, для контроля завершения потоков ManualResetEvent[] handles = new ManualResetEvent[10]; //создаем и запускаем 3 потока for (int i = 0; i < 10; i++) { handles[i] = new ManualResetEvent(false); (new Thread(new ParameterizedThreadStart(Download))).Start(handles[i]); } //ожидаем, пока все потоки отработают WaitHandle.WaitAll(handles); //проверяем ошибки, если были - выводим foreach (Exception ex in exceptions) { Console.WriteLine(ex.Message); } Console.WriteLine("Download completed"); //Console.ReadLine(); } Console.ReadKey(); }