2424 lines
125 KiB
C#
2424 lines
125 KiB
C#
using Api.Framework.Cps;
|
||
using Api.Framework.Enums;
|
||
using Api.Framework.Model;
|
||
using Api.Framework.Utils;
|
||
using CsharpHttpHelper;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading;
|
||
using System.Text;
|
||
using System.Web;
|
||
using NPOI.POIFS.Storage;
|
||
|
||
namespace Api.Framework.Tools
|
||
{
|
||
/// <summary>
|
||
/// 淘宝导购帮助类
|
||
/// </summary>
|
||
public static class TBHelper
|
||
{
|
||
#region 优惠券信息和商品字典缓存
|
||
private static string cache_tbitemid_key = "cache_tbitemid_key";
|
||
/// <summary>
|
||
/// 在存有优惠券的字典中查询商品编码
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <returns></returns>
|
||
public static string FindTBCouponByItemIdCache(string url)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(url)) return string.Empty;
|
||
var reg = Regex.Match(url, @"i\d+?/(?<图片字符串>(?:\d+?)[^\.]+?)\.");
|
||
if (!reg.Success) return string.Empty;
|
||
var imgStr = reg.Groups["图片字符串"].Value;
|
||
var item_id = string.Empty;
|
||
var value = ApiClient.Cache.Get<Dictionary<string, string>>(cache_tbitemid_key);
|
||
if (value == null)
|
||
return string.Empty;
|
||
var md5 = Util.GetMd5_32byte(imgStr);
|
||
if (value.ContainsKey(md5))
|
||
return item_id = value[md5];
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
return string.Empty;
|
||
}
|
||
/// <summary>
|
||
/// 将优惠券链接和商品编码保存在字典中
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="item_id"></param>
|
||
public static void SetTBCouponByItemIdCache(string url, string item_id)
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(item_id)) return;
|
||
|
||
var value = ApiClient.Cache.Get<Dictionary<string, string>>(cache_tbitemid_key);
|
||
if (value == null)
|
||
value = new Dictionary<string, string>();
|
||
else if (value.Count > 1000)
|
||
value.Clear();
|
||
|
||
var reg = Regex.Match(url, @"i\d+?/(?<图片字符串>(?:\d+?)[^\.]+?)\.");
|
||
if (!reg.Success) return;
|
||
var imgStr = reg.Groups["图片字符串"].Value;
|
||
var md5 = Util.GetMd5_32byte(imgStr);
|
||
if (!value.ContainsKey(md5))
|
||
value.Add(md5, item_id);
|
||
ApiClient.Cache.Set(cache_tbitemid_key, value, 30);
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
}
|
||
#endregion
|
||
|
||
#region 获取外围优惠券信息
|
||
#region xxx
|
||
///// <summary>
|
||
///// 通过商品id获取优惠券信息(爬大淘客优惠券信息)
|
||
///// </summary>
|
||
///// <param name="itemId"></param>
|
||
///// <returns></returns>
|
||
//public static string FindGoodsActivityIdAll(string itemId, out string sellerId, out double payPrice)
|
||
//{
|
||
// var activityId = FindGoodsActivityId_DaTaoKe(itemId, out sellerId, out payPrice);
|
||
// if (string.IsNullOrWhiteSpace(activityId))
|
||
// activityId = FindGoodsActivityId_HaoDanKu(itemId, out sellerId, out payPrice);
|
||
// if (string.IsNullOrWhiteSpace(activityId))
|
||
// activityId = FindGoodsActivityId_XuanDan(itemId, out sellerId, out payPrice);
|
||
// return activityId;
|
||
//}
|
||
|
||
//public static string FindGoodsActivityId_DaTaoKe(string itemId, out string sellerId, out double payPrice)
|
||
//{
|
||
// sellerId = string.Empty;
|
||
// payPrice = -1;
|
||
// var activityid = string.Empty;
|
||
// try
|
||
// {
|
||
// HttpItem item = new HttpItem()
|
||
// {
|
||
// URL = $"http://www.dataoke.com/search/?keywords={itemId}",
|
||
// Method = "GET",
|
||
// Timeout = 5000,
|
||
// ReadWriteTimeout = 10000,
|
||
// IsToLower = false,
|
||
// UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
|
||
// Accept = "text/html, application/xhtml+xml, */*",
|
||
// ContentType = "text/html"
|
||
// };
|
||
// HttpHelper http = new HttpHelper();
|
||
// var result = http.GetHtml(item);
|
||
// if (result == null)
|
||
// return activityid;
|
||
// //var regs = Regex.Matches(result.Html, @"<a href=""/item\?id=(?<gid>\d+)\&parts=商品库");
|
||
// var regs = Regex.Matches(result.Html, @"goods-items_(?<gid>\d+)");
|
||
// if (regs.Count != 1)
|
||
// return activityid;
|
||
// var gid = regs[0].Groups["gid"].Value?.Trim();
|
||
// if (string.IsNullOrWhiteSpace(gid)) return activityid;
|
||
// item.URL = $"http://www.dataoke.com/gettpl?gid={gid}&_={HttpExtend.GetTimeStamp()}000";
|
||
// result = http.GetHtml(item);
|
||
// if (result == null)
|
||
// return activityid;
|
||
// var html = result.Html;
|
||
// if (html.Contains($"item.htm?id={itemId}"))
|
||
// {
|
||
// var reg = Regex.Match(html, @"activityId=(?<活动ID>[A-Za-z0-9]+)");
|
||
// if (reg.Success)
|
||
// {
|
||
// var activity = reg.Groups["活动ID"].Value?.Trim();
|
||
// reg = Regex.Match(html, @"sellerId=(?<店铺ID>[^&]*)");
|
||
// if (reg.Success)
|
||
// sellerId = reg.Groups["店铺ID"].Value;
|
||
|
||
// List<string> regStrs = new List<string>() { @"后【(?<券后>\d+(?:[.]{1}\d+?)?)元】", @"仅(?<券后>\d+(?:[.]{1}\d+?)?)元秒杀" };
|
||
// foreach (var regStr in regStrs)
|
||
// {
|
||
// reg = Regex.Match(html, regStr);
|
||
// if (reg.Success)
|
||
// {
|
||
// payPrice = double.Parse(reg.Groups["券后"].Value);
|
||
// break;
|
||
// }
|
||
// }
|
||
|
||
|
||
// //var quan = -1d;
|
||
// //reg = Regex.Match(html, @"(?<券>\d+[[.]{1}\d+?]?)元优惠券");
|
||
// //if (reg.Success)
|
||
// // quan = double.Parse(reg.Groups["券"].Value);
|
||
// //if (price != -1 && quan != -1)
|
||
// // payPrice = quan + price;
|
||
// return activity;
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// Console.WriteLine("html = " + html);
|
||
// }
|
||
// }
|
||
// catch (Exception)
|
||
// { }
|
||
// return activityid;
|
||
//}
|
||
|
||
//public static string FindGoodsActivityId_HaoDanKu(string itemId, out string sellerId, out double payPrice)
|
||
//{
|
||
// payPrice = -1;
|
||
// sellerId = string.Empty;
|
||
// var activityid = string.Empty;
|
||
// try
|
||
// {
|
||
// HttpItem item = new HttpItem()
|
||
// {
|
||
// URL = $"https://www.haodanku.com/indexapi/get_allitem_list?keyword={itemId}",
|
||
// Method = "GET",
|
||
// Timeout = 5000,
|
||
// ReadWriteTimeout = 10000,
|
||
// IsToLower = false,
|
||
// UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
|
||
// Accept = "text/html, application/xhtml+xml, */*",
|
||
// ContentType = "application/json"
|
||
// };
|
||
// HttpHelper http = new HttpHelper();
|
||
// var result = http.GetHtml(item);
|
||
// if (result == null)
|
||
// return activityid;
|
||
// var dic = HttpExtend.JsonToDictionary(result.Html);
|
||
// if (dic == null) return activityid;
|
||
// if (dic.ContainsKey("item_info"))
|
||
// {
|
||
// var item_info = dic["item_info"] as ArrayList;
|
||
// if (item_info != null && item_info.Count != 0)
|
||
// {
|
||
// var data = item_info[0] as Dictionary<string, object>;
|
||
// if (data.ContainsKey("activityid"))
|
||
// {
|
||
// activityid = data["activityid"].ToString();
|
||
// if (data.ContainsKey("userid"))
|
||
// sellerId = data["userid"].ToString();
|
||
// var price = -1d;
|
||
// if (data.ContainsKey("itemendprice"))
|
||
// payPrice = double.Parse(data["itemendprice"].ToString());
|
||
// //var quan = -1d;
|
||
// //if (data.ContainsKey("couponmoney"))
|
||
// // quan = double.Parse(data["couponmoney"].ToString());
|
||
// //if (quan != -1 && price != -1)
|
||
// // oldPrice = price + quan;
|
||
// return activityid;
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// catch (Exception)
|
||
// { }
|
||
// return activityid;
|
||
//}
|
||
|
||
//public static string FindGoodsActivityId_XuanDan(string itemId, out string sellerId, out double payPrice)
|
||
//{
|
||
// payPrice = -1;
|
||
// sellerId = string.Empty;
|
||
// var activityid = string.Empty;
|
||
// try
|
||
// {
|
||
// HttpItem item = new HttpItem()
|
||
// {
|
||
// URL = $"http://www.xuandan.com/index.html?key={itemId}",
|
||
// Method = "GET",
|
||
// Timeout = 5000,
|
||
// ReadWriteTimeout = 10000,
|
||
// IsToLower = false,
|
||
// UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
|
||
// Accept = "text/html, application/xhtml+xml, */*",
|
||
// ContentType = "application/json"
|
||
// };
|
||
// HttpHelper http = new HttpHelper();
|
||
// var result = http.GetHtml(item);
|
||
// if (result == null)
|
||
// return activityid;
|
||
// var html = result.Html;
|
||
// if (string.IsNullOrWhiteSpace(html)) return activityid;
|
||
// var reg = Regex.Match(html, "领券:([^<]+)");
|
||
// if (reg.Success)
|
||
// {
|
||
// var url = reg.Groups[1].Value.Trim();
|
||
// reg = Regex.Match(url, @"activityId=(?<活动ID>[A-Za-z0-9]+)");
|
||
// if (reg.Success)
|
||
// {
|
||
// var activity = reg.Groups["活动ID"].Value?.Trim();
|
||
// reg = Regex.Match(html, @"sellerId=(?<店铺ID>[^&]*)");
|
||
// if (reg.Success)
|
||
// sellerId = reg.Groups["店铺ID"].Value;
|
||
// reg = Regex.Match(html, @"【券后(?<券后>\d+[[.]{1}\d+?]?)元】");
|
||
// if (reg.Success)
|
||
// payPrice = double.Parse(reg.Groups["券后"].Value);
|
||
// return activity;
|
||
// }
|
||
// }
|
||
// }
|
||
// catch (Exception)
|
||
// { }
|
||
// return activityid;
|
||
//}
|
||
#endregion
|
||
|
||
#region
|
||
/// <summary>
|
||
/// 通过商品id获取优惠券信息(爬大淘客优惠券信息)
|
||
/// </summary>
|
||
/// <param name="itemId"></param>
|
||
/// <returns></returns>
|
||
public static TbExCoupon FindGoodsCouponInfo(string itemId)
|
||
{
|
||
TbExCoupon result = FindGoodsActivityId_DaTaoKe(itemId);
|
||
if (result == null)
|
||
result = FindGoodsActivityId_HaoDanKu(itemId);
|
||
//if (result == null)
|
||
// result = FindGoodsActivityId_XuanDan(itemId);
|
||
return result;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 通过商品id获取大淘客的信息
|
||
/// </summary>
|
||
/// <param name="itemId">商品id</param>
|
||
/// <returns></returns>
|
||
public static TbExCoupon FindGoodsActivityId_DaTaoKe(string itemId)
|
||
{
|
||
TbExCoupon exCoupon = null;
|
||
try
|
||
{
|
||
HttpItem item = new HttpItem()
|
||
{
|
||
//http://dtkapi.ffquan.cn/go_getway/proxy/search?platform=1&page=1&sortType=4&kw=525918728400
|
||
URL = $"http://dtkapi.ffquan.cn/go_getway/proxy/search?platform=1&page=1&sortType=4&kw={itemId}",
|
||
Method = "GET",
|
||
Timeout = 5000,
|
||
ReadWriteTimeout = 10000,
|
||
IsToLower = false,
|
||
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
|
||
Accept = "text/html, application/xhtml+xml, */*",
|
||
ContentType = "text/html"
|
||
};
|
||
HttpHelper http = new HttpHelper();
|
||
var result = http.GetHtml(item);
|
||
if (result == null)
|
||
return exCoupon;
|
||
|
||
var tmp = JObject.Parse(result.Html);
|
||
var list = (JArray)tmp["data"]["search"]["list"];
|
||
if (list != null && list.Count == 1)
|
||
{
|
||
var data = list[0];
|
||
var gid = data["id"].ToString();
|
||
var coupon_amount = data["coupon_amount"].ToString();//券金额
|
||
var price = data["price"].ToString();//券后价
|
||
var original_price = data["original_price"].ToString();//原价
|
||
|
||
if (string.IsNullOrWhiteSpace(gid))
|
||
return exCoupon;
|
||
item.URL = $"http://www.dataoke.com/gettpl?gid={gid}&_={HttpExtend.GetTimeStamp()}000";
|
||
result = http.GetHtml(item);
|
||
if (result == null)
|
||
return exCoupon;
|
||
var html = result.Html;
|
||
if (html.Contains($"item.htm?id={itemId}"))
|
||
{
|
||
var reg = Regex.Match(html, @"activityId=(?<活动ID>[A-Za-z0-9]+)");
|
||
if (reg.Success)
|
||
{
|
||
exCoupon = new TbExCoupon();
|
||
exCoupon.activityId = reg.Groups["活动ID"].Value?.Trim();
|
||
reg = Regex.Match(html, @"sellerId=(?<店铺ID>[^&]*)");
|
||
if (reg.Success)
|
||
exCoupon.sellerId = reg.Groups["店铺ID"].Value;
|
||
|
||
exCoupon.endPrice = double.Parse(price);
|
||
exCoupon.salePrice = double.Parse(original_price);
|
||
exCoupon.couponAmount = double.Parse(coupon_amount);
|
||
return exCoupon;
|
||
}
|
||
}
|
||
//else
|
||
//{
|
||
// Console.WriteLine("html = " + html);
|
||
//}
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
return exCoupon;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 通过商品id获取好单库的信息
|
||
/// </summary>
|
||
/// <param name="itemId">商品id</param>
|
||
/// <returns></returns>
|
||
public static TbExCoupon FindGoodsActivityId_HaoDanKu(string itemId)
|
||
{
|
||
TbExCoupon exCoupon = null;
|
||
try
|
||
{
|
||
HttpItem item = new HttpItem()
|
||
{
|
||
URL = $"https://www.haodanku.com/indexapi/get_allitem_list?keyword={itemId}",
|
||
Method = "GET",
|
||
Timeout = 5000,
|
||
ReadWriteTimeout = 10000,
|
||
IsToLower = false,
|
||
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
|
||
Accept = "text/html, application/xhtml+xml, */*",
|
||
ContentType = "application/json"
|
||
};
|
||
HttpHelper http = new HttpHelper();
|
||
var result = http.GetHtml(item);
|
||
if (result == null)
|
||
return exCoupon;
|
||
var dic = HttpExtend.JsonToDictionary(result.Html);
|
||
if (dic == null)
|
||
return exCoupon;
|
||
if (dic.ContainsKey("item_info"))
|
||
{
|
||
var tmp = JObject.Parse(result.Html);
|
||
var list = (JArray)(tmp["item_info"]);
|
||
|
||
if (list != null && list.Count == 1)
|
||
{
|
||
exCoupon = new TbExCoupon();
|
||
var data = list[0];
|
||
exCoupon.activityId = data["activityid"].ToString();//优惠券id
|
||
exCoupon.sellerId = data["userid"].ToString();//店铺id
|
||
exCoupon.endPrice = double.Parse(data["itemendprice"].ToString());//券后价
|
||
exCoupon.couponAmount = double.Parse(data["couponmoney"].ToString());//券金额
|
||
exCoupon.salePrice = double.Parse(data["itemprice"].ToString());//原价
|
||
|
||
return exCoupon;
|
||
}
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
return exCoupon;
|
||
}
|
||
|
||
///// <summary>
|
||
///// 通过商品id获取选单的信息
|
||
///// </summary>
|
||
///// <param name="itemId">商品id</param>
|
||
///// <returns></returns>
|
||
//public static TbExCoupon FindGoodsActivityId_XuanDan(string itemId)
|
||
//{
|
||
// TbExCoupon exCoupon = null;
|
||
// try
|
||
// {
|
||
// HttpItem item = new HttpItem()
|
||
// {
|
||
// URL = $"http://www.xuandan.com/index.html?key={itemId}",
|
||
// Method = "GET",
|
||
// Timeout = 5000,
|
||
// ReadWriteTimeout = 10000,
|
||
// IsToLower = false,
|
||
// UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
|
||
// Accept = "text/html, application/xhtml+xml, */*",
|
||
// ContentType = "application/json"
|
||
// };
|
||
// HttpHelper http = new HttpHelper();
|
||
// var result = http.GetHtml(item);
|
||
// if (result == null)
|
||
// return exCoupon;
|
||
// var html = result.Html;
|
||
// if (string.IsNullOrWhiteSpace(html))
|
||
// return exCoupon;
|
||
// var reg = Regex.Match(html, "领券:([^<]+)");
|
||
// if (reg.Success)
|
||
// {
|
||
// var url = reg.Groups[1].Value.Trim();
|
||
// reg = Regex.Match(url, @"activityId=(?<活动ID>[A-Za-z0-9]+)");
|
||
// if (reg.Success)
|
||
// {
|
||
// exCoupon = new TbExCoupon();
|
||
// var activity = reg.Groups["活动ID"].Value?.Trim();
|
||
// reg = Regex.Match(html, @"sellerId=(?<店铺ID>[^&]*)");
|
||
// if (reg.Success)
|
||
// sellerId = reg.Groups["店铺ID"].Value;
|
||
// reg = Regex.Match(html, @"【券后(?<券后>\d+[[.]{1}\d+?]?)元】");
|
||
// if (reg.Success)
|
||
// payPrice = double.Parse(reg.Groups["券后"].Value);
|
||
// return exCoupon;
|
||
// }
|
||
// }
|
||
// }
|
||
// catch (Exception)
|
||
// { }
|
||
// return exCoupon;
|
||
//}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 外围淘宝商品信息(优惠券等信息)
|
||
/// </summary>
|
||
public class TbExCoupon
|
||
{
|
||
/// <summary>
|
||
/// 优惠券信息
|
||
/// </summary>
|
||
public string activityId { get; set; }
|
||
/// <summary>
|
||
/// 店铺Id
|
||
/// </summary>
|
||
public string sellerId { get; set; }
|
||
/// <summary>
|
||
/// 折后价(活动价)
|
||
/// </summary>
|
||
public double salePrice { get; set; }
|
||
/// <summary>
|
||
/// 优惠券金额
|
||
/// </summary>
|
||
public double couponAmount { get; set; }
|
||
/// <summary>
|
||
/// 折后价格减去优惠券金额
|
||
/// </summary>
|
||
public double endPrice { get; set; }
|
||
|
||
}
|
||
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 保存淘宝基础信息的类
|
||
/// </summary>
|
||
public class TbBaseInfo
|
||
{
|
||
/// <summary>
|
||
/// 宝贝ID
|
||
/// </summary>
|
||
public string ItemId { get; set; }
|
||
/// <summary>
|
||
/// 月销量
|
||
/// </summary>
|
||
public string SellCount { get; set; }
|
||
/// <summary>
|
||
/// 价格
|
||
/// </summary>
|
||
public string Price { get; set; }
|
||
/// <summary>
|
||
/// 店铺名称
|
||
/// </summary>
|
||
public string ShopName { get; set; }
|
||
/// <summary>
|
||
/// 店铺头像
|
||
/// </summary>
|
||
public string ShopIcon { get; set; }
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
public string UserId { get; set; }
|
||
/// <summary>
|
||
/// 商品主图
|
||
/// </summary>
|
||
public string ItemImage { get; set; }
|
||
/// <summary>
|
||
/// 商品标题
|
||
/// </summary>
|
||
public string ItemTitle { get; set; }
|
||
/// <summary>
|
||
/// 店铺Id
|
||
/// </summary>
|
||
public string SellerId { get; set; }
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 淘宝解析方法
|
||
/// </summary>
|
||
public class TbAnalysis
|
||
{
|
||
/// <summary>
|
||
/// 查找宝贝Id的正则表达式
|
||
/// </summary>
|
||
private string[] Reg_Item_Ids = { @"item[Ii]d=(?<id>\d{5,})", @"item_id=(?<id>\d{5,})", @"\?id=(?<id>\d{5,})", @"&id=(?<id>\d{5,})", @"/i(?<id>\d{5,})", @"num[Ii]id=(?<id>\d{5,})", @"\?id=(?<id>[a-zA-Z0-9]{5,}-[a-zA-Z0-9]{5,})" };
|
||
|
||
private string key = "tbanalysis_key";
|
||
|
||
/// <summary>
|
||
/// 通过宝贝id,查询宝贝所有的信息
|
||
/// </summary>
|
||
/// <param name="itemId">宝贝ID</param>
|
||
/// <returns>返回TbInfo对象,异常返回Null</returns>
|
||
public TbBaseInfo FindTbInfoByItemId(string itemId)
|
||
{
|
||
var html = "";
|
||
try
|
||
{
|
||
//var item = TbTools.GetH5Result("mtop.taobao.detail.getdetail", new { itemNumId = itemId, exParams = new { id = itemId }, detail_v = "3.1.1", ttid = "2018@taobao_iphone_9.9.9", utdid = "123123123123123" }, out html);
|
||
|
||
//var time = ApiClient.Cache.Get<DateTime>(key);
|
||
//if (time == null)
|
||
// ApiClient.Cache.Set(key, DateTime.Now, 10);
|
||
//else
|
||
// return null;
|
||
|
||
var item = TbTools.GetH5Result("mtop.taobao.detail.getdetail", new { itemNumId = itemId, exParams = new { id = itemId }, ttid = "2016@taobao_h5_2.0.0", isSec = "0", ecode = "0", AntiFlood = "true", AntiCreep = "true", H5Request = "true", type = "jsonp", dataType = "jsonp" }, out html);
|
||
|
||
if (item == null) return null;
|
||
if (item.ContainsKey("item"))
|
||
{
|
||
var _item = item["item"] as Dictionary<string, object>;
|
||
var sellerId = string.Empty;
|
||
//这里可以通过图片的地址获取店铺的id
|
||
if (_item.ContainsKey("images"))
|
||
{
|
||
var images = _item["images"] as ArrayList;
|
||
if (images != null && images.Count != 0)
|
||
{
|
||
var reg = Regex.Match(images[0].ToString(), @"http://img.alicdn.com/imgextra/i\d/(?<店铺Id>\d+)/");
|
||
if (reg.Success)
|
||
sellerId = reg.Groups["店铺Id"].Value;
|
||
}
|
||
}
|
||
|
||
var price = string.Empty;//价格
|
||
var sellCount = string.Empty;
|
||
if (item.ContainsKey("apiStack"))
|
||
{
|
||
var apiStack = item["apiStack"] as ArrayList;
|
||
if (apiStack != null && apiStack.Count != 0)
|
||
{
|
||
var _apiStack = apiStack[0] as Dictionary<string, object>;
|
||
if (_apiStack.ContainsKey("value"))
|
||
{
|
||
var valueJsonStr = _apiStack["value"].ToString();
|
||
var valueJson = HttpExtend.JsonToDictionary(valueJsonStr);
|
||
|
||
#region 获取销量 sellCount
|
||
var itemTag = valueJson["item"] as Dictionary<string, object>;
|
||
//if (itemTag.ContainsKey("sellCount"))
|
||
// sellCount = itemTag["sellCount"].ToString();
|
||
if (itemTag.ContainsKey("sellCount"))
|
||
{
|
||
sellCount = itemTag["sellCount"].ToString();
|
||
if (sellCount.Contains("万+"))
|
||
{
|
||
sellCount = ((int)(decimal.Parse(sellCount.Replace("万+", "")) * 10000m)).ToString();
|
||
}
|
||
}
|
||
else
|
||
sellCount = "0";
|
||
#endregion
|
||
|
||
#region 获取价格 price
|
||
var priceTag = valueJson["price"] as Dictionary<string, object>;
|
||
if (priceTag.ContainsKey("price"))
|
||
{
|
||
var priceSub = priceTag["price"] as Dictionary<string, object>;
|
||
if (priceSub.ContainsKey("priceText"))
|
||
price = priceSub["priceText"].ToString();
|
||
}
|
||
if (string.IsNullOrEmpty(price) && priceTag.ContainsKey("transmitPrice"))
|
||
{
|
||
var transmitPrice = priceTag["transmitPrice"] as Dictionary<string, object>;
|
||
if (transmitPrice.ContainsKey("priceText"))
|
||
price = transmitPrice["priceText"].ToString();
|
||
}
|
||
if (!string.IsNullOrEmpty(price) && price.Contains("-")) //??这里可能还存在带问号的价格,目前不清楚是什么情况,先空着
|
||
price = price.Split('-')[0];
|
||
if (string.IsNullOrEmpty(price))
|
||
price = "0";
|
||
#endregion
|
||
}
|
||
}
|
||
|
||
if (item.ContainsKey("seller"))
|
||
{
|
||
var _seller = item["seller"] as Dictionary<string, object>;
|
||
//店铺名称
|
||
if (_seller.ContainsKey("shopName"))
|
||
{
|
||
var shopName = _seller["shopName"].ToString();
|
||
//店铺图片
|
||
if (_seller.ContainsKey("shopIcon"))
|
||
{
|
||
string shopIcon = _seller["shopIcon"].ToString();
|
||
shopIcon = string.IsNullOrEmpty(shopIcon) ? "https://ss2.bdstatic.com/8_V1bjqh_Q23odCf/pacific/1659832877.jpg" : shopIcon.StartsWith("http") ? _seller["shopIcon"].ToString() : "http:" + _seller["shopIcon"].ToString();
|
||
if (string.IsNullOrEmpty(shopIcon) || shopName == "天猫超市")
|
||
shopIcon = "https://ss2.bdstatic.com/8_V1bjqh_Q23odCf/pacific/1659832877.jpg";
|
||
|
||
var userId = string.Empty;
|
||
if (_seller.ContainsKey("userId"))
|
||
userId = _seller["userId"].ToString();
|
||
//主图图片
|
||
var item_image = string.Empty;
|
||
if (_item.ContainsKey("images"))
|
||
item_image = (_item["images"] as ArrayList)[0].ToString();
|
||
//商品标题
|
||
var item_title = string.Empty;
|
||
if (_item.ContainsKey("title"))
|
||
item_title = _item["title"].ToString();
|
||
return new TbBaseInfo() { ItemId = itemId, ItemImage = item_image, ItemTitle = item_title, Price = price, SellCount = sellCount, ShopIcon = shopIcon, ShopName = shopName, UserId = userId, SellerId = sellerId };
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
//EventClient.OnEvent("+++++", ex.Message + " - " + ex.StackTrace + " @@@@@@@ " + html);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从字符串中获取淘宝宝贝id
|
||
/// </summary>
|
||
/// <param name="str">要查找的字符串</param>
|
||
/// <returns>返回查找到的宝贝id,未找到返回空,异常返回异常的信息</returns>
|
||
public string FindTbItemIdByStr(string str)
|
||
{
|
||
try
|
||
{
|
||
//获取淘宝天猫的宝贝id值
|
||
for (int i = 0; i < Reg_Item_Ids.Length; i++)
|
||
{
|
||
var reg = Regex.Match(str, Reg_Item_Ids[i].ToString());
|
||
if (reg.Success)
|
||
return reg.Groups["id"].Value;
|
||
}
|
||
return string.Empty;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return string.Empty;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 活动ID
|
||
/// </summary>
|
||
public string activityId = string.Empty;
|
||
|
||
#region 在线获取口令正则表达式
|
||
/// <summary>
|
||
/// 清空正则表达式
|
||
/// </summary>
|
||
public static void TklRegToEmpty()
|
||
{
|
||
tklReg = string.Empty;
|
||
}
|
||
|
||
private static string tklReg = string.Empty;
|
||
private static DateTime UpdatetklReg = DateTime.MinValue;
|
||
public static string GetTklReg()
|
||
{
|
||
if (UpdatetklReg.AddMinutes(5) < DateTime.Now)
|
||
tklReg = string.Empty;
|
||
if (string.IsNullOrWhiteSpace(tklReg))
|
||
{
|
||
var ali = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.阿里妈妈);
|
||
if (ali == null)
|
||
ali = new fl_cps_member() { cpstype = CpsType.阿里妈妈, username = "28592081" };
|
||
if (ali != null)
|
||
{
|
||
var cps = CpsClient.CreateAlimamaRequest(ali);
|
||
if (cps != null)
|
||
{
|
||
for (int i = 0; i < 3; i++)
|
||
{
|
||
try
|
||
{
|
||
var data = cps.SendServer("find_wangzhi", "webTool.asmx", new { name = "淘口令中文正则" }).message.ToString();
|
||
if (!string.IsNullOrWhiteSpace(data))
|
||
{
|
||
tklReg = data;
|
||
break;
|
||
}
|
||
continue;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
tklReg = string.Empty;
|
||
Thread.Sleep(500);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
UpdatetklReg = DateTime.Now;
|
||
}
|
||
if (string.IsNullOrWhiteSpace(tklReg))
|
||
{
|
||
//tklReg = @"(?<tkl>.*(/|!||,[^\u4e00-\u9fa5])(?<tkl1>[\u4e00-\u9fa5]{11,16})(十|[^\u4e00-\u9fa5]).*)";
|
||
tklReg = @"(?<tkl>.*?(?:(?:\d+:/(?:/|!|,|[^\u4e00-\u9fa5])?)|(?:[^\u4e00-\u9fa5])?)(?<tkl1>[\u4e00-\u9fa5]{11,16})(http:[^\u4e00-\u9fa5]+)?.*)(?:微|乐|云|品|麦|凌|十|口|广|哇|它|厂|[^\u4e00-\u9fa5])?.*";
|
||
}
|
||
return tklReg;
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 获取宝贝Id.解析所有的各种链接/淘口令/喵口令等,先解析商品类型的链接,在去解析其他类型的链接
|
||
/// </summary>
|
||
/// <param name="mess">要解析内容</param>
|
||
/// <param name="api">AlimamaApi对象</param>
|
||
/// <returns>返回宝贝Id,找不到返回空,异常返回异常的信息</returns>
|
||
public string FindItemIdByUrlAndTklAndMkl(string mess, AlimamaApi api, string[] pids = null)
|
||
{
|
||
try
|
||
{
|
||
if (mess.Length <= 8) return string.Empty;
|
||
string item_id = string.Empty;
|
||
|
||
if (string.IsNullOrWhiteSpace(mess) ||
|
||
Regex.IsMatch(mess, @"\[图片=(.+?)\]") ||
|
||
Regex.IsMatch(mess, @"\[视频=(.+?)\]") ||
|
||
Regex.IsMatch(mess, @"\[卡片=(.+?)\]") ||
|
||
Regex.IsMatch(mess, @"\[公告=(.+?)\]") ||
|
||
Regex.IsMatch(mess, @"\[语音=(.+?)\]") ||
|
||
mess.Contains("抖音口令") ||
|
||
mess.Contains("抖音搜索") ||
|
||
mess.Contains("8##") ||
|
||
mess.Contains("#小程序://京东")
|
||
)
|
||
return item_id;
|
||
|
||
var promotion_id = string.Empty;
|
||
var title = string.Empty;
|
||
if (!string.IsNullOrWhiteSpace(DYHelper.GetDyGoodsID(mess, out promotion_id, out title)))
|
||
return string.Empty;
|
||
|
||
item_id = FindTbItemIdByStr(mess);
|
||
if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
|
||
#region 2019年6月10日09:40:31 支持用户发送的语句中存在多个连接
|
||
var regs = HttpExtend.RegexMatchesUrl(mess);
|
||
if (regs != null && regs.Count != 0)
|
||
{
|
||
foreach (var item in regs)
|
||
{
|
||
try
|
||
{
|
||
var url = item;
|
||
item_id = FindTbItemIdByStr(url);//正常的淘宝天猫链接 [ https://detail.tmall.com/item.htm?id=576795776133 && https://item.taobao.com/item.htm?id=573396652123 ]
|
||
if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
|
||
if (mess.Contains("喵口令"))
|
||
{
|
||
try
|
||
{
|
||
var _app = api.SendServer("taobao.tbk.h5.top",
|
||
new { api = "mtop.tmall.share.initdialog", param = HttpHelper.ObjectToJson(new { data = HttpHelper.ObjectToJson(new { codes = new string[] { url }, wholeCode = HttpHelper.URLEncode(mess) }) }), v = "1.0" });
|
||
if (_app != null && _app.ok)
|
||
{
|
||
var result = _app.message as Dictionary<string, object>;
|
||
if (result != null && result.ContainsKey("dialog"))
|
||
{
|
||
result = result["dialog"] as Dictionary<string, object>;
|
||
if (result.ContainsKey("rightBtnActionUrl"))
|
||
{
|
||
item_id = FindTbItemIdByStr(result["rightBtnActionUrl"].ToString());
|
||
if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
}
|
||
//var regUrl = Regex.Match(url, @"(yukhj.com|m.tb.cn|m.yeeoq.top|m.uqlsi|m.rijni|m.gkqmj|m.mulwt|uland\\.taobao\\.com|sjtm.me|aa5d.com|d11a.com|f61[a-z].com|92db.com|yqt.so|e22a.com|jd.hk|f61b.com|detail.m.tmall.hk|k.83jie.com|ewqcxz.com|items.alitrip.com|tmqd.me|detail.tmall.hk|url.cn|taobao.com|mashort.cn|tmall.com|tb.cn|t.cn|detail.yao.95095.com|laiwang.com|jd.com|dwz.cn|juhuasuan.com|detail.m.tmall.com|zmnxbc.com|s.click.taobao|detail.liangxinyao.com)");
|
||
//var regUrl = Regex.Match(url, @"(yukhj.com|m.tb.cn|m.yeeoq.top|m.uqlsi|m.rijni|m.gkqmj|m.mulwt|uland\\.taobao\\.com|sjtm.me|aa5d.com|d11a.com|f61[a-z].com|92db.com|yqt.so|e22a.com|f61b.com|detail.m.tmall.hk|k.83jie.com|ewqcxz.com|items.alitrip.com|tmqd.me|detail.tmall.hk|url.cn|taobao.com|mashort.cn|tmall.com|tb.cn|t.cn|detail.yao.95095.com|laiwang.com|dwz.cn|juhuasuan.com|detail.m.tmall.com|zmnxbc.com|s.click.taobao|detail.liangxinyao.com|m.tb.cn)");
|
||
var regUrl = Regex.Match(url, @"(yukhj.com|m.tb.cn|m.yeeoq.top|m.uqlsi|m.rijni|m.gkqmj|m.mulwt|uland\\.taobao\\.com|sjtm.me|aa5d.com|d11a.com|f61[a-z].com|92db.com|yqt.so|e22a.com|f61b.com|detail.m.tmall.hk|k.83jie.com|ewqcxz.com|items.alitrip.com|tmqd.me|detail.tmall.hk|url.cn|taobao.com|mashort.cn|tmall.com|tb.cn|detail.yao.95095.com|laiwang.com|dwz.cn|juhuasuan.com|detail.m.tmall.com|zmnxbc.com|s.click.taobao|detail.liangxinyao.com|m.tb.cn)");
|
||
if (regUrl.Success)
|
||
{
|
||
|
||
item_id = getMTbCn(url, api);
|
||
if (!string.IsNullOrWhiteSpace(item_id))
|
||
{
|
||
return item_id;
|
||
}
|
||
|
||
var html = string.Empty;
|
||
for (int i = 0; i < 3; i++)
|
||
{
|
||
var http = new HttpHelper();
|
||
html = http.GetHtml(url).Html;
|
||
LogHelper.GetSingleObj().Error("淘宝请求", $"{url} => {html}");
|
||
|
||
if (html.Contains("超时操作"))
|
||
Thread.Sleep(500);
|
||
else break;
|
||
}
|
||
var reg = Regex.Match(html, @"var url = '(?<url>.*?)';");
|
||
if (reg.Success)
|
||
{
|
||
var oriUrl = reg.Groups["url"].Value;
|
||
if (string.IsNullOrWhiteSpace(oriUrl))
|
||
{
|
||
return "00000";
|
||
}
|
||
item_id = CouponByItemId(oriUrl, api);
|
||
}
|
||
if (string.IsNullOrWhiteSpace(item_id))
|
||
item_id = FindTbItemIdByStr(html);
|
||
if (!string.IsNullOrWhiteSpace(item_id)) return item_id;
|
||
|
||
if (url.Contains("uland.taobao"))
|
||
{
|
||
item_id = CouponByItemId(url, api);
|
||
if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
}
|
||
|
||
if (url.Contains("p.tb.cn"))//https://p.tb.cn/5syZScgn56Eycz3W1eRKPk
|
||
{
|
||
var locationA = TbTools.FindLocationByUrl(url);
|
||
if (!string.IsNullOrWhiteSpace(locationA))
|
||
item_id = FindTbItemIdByStr(locationA);
|
||
if (!string.IsNullOrWhiteSpace(item_id))
|
||
return item_id;
|
||
}
|
||
|
||
if (url.Contains("s.click.taobao"))//https://s.click.taobao.com/aFIAbOw 这种链接
|
||
{
|
||
var locationA = TbTools.FindLocationByUrl(url);
|
||
if (locationA != null)
|
||
{
|
||
if (!locationA.Contains("err.taobao.com"))
|
||
{
|
||
//item_id = CouponByItemId(mess, api);
|
||
item_id = CouponByItemId(url, api);
|
||
if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
var locationB = TbTools.FindLocationByUrl(locationA);
|
||
if (string.IsNullOrWhiteSpace(locationB)) { item_id = string.Empty; }
|
||
else
|
||
{
|
||
var goodsUrl = HttpHelper.URLDecode(locationB.Replace("https://s.click.taobao.com/t_js?tu=", "").Replace("http://s.click.taobao.com/t_js?tu=", ""));
|
||
var tb_url = TbTools.FindLocationByUrl(goodsUrl, locationB);//获取到的淘宝链接
|
||
if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
var _html = GetHTML(url);
|
||
reg = Regex.Match(_html, "real_jump_address = '(?<真正的跳转地址>[^']+)");
|
||
if (reg.Success)
|
||
{
|
||
var address = reg.Groups["真正的跳转地址"].Value;
|
||
//_html = GetHTML(HttpHelper.URLDecode(address).Replace("&", "&"), HttpHelper.URLDecode(url));
|
||
for (int i = 0; i < 3; i++)
|
||
{
|
||
_html = GetHTML(address.Replace("&", "&"), url);
|
||
item_id = FindTbItemIdByStr(_html);
|
||
if (!string.IsNullOrWhiteSpace(item_id))
|
||
return item_id;
|
||
else
|
||
{
|
||
//访问频繁,休息重试3次
|
||
Thread.Sleep(500);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
mess = mess.Replace(url, "");//清除没用的连接,如果连接无法解析,name下面的淘口令解析将会减轻负担
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
}
|
||
}
|
||
#endregion
|
||
//EventClient.OnEvent("", "A0 = " + item_id);
|
||
//if (!reg.Success || string.IsNullOrEmpty(item_id))//非链接 淘口令
|
||
if (string.IsNullOrEmpty(item_id))//非链接 淘口令
|
||
{
|
||
if (Util.IsXML(mess))
|
||
return string.Empty;
|
||
var _mess = Regex.Replace(mess, "Http:/[A-Za-z]", "", RegexOptions.IgnoreCase);
|
||
|
||
//_mess = Util.RemoveEmoji(_mess);
|
||
var tklTmps = new List<string>();
|
||
|
||
#region 正常口令
|
||
var regs1 = Regex.Matches(_mess, "[^A-Za-z0-9]?([A-Za-z0-9]{11})[^A-Za-z0-9]?");
|
||
foreach (Match item in regs1)
|
||
{
|
||
if (item.Success)
|
||
{
|
||
var tkl = item.Groups[1].Value;
|
||
if (Regex.IsMatch(tkl, @"(\d{11})"))
|
||
continue;
|
||
tklTmps.Add(tkl);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
var tklreg = GetTklReg();
|
||
regs1 = Regex.Matches(_mess, tklreg, RegexOptions.IgnoreCase);
|
||
foreach (Match item in regs1)
|
||
{
|
||
if (item.Success)
|
||
{
|
||
var tkl = _mess;// item.Groups["tkl"].Value;
|
||
tklTmps.Add(tkl);
|
||
}
|
||
}
|
||
|
||
//regs1 = Regex.Matches(_mess, @"[^A-Za-z0-9]?(?<tkl>[A-Za-z0-9]{5}(?<tmp>\[0x[A-Za-z0-9]{8}\]|[^A-Za-z0-9]{1,2})[A-Za-z0-9]{6})[^A-Za-z0-9]?");
|
||
//foreach (Match item in regs1)
|
||
//{
|
||
// if (item.Success)
|
||
// {
|
||
// var tkl = item.Groups["tkl"].Value.Replace(item.Groups["tmp"].Value, "");
|
||
// if (Regex.IsMatch(tkl, @"(\d{11})"))
|
||
// continue;
|
||
// tklTmps.Add(tkl);
|
||
// }
|
||
//}
|
||
|
||
|
||
//regs1 = Regex.Matches(_mess, @"[^A-Za-z0-9]?(?<tkl>[A-Za-z0-9]{5}(?<tmp>\[0x[A-Za-z0-9]{8}\]|[^A-Za-z0-9])[A-Za-z0-9]{6})[^A-Za-z0-9]?");
|
||
//foreach (Match item in regs1)
|
||
//{
|
||
// if (item.Success)
|
||
// {
|
||
// var tkl = item.Groups["tkl"].Value.Replace(item.Groups["tmp"].Value, "");
|
||
// if (Regex.IsMatch(tkl, @"(\d{11})"))
|
||
// continue;
|
||
// tklTmps.Add(tkl);
|
||
// }
|
||
//}
|
||
|
||
|
||
tklTmps = tklTmps.Distinct().ToList();
|
||
//if (tklTmps != null && tklTmps.Count != 0)
|
||
//{
|
||
// Dictionary<string, Exception> tklDic = new Dictionary<string, Exception>();
|
||
// foreach (string tkl in tklTmps)
|
||
// {
|
||
// if (!tklDic.ContainsKey(tkl))
|
||
// tklDic.Add(tkl, null);
|
||
// try
|
||
// {
|
||
// var h5 = new h5Top();
|
||
// try
|
||
// {
|
||
// item_id = h5.GetItemIdByTKL(tkl, api);
|
||
// if (!string.IsNullOrWhiteSpace(h5.activityId))
|
||
// activityId = h5.activityId;
|
||
// if (!string.IsNullOrWhiteSpace(item_id))
|
||
// return item_id;
|
||
// }
|
||
// catch (Exception ex)
|
||
// { }
|
||
|
||
// #region 调用淘宝接口
|
||
// if (api != null)
|
||
// {
|
||
// #region 推广位为空时,从数据库中获取推广位数据
|
||
// if (pids == null)
|
||
// {
|
||
// var adzoneInfo = ApiClient.GetSession().FindAdzoneInfos();
|
||
// if (adzoneInfo != null)
|
||
// {
|
||
// var adzones = adzoneInfo.Where(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.adzone_pid_cps_name == api.Member.username && f.adzone_pid != "").ToList();
|
||
|
||
// if (adzones != null && adzones.Count != 0)
|
||
// {
|
||
// var adzone = adzones[0];
|
||
// var pid = adzone.adzone_pid;
|
||
// pids = pid.Split('_');
|
||
// }
|
||
// }
|
||
// }
|
||
// #endregion
|
||
|
||
// if (pids != null)
|
||
// {
|
||
// for (int i = 0; i < 3; i++)
|
||
// {
|
||
// try
|
||
// {
|
||
// var result = api.AnalysisTkPassword(tkl, pids[3], pids[2]);
|
||
// if (result != null)
|
||
// {
|
||
|
||
// if (string.IsNullOrWhiteSpace(result.num_iid) == false)
|
||
// {
|
||
// activityId = result.activityId;
|
||
// return result.num_iid;
|
||
// }
|
||
// else if (string.IsNullOrWhiteSpace(result.origin_url) == false)
|
||
// {
|
||
// var origin_url = result.origin_url;
|
||
// var itemId = h5.AnalysisItemidByTljUrl(origin_url);
|
||
// if (string.IsNullOrWhiteSpace(itemId))
|
||
// {
|
||
// //https://mo.m.tmall.com/page/4575720?shop_id=467416422&tjm_src_bp=lianmeng&ali_trackid=2%3Amm_1754690195_2267550095_112115050458%3A1652094564_031_1590174982&union_lens=lensId%3ATAPI%401651496796%4021077da2_094a_18084e14332_2603%40022DmGvXUZxOvuwwSZ0LoETU&e=zrVP7yuy4rVotn5NVqnsNSD_KSPfxwxAjbefuUrtutMvRYPtLig9CvJWHexRmZcAI4Mf8vGNB9I5gQVXM_6rcSS0mLRQv4Bd_mADjKzV7vKVZ5v92btTAHsIrYMGBr2GBYPMuFXeq-4BuPt5MbeOhNcpn7YIi5VZD6mqIygp0v5Y1G4UzsUP7C8moQkRQa_XEW7UOyhDnSkfiCPkiD6Cz3R-90eN4O9vOXCP7dzEQOKfPMuLv_j__qqFFEONMR2_oHs8zUzui2caaWnKBfgFYluKPxpVF7I8PCr7uRbxpRR5dfvaanbnRU3etiyKgMVl&type=2&tk_cps_param=1754690195&tkFlag=0&tk_cps_ut=1&bxsign=tcdZEytOnK0_AhxV5fosBNu9idGZ5_8gUin4FkyHzftdv0tMZVkZO1xpC-uzCWNPNv09ZNRd_eheS4r7pSoiZhpyKVYrAm48FRABurbYVLtTmI&sourceType=other&suid=6af02016-55bc-4c67-8d07-07cfa0fb7513&ut_sk=1.XJLlO2HWVcIDABY8xNZnqB%2B8_21646297_1652094536032.TaoPassword-WeiXin.tjm_share&un=d09271ea3d8897ed2040bd82ddc2e1ea&share_crt_v=1&un_site=0&spm=a2159r.13376460.0.0&sp_abtk=common_tjm_share_commonInfo&sp_tk=5bCP5LuW5LiK6IO95pyJ5a2Q5LqG5Y676LW35a%2B55LqG&bc_fl_src=share-480627500109-1-0
|
||
|
||
// if (origin_url.Contains("tmall.com"))
|
||
// {
|
||
// var res = HttpExtend.GetHtml(new HttpHelper(), origin_url);
|
||
// if (!string.IsNullOrWhiteSpace(res.Html))
|
||
// {
|
||
// var reg = Regex.Match(res.Html, @"item_id=""(\d+)""", RegexOptions.IgnoreCase);
|
||
// if (reg.Success)
|
||
// {
|
||
// return reg.Groups[1].Value;
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// //945122237(潜意识)请求阿里妈妈接口:{"error_response":{"code":15,"msg":"Remote service error","sub_code":"isv.parse-result-invalid","sub_msg":"淘口令解析结果无效","request_id":"z24mo2uo0obo"}} - 在 Api.Framework.Cps.AlimamaApi.SendTaobaoServer(String _api, Object _data, String _appkey, String _appsecret) 位置 C:\xxxxxxxxxxxxxxxxx
|
||
// LogHelper.GetSingleObj().Debug("tkl解析", tkl + " = " + mess + "; 异常:" + ex.Message);
|
||
// if (i >= 1)
|
||
// throw ex;
|
||
// }
|
||
// }
|
||
// }
|
||
// }
|
||
// #endregion
|
||
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// tklDic[tkl] = ex;
|
||
// if (tklDic.Count < tklTmps.Count)
|
||
// continue;
|
||
// LogHelper.GetSingleObj().Debug("", $"Oo:{mess} tkl:{tkl} - {ex.Message} - {ex.StackTrace}");
|
||
// throw ex;
|
||
// }
|
||
// }
|
||
|
||
// //if (tklDic.Count >= tklTmps.Count)
|
||
// //{
|
||
// // var ex = tklDic.Values.ToList()[0];
|
||
// // throw ex;
|
||
// //}
|
||
//}
|
||
|
||
if (tklTmps.Count > 0)
|
||
{
|
||
mess = Util.RemoveEmoji(mess);
|
||
mess = Regex.Replace(mess, @"[]", "");//这里有两个看不见的字符,两个字符不一样,都要删除了(这个只针对Ipad协议)
|
||
|
||
item_id = GetItemInfoByPass(mess, tklTmps, api, pids);
|
||
}
|
||
}
|
||
return item_id;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
//EventClient.OnEvent(this, $"mess={mess}{ex.Message},{ex.StackTrace}");
|
||
//淘口令解析结果无效,当前淘口令无效
|
||
if (ex.Message.Contains(@"""淘口令转链处理异常""") || ex.Message.Contains(@"淘口令解析结果无效") || ex.Message.Contains(@"""淘口令解析结果"))
|
||
{
|
||
throw ex;
|
||
}
|
||
return string.Empty;
|
||
}
|
||
return string.Empty;
|
||
}
|
||
|
||
private string getMTbCn(string url, AlimamaApi api)
|
||
{
|
||
try
|
||
{
|
||
if (!url.ToLower().Contains("m.tb.cn"))
|
||
{
|
||
return string.Empty;
|
||
}
|
||
|
||
for (int i = 0; i < 3; i++)
|
||
{
|
||
HttpHelper http = new HttpHelper();
|
||
HttpItem item = new HttpItem()
|
||
{
|
||
URL = url,
|
||
Method = "GET",
|
||
Timeout = 10000,
|
||
ReadWriteTimeout = 30000,
|
||
IsToLower = false,
|
||
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
|
||
Accept = "text/html, application/xhtml+xml, */*",
|
||
ContentType = "application/x-www-form-urlencoded",
|
||
Referer = "",
|
||
Allowautoredirect = true,
|
||
AutoRedirectCookie = false,
|
||
Postdata = ""
|
||
};
|
||
HttpResult result = http.GetHtml(item);
|
||
string html = result.Html;
|
||
|
||
LogHelper.GetSingleObj().Error("淘宝解析请求", $"{url} =>> {html}");
|
||
|
||
var item_id = string.Empty;
|
||
|
||
var reg = Regex.Match(html, @"var url = '(?<url>.*?)';");
|
||
if (reg.Success)
|
||
{
|
||
var oriUrl = reg.Groups["url"].Value;
|
||
if (string.IsNullOrWhiteSpace(oriUrl))
|
||
{
|
||
return "00000";
|
||
}
|
||
item_id = CouponByItemId(oriUrl, api);
|
||
}
|
||
if (string.IsNullOrWhiteSpace(item_id))
|
||
item_id = FindTbItemIdByStr(html);
|
||
if (!string.IsNullOrWhiteSpace(item_id))
|
||
return item_id;
|
||
Thread.Sleep(500);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.GetSingleObj().Error("淘宝解析请求异常", $"{url} -> {ex.Message}");
|
||
}
|
||
return string.Empty;
|
||
}
|
||
|
||
private string GetItemInfoByPass(string mess, List<string> tklTmps, AlimamaApi api, string[] pids = null)
|
||
{
|
||
var item_id = string.Empty;
|
||
|
||
var h5 = new h5Top();
|
||
|
||
foreach (string tkl in tklTmps)
|
||
{
|
||
try
|
||
{
|
||
item_id = h5.GetItemIdByTKL(tkl, api);
|
||
if (!string.IsNullOrWhiteSpace(h5.activityId))
|
||
activityId = h5.activityId;
|
||
if (!string.IsNullOrWhiteSpace(item_id))
|
||
return item_id;
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
}
|
||
|
||
try
|
||
{
|
||
#region 调用淘宝接口
|
||
if (api != null)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(mess))
|
||
{
|
||
return string.Empty;
|
||
}
|
||
|
||
#region 推广位为空时,从数据库中获取推广位数据
|
||
if (pids == null)
|
||
{
|
||
var adzoneInfo = ApiClient.GetSession().FindAdzoneInfos();
|
||
if (adzoneInfo != null)
|
||
{
|
||
var adzones = adzoneInfo.Where(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.adzone_pid_cps_name == api.Member.username && f.adzone_pid != "").ToList();
|
||
|
||
if (adzones != null && adzones.Count != 0)
|
||
{
|
||
var adzone = adzones[0];
|
||
var pid = adzone.adzone_pid;
|
||
pids = pid.Split('_');
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
if (pids != null)
|
||
{
|
||
for (int i = 0; i < 3; i++)
|
||
{
|
||
try
|
||
{
|
||
var result = api.AnalysisTkPassword(mess, pids[3], pids[2]);
|
||
if (result != null)
|
||
{
|
||
|
||
if (string.IsNullOrWhiteSpace(result.num_iid) == false)
|
||
{
|
||
activityId = result.activityId;
|
||
return result.num_iid;
|
||
}
|
||
else if (string.IsNullOrWhiteSpace(result.origin_url) == false)
|
||
{
|
||
var origin_url = result.origin_url;
|
||
var itemId = h5.AnalysisItemidByTljUrl(origin_url);
|
||
if (string.IsNullOrWhiteSpace(itemId))
|
||
{
|
||
//https://mo.m.tmall.com/page/4575720?shop_id=467416422&tjm_src_bp=lianmeng&ali_trackid=2%3Amm_1754690195_2267550095_112115050458%3A1652094564_031_1590174982&union_lens=lensId%3ATAPI%401651496796%4021077da2_094a_18084e14332_2603%40022DmGvXUZxOvuwwSZ0LoETU&e=zrVP7yuy4rVotn5NVqnsNSD_KSPfxwxAjbefuUrtutMvRYPtLig9CvJWHexRmZcAI4Mf8vGNB9I5gQVXM_6rcSS0mLRQv4Bd_mADjKzV7vKVZ5v92btTAHsIrYMGBr2GBYPMuFXeq-4BuPt5MbeOhNcpn7YIi5VZD6mqIygp0v5Y1G4UzsUP7C8moQkRQa_XEW7UOyhDnSkfiCPkiD6Cz3R-90eN4O9vOXCP7dzEQOKfPMuLv_j__qqFFEONMR2_oHs8zUzui2caaWnKBfgFYluKPxpVF7I8PCr7uRbxpRR5dfvaanbnRU3etiyKgMVl&type=2&tk_cps_param=1754690195&tkFlag=0&tk_cps_ut=1&bxsign=tcdZEytOnK0_AhxV5fosBNu9idGZ5_8gUin4FkyHzftdv0tMZVkZO1xpC-uzCWNPNv09ZNRd_eheS4r7pSoiZhpyKVYrAm48FRABurbYVLtTmI&sourceType=other&suid=6af02016-55bc-4c67-8d07-07cfa0fb7513&ut_sk=1.XJLlO2HWVcIDABY8xNZnqB%2B8_21646297_1652094536032.TaoPassword-WeiXin.tjm_share&un=d09271ea3d8897ed2040bd82ddc2e1ea&share_crt_v=1&un_site=0&spm=a2159r.13376460.0.0&sp_abtk=common_tjm_share_commonInfo&sp_tk=5bCP5LuW5LiK6IO95pyJ5a2Q5LqG5Y676LW35a%2B55LqG&bc_fl_src=share-480627500109-1-0
|
||
|
||
if (origin_url.Contains("tmall.com"))
|
||
{
|
||
var res = HttpExtend.GetHtml(new HttpHelper(), origin_url);
|
||
if (!string.IsNullOrWhiteSpace(res.Html))
|
||
{
|
||
var reg = Regex.Match(res.Html, @"item_id=""(\d+)""", RegexOptions.IgnoreCase);
|
||
if (reg.Success)
|
||
{
|
||
return reg.Groups[1].Value;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
//945122237(潜意识)请求阿里妈妈接口:{"error_response":{"code":15,"msg":"Remote service error","sub_code":"isv.parse-result-invalid","sub_msg":"淘口令解析结果无效","request_id":"z24mo2uo0obo"}} - 在 Api.Framework.Cps.AlimamaApi.SendTaobaoServer(String _api, Object _data, String _appkey, String _appsecret) 位置 C:\xxxxxxxxxxxxxxxxx
|
||
LogHelper.GetSingleObj().Debug("tkl解析", mess + "; 异常:" + ex.Message);
|
||
if (i >= 1)
|
||
throw ex;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.GetSingleObj().Debug("", $"Oo:{mess} - {ex.Message} - {ex.StackTrace}");
|
||
throw ex;
|
||
}
|
||
|
||
return item_id;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取宝贝Id.解析所有的各种链接/淘口令/喵口令等,先解析商品类型的链接,在去解析其他类型的链接
|
||
/// </summary>
|
||
/// <param name="mess">要解析内容</param>
|
||
/// <param name="api">AlimamaApi对象</param>
|
||
/// <returns>返回宝贝Id,找不到返回空,异常返回异常的信息</returns>
|
||
[Obsolete("过时的")]
|
||
public string FindItemIdByUrlAndTklAndMkl(string mess, AlimamaApi api)
|
||
{
|
||
try
|
||
{
|
||
string item_id = string.Empty;
|
||
item_id = FindTbItemIdByStr(mess);
|
||
if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
|
||
#region 2019年6月10日09:40:31 支持用户发送的语句中存在多个连接
|
||
var regs = HttpExtend.RegexMatchesUrl(mess);
|
||
if (regs != null && regs.Count != 0)
|
||
{
|
||
foreach (var item in regs)
|
||
{
|
||
try
|
||
{
|
||
var url = item;
|
||
item_id = FindTbItemIdByStr(url);//正常的淘宝天猫链接 [ https://detail.tmall.com/item.htm?id=576795776133 && https://item.taobao.com/item.htm?id=573396652123 ]
|
||
if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
|
||
if (mess.Contains("喵口令"))
|
||
{
|
||
try
|
||
{
|
||
var _app = api.SendServer("taobao.tbk.h5.top",
|
||
new { api = "mtop.tmall.share.initdialog", param = HttpHelper.ObjectToJson(new { data = HttpHelper.ObjectToJson(new { codes = new string[] { url }, wholeCode = HttpHelper.URLEncode(mess) }) }), v = "1.0" });
|
||
if (_app != null && _app.ok)
|
||
{
|
||
var result = _app.message as Dictionary<string, object>;
|
||
if (result != null && result.ContainsKey("dialog"))
|
||
{
|
||
result = result["dialog"] as Dictionary<string, object>;
|
||
if (result.ContainsKey("rightBtnActionUrl"))
|
||
{
|
||
item_id = FindTbItemIdByStr(result["rightBtnActionUrl"].ToString());
|
||
if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
}
|
||
//var regUrl = Regex.Match(url, @"(yukhj.com|m.tb.cn|m.yeeoq.top|m.uqlsi|m.rijni|m.gkqmj|m.mulwt|uland\\.taobao\\.com|sjtm.me|aa5d.com|d11a.com|f61[a-z].com|92db.com|yqt.so|e22a.com|jd.hk|f61b.com|detail.m.tmall.hk|k.83jie.com|ewqcxz.com|items.alitrip.com|tmqd.me|detail.tmall.hk|url.cn|taobao.com|mashort.cn|tmall.com|tb.cn|t.cn|detail.yao.95095.com|laiwang.com|jd.com|dwz.cn|juhuasuan.com|detail.m.tmall.com|zmnxbc.com|s.click.taobao|detail.liangxinyao.com)");
|
||
//var regUrl = Regex.Match(url, @"(yukhj.com|m.tb.cn|m.yeeoq.top|m.uqlsi|m.rijni|m.gkqmj|m.mulwt|uland\\.taobao\\.com|sjtm.me|aa5d.com|d11a.com|f61[a-z].com|92db.com|yqt.so|e22a.com|f61b.com|detail.m.tmall.hk|k.83jie.com|ewqcxz.com|items.alitrip.com|tmqd.me|detail.tmall.hk|url.cn|taobao.com|mashort.cn|tmall.com|tb.cn|t.cn|detail.yao.95095.com|laiwang.com|dwz.cn|juhuasuan.com|detail.m.tmall.com|zmnxbc.com|s.click.taobao|detail.liangxinyao.com|m.tb.cn)");
|
||
var regUrl = Regex.Match(url, @"(yukhj.com|m.tb.cn|m.yeeoq.top|m.uqlsi|m.rijni|m.gkqmj|m.mulwt|uland\\.taobao\\.com|sjtm.me|aa5d.com|d11a.com|f61[a-z].com|92db.com|yqt.so|e22a.com|f61b.com|detail.m.tmall.hk|k.83jie.com|ewqcxz.com|items.alitrip.com|tmqd.me|detail.tmall.hk|url.cn|taobao.com|mashort.cn|tmall.com|tb.cn|detail.yao.95095.com|laiwang.com|dwz.cn|juhuasuan.com|detail.m.tmall.com|zmnxbc.com|s.click.taobao|detail.liangxinyao.com|m.tb.cn)");
|
||
if (regUrl.Success)
|
||
{
|
||
var html = string.Empty;
|
||
for (int i = 0; i < 3; i++)
|
||
{
|
||
var http = new HttpHelper();
|
||
html = http.GetHtml(url).Html;
|
||
if (html.Contains("超时操作"))
|
||
Thread.Sleep(500);
|
||
else break;
|
||
}
|
||
var reg = Regex.Match(html, @"var url = '(?<url>.+?)';");
|
||
if (reg.Success)
|
||
item_id = CouponByItemId(reg.Groups["url"].Value, api);
|
||
if (string.IsNullOrWhiteSpace(item_id))
|
||
item_id = FindTbItemIdByStr(html);
|
||
if (!string.IsNullOrWhiteSpace(item_id)) return item_id;
|
||
|
||
if (url.Contains("uland.taobao"))
|
||
{
|
||
item_id = CouponByItemId(url, api);
|
||
if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
}
|
||
|
||
if (url.Contains("s.click.taobao"))//https://s.click.taobao.com/aFIAbOw 这种链接
|
||
{
|
||
#region 2022年9月5日15:28:32
|
||
//var locationA = TbTools.FindLocationByUrl(url);
|
||
//if (locationA != null)
|
||
//{
|
||
// if (!locationA.Contains("err.taobao.com"))
|
||
// {
|
||
// //item_id = CouponByItemId(mess, api);
|
||
// item_id = CouponByItemId(url, api);
|
||
// if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
// var locationB = TbTools.FindLocationByUrl(locationA);
|
||
// if (string.IsNullOrWhiteSpace(locationB)) { item_id = string.Empty; }
|
||
// else
|
||
// {
|
||
// var goodsUrl = HttpHelper.URLDecode(locationB.Replace("https://s.click.taobao.com/t_js?tu=", "").Replace("http://s.click.taobao.com/t_js?tu=", ""));
|
||
// var tb_url = TbTools.FindLocationByUrl(goodsUrl, locationB);//获取到的淘宝链接
|
||
// if (!string.IsNullOrEmpty(item_id)) return item_id;
|
||
// }
|
||
// }
|
||
//}
|
||
//else
|
||
//{
|
||
// var _html = GetHTML(url);
|
||
// reg = Regex.Match(_html, "real_jump_address = '(?<真正的跳转地址>[^']+)");
|
||
// if (reg.Success)
|
||
// {
|
||
// var address = reg.Groups["真正的跳转地址"].Value;
|
||
// //_html = GetHTML(HttpHelper.URLDecode(address).Replace("&", "&"), HttpHelper.URLDecode(url));
|
||
// for (int i = 0; i < 3; i++)
|
||
// {
|
||
// _html = GetHTML(address.Replace("&", "&"), url);
|
||
// item_id = FindTbItemIdByStr(_html);
|
||
// if (!string.IsNullOrWhiteSpace(item_id))
|
||
// return item_id;
|
||
// else
|
||
// {
|
||
// //访问频繁,休息重试3次
|
||
// Thread.Sleep(500);
|
||
// }
|
||
// }
|
||
// }
|
||
|
||
//}
|
||
#endregion
|
||
|
||
var result = api.AnalysisTkUrl(url, "2");
|
||
if (result != null)
|
||
{
|
||
return result.item_id;
|
||
}
|
||
|
||
}
|
||
}
|
||
mess = mess.Replace(url, "");//清除没用的连接,如果连接无法解析,name下面的淘口令解析将会减轻负担
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
//if (!reg.Success || string.IsNullOrEmpty(item_id))//非链接 淘口令
|
||
if (string.IsNullOrEmpty(item_id))//非链接 淘口令
|
||
{
|
||
if (Util.IsXML(mess))
|
||
return string.Empty;
|
||
var _mess = Regex.Replace(mess, "Http:/[A-Za-z]", "", RegexOptions.IgnoreCase);
|
||
var regs1 = Regex.Matches(_mess, "[^A-Za-z0-9]?([A-Za-z0-9]{11})[^A-Za-z0-9]?");
|
||
foreach (Match item in regs1)
|
||
{
|
||
try
|
||
{
|
||
if (item.Success)
|
||
{
|
||
try
|
||
{
|
||
if (api != null)
|
||
{
|
||
try
|
||
{
|
||
var adzoneInfo = ApiClient.GetSession().FindAdzoneInfos();
|
||
if (adzoneInfo != null)
|
||
{
|
||
var adzones = adzoneInfo.Where(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.adzone_pid_cps_name == api.Member.username && f.adzone_pid != "").ToList();
|
||
|
||
if (adzones != null)
|
||
{
|
||
foreach (var adzone in adzones)
|
||
{
|
||
try
|
||
{
|
||
var pid = adzone.adzone_pid;
|
||
var pids = pid.Split('_');
|
||
var result = api.AnalysisTkPassword(item.Groups[1].Value, pids[3], pids[2]);
|
||
if (result != null)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(result.num_iid) == false)
|
||
{
|
||
return result.num_iid;
|
||
}
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent("", ex.Message + " _ " + ex.StackTrace);
|
||
}
|
||
}
|
||
|
||
var h5 = new h5Top();
|
||
item_id = h5.GetItemIdByTKL(item.Groups[1].Value, api);
|
||
if (!string.IsNullOrWhiteSpace(h5.activityId))
|
||
activityId = h5.activityId;
|
||
if (!string.IsNullOrWhiteSpace(item_id))
|
||
return item_id;
|
||
else
|
||
{
|
||
}
|
||
//var _temp = api.SendServer("com.taobao.redbull.getpassworddetail", new { password = item.Groups[1].Value });
|
||
//if (_temp != null && _temp.ok)
|
||
//{
|
||
// if (_temp.message.ToString().Contains("口令不存在"))
|
||
// return string.Empty;
|
||
// var _obj = _temp.message as Dictionary<string, object>;
|
||
// var _url = string.Empty;
|
||
// if (_obj.ContainsKey("url"))
|
||
// _url = _obj["url"] as string;
|
||
// if (!string.IsNullOrEmpty(_url)) return CouponByItemId(_url, api);
|
||
//}
|
||
//else return "服务器繁忙,请稍后重试!";
|
||
}
|
||
catch (Exception ex)
|
||
{ throw ex; }
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent("", "C = " + ex.Message + ex.StackTrace);
|
||
// throw ex;
|
||
}
|
||
}
|
||
}
|
||
return item_id;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent(this, $"mess={mess}{ex.Message},{ex.StackTrace}");
|
||
return string.Empty;
|
||
}
|
||
}
|
||
|
||
#region 私域数据相关
|
||
/// <summary>
|
||
/// 私域查詢数据的類型
|
||
/// </summary>
|
||
public enum PublisherType
|
||
{
|
||
SpecialId = 0,
|
||
ExternalId = 1
|
||
}
|
||
|
||
/// <summary>
|
||
/// 私域用户备案信息查询
|
||
/// </summary>
|
||
/// <param name="api"></param>
|
||
/// <param name="_data"></param>
|
||
/// <returns>special_id</returns>
|
||
public string FindTbPublisherInfoFirst(AlimamaApi api, object _data, PublisherType publisherType = PublisherType.SpecialId)
|
||
{
|
||
try
|
||
{
|
||
var result = api.SendTaobao("taobao.tbk.sc.publisher.info.get", _data);
|
||
if (result != null && result.ContainsKey("data"))
|
||
{
|
||
var data = result["data"] as Dictionary<string, object>;
|
||
if (data != null && data.ContainsKey("inviter_list"))
|
||
{
|
||
var inviter_list = data["inviter_list"] as Dictionary<string, object>;
|
||
if (inviter_list != null && inviter_list.ContainsKey("map_data"))
|
||
{
|
||
var map_data = inviter_list["map_data"] as ArrayList;
|
||
if (map_data != null && map_data.Count != 0)
|
||
{
|
||
if (map_data.Count == 1)
|
||
{
|
||
foreach (Dictionary<string, object> item in map_data)
|
||
{
|
||
if (publisherType == PublisherType.SpecialId && item.ContainsKey("special_id"))
|
||
return item["special_id"].ToString();
|
||
if (publisherType == PublisherType.ExternalId && item.ContainsKey("external_id"))
|
||
return item["external_id"].ToString();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent(this, $"查询私域绑定关系异常:{ex.Message},{ex.StackTrace}");
|
||
}
|
||
return string.Empty;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 私域用户备案信息查询,默认查询所有
|
||
/// </summary>
|
||
/// <param name="api">AlimamaApi对象</param>
|
||
/// <param name="_data">参数</param>
|
||
public ArrayList FindTbPublisherInfoAll(AlimamaApi api, object _data = null)
|
||
{
|
||
try
|
||
{
|
||
if (_data == null)
|
||
_data = new { info_type = 2, relation_app = "common", page_size = 1000, external_type = 1 };
|
||
|
||
var result = api.SendTaobao("taobao.tbk.sc.publisher.info.get", _data);
|
||
if (result != null && result.ContainsKey("data"))
|
||
{
|
||
var data = result["data"] as Dictionary<string, object>;
|
||
if (data != null && data.ContainsKey("inviter_list"))
|
||
{
|
||
var inviter_list = data["inviter_list"] as Dictionary<string, object>;
|
||
if (inviter_list != null && inviter_list.ContainsKey("map_data"))
|
||
{
|
||
var map_data = inviter_list["map_data"] as ArrayList;
|
||
if (map_data != null && map_data.Count != 0)
|
||
return map_data;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent(this, $"查询私域绑定关系异常:{ex.Message},{ex.StackTrace}");
|
||
}
|
||
return new ArrayList();
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 是否运行淘宝私域功能,用于同步私域数据
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool IsRunTBSiYu()
|
||
{
|
||
try
|
||
{
|
||
for (int i = 0; i < 2; i++)
|
||
{
|
||
var config = Util.ReadConfig("插件-淘宝返利-配置", true);//刷新积分管理中的配置文件
|
||
if (config == null) continue;
|
||
if (config.ContainsKey("QueryComparisonSwitch"))
|
||
{
|
||
var qComSwitch = config["QueryComparisonSwitch"].ToString();
|
||
|
||
return qComSwitch == "0";
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
return false;
|
||
}
|
||
|
||
|
||
public static string BizSceneId(string itemId)
|
||
{
|
||
var bizSceneId = "2";
|
||
if (itemId.Contains("-"))
|
||
{
|
||
bizSceneId = "1";
|
||
}
|
||
|
||
return bizSceneId;
|
||
}
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="api"></param>
|
||
/// <param name="item_id"></param>
|
||
/// <param name="pid_split"></param>
|
||
/// <param name="external_id"></param>
|
||
/// <param name="external_id"></param>
|
||
/// <returns></returns>
|
||
public Dictionary<string, object> GetPrivilege(AlimamaApi api, string item_id, string[] pid_split, string biz_scene_id, string external_id = "", string special_id = "")
|
||
{
|
||
biz_scene_id = BizSceneId(item_id);
|
||
|
||
object data = null;
|
||
if (!string.IsNullOrWhiteSpace(external_id) && !string.IsNullOrWhiteSpace(special_id))
|
||
data = new { item_id = item_id, site_id = pid_split[2], adzone_id = pid_split[3], special_id = special_id, external_id = external_id, biz_scene_id = biz_scene_id };
|
||
else if (!string.IsNullOrWhiteSpace(external_id))
|
||
data = new { item_id = item_id, site_id = pid_split[2], adzone_id = pid_split[3], external_id = external_id, biz_scene_id = biz_scene_id };
|
||
else
|
||
data = new { item_id = item_id, site_id = pid_split[2], adzone_id = pid_split[3], biz_scene_id = biz_scene_id };
|
||
|
||
//data = new { item_id = item_id, site_id = pid_split[2], adzone_id = pid_split[3], special_id = special_id };
|
||
|
||
var privilege = api.SendTaobao("taobao.tbk.privilege.get", data);
|
||
if (privilege == null)
|
||
{
|
||
for (int i = 0; i <= 1; i++)
|
||
{
|
||
privilege = api.SendTaobao("taobao.tbk.privilege.get", data);
|
||
if (privilege != null) break;
|
||
Thread.Sleep(200);
|
||
}
|
||
}
|
||
if (privilege == null) throw new Exception("@高佣金转链失败");
|
||
return privilege;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将淘宝url生成淘口令
|
||
/// </summary>
|
||
/// <param name="url">宝贝地址[优惠券地址|宝贝地址]</param>
|
||
/// <param name="title">标题</param>
|
||
/// <param name="logo">图片url[宝贝图片,非本地图片]</param>
|
||
/// <param name="api">AlimamaApi对象</param>
|
||
/// <returns>返回淘口令或空</returns>
|
||
public string FindTKL(string _url, string title, string logo, AlimamaApi api)
|
||
{
|
||
try
|
||
{
|
||
var url = _url;
|
||
//注:转淘口令的时候 title 参数存在一些管检测,需要处理。一下转不出来的,将以“淘宝精选商品”为标题
|
||
title = title.Replace("", "").Replace("总复习", "");
|
||
//title = "123456789";
|
||
Dictionary<string, object> tkl_json = null;
|
||
for (int i = 0; i <= 4; i++)
|
||
{
|
||
if (title.Length < 5)
|
||
title = "淘宝商品:" + title;
|
||
try
|
||
{
|
||
tkl_json = api.SendTaobao("taobao.tbk.tpwd.create", new { url = url, text = title, logo = logo });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.Message.Contains("sub_msg\":\"title have illegal characters"))
|
||
{
|
||
title = "内部优惠通道";
|
||
if (i > 2)
|
||
{
|
||
var tbAnalysis = new TBHelper.TbAnalysis();
|
||
url = tbAnalysis.FindShortUrlBySrcUrl(url, api);
|
||
}
|
||
continue;
|
||
}
|
||
}
|
||
if (tkl_json == null)
|
||
{
|
||
Thread.Sleep(200);
|
||
}
|
||
else
|
||
{
|
||
if ((tkl_json["data"] as Dictionary<string, object>).Count != 0)
|
||
break;
|
||
}
|
||
title = "内部优惠通道";
|
||
}
|
||
if (tkl_json == null) throw new Exception();
|
||
var data = tkl_json["data"] as Dictionary<string, object>;
|
||
if (data.Count == 0) throw new Exception("生成淘口令失败");
|
||
var isoTkl = data["model"].ToString();
|
||
|
||
//Console.WriteLine(isoTkl);
|
||
|
||
var tkl = data["password_simple"].ToString();
|
||
//Console.WriteLine(tkl);
|
||
return tkl;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent(this, $"生成淘口令异常:{_url},{ex.Message},{ex.StackTrace}");
|
||
return string.Empty;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将淘宝url生成淘口令
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="api"></param>
|
||
/// <returns></returns>
|
||
public TklInfo FindTKL(string url, AlimamaApi api)
|
||
{
|
||
try
|
||
{
|
||
Dictionary<string, object> tkl_json = null;
|
||
for (int i = 0; i <= 4; i++)
|
||
{
|
||
try
|
||
{
|
||
tkl_json = api.SendTaobao("taobao.tbk.tpwd.create", new { url = url });
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.Message.Contains("sub_msg\":\"title have illegal characters"))
|
||
{
|
||
if (i > 2)
|
||
{
|
||
var tbAnalysis = new TBHelper.TbAnalysis();
|
||
url = tbAnalysis.FindShortUrlBySrcUrl(url, api);
|
||
}
|
||
continue;
|
||
}
|
||
}
|
||
if (tkl_json == null)
|
||
{
|
||
Thread.Sleep(200);
|
||
}
|
||
else
|
||
{
|
||
if ((tkl_json["data"] as Dictionary<string, object>).Count != 0)
|
||
break;
|
||
}
|
||
}
|
||
if (tkl_json == null) throw new Exception();
|
||
var data = tkl_json["data"] as Dictionary<string, object>;
|
||
if (data.Count == 0) throw new Exception("生成淘口令失败");
|
||
var model = data["model"].ToString();
|
||
|
||
var isoTkl = string.Empty;
|
||
var urlTmp = HttpExtend.RegexMatchUrl(model);
|
||
if (!string.IsNullOrWhiteSpace(urlTmp))
|
||
isoTkl = urlTmp.Replace("m.tb", "s.tb");
|
||
|
||
//Console.WriteLine(isoTkl);
|
||
var tkl = data["password_simple"].ToString();
|
||
|
||
//Console.WriteLine(tkl);
|
||
return new TklInfo() { tkl = tkl, isoUrl = isoTkl };
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent(this, $"生成淘口令异常:{url},{ex.Message},{ex.StackTrace}");
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 淘口令信息
|
||
/// </summary>
|
||
public class TklInfo
|
||
{
|
||
public string isoUrl { get; set; }
|
||
public string tkl { get; set; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成淘宝短连接
|
||
/// </summary>
|
||
/// <param name="url">url链接</param>
|
||
/// <param name="api">AlimamaApi对象</param>
|
||
/// <returns>返回缩短后的链接或原始链接</returns>
|
||
public string FindShortUrlBySrcUrl(string url, AlimamaApi api)
|
||
{
|
||
try
|
||
{
|
||
Dictionary<string, object> result = null;
|
||
for (int i = 0; i <= 2; i++)
|
||
{
|
||
result = api.SendTaobao("taobao.tbk.spread.get", new { requests = "[{\"url\":\"" + url + "\"}]" });
|
||
if (result == null)
|
||
Thread.Sleep(200);
|
||
else
|
||
{
|
||
if (result.ContainsKey("tbk_spread"))
|
||
{
|
||
var content = ((result["tbk_spread"] as ArrayList)[0] as Dictionary<string, object>)["content"].ToString();
|
||
if (!string.IsNullOrEmpty(content))
|
||
return content;
|
||
}
|
||
}
|
||
}
|
||
return url;
|
||
//var tbk_spread = result["tbk_spread"] as ArrayList;
|
||
//var content = (tbk_spread[0] as Dictionary<string, object>)["content"].ToString();
|
||
//return content;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
EventClient.OnEvent(this, $"生成短连接失败:{url},{ex.Message},{ex.StackTrace}");
|
||
return url;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 替换淘口令前后符号(系统配置)
|
||
/// </summary>
|
||
/// <param name="tkl">需要替换的淘口令</param>
|
||
/// <returns></returns>
|
||
public string ReplaceTklModifier(string tkl)
|
||
{
|
||
try
|
||
{
|
||
//淘口令的前/后符号有没有设置.有设置将替换掉
|
||
if (!string.IsNullOrWhiteSpace(ApiClient.Setting.SystemConfig.TklStr_First))
|
||
{
|
||
var modifier = Util.FindTKlRandomModifier(ApiClient.Setting.SystemConfig.TklStr_First);
|
||
tkl = modifier + tkl.Substring(1, tkl.Length - 1);
|
||
}
|
||
if (!string.IsNullOrWhiteSpace(ApiClient.Setting.SystemConfig.TKLStr_End))
|
||
{
|
||
var modifier = Util.FindTKlRandomModifier(ApiClient.Setting.SystemConfig.TKLStr_End);
|
||
tkl = tkl.Substring(0, tkl.Length - 1) + modifier;
|
||
}
|
||
|
||
if (tkl.Length == 13 && (!Regex.IsMatch(tkl, @"(\d.[A-Za-z0-9]{11}.:\/)") || !Regex.IsMatch(tkl, @"(\d\s[A-Za-z0-9]+\s.[A-Za-z0-9]{11}.\/)")))
|
||
tkl = "8 AA1111 " + tkl + "/";
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
return tkl;
|
||
}
|
||
|
||
#region 通过优惠券链接获取淘宝宝贝的id
|
||
/// <summary>
|
||
/// 通过优惠券链接获取淘宝宝贝的id
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
public string CouponByItemId(string url, AlimamaApi api)
|
||
{
|
||
try
|
||
{
|
||
var itemId = 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(@"uland.taobao.com/ccoupon"))
|
||
{
|
||
var x_reg = Regex.Match(url, "e=([^&]+)");
|
||
if (x_reg.Success)
|
||
{
|
||
h5Top h5 = new h5Top();
|
||
//https://h5api.m.taobao.com/h5/mtop.taobao.baichuan.smb.get/1.0/?jsv=2.4.0&appKey=12574478&t=1580101872147&sign=0d0cd5819f67b6568a77de5867f151f9&api=mtop.taobao.baichuan.smb.get&v=1.0&type=originaljson&dataType=jsonp&timeout=10000
|
||
var rst = h5.M_GET("mtop.alimama.union.xt.biz.zhibo.api.entry", new
|
||
{
|
||
floorId = 13193,
|
||
variableMap = "{\"e\":\"" + HttpHelper.URLDecode(x_reg.Groups[1].Value) + "\",\"type\":\"nBuy\",\"buyMoreSwitch\":\"0\",\"union_lens\":\"lensId:" + HttpHelper.URLDecode(Regex.Match(url, "=lensId:([^&]+)").Groups[1].Value) + "\",\"recoveryId\":\"" + Api.Framework.Tools.Util.GetRandomString(16) + "_" + HttpExtend.GetTimeStamp() + "\"}"
|
||
});
|
||
|
||
if (rst != null && rst.ContainsKey("resultList"))
|
||
{
|
||
var resultList = rst["resultList"] as ArrayList;
|
||
if (resultList != null && resultList.Count != 0)
|
||
{
|
||
var result = resultList[0] as Dictionary<string, object>;
|
||
if (result != null && result.ContainsKey("itemId"))
|
||
return result["itemId"].ToString();
|
||
}
|
||
}
|
||
//Console.WriteLine(HttpHelper.ObjectToJson(rst));
|
||
}
|
||
}
|
||
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 (!string.IsNullOrWhiteSpace(location) && 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="))
|
||
|
||
|
||
|
||
if (location.Contains("uland.taobao.com/coupon/edetail?"))
|
||
{
|
||
|
||
var reg = Regex.Match(location, @"(?:&|\?)itemId=(\d+)", RegexOptions.IgnoreCase);
|
||
if (reg.Success)
|
||
return reg.Groups[1].Value;
|
||
|
||
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 coupon = api.CheckCoupon(uland.Groups[1].Value);
|
||
if (coupon != null)
|
||
return coupon.itemId;
|
||
}
|
||
else
|
||
{
|
||
//Console.WriteLine(location);
|
||
var x_reg = Regex.Match(location, "e=([^&]+)");
|
||
if (x_reg.Success)
|
||
{
|
||
h5Top h5 = new h5Top();
|
||
//https://h5api.m.taobao.com/h5/mtop.taobao.baichuan.smb.get/1.0/?jsv=2.4.0&appKey=12574478&t=1580101872147&sign=0d0cd5819f67b6568a77de5867f151f9&api=mtop.taobao.baichuan.smb.get&v=1.0&type=originaljson&dataType=jsonp&timeout=10000
|
||
var rst = h5.M_GET("mtop.alimama.union.xt.biz.quan.api.entry", new { floorId = 13193, variableMap = "{\"e\":\"" + HttpHelper.URLDecode(x_reg.Groups[1].Value) + "\",\"src\":\"" + HttpHelper.URLDecode(Regex.Match(location, "&src=([^&]+)").Groups[1].Value) + "\",\"af\":\"4\",\"type\":\"nBuy\",\"buyMoreSwitch\":\"0\",\"union_lens\":\"lensId:" + HttpHelper.URLDecode(Regex.Match(location, "=lensId:([^&]+)").Groups[1].Value) + "\",\"recoveryId\":\"" + Api.Framework.Tools.Util.GetRandomString(16) + "_" + HttpExtend.GetTimeStamp() + "\"}" });
|
||
//Console.WriteLine(HttpHelper.ObjectToJson(rst));
|
||
|
||
if (rst != null && rst.ContainsKey("resultList"))
|
||
{
|
||
var resultList = rst["resultList"] as ArrayList;
|
||
for (int i = 0; i < resultList.Count; i++)
|
||
{
|
||
var dic = resultList[i] as Dictionary<string, object>;
|
||
if (dic.ContainsKey("itemId"))
|
||
{
|
||
return dic["itemId"].ToString();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//"系统日志,分析地址失败:" + 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 FindTbItemIdByStr(GetHTML(location));
|
||
}
|
||
else if (!string.IsNullOrEmpty(location) && !first)
|
||
{
|
||
url = location;
|
||
first = true;
|
||
goto Next;
|
||
}
|
||
|
||
#region
|
||
if (string.IsNullOrWhiteSpace(location))
|
||
{
|
||
var _html = GetHTML(url);
|
||
var reg = Regex.Match(_html, "real_jump_address = '(?<真正的跳转地址>[^']+)");
|
||
if (reg.Success)
|
||
{
|
||
var address = reg.Groups["真正的跳转地址"].Value;
|
||
//_html = GetHTML(HttpHelper.URLDecode(address).Replace("&", "&"), HttpHelper.URLDecode(url));
|
||
for (int i = 0; i < 3; i++)
|
||
{
|
||
_html = GetHTML(address.Replace("&", "&"), url);
|
||
itemId = FindTbItemIdByStr(_html);
|
||
if (!string.IsNullOrWhiteSpace(itemId))
|
||
return itemId;
|
||
else
|
||
{
|
||
//访问频繁,休息重试3次
|
||
Thread.Sleep(500);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
else if (url.Contains("m.tb.cn"))
|
||
{
|
||
string tempHtml = GetHTML(url);
|
||
tempHtml = HttpHelper.URLDecode(tempHtml);
|
||
var reg = Regex.Match(tempHtml, "url = '(?<链接>.*?)';");
|
||
if (reg.Success)
|
||
{
|
||
var _url = reg.Groups["链接"].Value;
|
||
_url = HttpHelper.URLDecode(_url);
|
||
itemId = FindTbItemIdByStr(_url);
|
||
if (!string.IsNullOrWhiteSpace(itemId)) return itemId;
|
||
}
|
||
}
|
||
else if (url.Contains("h5.m.taobao.com"))
|
||
{
|
||
var _url = HttpHelper.URLDecode(HttpHelper.URLDecode(url));
|
||
itemId = FindTbItemIdByStr(_url);
|
||
if (!string.IsNullOrWhiteSpace(itemId)) return itemId;
|
||
}
|
||
else if (url.Contains("mo.m.tmall.com"))
|
||
{
|
||
string tempHtml = GetHTML(url);
|
||
itemId = FindTbItemIdByStr(tempHtml);
|
||
if (!string.IsNullOrWhiteSpace(itemId)) return itemId;
|
||
}
|
||
else if (url.Contains("uland.taobao"))
|
||
{
|
||
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
//Console.WriteLine(ex.Message);
|
||
}
|
||
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 ex)
|
||
{
|
||
//EventClient.OnEvent(this, $"Location:{url},{ex.Message},{ex.StackTrace}");
|
||
}
|
||
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);
|
||
//var location = result.Header.Get("Location");
|
||
return result.Html;
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
return string.Empty;
|
||
}
|
||
#endregion
|
||
|
||
}
|
||
|
||
public class TbTools
|
||
{
|
||
#region H5
|
||
|
||
/// <summary>
|
||
/// 获取宝贝的基础信息
|
||
/// </summary>
|
||
/// <param name="api">[比如: mtop.taobao.detail.getdetail]</param>
|
||
/// <param name="data">链接附带的参数 例如: new { itemNumId = itemId, exParams = new { id = itemId }, ttid = "2016@taobao_h5_2.0.0", isSec = "0", ecode = "0", AntiFlood = "true", AntiCreep = "true", H5Request = "true", type = "jsonp", dataType = "jsonp" }</param>
|
||
/// <param name="html">将获取到的内容以out的形式返回给调用</param>
|
||
/// <param name="version">版本号默认[6.0]</param>
|
||
/// <returns>Dictionary<string, object></returns>
|
||
public static Dictionary<string, object> GetH5Result(string api, object data, out string html, string version = "6.0")
|
||
{
|
||
//bool next = false;
|
||
int maxnumber = 5;
|
||
int number = 0;
|
||
Next:
|
||
number++;
|
||
string time = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000).ToString();
|
||
string mh5Tk = HttpExtend.GetCookiesValue("_m_h5_tk", Cookies);
|
||
mh5Tk = string.IsNullOrEmpty(mh5Tk) ? "f64aa152f943661f750e6212ebc98804_1481170439770" : mh5Tk;
|
||
string json = (data.GetType() == typeof(string)) ? data.ToString() : HttpHelper.ObjectToJson(data).Replace("\\u0026", "&").Replace("_params", "param");
|
||
string temp_ = mh5Tk + "&" + time + "&" + appKey + "&" + json;
|
||
string sign = HttpExtend.GetMD5String(temp_);
|
||
string send = HttpExtend.UrlEncode(json);
|
||
string temp = string.Format("http://h5api.m.taobao.com/h5/{5}/{4}/?jsv=2.4.11&v={4}&api={5}&appKey={0}&t={1}&callback=mtopjsonp1&type=jsonp&sign={2}&data={3}", appKey, time, sign, send, version, api);
|
||
html = M_GET_HTML(temp).Trim();
|
||
if (html.Contains("令牌过期") || html.Contains("令牌为空") || html.Contains("失效"))
|
||
{
|
||
if (number <= 2) goto Next;
|
||
}
|
||
if (html.Contains("挤爆") && number <= maxnumber)
|
||
{
|
||
|
||
Dictionary<string, object> _json = HttpExtend.JsonToDictionary(html);
|
||
|
||
string _url = (_json["data"] as Dictionary<string, object>)["url"].ToString();
|
||
//next = false;
|
||
goto Next;
|
||
}
|
||
|
||
var reg = Regex.Match(html, @"^mtopjsonp1\((.*?)\)$", RegexOptions.IgnoreCase);
|
||
if (reg.Success)
|
||
{
|
||
var dic = HttpExtend.JsonToDictionary(reg.Groups[1].Value);
|
||
if (dic.ContainsKey("data") && html.Contains("SUCCESS::调用成功"))
|
||
return dic["data"] as Dictionary<string, object>;
|
||
throw new Exception(reg.Groups[1].Value);
|
||
}
|
||
throw new Exception(api + " = " + html);
|
||
}
|
||
|
||
private const string _key = "<RSAKeyValue><Modulus>wmUZgme6iOPOaMTZtPu5DKQ8Dph9fSgy6abFyQv25MD8xOMPBBo0wsLUOVt8r9sk+li11mIg1fs1HPJ75UmIle+w9xhIx/nsXWejuQhXlQV0VyJtzI+id8G+FcyYaog91MfPndJoO3m30fSjE/0KDLDmv1EeQcP24RAzeA29p7c=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
|
||
private const int _appid = 10004;
|
||
//private static string _host = "https://cps.api.52cmg.cn";
|
||
|
||
private static string _cookies = string.Empty;
|
||
|
||
private static string Cookies
|
||
{
|
||
get
|
||
{
|
||
if (string.IsNullOrEmpty(_cookies))
|
||
{
|
||
HttpHelper http = new HttpHelper();
|
||
string cookies = http.GetHtml(new HttpItem() { URL = "http://api.m.taobao.com/h5/mtop.alimama.union.hsf.coupon.get/1.0/?v=1.0&api=mtop.alimama.union.hsf.coupon.get&appKey=12574478&t=1521082116428&callback=mtopjsonp1&type=jsonp&sign=e102e09c3da2505790412f4b5904cfec&data=%7B%22e%22%3A%22kKwRS8jKFeIGQASttHIRqX4ccS%252BmEB0fPkvI%252B9vDGidk3qypIGt1zv02HjWWzK8QrVVJXxMdEXoJP%252FpVy45HN79fwBwwUiqlbZt%252F%252F9VQxkYKu2xm%252BXzUVW7PVn13QcLNcISolD8QJ4rvZt%252BicRrQwg%253D%253D%22%2C%22pid%22%3A%22mm_96747532_17602904_63786428%22%7D", Timeout = 5000, ReadWriteTimeout = 5000 }).Cookie;
|
||
_cookies = HttpHelper.GetSmallCookie(cookies);
|
||
}
|
||
return _cookies;
|
||
}
|
||
set { _cookies = value; }
|
||
}
|
||
|
||
private const string appKey = "12574478";
|
||
const string keyword = "亲,小二正忙,滑动一下马上回来|挤爆|rgv587_flag";
|
||
private static string M_GET_HTML(string tempUrl)
|
||
{
|
||
try
|
||
{
|
||
HttpItem item = new HttpItem()
|
||
{
|
||
URL = tempUrl,//URL 必需项
|
||
Method = "get",//URL 可选项 默认为Get
|
||
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
|
||
Cookie = Cookies + ";random=" + new Random().Next(),//字符串Cookie 可选项
|
||
Referer = "http://m.taobao.com",//来源URL 可选项
|
||
Timeout = 5000,//连接超时时间 可选项默认为100000
|
||
ReadWriteTimeout = 5000,//写入Post数据超时时间 可选项默认为30000
|
||
UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统 可选项有默认值
|
||
ContentType = "text/html",//返回类型 可选项有默认值
|
||
Allowautoredirect = false,//是否根据301跳转 可选项
|
||
};
|
||
HttpHelper http = new HttpHelper();
|
||
int i = 0;
|
||
Next:
|
||
HttpResult result = http.GetHtml(item);
|
||
string html = result.Html;
|
||
if (Regex.IsMatch(html, keyword))
|
||
{
|
||
var r = item.SetProxyIP(http, new AuthEndpoint()
|
||
{
|
||
Host = ApiClient.Setting.SystemConfig.cps_server_api + "api/webtool.asmx/send_data",
|
||
Appid = _appid,
|
||
Enckey = _key,
|
||
Method = "find_proxy"
|
||
});
|
||
i++;
|
||
if (r && i < 3) goto Next;
|
||
}
|
||
if (!string.IsNullOrEmpty(result.Cookie))
|
||
{
|
||
Cookies = HttpExtend.UpdateCookies(Cookies, result.Cookie);
|
||
}
|
||
return html;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return string.Empty;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 链接跳转获取Location
|
||
/// </summary>
|
||
/// <param name="url">要获取Location的链接</param>
|
||
/// <param name="referer">来源</param>
|
||
/// <param name="allowautoredirect">是否跳转</param>
|
||
/// <returns></returns>
|
||
public static string FindLocationByUrl(string url, string referer = "", bool allowautoredirect = false)
|
||
{
|
||
try
|
||
{
|
||
HttpHelper http = new HttpHelper();
|
||
HttpItem item = new HttpItem()
|
||
{
|
||
URL = url,
|
||
IsToLower = false,
|
||
Timeout = 5000,
|
||
ReadWriteTimeout = 30000,
|
||
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",
|
||
Referer = referer,
|
||
Allowautoredirect = allowautoredirect
|
||
};
|
||
HttpResult result = http.GetHtml(item);
|
||
return result.Header.Get("Location");
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
return string.Empty;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发送API数据
|
||
/// </summary>
|
||
/// <param name="method">API接口</param>
|
||
/// <param name="param">参数</param>
|
||
/// <returns></returns>
|
||
public static WebResult SendData(string method, object param = null, bool flag = true)
|
||
{
|
||
int num = 0;
|
||
Next:
|
||
try
|
||
{
|
||
AuthEndpoint end = new AuthEndpoint()
|
||
{
|
||
Appid = _appid,
|
||
Enckey = _key,
|
||
Host = ApiClient.Setting.SystemConfig.cps_server_api + (flag ? "api/alimama.asmx/send_data" : "api/webtool.asmx/send_data"),
|
||
Method = method
|
||
};
|
||
if (param != null)
|
||
{
|
||
if (param.GetType() == typeof(Dictionary<string, object>))
|
||
{
|
||
var _param = param as Dictionary<string, object>;
|
||
foreach (var item in _param)
|
||
{
|
||
end.Param[item.Key] = item.Value;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
var _param = param.GetType().GetProperties();
|
||
foreach (var item in _param)
|
||
{
|
||
end.Param[item.Name] = item.GetValue(param);
|
||
}
|
||
}
|
||
|
||
}
|
||
HttpHelper http = new HttpHelper(); return http.SendData(end, true);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
if (ex.Message == "未将对象引用设置到对象的实例。" && num <= 4)
|
||
{
|
||
num++;
|
||
goto Next;
|
||
}
|
||
return new CsharpHttpHelper.WebResult() { ok = false, message = ex.Message };
|
||
}
|
||
}
|
||
|
||
public static string GetLocation(string url)
|
||
{
|
||
try
|
||
{
|
||
HttpHelper http = new HttpHelper();
|
||
HttpItem item = new HttpItem()
|
||
{
|
||
URL = url
|
||
};
|
||
HttpResult result = http.GetHtml(item);
|
||
var _url = result.Header.Get("Location");
|
||
if (string.IsNullOrEmpty(_url))
|
||
{
|
||
return url;
|
||
}
|
||
else
|
||
return _url;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
|
||
}
|
||
return string.Empty;
|
||
}
|
||
|
||
public static string GetLocation(string url, string referer = "", bool allowautoredirect = false)
|
||
{
|
||
try
|
||
{
|
||
HttpHelper http = new HttpHelper();
|
||
HttpItem item = new HttpItem()
|
||
{
|
||
URL = url,
|
||
IsToLower = false,
|
||
Timeout = 5000,
|
||
ReadWriteTimeout = 30000,
|
||
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",
|
||
Referer = referer,
|
||
Allowautoredirect = allowautoredirect
|
||
};
|
||
HttpResult result = http.GetHtml(item);
|
||
return result.Header.Get("Location");
|
||
}
|
||
catch (Exception)
|
||
{
|
||
}
|
||
return string.Empty;
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
} |