Files
KJD_WebService/Work/Business/CatBLL.cs
T

130 lines
5.4 KiB
C#

using AllBase;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Work.Api;
namespace Work.Business
{
public class CatBLL
{
Dictionary<string, List<int>> dicCat = new Dictionary<string, List<int>>();
public static List<CatInfo> lstCatInfo = new List<CatInfo>();
public void GetCat1()
{
var Shop = GetShopLoginInfo.Shops.Find(s => s.ShopName == "pdd49094313446");
string url = "https://mms.pinduoduo.com/vodka/v2/mms/cat1List";
string refere = "https://mms.pinduoduo.com/goods/category";
string htmlx = GetPostTaobaoHtml.UploadTaobaoPost(url, refere, true, "", Shop.Cookies);
var mc = BaseRegex.GetMatchRegex(ref htmlx, @"\{[^}]+?""stapleName"":\[([\w\W]+?)\]\}");
dicCat.Clear();
for (int i = 0; mc != null && i < mc.Count; i++)
{
var item = mc[i].Groups[0].Value;
int catid = BaseRegex.GetIntJson(ref item, "id");//商品ID
string stapleName = mc[i].Groups[1].Value;//商品ID
if (dicCat.ContainsKey(stapleName))
{
dicCat[stapleName].Add(catid);
}
else
{
List<int> lst = new List<int>();
lst.Add(catid);
dicCat.Add(stapleName, lst);
}
string name = BaseRegex.GetStringJson(ref item, "name");//商品ID
CatInfo cat = new CatInfo();
cat.CatId = catid;
cat.CatName = name;
lstCatInfo.Add(cat);
}
}
public void GetAllCat()
{
var Shop = GetShopLoginInfo.Shops.Find(s => s.ShopName == "pdd49094313446");
if (Shop == null)
return;
lstCatInfo.Clear();
if (File.Exists("full_cat_json.txt"))
{
lstCatInfo = JsonHelper.DeserializeJsonToObject<List<CatInfo>>(FileBase.ReadAllText("full_cat_json.txt"));
}
var catNotGetChild = new CatInfo { CatId = 0, CatLevel = 0, CatName = "", ParentCatId = 0 };
string refere = "https://mms.pinduoduo.com/Pdd.html";
while (catNotGetChild != null)
{
try
{
System.Threading.Thread.Sleep(2000 + Randomx.GetId(2000));
string url = "https://mms.pinduoduo.com/vodka/v2/mms/categories?parentId=" + catNotGetChild.CatId;
string html = GetPostTaobaoHtml.GetJsonPost(url, refere, true, "", Shop.Cookies);
JObject jObj = JObject.Parse(html);
JArray jArr = jObj["result"] as JArray;
foreach (var item in jArr)
{
var itemStr = item.ToString();
int level = BaseRegex.GetIntJson(ref itemStr, "level");
int catId = BaseRegex.GetIntJson(ref itemStr, "id");
bool isLeaf = BaseRegex.GetIntJson(ref itemStr, "leaf") == 1;
int parentCatId = BaseRegex.GetIntJson(ref itemStr, "parent_id");
string catName = BaseRegex.GetStringJson(ref itemStr, "cat_name");
CatInfo c = new CatInfo
{
CatId = catId,
CatLevel = level,
CatName = catName,
ParentCatId = parentCatId,
IsLeaf = isLeaf,
cat_id_1 = BaseRegex.GetIntJson(ref itemStr, "cat_id_1"),
cat_id_2 = BaseRegex.GetIntJson(ref itemStr, "cat_id_2"),
cat_id_3 = BaseRegex.GetIntJson(ref itemStr, "cat_id_3"),
cat_id_4 = BaseRegex.GetIntJson(ref itemStr, "cat_id_4"),
};
if (!lstCatInfo.Any(lc => lc.CatId == c.CatId))
lstCatInfo.Add(c);
}
catNotGetChild.IsGet = true;
FileBase.WriteAllText("full_cat_json.txt", JsonHelper.SerializeObject(lstCatInfo));
catNotGetChild = lstCatInfo.FirstOrDefault(lc => lc.IsGet == false && lc.CatLevel < 4 && !lc.IsLeaf);
}
catch (Exception ex)
{
}
}
if (lstCatInfo.Count > 0)
{
string catstr = "";
foreach (var item in lstCatInfo)
{
catstr += item.CatId + "," + item.cat_id_1 + "," + item.cat_id_2 + "," + item.cat_id_3 + "," + item.cat_id_4 + "\r\n";
}
FileBase.WriteAllText("full_cat.txt", catstr);
}
}
}
public class CatInfo
{
public int CatId { get; set; }
public int CatLevel { get; set; }
public int ParentCatId { get; set; }
public string CatName { get; set; }
public bool IsGet { get; set; }
public bool IsLeaf { get; set; }
public int cat_id_1 { get; set; }
public int cat_id_2 { get; set; }
public int cat_id_3 { get; set; }
public int cat_id_4 { get; set; }
}
}