/// <summary> /// Запрашивает работу с Hub по буллетинам (не инстанций) /// </summary> static void AskForAggregateBulletinWork() { DCT.Execute(d => { d._SessionInfo.HashUID = "Engine"; d._SessionInfo.SessionUID = "Engine"; using (var client = new EngineService()) { var result = client.Ping(); Console.WriteLine($"Ping = {result}"); client.CollectionLoad <AggregateBulletinPackage>(AskForAggregateBulletinWorkCallback); } }); }
public static IWebElement DoAction(By query, Action <IWebElement> after = null) { var result = default(IWebElement); DCT.Execute(d => { var element = driver.FindElement(query); if (element != null && after != null) { after(element); } result = element; }); return(result); }
public static string GetTitle(string type) { var result = ""; DCT.Execute(c => { switch (type) { default: result = "May be title?"; break; } }); return(result); }
public static byte[] ConvertImageToByte(BitmapSource Image) { byte[] result = null; DCT.Execute(data => { using (var ms = new MemoryStream()) { var be = new PngBitmapEncoder(); be.Frames.Add(BitmapFrame.Create(Image)); be.Save(ms); result = ms.ToArray(); } }); return(result); }
///------------------------------------------------------------------------------------------------- /// <summary> Запрашивает работу с Hub по профилю </summary> /// /// <remarks> SV Milovanov, 19.02.2018. </remarks> ///------------------------------------------------------------------------------------------------- static void AskForProfileWork() { DCT.Execute(d => { d._SessionInfo.HashUID = "Engine"; d._SessionInfo.SessionUID = "Engine"; using (var client = new EngineService()) { var result = client.Ping(); Console.WriteLine($"Ping = {result}"); client.CollectionLoad <AccessCache>(WorkRouter.AssignProfileWork); } }); }
public override string GetFieldValue(Dictionary <string, FieldPackage> fields, string name) { var result = string.Empty; DCT.Execute(d => { var fieldPackage = fields.FirstOrDefault(q => q.Key == name).Value; if (fieldPackage == null) { return; } var attribute = fieldPackage.HasId ? "id" : "name"; var form = WebDriver.FindMany(By.TagName(fieldPackage.Tag)).FirstOrDefault(q => q.GetAttribute(attribute) == fieldPackage.HtmlId); if (form == null) { if (fieldPackage.Tag == "select" && fieldPackage.Options != null && fieldPackage.Options.Length > 0) { var code = Regex.Match(fieldPackage.HtmlId, @"\d+").Value; form = WebDriver.FindMany(By.TagName("input")).Where(q => q.GetAttribute("name") == $"params[]{code}").FirstOrDefault(q => q.GetAttribute("checked") != null); var formValue = form.GetAttribute("value"); var option = fieldPackage.Options.FirstOrDefault(q => q.Value == formValue); if (option != null) { result = option.Text; } } return; } if (fieldPackage.Tag == "select" && fieldPackage.Options != null) { var selected = form.GetAttribute("value"); var optionTag = fieldPackage.Options.FirstOrDefault(q => q.Value == selected); if (optionTag != null) { result = optionTag.Text; } } else { result = form.GetAttribute("value"); } }); return(result); }
///------------------------------------------------------------------------------------------------- /// <summary> Авторизация на борде </summary> /// /// <remarks> SV Milovanov, 02.02.2018. </remarks> /// /// <returns> True if it succeeds, false if it fails. </returns> ///------------------------------------------------------------------------------------------------- public override bool Auth() { var result = false; DCT.Execute(data => { var access = data.Db1.Accesses.FirstOrDefault(q => q.BoardId == Uid && q.UserId == data.Objects.CurrentUser.Id && q.State == (int)AccessState.Activated); if (access == null) { return; } data.Objects.CurrentAccess = access; WebWorker.DownloadPage(LoginUrl, (doc) => { if (WebWorker.WebDocument != null) { var e = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>(); var loginForm = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>() .FirstOrDefault(q => q.GetAttribute("name") == "login"); if (loginForm != null) { loginForm.SetAttribute("value", access.Login); } var passwordForm = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>() .FirstOrDefault(q => q.GetAttribute("type") == "password"); if (passwordForm != null) { passwordForm.SetAttribute("value", access.Password); } var signIn = WebWorker.WebDocument.GetElementsByTagName("button").Cast <HtmlElement>() .FirstOrDefault(btn => btn.InnerText == "Войти" && btn.GetAttribute("type") == "submit"); if (signIn != null) { signIn.InvokeMember("click"); } } }); //Без принудительного ожидания даже с Application.DoEvents авторизация не сработает, если перейти на другую страницу WebWorker.JustWait(2); result = true; }); return(result); }
protected override bool Auth() { var result = false; DCT.Execute(d => { UiHelper.UpdateActionState("Поиск валидного прокси"); ProxyManager.UseProxy(); UiHelper.UpdateActionState("Навигация на страницу авторизации"); WebWorker.NavigatePage(LoginUrl); if (WebWorker.WebDocument != null) { UiHelper.UpdateActionState("Заполнение логина и пароля"); var e = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>(); var loginForm = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>() .FirstOrDefault(q => q.GetAttribute("name") == "login"); if (loginForm != null) { loginForm.SetAttribute("value", currentAccess.Login); } var passwordForm = WebWorker.WebDocument.GetElementsByTagName("input").Cast <HtmlElement>() .FirstOrDefault(q => q.GetAttribute("type") == "password"); if (passwordForm != null) { passwordForm.SetAttribute("value", currentAccess.Password); } var signIn = WebWorker.WebDocument.GetElementsByTagName("button").Cast <HtmlElement>() .FirstOrDefault(btn => btn.InnerText == "Войти" && btn.GetAttribute("type") == "submit"); if (signIn != null) { signIn.InvokeMember("click"); } } else { UiHelper.UpdateActionState("Страница авторизации is NULL"); Thread.Sleep(2000); } UiHelper.UpdateActionState("Ожидание авторизации..."); Thread.Sleep(15000); result = true; }); return(result); }
public static IEnumerable <BulletinTemplateCache> FilterByPattern(this IEnumerable <BulletinTemplateCache> collection, string pattern) { var result = collection; DCT.Execute(d => { // Исключаем шаблоны с запрещенными словами var temp = collection.Where(q => forbiddenWords.All(x => !q.Description.Contains(x)) && forbiddenWords.All(x => !q.Title.Contains(x))).ToArray(); // Фильтруем шаблоны по словам в тайтле var words = pattern.Split(' '); result = temp.Where(q => words.All(x => q.Title.Contains(x))).ToArray(); }); return(result); }
///------------------------------------------------------------------------------------------------- /// <summary> Загружает все группы и поля Board. </summary> /// /// <remarks> SV Milovanov, 02.02.2018. </remarks> ///------------------------------------------------------------------------------------------------- public override void ReloadGroups() { DCT.Execute(data => { //Exit(); //Auth(); Thread.Sleep(1000); WebWorker.DownloadPage("https://www.avito.ru/additem", (doc) => { var groupContainer = GroupContainerList.Get(Uid); groupContainer.Reinitialize(); }); }); }
static void ALMtest() { var almo = new ALMModel(); DCT.Execute(c => { almo._SetState(ALMModelState.First); }); DCT.Execute(c => { almo._SetState(ALMModelState.Create); almo._SetState(ALMModelState.First); almo._SetState(ALMModelState.Second); almo._SetState(ALMModelState.Third); }); }
public override void GetBulletinList(IEnumerable <TaskCache_old> tasks) { DCT.Execute(d => { var bulletins = new List <BulletinPackage>(); foreach (var task in tasks) { var access = task.AccessPackage; var r = GetBulletinList(access); bulletins.AddRange(r); task.State = (int)TaskCacheState.Completed; } //DriverTaskHelper.Complete(tasks); }); }
private static string SendGet(string url) { var result = ""; DCT.Execute(c => { using (var client = new HttpClient(new HttpClientHandler())) { client.Timeout = TimeSpan.FromMilliseconds(10000); var request = new HttpRequestMessage(HttpMethod.Get, url); var response = client.SendAsync(request).Result; result = response.Content.ReadAsStringAsync().Result; } }); return(result); }
static void AskForWork() { DCT.Execute(d => { d._SessionInfo.HashUID = "Engine"; d._SessionInfo.SessionUID = "Engine"; using (var client = new EngineService()) { var result = client.Ping(); Console.WriteLine($"Ping = {result}"); client.SendQueryCollection <TaskCache_old>("Load", AskForWorkCallback, objects: Enumerable.Empty <TaskCache_old>()); //client.CollectionLoad<TaskCache>(AskForWorkCallback); } }); }
/// <summary> Executes the given arguments. </summary> /// /// <remarks> AM Kozhevnikov, 07.02.2018. </remarks> /// /// <throwses cref="NullReferenceException"> Thrown when a value was unexpectedly null. </throwses> /// /// <param name="args"> The arguments. </param> static void execute(object args) { DCT.Execute(data => { if (args == null) { throw new NullReferenceException("CurrentDispatcher.Execute - Action не может быть пустым"); } var action = (Action)args; var sw = new Stopwatch(); sw.Start(); action?.Invoke(); sw.Stop(); //if (sw.ElapsedMilliseconds > 500) // DCT.SendInfo("DispatcherHelper очень долго выполнялась операция, продолжительность " + sw.ElapsedMilliseconds + Environment.NewLine + "Описание операции: " + action.ToString()); }); }
public override void GetBulletinDetails(IEnumerable <BulletinPackage> packages) { UiHelper.UpdateWorkState("Выгрузка полей для списка буллетинов"); DCT.Execute(d => { { var fieldValueContainer = FieldValueContainerList.Get(Uid); var accessContainer = AccessContainerList.Get(Uid); var accessCollection = packages.Cast <BulletinPackage>().Where(q => q.Access != null).GroupBy(q => q.Access.Login).Select(q => new { Access = q.Key, Collection = q.ToList() }).ToList(); foreach (var a in accessCollection) { UiHelper.UpdateObjectState($"Access {a.Access}"); var bulletins = a.Collection; foreach (var bulletin in bulletins) { UiHelper.UpdateActionState("Попытка авторизоваться"); Thread.Sleep(2000); if (accessContainer.TryAuth(bulletin.Access)) { Thread.Sleep(1000); var url = Path.Combine(bulletin.Url, "edit"); UiHelper.UpdateActionState($"Переход на страницу {url}"); Thread.Sleep(2000); var values = new Dictionary <string, string>(); foreach (var pair in bulletin.AccessFields) { var v = fieldValueContainer.GetFieldValue(bulletin.AccessFields, pair.Key); values.Add(pair.Key, v); } bulletin.ValueFields = values; bulletin.State = (int)CheckBulletinState(bulletin.Url); } } } } { UiHelper.UpdateActionState("Отправка коллбека"); Thread.Sleep(1000); } }); }
public override void EditBulletins(IEnumerable <BulletinPackage> packages) { DCT.Execute(d => { { var fieldValueContainer = FieldValueContainerList.Get(Uid); var accessContainer = AccessContainerList.Get(Uid); foreach (var bulletin in packages) { var name = bulletin.ValueFields["Название объявления"]; UiHelper.UpdateObjectState($"Bulletin {name}, state = {bulletin.State}"); UiHelper.UpdateActionState("Попытка авторизоваться"); if (accessContainer.TryAuth(bulletin.Access)) { UiHelper.UpdateActionState("Ожидание прогрузки страницы"); Thread.Sleep(2000); Tools.WebWorker.NavigatePage(Path.Combine(bulletin.Url, "edit")); UiHelper.UpdateActionState("Установка значений"); Thread.Sleep(1000); SetValueFields(bulletin, fieldValueContainer); Thread.Sleep(1000); ContinueAddOrEdit(EnumHelper.GetValue <BulletinState>(bulletin.State)); Thread.Sleep(1000); Publicate(bulletin); } } } { foreach (var b in packages) { b.State = (int)BulletinState.OnModeration; var name = b.ValueFields["Название объявления"]; UiHelper.UpdateObjectState($"Bulletin {name}, state = {b.State}"); Thread.Sleep(1000); } UiHelper.UpdateActionState("Отправка коллбека"); Thread.Sleep(1000); } }); }
void ContinueAddOrEdit(BulletinState state) { DCT.Execute(d => { if (state == BulletinState.Edited) { UiHelper.UpdateActionState("Выбор \"Продолжить без пакет\""); } else if (state == BulletinState.WaitPublication || state == BulletinState.WaitRepublication) { UiHelper.UpdateActionState("Выбор \"Продолжить с пакетом «Обычная продажа»\""); } else { UiHelper.UpdateActionState("Выбор \"Продолжить\""); } }); }
public static void Execute(Action action, bool isAsync = true) { DCT.Execute(data => { if (action == null) { throw new NullReferenceException("CurrentDispatcher.Execute - Action не может быть пустым"); } if (isAsync) { CurrentSynchronizationContext.Post((a) => execute(a), action); } else { CurrentSynchronizationContext.Send((a) => execute(a), action); } }); }
public void SetContent(ModalWindow content) { DCT.Execute((data) => { if (content == null) { IsEnable = false; } else { Window = content; DialogContent = Window.Content; IsEnable = true; } RaisePropertyChanged(() => IsEnable); RaisePropertyChanged(() => DialogContent); }); }
///------------------------------------------------------------------------------------------------- /// <summary> Handles the element. </summary> /// /// <remarks> SV Milovanov, 30.01.2018. </remarks> /// /// <param name="element"> The element. </param> /// <param name="currentLabel"> The current label. </param> /// <param name="labelClass"> The label class. </param> /// <param name="hiddenClass"> The hidden class. </param> ///------------------------------------------------------------------------------------------------- void HandleElement(HtmlElement element, string currentLabel, string labelClass, string hiddenClass) { if (!element.CanHaveChildren) { return; } DCT.Execute(data => { foreach (HtmlElement ch in element.Children) { if (ch.GetAttribute("className").Contains(hiddenClass)) { continue; } if (nextStage == ParsingBlockStage.None || nextStage == ParsingBlockStage.Label) { if (ch.GetAttribute("className").Contains(labelClass)) { if (ch.CanHaveChildren && ch.Children.Count > 0 && string.IsNullOrEmpty(ch.InnerText)) { var span = ch.Children[0]; currentLabel = span.InnerText; } else { currentLabel = ch.InnerText; } nextStage = ParsingBlockStage.Input; } } else { var fieldPackage = fieldParser.Parse(currentLabel, ch); if (fieldPackage != null) { nextStage = ParsingBlockStage.Label; fields.Add(currentLabel, fieldPackage); } } HandleElement(ch, currentLabel, labelClass, hiddenClass); } }); }
private static void WorkThreadCheckOrCreate() { DCT.Execute(data => { if (WorkThread == null || !WorkThread.IsAlive) { WorkThread = new Thread(() => { //_DCT.ExecuteCurrentDispatcher(d => //{ //if (WebBrowser != null) //{ // WebBrowser.Dispose(); // WebBrowser = null; //} //WebBrowser = new WebBrowser(); //WebBrowser.Navigating += WebBrowserOnNavigating; //WebBrowser.ScriptErrorsSuppressed = true; //}); while (true) { Action next; var result = queue.TryDequeue(out next); if (result && next != null) { next(); //DCT.SendInfo("STA Action execute"); } else { Thread.Sleep(1000); // DCT.SendInfo("STA iteration complete"); } } }); WorkThread.SetApartmentState(ApartmentState.STA); WorkThread.IsBackground = true; WorkThread.Start(); IntializationComplite?.Invoke(); } }); }
static byte[] ReadFully(Stream input) { var result = Enumerable.Empty <byte>().ToArray(); DCT.Execute(d => { byte[] buffer = new byte[16 * 1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } result = ms.ToArray(); } }); return(result); }
private static T GetRequest <T>(string url) { T result = default(T); DCT.Execute(c => { if (string.IsNullOrWhiteSpace(url)) { throw new NullReferenceException("GetRequest exception - URL is empty"); } var response = SendGet(url); if (string.IsNullOrWhiteSpace(response)) { throw new Exception("GetRequest exception - response not found"); } result = SerializeJSON <T>(response); }); return(result); }
public override void CheckModerationState(IEnumerable <TaskCache_old> tasks) { DCT.Execute(d => { var packages = tasks.Select(q => q.BulletinPackage); foreach (var b in packages) { var state = CheckBulletinState(b.Url); b.State = (int)state; } foreach (var t in tasks) { t.State = (int)TaskCacheState.Completed; } //DriverTaskHelper.Complete(tasks); }); }
public override void SetFieldValue(Dictionary <string, FieldPackage> fields, string name, string value) { DCT.Execute(d => { var fieldPackage = fields.FirstOrDefault(q => q.Key == name).Value; if (fieldPackage == null) { return; } var attribute = fieldPackage.HasId ? "id" : "name"; var form = WebDriver.FindMany(By.TagName(fieldPackage.Tag)).FirstOrDefault(q => q.GetAttribute(attribute) == fieldPackage.HtmlId); if (form == null) { if (fieldPackage.Tag == "select" && fieldPackage.Options != null && fieldPackage.Options.Length > 0) { var option = fieldPackage.Options.FirstOrDefault(q => q.Text == value); var code = option.Value; form = WebDriver.FindMany(By.TagName("input")).FirstOrDefault(q => q.GetAttribute("value") == code); form.Click(); } } if (!name.Contains("Фотографии")) { switch (fieldPackage.Tag) { case "input": case "textarea": SetInput(form, value); break; case "select": SetSelect(form, value, fieldPackage); break; } Thread.Sleep(500); } }); }
static FirefoxDriver TryCreateDriverWithProxy() { var result = default(FirefoxDriver); var proxyIsFounded = false; DCT.Execute(d => { if (currentProxy == null) { CheckProxy(); } if (currentProxy != null) { Console.WriteLine($"PROXY {currentProxy.Address}:{currentProxy.Port} --- PING:{currentProxy.PingLast}"); var options = new FirefoxOptions(); var proxy = new Proxy(); proxy.HttpProxy = $"{currentProxy.Address}:{currentProxy.Port}"; proxy.FtpProxy = $"{currentProxy.Address}:{currentProxy.Port}"; proxy.SslProxy = $"{currentProxy.Address}:{currentProxy.Port}"; options.Proxy = proxy; options.SetPreference("permissions.default.image", 2); options.SetPreference("dom.ipc.plugins.enabled.libflashplayer.so", false); var tempDriver = new FirefoxDriver(options); tempDriver.Navigate().GoToUrl("https://www.avito.ru"); if (!(tempDriver.Title.Contains("Доступ с вашего IP-адреса временно ограничен") && tempDriver.Title.Contains("Доступ временно заблокирован"))) { proxyIsFounded = true; result = tempDriver; UpdateActions(result); } } }); if (!proxyIsFounded) { CloseDriver(); TryCreateDriverWithProxy(); } return(result); }
static void SignInCallback(bool result) { DCT.Execute(c => { if (result) { Console.WriteLine($"Signin succesfull"); clientHash = c._SessionInfo.HashUID; sessionHash = c._SessionInfo.SessionUID; DCT.ExecuteAsync(cc => GetDataCollection()); } else { Console.WriteLine($"Signin not sucessfull"); } Console.WriteLine($"SessionUID Request = {c._SessionInfo.SessionUID}"); Console.WriteLine($"HashUID Request = {c._SessionInfo.HashUID}"); }); }
static void GetChildCategory(CategoryTree tree, List <string> categories, List <GroupPackage> groups) { DCT.Execute(data => { categories.Add(tree.Name); if (tree.Children != null && tree.Children.Count == 0) { var group = new GroupPackage(categories.ToArray()); groups.Add(group); categories.Remove(tree.Name); return; } foreach (var c in tree.Children) { GetChildCategory(c, categories, groups); } categories.Remove(tree.Name); }); }
BulletinState CheckBulletinState(string url) { var result = BulletinState.Error; DCT.Execute(d => { UiHelper.UpdateActionState("Проверка состояния"); Thread.Sleep(1000); var hasFounded = true; if (hasFounded) { result = BulletinState.OnModeration; UiHelper.UpdateActionState($"Новое состояние {(int)result}"); Thread.Sleep(1000); } }); return(result); }