public async Task SaveSessionDataAsync(ApiRegInfo apiRegInfo, Manufacturer manufacturer, int numberOfResult, int startingRecord, Proxy proxy) { ApiSearchSession apiSearchSession = await _context.ApiSearchSessions.FirstOrDefaultAsync(a => a.ApiRegInfo == apiRegInfo && a.Date == DateTime.Today && a.Manufacturer == manufacturer); if (apiSearchSession != null) { apiSearchSession.CountOfRequests++; apiSearchSession.Manufacturer = manufacturer; //apiSearchSession.NumberOfResult = numberOfResult; //apiSearchSession.StartingRecord = startingRecord; apiSearchSession.Proxy = proxy; } else { _context.ApiSearchSessions.Add(new ApiSearchSession { Date = DateTime.Today, ApiRegInfo = apiRegInfo, CountOfRequests = 1, Manufacturer = manufacturer, //NumberOfResult = numberOfResult, //StartingRecord = startingRecord, Proxy = proxy }); } await _context.SaveChangesAsync(); }
private async void simpleButton5_Click(object sender, EventArgs e) { using (Domain.ApplicationContext context = new Domain.ApplicationContext()) { List <Manufacturer> manufacturers = await context.Manufacturers.Where(m => m.MouserID != 0 && m.SearchText == null).ToListAsync(); ApiRegInfo apiRegInfo = await context.ApiRegInfos.FirstOrDefaultAsync(a => a.IsActive); Proxy proxy = await context.Proxies.FirstOrDefaultAsync(p => p.IsActive); string[] alphabet = { ".", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; foreach (Manufacturer manufacturer in manufacturers) { foreach (string letter in alphabet) { this.labelControl4.Text = "[" + letter + "] - " + manufacturer.MouserID + " - " + manufacturer.NameAPI; this.Refresh(); Thread.Sleep(1500); await Mouser.Service.Api.Methods.SearchByKeywordMfrRequestSetResulCountAsync(context, proxy, apiRegInfo, letter, manufacturer); } } } MessageBox.Show("Done!"); }
public async Task AddIterationAsync(ApiRegInfo apiRegInfo, Manufacturer manufacturer, Proxy proxy) { ApiSearchSession apiSearchSession = await _context.ApiSearchSessions .FirstOrDefaultAsync(a => a.ApiRegInfo == apiRegInfo && a.Date == DateTime.Today && a.Proxy == proxy && a.Manufacturer == manufacturer); if (apiSearchSession != null) { apiSearchSession.CountOfRequests++; } else { _context.ApiSearchSessions.Add(new ApiSearchSession { Date = DateTime.Today, ApiRegInfo = apiRegInfo, CountOfRequests = 1 }); } await _context.SaveChangesAsync(); }
public static async Task SearchByKeywordMfrRequestAsync(ApplicationContext context, Proxy proxy, ApiRegInfo apiRegInfo, string keyword, Manufacturer manufacturer, int records, int startingRecord) { using (var searchAPI = new SearchAPI()) { var header = new MouserHeader(); header.AccountInfo = new AccountInfo(); header.AccountInfo.PartnerID = apiRegInfo.PartnerId; searchAPI.MouserHeaderValue = header; WebProxy wp = new WebProxy(proxy.IPAddress, true); if (proxy.UserName != "") { ICredentials credentials = new NetworkCredential(proxy.UserName, proxy.Password); wp.Credentials = credentials; } searchAPI.Proxy = wp; ResultParts resultParts = searchAPI.SearchByKeywordAndManufacturer(keyword, manufacturer.MouserID, records, startingRecord, String.Empty, String.Empty); EFManufacturer eFManufacturer = new EFManufacturer(context); manufacturer.StartingRecord = startingRecord; manufacturer.NumberOfResult = resultParts.NumberOfResult; await eFManufacturer.AddOrUpdateAsync(manufacturer, manufacturer.Id); foreach (var resultPart in resultParts.Parts.ToList()) { await SaveGoodAsync(context, resultPart, manufacturer); } } }
private async Task StartProcessAsync() { int maxIteration = 10; using (Domain.ApplicationContext context = new Domain.ApplicationContext()) using (StreamWriter sw = new StreamWriter(writePath, true, System.Text.Encoding.UTF8)) { sw.WriteLine(DateTime.Now.ToString() + " Start"); ApiSearchSession apiSearchSession = null; EFApiSearchSession eFApiSearchSession = new EFApiSearchSession(context); try { //Получаем PartnerID EFApiRegInfo eFApiRegInfo = new EFApiRegInfo(context); ApiRegInfo availableApiRegInfo = await eFApiRegInfo.GetAvailableParnterIdAsync(); sw.WriteLine(availableApiRegInfo.PartnerId); //Получаем прокси EFProxy eFProxy = new EFProxy(context); Proxy availableProxy = eFProxy.GetAvailableProxyAsync().Result; sw.WriteLine(availableProxy.IPAddress); //Получаем свободного производителя EFManufacturer eFManufacturer = new EFManufacturer(context); Manufacturer availableManufacturer = eFManufacturer.GetAvailableManufacturerAsync().Result; sw.WriteLine(availableManufacturer.MouserID); //if (availableApiRegInfo != null && availableProxy != null && availableManufacturer != null) //{ //Создаем очередь apiSearchSession = new ApiSearchSession { ApiRegInfo = availableApiRegInfo, Date = DateTime.Today, IsBusy = true, Manufacturer = availableManufacturer, Proxy = availableProxy }; eFApiSearchSession.AddOrUpdateAsync(apiSearchSession, -1).Wait(); //Запускаем запросы api int count = 1; while (count <= maxIteration) { count++; } eFApiSearchSession.SetNotBusyAsync(apiSearchSession).Wait(); System.Environment.Exit(1); //} } catch (Exception ex) { sw.WriteLine(DateTime.Now.ToString() + " " + ex.Message + ex.InnerException.Message); } finally { //Очередь освобождаем if (apiSearchSession != null) { eFApiSearchSession.SetNotBusyAsync(apiSearchSession).Wait(); } System.Environment.Exit(1); } } }
//private static string writePath = @"D:\MouserClient\MouserClientApi" + Guid.NewGuid() + ".log"; static async Task Main(string[] args) { ApiSearchSession apiSearchSession = null; try { int maxIteration = 5; using (Domain.ApplicationContext context = new Domain.ApplicationContext()) //using (StreamWriter sw = new StreamWriter(writePath, true, System.Text.Encoding.UTF8)) { //sw.WriteLine(DateTime.Now.ToString() + " Start"); EFApiSearchSession eFApiSearchSession = new EFApiSearchSession(context); //Получаем PartnerID EFApiRegInfo eFApiRegInfo = new EFApiRegInfo(context); ApiRegInfo availableApiRegInfo = await eFApiRegInfo.GetAvailableParnterIdAsync(); //sw.WriteLine(availableApiRegInfo?.PartnerId); try { //Получаем прокси EFProxy eFProxy = new EFProxy(context); Proxy availableProxy = await eFProxy.GetAvailableProxyAsync(); //sw.WriteLine(availableProxy?.IPAddress); //Получаем свободного производителя EFManufacturer eFManufacturer = new EFManufacturer(context); Manufacturer availableManufacturer = await eFManufacturer.GetAvailableManufacturerAsync(); //sw.WriteLine(availableManufacturer?.MouserID); if (availableApiRegInfo != null && availableProxy != null && availableManufacturer != null && !context.ApiSearchSessions.Where(a => a.IsBusy && a.Date == DateTime.Today).Select(a => a.Manufacturer.Id).Contains(availableManufacturer.Id)) { //Создаем очередь apiSearchSession = new ApiSearchSession { ApiRegInfo = availableApiRegInfo, Date = DateTime.Today, IsBusy = true, Manufacturer = availableManufacturer, Proxy = availableProxy, MachineName = Environment.MachineName, CreateDate = DateTime.Now }; await eFApiSearchSession.AddOrUpdateAsync(apiSearchSession, -1); //Запускаем запросы api int count = 1; int countIteration = availableManufacturer.StartingRecord != 0 ? (availableManufacturer.StartingRecord - 1) / 50 + 1 : 0; int startingRecord = 0; while (count <= maxIteration && (startingRecord == 0 || startingRecord < availableManufacturer.NumberOfResult)) { startingRecord = countIteration != 0 ? countIteration * 50 + 1 : 0; await Mouser.Service.Api.Methods.SearchByKeywordMfrRequestAsync (context, availableProxy, availableApiRegInfo, availableManufacturer.SearchText ?? ".", availableManufacturer, 50, startingRecord); await eFApiSearchSession.AddIterationAsync(apiSearchSession); count++; countIteration++; } await eFApiSearchSession.SetNotBusyAsync(apiSearchSession); //sw.WriteLine(DateTime.Now.ToString() + " Stop"); } } catch (Exception ex) { if (ex.Message.Contains("Error Code: 429") || ex.Message.Contains("Invalid unique identifier")) { availableApiRegInfo.IsActive = false; await eFApiRegInfo.AddOrUpdateAsync(availableApiRegInfo, availableApiRegInfo.Id); } if (apiSearchSession != null) { apiSearchSession.Description = ex.Message + ex.InnerException?.Message; await eFApiSearchSession.AddOrUpdateAsync(apiSearchSession, apiSearchSession.Id); await eFApiSearchSession.SetNotBusyAsync(apiSearchSession); } //using (StreamWriter sw = new StreamWriter(writePath, true, System.Text.Encoding.UTF8)) //{ // sw.WriteLine(DateTime.Now.ToString() + " " + ex.Message + ex.InnerException?.Message); // sw.WriteLine(DateTime.Now.ToString() + " Error Stop (2)"); //} } finally { //Очередь освобождаем if (apiSearchSession != null) { await eFApiSearchSession.SetNotBusyAsync(apiSearchSession); } } } } catch (Exception ex) { //using (StreamWriter sw = new StreamWriter(writePath, true, System.Text.Encoding.UTF8)) //{ // sw.WriteLine(DateTime.Now.ToString() + " " + ex.Message + ex.InnerException?.Message); // sw.WriteLine(DateTime.Now.ToString() + " Error Stop (2)"); //} } }