Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Runtime.CompilerServices;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Web;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using App_for_woobe.Annotations;
- using HtmlAgilityPack;
- using OpenQA.Selenium;
- using OpenQA.Selenium.PhantomJS;
- using OpenQA.Selenium.Remote;
- using OpenQA.Selenium.Support.Extensions;
- namespace App_for_woobe
- {
- /// <summary>
- /// Логика взаимодействия для MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window, INotifyPropertyChanged
- {
- private string _category;
- private bool _useProxy;
- private bool _authorizeProxy;
- public Dictionary<string, string> CityDictionary => App_for_woobe.Resources.CityDictionary;
- public Dictionary<string, string> CategoryDictionary => App_for_woobe.Resources.CategoryDictionary;
- public Dictionary<string, string> AutoTypeDictionary => App_for_woobe.Resources.AutoTypeDictionary;
- public Dictionary<string, string> MarksDictionary => Category == null || Category == "Авто" ? null : App_for_woobe.Resources.MarksDictionary[Category];
- public string City { get; set; }
- public string Category
- {
- get { return _category; }
- set { _category = value;
- OnPropertyChanged(nameof(AutoVisibility));
- OnPropertyChanged(nameof(MarksDictionary));
- }
- }
- public string Mark { get; set; }
- public List<string> AutoType { get; set; }
- public string AutoRepairType { get; set; }
- public string AutoMark { get; set; }
- public string Phone { get; set; }
- public List<Account> Accounts { get; set; } = new List<Account>();
- public int CommentScore { get; set; }
- public string CommentText { get; set; }
- public bool UseProxy
- {
- get { return _useProxy; }
- set { _useProxy = value; OnPropertyChanged(nameof(UseProxy)); }
- }
- public List<Proxy> Proxies { get; set; } = new List<Proxy>();
- public Visibility AutoVisibility => Category == "Авто" ? Visibility.Visible : Visibility.Collapsed;
- public event PropertyChangedEventHandler PropertyChanged;
- [NotifyPropertyChangedInvocator]
- protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- public MainWindow()
- {
- DataContext = this;
- InitializeComponent();
- }
- private void PortPreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- if (!char.IsDigit(e.Text, e.Text.Length - 1))
- e.Handled = true;
- }
- private void AccountsClick(object sender, RoutedEventArgs e)
- {
- var win = new AccountsAddWindow() { Accounts = Accounts };
- win.ShowDialog();
- if (win.DialogResult == true)
- Accounts = win.Accounts;
- }
- private void ProxiesClick(object sender, RoutedEventArgs e)
- {
- var win = new ProxyWindow() { Proxies = Proxies};
- win.ShowDialog();
- if(win.DialogResult == true)
- Proxies = win.Proxies;
- }
- private async void ScanClick(object sender, RoutedEventArgs e)
- {
- if (City == null || Category == null)
- {
- MessageBox.Show("Город или категория не задана.");
- return;
- }
- ScanBtn.IsEnabled = false;
- try
- {
- await Scan();
- }
- catch (Exception ee)
- {
- MessageBox.Show(ee.ToString(), "Ошибка");
- return;
- }
- finally
- {
- ScanBtn.IsEnabled = true;
- }
- }
- private IEnumerable<Proxy> ProxyNumerable()
- {
- int i = 0;
- while(true)
- while (i < Proxies.Count)
- yield return Proxies[i];
- }
- private IEnumerator<Proxy> _proxyNumerator;
- private PhantomJSDriver CreateDriver()
- {
- var service = PhantomJSDriverService.CreateDefaultService(Environment.CurrentDirectory);
- service.HideCommandPromptWindow = true;
- if (UseProxy)
- {
- _proxyNumerator.MoveNext();
- var proxy = _proxyNumerator.Current;
- service.Proxy = $"{proxy.IP}:{proxy.Port}";
- service.ProxyType = "socks5";
- if (proxy.Authorize)
- {
- service.ProxyAuthentication = $"{proxy.Login}:{proxy.Password}";
- }
- }
- return new PhantomJSDriver(service);
- }
- private async Task Scan(int count = 1)
- {
- var url = $"http://zoon.ru/{CityDictionary[City]}/{CategoryDictionary[Category]}/";
- if (!string.IsNullOrWhiteSpace(Mark))
- url += $"type/{MarksDictionary[Mark]}/";
- if(UseProxy)
- _proxyNumerator = ProxyNumerable().GetEnumerator();
- var items = await Task.Factory.StartNew(() =>
- {
- var dr = CreateDriver();
- dr.Url = url;
- var ret = GetItemsURL(dr, count);
- dr.Dispose();
- return ret;
- });
- const int taskNum = 10;
- for (var i = 0; i < items.Count + taskNum; i += taskNum)
- {
- var drivers = new List<PhantomJSDriver>();
- var tasks = items.Skip(i).Take(taskNum).Select(u => Task.Factory.StartNew(() =>
- {
- var dr = CreateDriver();
- drivers.Add(dr);
- Spam(dr, u, true, true);
- }));
- await Task.WhenAll(tasks.ToArray());
- foreach (var d in drivers)
- {
- d.Dispose();
- }
- }
- }
- private HashSet<string> GetItemsURL(PhantomJSDriver driver, int count)
- {
- var items = new HashSet<string>();
- while (items.Count < count)
- {
- var oldcount = items.Count;
- items.UnionWith(ParseHtml(driver));
- if (items.Count != 0 && items.Count == oldcount)
- {
- Thread.Sleep(500);
- continue;
- }
- try
- {
- var nextbtn = driver.FindElement(By.CssSelector("[class*='js-next-page']"));
- nextbtn.Click();
- Thread.Sleep(500);
- }
- catch (NoSuchElementException)
- {
- break;
- }
- }
- return new HashSet<string>(items.Take(count));
- }
- private static IEnumerable<string> ParseHtml(ISearchContext driver)
- {
- var items = driver.FindElements(By.CssSelector("[class*='service-item pd-m js-results-item']"));
- var ret = new List<string>();
- foreach (var i in items)
- {
- try
- {
- ret.Add(i.FindElement(By.CssSelector("[class*='js-item-url']")).GetAttribute("href"));
- }
- catch (NoSuchElementException) { continue; }
- }
- return ret;
- }
- private void Spam(RemoteWebDriver driver, string url, bool call, bool comment, Account account, int trycount = 0)
- {
- driver.Url = url;
- if (call)
- {
- try
- {
- var callmeopenbtn =
- driver.FindElement(
- By.CssSelector("[class*='button button-primary button-action action-button-target']"));
- callmeopenbtn.Click();
- Thread.Sleep(200);
- bool next = true;
- try
- {
- driver.ExecuteJavaScript($"document.getElementsByName(\"phone\")[0].value = \"{Phone}\"");
- Thread.Sleep(200);
- }
- catch
- {
- if (trycount == 5)
- return;
- Spam(driver, url, true, false, account, trycount++);
- next = false;
- }
- if (next)
- {
- var callmebtn =
- driver.FindElement(
- By.CssSelector("[class*='button button-purple button-primary button34 pull-left']"));
- callmebtn.Click();
- Thread.Sleep(200);
- }
- }
- catch (NoSuchElementException)
- {
- }
- }
- if (comment)
- {
- try
- {
- var cmntbtn = driver.FindElement(By.CssSelector("[class*='button-comment-add']"));
- cmntbtn.Click();
- Thread.Sleep(100);
- driver.ExecuteScript($"document.getElementsByName(\"content\")[0].value = \"{CommentText}\"");
- Thread.Sleep(100);
- var stars =
- driver.FindElement(By.CssSelector("[class*='comment-form-container']"))
- .FindElements(By.CssSelector("[class*='star-item']"));
- stars[CommentScore].Click();
- Thread.Sleep(200);
- var sendbtn = driver.FindElement(By.CssSelector("[class*='button button-primary pull-left mr10']"));
- sendbtn.Click();
- Thread.Sleep(100);
- var loginbtn = driver.FindElement(By.CssSelector("[class*='button-holder-auth zn js-login-zoon']"));
- sendbtn.Click();
- Thread.Sleep(100);
- driver.ExecuteScript($"document.getElementsByName(\"email\")[0].value = \"{Accounts[0].Login}\"");
- Thread.Sleep(20);
- driver.ExecuteScript($"document.getElementsByName(\"password\")[0].value = \"{Accounts[0].Password}\"");
- Thread.Sleep(20);
- driver.ExecuteScript(
- "document.getElementsByClassName(\"button button-large button-purple middle\")[0].click()");
- Thread.Sleep(100);
- }
- catch (NoSuchElementException)
- {
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement