using Api.Framework;
using Api.Framework.Enums;
using Api.Framework.Model;
using Api.Framework.Tools;
using CsharpHttpHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Weixin.TBCirclePromotion.Entitys;
using static Weixin.TBCirclePromotion.Enums;
namespace Weixin.TBCirclePromotion
{
///
/// 解析商品
///
public class AnalyzeGoods
{
///
/// 通过商品的id查询返回获取到自己返利商品信息
///
///
///
public fl_plugin_tbcirclepromotion_goodsinfos FindGoodsInfoToGoodsId(string goods_id, string pid_cps_name = "", string pid = "", string quan = "", string biz_scene_id = "2")
{
var session = ApiClient.GetSession();
try
{
if (!string.IsNullOrEmpty(goods_id))//符合搜索宝贝id的条件
{
var member = (string.IsNullOrEmpty(pid_cps_name) ? CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.阿里妈妈) : CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.阿里妈妈 && f.username == pid_cps_name));
if (member == null) { return null; }
if (string.IsNullOrWhiteSpace(pid))
{
var adzone = session.FindAdzoneInfos().FirstOrDefault(f => f.alliance_id == (int)CpsType.阿里妈妈 && f.adzone_pid_cps_name == member.username);
pid = adzone.adzone_pid;
}
var api = CpsClient.CreateAlimamaRequest(member);
var itemInfo = api.GetItemInfo(goods_id, "2");
if (itemInfo != null && itemInfo.n_tbk_item != null && itemInfo.n_tbk_item.Count > 0)
{
var tbk_item = itemInfo.n_tbk_item[0];
//店铺名称
var nick = tbk_item.nick;
//商品主图
var pict_url = tbk_item.pict_url;
//商品id
goods_id = tbk_item.num_iid;
//商品标题
var title = tbk_item.title;
//商品折扣价格(当前售价)
var zk_final_price = double.Parse(tbk_item.zk_final_price);
//30天销量
var volume = tbk_item.volume;
var goods_desc = string.Empty;
var coupon_remain_quantity = string.Empty;
biz_scene_id = TBHelper.TbAnalysis.BizSceneId(goods_id);
var pids = pid.Split('_');
var privilege = api.SendTaobao("taobao.tbk.privilege.get", new { item_id = goods_id, site_id = pids[2], adzone_id = pids[3], biz_scene_id = biz_scene_id });
var privilege_data = privilege["data"] as Dictionary;
//最高佣金率
var max_commission_rate = double.Parse(privilege_data["max_commission_rate"].ToString());
//优惠券地址
var coupon_click_url = string.Empty;
var coupon_price = 0.00d;
if (privilege_data.ContainsKey("coupon_info"))
{
var quan_reg = Regex.Match(privilege_data["coupon_info"].ToString(), @"满\d+元减(\d+)元");
if (quan_reg.Success) coupon_price = double.Parse(quan_reg.Groups[1].Value);
coupon_click_url = privilege_data["coupon_click_url"].ToString();
}
else
{
coupon_price = 0.00d;
if (!string.IsNullOrWhiteSpace(quan))
coupon_click_url = privilege_data["coupon_click_url"].ToString() + "&activityId=" + quan;
}
//检查优惠券有效性
var coupon = api.CheckCoupon(coupon_click_url);
//查到宝贝返利存在异常,直接提示没有返利
if (coupon != null)
{
//优惠券过期
if (coupon != null && coupon.retStatus != 0)
{
//coupon_click_url = coupon.clickUrl;
//coupon_price = 0.00d;
if (!string.IsNullOrWhiteSpace(quan))
coupon_click_url += "&activityId=" + quan;
else
coupon_click_url = privilege_data["item_url"].ToString();
coupon_price = 0.00d;
}
}
return new fl_plugin_tbcirclepromotion_goodsinfos() { cur_time = DateTime.Today, goods_id = goods_id, goods_name = title, mall_name = nick, goods_thumbnail_url = pict_url, sold_quantity = volume, normal_price = zk_final_price, coupon_discount = coupon_price, promotion_rate = max_commission_rate, shopping_url = coupon_click_url, goods_desc = goods_desc, coupon_remain_quantity = coupon_remain_quantity };
}
}
return null;
}
catch (Exception)
{
return null;
}
}
StringBuilder strb = new StringBuilder();
#region 采集数据 xxx
/////
///// 数据采集
/////
//public void GatherFactory()
//{
// strb.Length = 0;
// var session = ApiClient.GetSession();
// try
// {
// strb.Append(@"http://www.dataoke.com/qlist/?px=zx&");
// switch (Class1.Config.Stage)
// {
// case StageType.聚划算:
// strb.Append(@"h=tqg_jhs_ju&");
// break;
// case StageType.淘抢购:
// strb.Append(@"h=tqg_jhs&");
// break;
// case StageType.天猫:
// strb.Append(@"t=tmall&");
// break;
// case StageType.金牌卖家:
// strb.Append(@"t=tm_jpmj&");
// break;
// case StageType.海淘:
// strb.Append(@"haitao=1&");
// break;
// }
// if (!string.IsNullOrWhiteSpace(Class1.Config.Category))
// {
// string[] categorys = Class1.Config.Category.Split(',');
// for (int i = 0; i < categorys.Length; i++)
// {
// categorys[i] = ((int)((CategoryType)Enum.Parse(typeof(CategoryType), categorys[i]))).ToString();
// }
// strb.Append(string.IsNullOrWhiteSpace(Class1.Config.Category) ? string.Empty : $"cid={string.Join(",", categorys)}&");
// }
// strb.Append(Class1.Config.Price1 == null ? string.Empty : $"price1={Class1.Config.Price1}&");
// strb.Append(Class1.Config.Price2 == null ? string.Empty : $"price2={Class1.Config.Price2}&");
// strb.Append(Class1.Config.CouponMoney == null ? string.Empty : $"coupon_money={Class1.Config.CouponMoney}&");
// strb.Append(Class1.Config.Sales == null ? string.Empty : $"sales={Class1.Config.Sales}&");
// strb.Append(Class1.Config.Rate == null ? string.Empty : $"tk_rate={Class1.Config.Rate}&");
// var helper = new HttpHelper();
// var result = helper.GetHtml(helper.GetItem(strb.ToString()));//获取网页对象
// var doc = new HtmlDocument();//加载源代码,获取文档对象
// doc.LoadHtml(result.Html);//加载网页信息
// var res = doc.DocumentNode.SelectSingleNode("/html/body/div[4]/div[3]/div[1]");
// if (res == null)
// res = doc.DocumentNode.SelectSingleNode("/html/body/div[3]/div[3]/div[1]");
// if (res != null)
// {
// var goodsDivs = res.SelectNodes("div[@class='goods-item ']");
// session.BeginTransaction();
// List goodsinfos = new List();
// foreach (var item in goodsDivs)
// {
// //{gid:'17098881',cid:'2',price:'14.9',coupon:'25',title:'标题链接'}
// try
// {
// var reg = Regex.Match(item.InnerHtml, @"{gid:'(?\d{5,10})',cid:'(?\d+)',price:'[.\d]+',coupon:'\d+',title:'标题链接'}");
// if (reg.Success)
// {
// var gid = reg.Groups["gid"].Value;
// var cid = reg.Groups["cid"].Value;
// string url = "http://www.dataoke.com/gettpl?gid=" + gid + "&_=" + ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000);
// result = helper.GetHtml(helper.GetItem(url));
// if (result != null)
// {
// var content1 = result.Html;
// if (content1.Contains("元定金,再领")) continue;
// var content2 = item.InnerText.Replace("\n", "").Replace("\t", "").Replace(" ", "");
// //加入收藏复制文案荷仕兰成人系列中老年奶粉900g券后价¥43营销计划30.1%券¥140销量1428优惠券剩余:100000/100000店铺:放单人:
// //(?<佣金比例>[.\d]+)%券¥(?<优惠券>\d+)销量(?<销量>\d+)优惠券剩余:(?<优惠券剩余>\d+)/(\d+)
// reg = Regex.Match(content2, @"(?<佣金比例>[.\d]+)%券¥(?<优惠券>\d+)销量(?<销量>[^优]+)优惠券剩余:(?<优惠券剩余>\d+)/(\d+)");
// if (reg.Success)
// {
// var goods = new fl_plugin_tbcirclepromotion_goodsinfos();
// goods.promotion_rate = double.Parse(reg.Groups["佣金比例"].Value); //佣金
// var isWan = reg.Groups["销量"].Value.Contains("万"); //是否包含 万 字
// var sales = reg.Groups["销量"].Value.Replace("万", "").Replace("+", ""); //把万字去掉
// goods.sold_quantity = (int)(double.Parse(sales) * (isWan ? 10000 : 1)); //销量
// goods.coupon_remain_quantity = reg.Groups["优惠券剩余"].Value; //优惠券剩余
// goods.category_id = cid;
// var values = content1.Split(new string[] { "" }, StringSplitOptions.RemoveEmptyEntries);
// goods.goods_thumbnail_url = values[0].Replace(@"
", "");
// goods.goods_name = values[1];
// var final_price = double.Parse(values[2].Replace("券后【", "").Replace("元】包邮秒杀", ""));//券后价格
// goods.coupon_discount = double.Parse(reg.Groups["优惠券"].Value);
// goods.normal_price = final_price + goods.coupon_discount;
// reg = Regex.Match(values[4], @"id=(?\d{5,})");
// goods.goods_id = reg.Success ? reg.Groups["id"].Value : "0";
// goods.goods_desc = values[5];
// reg = Regex.Match(values[3], @"href=""(.*?(?activityId=.*?))""");
// goods.goods_quan = reg.Success ? reg.Groups["activityId"].Value : string.Empty;
// goods.state = Enums.StateType.未推广;
// goods.goods_source = Enums.GoodsSourceType.接口采集;
// goods.cur_time = DateTime.Today;
// //session.Insertable(goods).ExecuteCommand();
// goodsinfos.Add(goods);
// }
// }
// }
// }
// catch { }
// }
// if (goodsinfos.Count != 0)
// session.Insertable(goodsinfos.ToArray()).ExecuteCommand();
// session.Commit();
// }
// }
// catch (Exception ex)
// {
// session.Rollback();
// EventClient.OnEvent(this, $"淘宝采集:{ex.Message}{ex.StackTrace}");
// }
//}
#endregion
Dictionary dics = new Dictionary() { { "今日上新", "new_today/1/" }, { "高评分", "grade/1/" }, { "天猫", "tianmao/1/" }, { "视频单", "video/1/" }, { "聚划算", "cheap/1/" }, { "淘抢购", "rob/1/" }, { "营销返利", "rebate/1/" }, { "天猫超市", "tianmaoshop/1/" } };
#region 采集好单据数据
public void GatherFactory()
{
strb.Length = 0;
var session = ApiClient.GetSession();
try
{
//var cid = Class1.Config.HDK_Category == CategoryType.全部 ? string.Empty : $"cid/{(int)Class1.Config.HDK_Category}/";
//var stage = Class1.Config.HDK_Stage == StageType.不限 ? string.Empty : dics[Class1.Config.HDK_Stage.ToString()];
//var url = $@"https://www.haodanku.com/index/index/nav/3/{cid}{stage}starttime/30/p/1.html?sale_min={Class1.Config.HDK_Sales}&tkrates_min={Class1.Config.HDK_Rate}&tkmoney_min={Class1.Config.HDK_Brokerage}&avg_min={Class1.Config.HDK_Avgmin}&coupon_min={Class1.Config.HDK_CouponMoney}&price_min={Class1.Config.HDK_Price1}&price_max={Class1.Config.HDK_Price2}";
var r = new Random();
var url = string.Empty;
switch (Class1.Config.HDK_GatherType)
{
case GatherType.实时销量榜:
{
var p = r.Next(1, 6);
url = $"https://www.haodanku.com/index/index/nav/1/p/{p}.html";
}
break;
case GatherType.今日爆单榜:
{
var p = r.Next(1, 6);
url = $"https://www.haodanku.com/index/index/nav/7/p/{p}.html";
}
break;
case GatherType.昨日爆单榜:
url = "https://www.haodanku.com/index/index/nav/7/status/1.html";
break;
case GatherType.出单指数榜:
url = "https://www.haodanku.com/index/teamsale/nav/7/status/2.html";
break;
case GatherType.自定义条件:
default:
{
var cid = Class1.Config.HDK_Category == CategoryType.全部 ? string.Empty : $"cid/{(int)Class1.Config.HDK_Category}/";
var stage = Class1.Config.HDK_Stage == StageType.不限 ? string.Empty : dics[Class1.Config.HDK_Stage.ToString()];
url = $@"https://www.haodanku.com/index/index/nav/3/{cid}{stage}starttime/30/p/1.html?sale_min={Class1.Config.HDK_Sales}&tkrates_min={Class1.Config.HDK_Rate}&tkmoney_min={Class1.Config.HDK_Brokerage}&avg_min={Class1.Config.HDK_Avgmin}&coupon_min={Class1.Config.HDK_CouponMoney}&price_min={Class1.Config.HDK_Price1}&price_max={Class1.Config.HDK_Price2}";
}
break;
}
//HttpHelper http = new HttpHelper();
//HttpItem item = new HttpItem();
//item.URL = url;
//var result = http.GetHtml(item);
//var html = result.Html;
////var reg = Regex.Matches(html, @"领券:(?<优惠券地址>.*?)
下单:(?<商品链接>.*?)
(?<介绍>[^"""">]+)");
//var reg = Regex.Matches(html, @"
[^""]+?jpg[^""]*?)""[\w\W]*?
(?<标题>([^<]+?))
[\w\W]*?领券:(?<优惠券地址>[^\<]*?)
下单:(?<商品链接>.*?)
(?<介绍>[^""]+)[\w\W]+?[\w\W]*?(?<券后价>[^\<]+)[\w\W]*?(?<佣金比>[^\<]+)[\w\W]*?(?<佣金>[^\<]+)[\w\W]*?¥(?<优惠券面额>[^\<]+)");
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem();
item.URL = url;
var result = http.GetHtml(item);
var html = result.Html;
//var reg = Regex.Matches(html, @"领券:(?<优惠券地址>.*?)
下单:(?<商品链接>.*?)
(?<介绍>[^"""">]+)");
var regStr = @"
(?<标题>.+?)
.+?
\s+?领券:(?<优惠券地址>[\w\W]+?)
下单:(?<商品链接>[\w\W]+?)
(?<介绍>[^""""]*?)"">[\w\W]+?[\w\W]+?券后\s+?(?<券后价>.+?)[\w\W]+?营销\s+?(?<佣金比>.+?)%\s+?\s+?\s+?佣金\s+?(?<佣金>.+?)\s+?\s+?\s+?\s+?