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 void Test() { using (SqliteContext scontext = new SqliteContext()) { EFCategory eFCategory = new EFCategory(dbContext); EFManufacturer eFManufacturer = new EFManufacturer(dbContext); //EFGood eFGood = new EFGood(dbContext); //int count = 0; //int manufacturerId = 0; //Manufacturer manufacturer = null; //int categoryId = 0; //Category category = null; string writePath = @"D:\goods.sql"; using (StreamWriter sw = new StreamWriter(writePath, false, System.Text.Encoding.UTF8)) { foreach (var good in scontext.Goods) { //if (count >= 10) break; sw.WriteLine(@"" + "INSERT [dbo].[Goods] " + "([DataSheetUrl], " + "[Description], " + "[ImagePath]," + "[ManufacturerPartNumber]," + "[Min]," + "[Mult]," + "[MouserPartNumber]," + "[ProductDetailUrl]," + "[Reeling]," + "[ROHSStatus]," + "[Category_Id]," + "[Manufacturer_Id]) " + "VALUES (" + "N'" + good.DataSheetUrl + "', " + "N'" + good.Description.Replace("'", "") + "', " + "N'" + good.ImagePath + "'," + "N'" + good.ManufacturerPartNumber + "'," + good.Min + "," + good.Mult + "," + "N'" + good.MouserPartNumber + "'," + "N'" + good.ProductDetailUrl + "'," + "'" + good.Reeling + "'," + "N'" + good.ROHSStatus + "'," + good.CategoryId + "," + good.ManufacturerId + ")"); //count++; } } //foreach (var good in scontext.Goods) //{ //if (good.ManufacturerId != manufacturerId) //{ // manufacturerId = good.ManufacturerId; // manufacturer = await eFManufacturer.FindByIdAsync(good.ManufacturerId); //} //if (good.CategoryId != categoryId) //{ // categoryId = good.CategoryId; // category = await eFCategory.FindByIdAsync(good.CategoryId); //} //dbContext.Goods.Add(new Domain.Entities.Good //{ // Category = category, // Manufacturer = manufacturer, // DataSheetUrl = good.DataSheetUrl, // Description = good.Description, // ImagePath = good.ImagePath, // ManufacturerPartNumber = good.ManufacturerPartNumber, // Min = good.Min, // MouserPartNumber = good.MouserPartNumber, // Mult = good.Mult, // ProductDetailUrl = good.ProductDetailUrl, // Reeling = good.Reeling, // ROHSStatus = good.ROHSStatus //}); //count++; //if (count % 1000 == 0) //{ // await dbContext.SaveChangesAsync(); //} //await eFGood.AddOrUpdateAsync(new Domain.Entities.Good //{ // Category = await eFCategory.FindByIdAsync(good.CategoryId), // Manufacturer = await eFManufacturer.FindByIdAsync(good.ManufacturerId), // DataSheetUrl = good.DataSheetUrl, // Description = good.Description, // ImagePath = good.ImagePath, // ManufacturerPartNumber = good.ManufacturerPartNumber, // Min = good.Min, // MouserPartNumber = good.MouserPartNumber, // Mult = good.Mult, // ProductDetailUrl = good.ProductDetailUrl, // Reeling = good.Reeling, // ROHSStatus = good.ROHSStatus //}, -1); //} //await dbContext.SaveChangesAsync(); MessageBox.Show("Готово"); //EFGood eFGood = new EFGood(dbContext); //foreach(var good in scontext.Goods) //{ // var dd = good; //} } }
//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)"); //} } }