using Api.Framework.Cps; using Api.Framework.Tools; using CsharpHttpHelper; using System; using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using static Api.Framework.Tools.TBHelper.TbAnalysis; namespace Weixin.CircleTools { public class TbApi { /// /// 将别人的淘口令转成自己推广位的淘口令 /// /// 别人的淘口令 /// 淘宝cps对象 /// public TklInfo TKLToOwnTKL(string _tkl, string pid, AlimamaApi api, string biz_scene_id = "2") { try { int num = 0; var pids = pid.Split('_'); Next: var tbAnalysis = new TBHelper.TbAnalysis(); var itemId = string.Empty; try { itemId = tbAnalysis.FindItemIdByUrlAndTklAndMkl(_tkl, api, pids); } catch (Exception ex) { } if (!string.IsNullOrWhiteSpace(itemId)) { var activityIdStr = tbAnalysis.activityId; if (string.IsNullOrWhiteSpace(activityIdStr)) { //activityIdStr = TBHelper.FindGoodsActivityIdAll(itemId, out sellerId, out payPrice); var exCoupon = TBHelper.FindGoodsCouponInfo(itemId); if (exCoupon != null) activityIdStr = exCoupon.activityId; } biz_scene_id = TBHelper.TbAnalysis.BizSceneId(itemId); var privilege = api.SendTaobao("taobao.tbk.privilege.get", new { item_id = itemId, site_id = pids[2], adzone_id = pids[3], biz_scene_id = biz_scene_id }); if (privilege == null) return null; var privilege_data = privilege["data"] as Dictionary; #region 生成自己的淘口令 return tbAnalysis.FindTKL(privilege_data["coupon_click_url"].ToString() + (string.IsNullOrWhiteSpace(activityIdStr) ? string.Empty : "&activityId=" + activityIdStr), api); #endregion } else { if (num <= 2) { num++; goto Next; } } } catch (Exception) { } return null; } /// /// 通过优惠券链接获取淘宝宝贝的id /// /// private string CouponByItemId(string url, AlimamaApi api) { try { var tbAnalysis = new Api.Framework.Tools.TBHelper.TbAnalysis(); var itemId = tbAnalysis.FindTbItemIdByStr(url); if (!string.IsNullOrEmpty(itemId)) return itemId; if (url.Contains(@"uland.taobao.com/coupon"))//这是针对淘宝联盟有优惠券的接口.//https://uland.taobao.com/coupon/edetail { var coupon = api.CheckCoupon(url);//将返利接口转成淘宝宝贝接口 if (coupon != null) return coupon.itemId; } else if (url.Contains(@"s.click.taobao.com"))//这是针对淘宝联盟没有全的接口.//https://s.click.taobao.com/t?e=m { var html = string.Empty; //获得首次Location 地址 bool first = false; Next: string location = GetLocation(url); if (location.StartsWith("https://s.click.taobao.com/t?e=")) { var _location = GetLocation(location); if (!string.IsNullOrEmpty(_location)) location = _location; } if (!string.IsNullOrEmpty(location))//跳转地址存在 { if (location.Contains("uland.taobao.com/coupon/edetail?e=")) { string tempHtml = GetHTML(location); tempHtml = HttpHelper.URLDecode(tempHtml); var uland = Regex.Match(tempHtml, "&req_url=([^\"]+)"); if (uland.Success) { var param_e = Regex.Match(uland.Groups[1].Value, "e=([^&]+)"); if (!param_e.Success) return string.Empty; //var param_result = h5Top.mtop_alimama_union_hsf_coupon_get(uland.Groups[1].Value); //// Dictionary param_result = Client.ClientPro.GetOpenResult("GetCoupon", "api/alimama.ashx", new { e = param_e.Groups[1].Value, dx = "1", src = "tblm_lmapp", scm = "20140618.1.01010001.s101c6", pid = "mm_33231688_7050284_23466709" }, true) as Dictionary; //if (param_result != null && param_result.ContainsKey("item")) //{ // var _item = param_result["item"] as Dictionary; // return "https://item.taobao.com/item.htm?id=" + _item["itemId"]; //} var coupon = api.CheckCoupon(uland.Groups[1].Value); if (coupon != null) return coupon.itemId; } else { //"系统日志,分析地址失败:" + url;} } } else if (location.Contains("uland.taobao.com")) { var _reg = Regex.Match(location, "itemId=(\\d+)", RegexOptions.IgnoreCase); if (_reg.Success) return _reg.Groups[1].Value; } else if (location.Contains("tu=")) { //获得二次访问地址 string next = HttpHelper.URLDecode(location.Replace("http://s.click.taobao.com/t_js?tu=", "").Replace("https://s.click.taobao.com/t_js?tu=", "")); html = HttpHelper.URLDecode(GetHTML(next, location)); var reg = Regex.Match(html, "&itemId=(\\d+)", RegexOptions.IgnoreCase); if (reg.Success) return reg.Groups[1].Value; } else return tbAnalysis.FindTbItemIdByStr(GetHTML(location)); } else if (!string.IsNullOrEmpty(location) && !first) { url = location; first = true; goto Next; } } } catch (Exception) { } return string.Empty; } private string GetLocation(string url) { try { HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = url, Method = "get", IsToLower = false, Cookie = "", Referer = "", Postdata = "", Timeout = 5000, ReadWriteTimeout = 15000, UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.154 Safari/537.36 LBBROWSER", ContentType = "application/x-www-form-urlencoded", Allowautoredirect = false, ProxyIp = "" }; HttpResult result = http.GetHtml(item); return result.Header.Get("Location"); } catch (Exception) { } return string.Empty; } private string GetHTML(string url, string rf = "", string postData = "") { try { HttpHelper http = new HttpHelper(); HttpItem item = new HttpItem() { URL = url, Method = (postData == "" ? "get" : "post"), Postdata = (postData == "" ? "" : postData), IsToLower = false, Referer = rf, Timeout = 5000, ReadWriteTimeout = 15000, UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.154 Safari/537.36 LBBROWSER", ContentType = "application/x-www-form-urlencoded", Allowautoredirect = true }; HttpResult result = http.GetHtml(item); return result.Html; } catch (Exception) { } return string.Empty; } } }