using System; using System.Collections.Generic; using System.Text; using AllBase; using System.IO; using System.Linq; using System.Threading; namespace Work.CollectClass { public class ServerTemp { /// /// 获取服务器缓存 /// /// 来源网站 /// 商品ID /// 缓存是否可用 /// public static string Get(int siteId, string pid, out bool canUse) { canUse = false; if (siteId == -1) pid = "key-" + pid; else if (siteId == 1) pid = "tb-" + pid; else if (siteId == 2) pid = "pd-" + pid; else if (siteId == 3) pid = "al-" + pid; else if (siteId == 4) pid = "wd-" + pid; else if (siteId == 5)//京喜商品的前缀约定为:jx- pid = "jx-" + pid; else if (siteId == 6) pid = "dy-" + pid; //string domain = "t3.1n11.com"; //domain = "t1.taojinhu.com:6788"; //Random rd = new Random(); //var iR = rd.Next(1, 100); //if (iR > 35) string domain = "temp-new1.taojinhu.com:10019"; bool first = true; Again: string url = "https://" + domain + "/cache.php?id=" + pid; string data = UserLogin.GetSignaturex(); //获取服务器加密 long timeStamp; string getCse = CacheService(pid, out timeStamp); if (!string.IsNullOrEmpty(getCse)) { url += "&csign=" + getCse + "&tsign=" + timeStamp; } string html = GetHtml.Utf8(url, 20); if (string.IsNullOrEmpty(html) && first) { first = false; domain = "t3.1n11.com"; goto Again; } if (string.IsNullOrEmpty(html) || html.Length < 10 || html == "none" || html == "out time") return ""; string date = BaseRegex.GetMatchRegexString(ref html, @"([\d]{4}-[\d]{2}-[\d]{2})"); if (string.IsNullOrEmpty(date)) return ""; DateTime time; if (!DateTime.TryParse(date, out time)) return ""; if (time.AddDays(14) > BaseClass.GetServerTimeString()) //time.AddDays(7) canUse = true; if (time.AddDays(180) > BaseClass.GetServerTimeString() && pid.Contains("content")) //time.AddDays(7) canUse = true; return html; } /// /// 获取淘宝服务器缓存 /// /// 来源网站 /// 商品ID /// 缓存是否可用 /// public static string GetTaoBao(int siteId, string pid, out bool canUse) { canUse = false; if (siteId == -1) pid = "key-" + pid; if (siteId == 1) pid = "tb-" + pid; if (siteId == 2) pid = "pd-" + pid; if (siteId == 3) pid = "al-" + pid; //string domain = "t3.1n11.com"; //domain = "t1.taojinhu.com:6788";//备用 //Random rd = new Random(); //var iR = rd.Next(1, 100); //if (iR > 35) string domain = "temp-new1.taojinhu.com:10019"; bool first = true; Again: string url = "https://" + domain + "/cache.php?id=" + pid; //获取服务器加密 long timeStamp; string getCse = CacheService(pid, out timeStamp); if (!string.IsNullOrEmpty(getCse)) { url += "&csign=" + getCse + "&tsign=" + timeStamp; } string html = GetHtml.Utf8(url, 20); if (string.IsNullOrEmpty(html) && first) { first = false; domain = "t3.1n11.com";//tempx.1n11.com goto Again; } if (string.IsNullOrEmpty(html) || html.Length < 10 || html == "none" || html == "out time" || html == "error time" || html == "error code") return ""; string date = BaseRegex.GetMatchRegexString(ref html, @"([\d]{4}-[\d]{2}-[\d]{2})"); if (string.IsNullOrEmpty(date)) return ""; DateTime time; if (!DateTime.TryParse(date, out time)) return ""; if (time.AddDays(7) > BaseClass.GetServerTimeString())//time.AddDays(7) canUse = true; if (time.AddDays(180) > BaseClass.GetServerTimeString() && pid.Contains("content")) //time.AddDays(7) canUse = true; return html; } public static bool Set(int siteId, string pid, string content) { if (siteId == -1) pid = "key-" + pid; else if (siteId == 1) pid = "tb-" + pid; else if (siteId == 2) pid = "pd-" + pid; else if (siteId == 3) pid = "al-" + pid; else if (siteId == 4) pid = "wd-" + pid; else if (siteId == 5) pid = "jx-" + pid; else if (siteId == 6) pid = "dy-" + pid; string domain = "t3a.1n11.com"; string domainx = "temp-new1.taojinhu.com:10018"; byte[] contentByte = Encoding.UTF8.GetBytes(content); string url = "https://" + domain + "/cache.php?act=savex&id=" + pid; string urlx = "https://" + domainx + "/cache.php?act=savex&id=" + pid; //获取服务器加密 long timeStamp; string getCse = CacheService(pid, out timeStamp); if (!string.IsNullOrEmpty(getCse)) { url += "&csign=" + getCse + "&tsign=" + timeStamp; urlx += "&csign=" + getCse + "&tsign=" + timeStamp; } string html = GetHtml.RequestImg(url, contentByte); html = GetHtml.RequestImg(urlx, contentByte); return html == "ok"; } //密码 = 当前月份(202003)+ dfhjew&*4<>.,/-=+~`8sazfhy&6544#^fdg/DfgJSB //签名变量csign算法 = md5(密码 + 时间戳(秒)+ $id(例子:tb-main-123456)+ 密码) private static string EncryptionPassword = "dfhjew&*4<>.,/-=+~`8sazfhy&6544#^fdg/DfgJSB"; public static string CacheService(string ProductId, out long time) { string NewEncryptionPassword = BaseClass.GetServerTimeString().ToString("yyyyMM") + EncryptionPassword; time = BaseClass.DateTimeToTimestampSeconds(DateTime.Now) + BaseClass.TimeDifferent; if (string.IsNullOrEmpty(ProductId)) { return ""; } string csign = ""; csign = Md5Base.GetMd5(NewEncryptionPassword + time + ProductId + NewEncryptionPassword).ToLower(); return csign; } /// /// 获取微店缓存上家数据 /// /// /// public static List> GetWeiDianCache(ProductCollectBase upProduct) { var mappingResultWd = new List>(); string tagStr = "vd"; string vdHtml = ""; string fromStr = ""; if (upProduct.from_site_id == 1) fromStr = "tb-"; if (upProduct.from_site_id == 3) fromStr = "al-"; else if (upProduct.from_site_id == 2) fromStr = "pd-"; string url = BaseClass.BuildObjString("data_change&act=get_pid"); string data = ""; // 根据商品id查找缓存 if (string.IsNullOrEmpty(vdHtml) || vdHtml.ToLower().Contains("error")) { long insertGoodId = Convert.ToInt64(upProduct.from_id) % 1024; data = "&product_id=" + fromStr + tagStr + upProduct.from_id + "&img_id=" + "&suffix=" + insertGoodId + "&site_id=&" + UserLogin.GetSignaturex(); vdHtml = GetHtml.PostUtf8(url, data); if (string.IsNullOrEmpty(vdHtml) || vdHtml.ToLower().Contains("error")) vdHtml = GetHtml.PostUtf8(url, data); // 读取缓存成功 if (!string.IsNullOrEmpty(vdHtml) && !vdHtml.ToLower().Contains("error")) { string siteId = BaseRegex.GetMatchRegexString(vdHtml, "\"site_id\":\"([\\s\\S]+?)\""); string content = BaseRegex.GetMatchRegexString(vdHtml, "\"content\":\"([\\s\\S]+?)(])\""); if (content != null) { content = content.Replace("\\", "") + "]"; } else { content = BaseRegex.GetMatchRegexString(vdHtml, "\"content\":\"([\\s\\S]+?)\""); } try { var tupleItems = content.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); foreach (var tupleItem in tupleItems) { var items = tupleItem.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); if (items.Length >= 2 && items[0].Length > 5) { try { var item1 = items[0]; var item2 = items[1]; var item3 = items.Length >= 3 ? items[2] : items[1]; mappingResultWd.Add(new Tuple(item1, Convert.ToInt32(item2), Convert.ToInt32(item3))); } catch { } } else if (items.Length == 1 && items[0].StartsWith("tit_")) { // 微店商品标题 //if (string.IsNullOrEmpty(prod.ProductTitle)) //{ // prod.ProductTitle = items[0].Replace("tit_", ""); // prod.ProductTitle = GetHtml.Unicode2String(prod.ProductTitle).Replace("\\/", "/"); //} } } } catch { } } } return mappingResultWd; } /// /// 根据商品标题寻找微店商品缓存 /// /// 商品标题 /// /// /// public static List> GetFakeCacheVDianTitle(string productTitle) { var result = new List>(); var html = ""; if (string.IsNullOrEmpty(productTitle)) return result; string url = BaseClass.BuildObjString("data_change&act=get_cache_title"); string data = ""; // 根据商品id查找缓存 if (string.IsNullOrEmpty(html) || html.ToLower().Contains("error")) { string tid = Md5Base.GetMd5(productTitle).Substring(8, 16); long suffix = Math.Abs(BaseClass.GetLong(tid)) % 1024; data = "&tid=" + tid + "&suffix=" + suffix + "&" + UserLogin.GetSignaturex(); html = GetHtml.PostUtf8(url, data); if (string.IsNullOrEmpty(html)) html = GetHtml.PostUtf8(url, data); } // 读取缓存成功 if (!string.IsNullOrEmpty(html) && !html.ToLower().Contains("error")) { string content = BaseRegex.GetMatchRegexString(html, "\"content\":\"([\\s\\S]+?)(])\""); if (content != null) { content = content.Replace("\\", "") + "]"; } else { content = BaseRegex.GetMatchRegexString(html, "\"content\":\"([\\s\\S]+?)\""); } try { var tupleItems = content.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); foreach (var tupleItem in tupleItems) { var items = tupleItem.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); if (items.Length >= 2 && items[0].Length > 5) { try { var item1 = items[0]; var item2 = items[1]; var item3 = items.Length >= 3 ? items[2] : items[1]; result.Add(new Tuple(item1, Convert.ToInt32(item2), Convert.ToInt32(item3))); } catch { } } } } catch { } return result; } return null; } /// /// 根据价格区间筛选可以满足Relation条件的微店商品 /// /// /// /// /// public static List> FilteWeiDianProducts(List> prodList, int minPrice, int maxPrice) { if (prodList == null) return new List>(); if (prodList.Count == 0) return prodList; var result = prodList.Where(p => !string.IsNullOrEmpty(p.Item1) && p.Item2 >= minPrice && p.Item3 >= maxPrice && ((p.Item3 < maxPrice * 3.5M) || (p.Item3 < maxPrice + 3000))).ToList(); return result; } /// /// 获取净化数据 /// /// /// public static string GetJinHuaCache(string pid) { string url = BaseClass.LoginApiUrl + "?obj=pinbaobao/fession_api&act=get_detail"; StringBuilder sb = new StringBuilder(); sb.Append("&fession_id=" + pid); string data = UserLogin.GetSignaturex() + sb; string html = GetHtml.PostUtf8(url, data); html = GetHtml.Decode(html).Replace("\\/", "/").Replace(@"\", ""); return html; } } }