old_flsystem/应用/Weixin.CircleTools/AnalyzeGoods.cs

194 lines
8.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Api.Framework;
using Api.Framework.Enums;
using CsharpHttpHelper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Api.Framework.Tools;
using Weixin.CircleTools.Entitys;
namespace Weixin.CircleTools
{
/// <summary>
/// 解析商品
/// </summary>
class AnalyzeGoods
{
/// <summary>
/// 通过商品的id查询返回获取到自己返利商品信息
/// </summary>
/// <param name="goods_id"></param>
/// <returns></returns>
public goodsinfos FindGoodsInfoToItemId(string goods_id, string pid_cps_name = "", string pid = "", 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; }
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<string, object>;
//最高佣金率
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;
coupon_click_url = privilege_data["coupon_click_url"].ToString();
}
return new 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 };
}
}
}
catch (Exception)
{ }
return null;
}
public void AnalysisText(string message, goodsinfos goodsinfo)
{
try
{
var list = new string[] { @"(?<短标题>[\w\W]+)\s?(优惠\s?券|领券|抢券)[:]\s?(?<优惠券>[\w\W]+)\s+(下单链接|下单|抢购|秒杀)[:]\s?(?<商品>[\w\W]+)\s+(?<长文案>[\w\W]+)", @"\[image=(?<图片地址>[\w\W]+)\]\s+(?<短标题>[\w\W]+)\s+(优惠券|领券|抢券)[:]\s?(?<优惠券>[\w\W]+)\s+(下单|抢购|秒杀)[:]\s?(?<商品>[\w\W]+)\s+(?<长文案>[\w\W]+)" };
foreach (string item in list)
{
var reg = Regex.Match(message, item);
if (reg.Success)
{
goodsinfo.goods_desc = reg.Groups["长文案"].ToString().Trim();
}
}
}
catch (Exception ex)
{ }
}
//public object FindResult(string method, object param = null)
//{
// string msd = method;
// int page = 0;
//Next:
// page++;
// string sign = string.Empty;
// string str = string.Empty;
// System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
// long t = (DateTime.Now.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
// if (param != null)
// {
// StringBuilder sb = new StringBuilder("now=" + t + "&");
// Type type = param.GetType();
// System.Reflection.PropertyInfo[] ps = type.GetProperties();
// foreach (PropertyInfo i in ps)
// {
// string name = i.Name;
// object value = i.GetValue(param, null);
// sb.Append(name + "=" + HttpHelper.URLEncode(value.ToString(), Encoding.UTF8));
// sb.Append("&");
// }
// str = sb.ToString();
// sign = HttpHelper.URLDecode((str.Substring(0, str.Length - 1) + "cps-api"), Encoding.UTF8);
// sign = sign.GetHashCode().ToString();
// }
// str += "sign=" + sign;
// HttpHelper http = new HttpHelper();
// HttpItem item = new HttpItem()
// {
// URL = _host + "/api/alimama.asmx/" + method + "?v=1.0.0.1",//URL 必需项
// Method = "post",//URL 可选项 默认为Get
// Timeout = 1000 * 30,//连接超时时间 可选项默认为100000
// ReadWriteTimeout = 1000 * 30,//写入Post数据超时时间 可选项默认为30000
// IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
// Cookie = "",//字符串Cookie 可选项
// 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 = "http://license.52cmg.cn",//来源URL 可选项
// Postdata = str,//Post数据 可选项GET时不需要写
// };
// string html = string.Empty;
// try
// {
// html = http.GetHtml(item).Html;
// }
// catch (Exception)
// {
// if (page < 5)
// {
// page++;
// Thread.Sleep(500);
// goto Next;
// }
// }
// var dic = HttpExtend.JsonToDictionary(html);
// if (dic == null) throw new Exception(html);
// if (dic["ok"].ToString().ToUpper() == "FALSE")
// {
// string msg = dic["message"].ToString();
// if (page < 5 && string.IsNullOrEmpty(dic["message"].ToString()) && !msg.Contains("未授权") && !msg.Contains("Invalid session"))
// {
// Console.WriteLine("异常:" + msg);
// Thread.Sleep(500);
// goto Next;
// }
// throw new Exception(dic["message"].ToString());
// }
// return dic["message"];
//}
}
}