public static bool DownloadFile(object id) { string str = ""; str = "http://m.001bz.in/download/download.php?filetype=txt&filename=" + id; WebClientHelper httpClient = new WebClientHelper(); try { Log(id + "正在下载"); httpClient.DownloadFile(str, "1"); Log(id + "下载完成"); return(true); } catch (Exception e) { Log(id + "下载异常,重新下载"); myDelegate.BeginInvoke(id, Completed, null); return(false); } }
/// <summary> /// 抓取单个商品 /// </summary> /// <param name="searchRst"></param> /// <returns></returns> bool getProduct(SearchProductResult searchRst) { if (searchRst == null) { return(false); } #region//TODO 抓取商品数据 var detailsHtml = WebClientHelper.GetContent(searchRst.Link, detailsOption.Encoding); if (string.IsNullOrWhiteSpace(detailsHtml)) { return(false); } //商品装载数据 ProductSetupResult setup = null; Regex setupRegex = new Regex(detailsOption.SetupRegex.Pattern, RegexOptions.IgnoreCase); Match setupMatch = setupRegex.Match(detailsHtml); if (setupMatch != null) { string setupData = setupMatch.Groups[detailsOption.SetupRegex.GroupName].Value; setup = JsonConvert.DeserializeObject <ProductSetupResult>(setupData); } if (setup == null || setup.ItemDO == null) { return(false); } //定义商品ID long productId = Tools.NewId(); //图片 Dictionary <string, string> proImgDic = new Dictionary <string, string>();//key为“商品ID_v序号”组成,value为采集的图片地址 Regex imagesRegex = new Regex(detailsOption.ImagesDataRegex.Pattern, RegexOptions.IgnoreCase); Match imagesMatch = imagesRegex.Match(detailsHtml); if (imagesMatch != null) { string imagesData = imagesMatch.Groups[detailsOption.ImagesDataRegex.GroupName].Value; Regex singleImgRegex = new Regex(detailsOption.SingleImageRegex.Pattern, RegexOptions.IgnoreCase); MatchCollection singleImgMatches = singleImgRegex.Matches(imagesData); Regex removeRegex = new Regex(detailsOption.ImageSrcRemoveRegex.Pattern, RegexOptions.IgnoreCase); int proIdx = 0; foreach (Match m in singleImgMatches) { string src = m.Groups[detailsOption.SingleImageRegex.GroupName].Value.GetFullLink(); //移除缩略图标识,保留原图地址 src = removeRegex.Replace(src, ""); proImgDic.Add($"{setup.ItemDO.ItemId}_v{++proIdx}", src); } } //商品描述 string desc = null; var descHtml = WebClientHelper.GetContent(setup.Api.DescUrl, detailsOption.Encoding); Regex descRegex = new Regex(detailsOption.DescRegex.Pattern, RegexOptions.IgnoreCase); Match descMatch = descRegex.Match(descHtml); if (descMatch != null) { desc = descMatch.Groups[detailsOption.DescRegex.GroupName].Value; } //描述中的图片 Dictionary <string, string> descImgDic = new Dictionary <string, string>();//key为“商品ID_d序号”组成,value为采集的图片地址 Regex descImgRegex = new Regex(detailsOption.DescImageRegex.Pattern, RegexOptions.IgnoreCase); MatchCollection descImgMatches = descImgRegex.Matches(desc); int descIdx = 0; foreach (Match m in descImgMatches) { string src = m.Groups[detailsOption.DescImageRegex.GroupName].Value; //当前图片标识 string currentImgTag = $"{setup.ItemDO.ItemId}_d{++descIdx}"; //将详情描述中的当前图片地址用标识符替换以占位,待上传后用新地址替换 desc = desc.Replace(src, currentImgTag); descImgDic.Add(currentImgTag, src.GetFullLink()); } #endregion //下载商品展示图 proImgDic = WebClientHelper.DownloadFile(proImgDic, uploadOption.VisitAddress, uploadOption.SaveDirectory); //下载商品描述图 descImgDic = WebClientHelper.DownloadFile(descImgDic, uploadOption.VisitAddress, uploadOption.SaveDirectory); //将描述中的图更换为上传后的地址 foreach (var img in descImgDic) { //将描述中的标识符替换为上传后的图片地址 desc = desc.Replace(img.Key, img.Value); } #region // 解析成产品库数据 string title = setup.ItemDO.Title; if (detailsOption.ReplaceItems.Any()) { foreach (var rep in detailsOption.ReplaceItems) { title = title.Replace(rep.SourceText, rep.ReplaceTo); desc = desc.Replace(rep.SourceText, rep.ReplaceTo); } } //商品 Product product = new Product { BrandID = long.Parse(setup.ItemDO.BrandId), CategoryID = searchRst.CategoryId, CreateTime = DateTime.Now, Intro = desc, IsDelete = false, mainPic = proImgDic.Values.FirstOrDefault(), Path = searchRst.Link, Pics = string.Join(",", proImgDic.Values), ProductID = productId, Properties = string.Empty, Source = collectorType, Title = title, UpdateTime = DateTime.Now, Weight = float.Parse(setup.ItemDO.Weight), SourceProductID = long.Parse(setup.ItemDO.ItemId) }; //SKU ProductSku sku = new ProductSku { CreateTime = product.CreateTime, IsDelete = false, Name = product.Title, ProductID = product.ProductID, SalePrice = decimal.Parse(setup.Detail.DefaultItemPrice), SkuID = Tools.NewId(), UpdateTime = product.UpdateTime, Weight = product.Weight }; #endregion #region // 保存到数据库 using (var db = new DataContext()) { db.Product.Add(product); db.ProductSku.Add(sku); return(db.SaveChanges() > 0); } #endregion }
internal void CheckVersion(bool IsStartUp, bool IsManually) { try { if (IsStartUp) { Thread.Sleep(7000); } WebClientHelper webclientHelper = new WebClientHelper(); ProxyInfo proxyinfo = ConfigCtrl.GetProxy(); webclientHelper.SetProxy(proxyinfo.Server, proxyinfo.Port, proxyinfo.UserName, proxyinfo.Password); if (proxyinfo.Enable) { webclientHelper.EnableProxy(); } Stream updatestream = null; try { // Download the update info file to the memory, updatestream = webclientHelper.OpenRead(REMOTE_URI + UPDATE_FILE); } catch (Exception ex) { LogHelper.Write("Download update.xml", REMOTE_URI + UPDATE_FILE, ex, LogSeverity.Error); return; } if (updatestream == null) { LogHelper.Write("Download update.xml", REMOTE_URI + UPDATE_FILE, LogSeverity.Error); return; } // read and close the stream using (System.IO.StreamReader streamReader = new System.IO.StreamReader(updatestream, System.Text.Encoding.GetEncoding("GB2312"))) { string updateInfo = streamReader.ReadToEnd(); // if something was read if (!string.IsNullOrEmpty(updateInfo)) { //LogHelper.Write("Johnny.Kaixin.WinUI.AutoUpdate.CheckVersion.updateInfo:", updateInfo, LogSeverity.Info); string newVersion = JsonHelper.GetMid(updateInfo, "<Version Num = \"", "\"/>"); if (String.IsNullOrEmpty(newVersion)) { LogHelper.Write("Get Version", "newVersion is null", LogSeverity.Info); return; } if (CompareVersions(Assembly.GetExecutingAssembly().GetName().Version.ToString(), newVersion)) { // Download the auto update program to the application // path, so you always have the last version runing if (webclientHelper.DownloadFile(REMOTE_URI + "AutoUpdate.exe", Application.StartupPath + "\\AutoUpdate.exe")) { if (NewVersionFound != null) { NewVersionFound(newVersion, Application.StartupPath + "\\AutoUpdate.exe", System.Web.HttpUtility.UrlEncode(updateInfo)); } } else { LogHelper.Write("Download AutoUpdate.exe failed", REMOTE_URI + "AutoUpdate.exe", LogSeverity.Error); return; } } else if (IsManually) { if (LatestVersionConfirmed != null) { LatestVersionConfirmed(Assembly.GetExecutingAssembly().GetName().Version.ToString()); } } } } } catch (Exception ex) { LogHelper.Write("AutoUpdate.CheckVersion", ex, LogSeverity.Error); } }
internal void CheckVersion(bool IsStartUp, bool IsManually) { try { if (IsStartUp) Thread.Sleep(7000); WebClientHelper webclientHelper = new WebClientHelper(); ProxyInfo proxyinfo = ConfigCtrl.GetProxy(); webclientHelper.SetProxy(proxyinfo.Server, proxyinfo.Port, proxyinfo.UserName, proxyinfo.Password); if (proxyinfo.Enable) webclientHelper.EnableProxy(); Stream updatestream = null; try { // Download the update info file to the memory, updatestream = webclientHelper.OpenRead(REMOTE_URI + UPDATE_FILE); } catch (Exception ex) { LogHelper.Write("Download update.xml", REMOTE_URI + UPDATE_FILE, ex, LogSeverity.Error); return; } if (updatestream == null) { LogHelper.Write("Download update.xml", REMOTE_URI + UPDATE_FILE, LogSeverity.Error); return; } // read and close the stream using (System.IO.StreamReader streamReader = new System.IO.StreamReader(updatestream, System.Text.Encoding.GetEncoding("GB2312"))) { string updateInfo = streamReader.ReadToEnd(); // if something was read if (!string.IsNullOrEmpty(updateInfo)) { //LogHelper.Write("Johnny.Kaixin.WinUI.AutoUpdate.CheckVersion.updateInfo:", updateInfo, LogSeverity.Info); string newVersion = JsonHelper.GetMid(updateInfo, "<Version Num = \"", "\"/>"); if (String.IsNullOrEmpty(newVersion)) { LogHelper.Write("Get Version", "newVersion is null", LogSeverity.Info); return; } if (CompareVersions(Assembly.GetExecutingAssembly().GetName().Version.ToString(), newVersion)) { // Download the auto update program to the application // path, so you always have the last version runing if (webclientHelper.DownloadFile(REMOTE_URI + "AutoUpdate.exe", Application.StartupPath + "\\AutoUpdate.exe")) { if (NewVersionFound != null) NewVersionFound(newVersion, Application.StartupPath + "\\AutoUpdate.exe", System.Web.HttpUtility.UrlEncode(updateInfo)); } else { LogHelper.Write("Download AutoUpdate.exe failed", REMOTE_URI + "AutoUpdate.exe", LogSeverity.Error); return; } } else if (IsManually) { if (LatestVersionConfirmed != null) LatestVersionConfirmed(Assembly.GetExecutingAssembly().GetName().Version.ToString()); } } } } catch (Exception ex) { LogHelper.Write("AutoUpdate.CheckVersion", ex, LogSeverity.Error); } }