Files
KJD_WebService/Work/CollectClass/BigDataBuild.cs
T
2025-06-29 19:38:31 +08:00

437 lines
17 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using AllBase;
using Work.Api;
using Work.ManageClass;
namespace Work.CollectClass
{
public class BigDataBuild
{
public static Dictionary<string, int> Keywords = new Dictionary<string, int>();
public List<string> NoKeyword = new List<string>();
public static List<TaobaoProductBase> Product = new List<TaobaoProductBase>();
public static List<TaobaoProductBase> ProductTemp = new List<TaobaoProductBase>();
public static bool Running = false, AutoAdd = false, OnlyFree = false,BuildAutoAdd=false;
public static int BeginPrice = 0, EndPrice = 0, BeginSaleCount = 0, EndSaleCount = 0, GetCount = 0, MaxCount = 0, ThisCount = 0;
public int SiteId = 1;
public string Name;
public static string SearchUrl = "", MainCatName = "";
public BigDataBuild(string name)
{
if (Keywords.Count == 0)
IniKeyword();
Name = name;
}
public void Get(int siteId, string noKeyword)
{
NoKeyword.Clear();
string[] l = noKeyword.Split('\n');
foreach (var s in l)
{
string ss = s.Trim();
if (string.IsNullOrEmpty(s))
continue;
NoKeyword.Add(ss);
}
SiteId = siteId;
new Thread(Get).Start();
}
public string GetRandTitle(string title)
{
var m = BaseRegex.GetMatchRegex(ref title, @"(【[\w\W]+?】)");
string before = "";
for (int i = 0; i < m.Count; i++)
{
before += m[i].Groups[1].Value;
title = title.Replace(m[i].Groups[1].Value, "");
}
if (string.IsNullOrEmpty(title))
return "";
List<string> keyx = new Segment().Get(title);
string newTitle = "",left = title+" ";
foreach (string s in keyx)
{
newTitle += s;
for (int i = 0; i < left.Length - s.Length; i++)
{
string key = left.Substring(i, s.Length);
if (key == s)
{
if (i > 0)
left = left.Substring(0, i) + " " + left.Substring(i + s.Length);
else
left = left.Substring(s.Length);
break;
}
}
}
return before+newTitle + left.Replace(" ","");
}
public void Get()
{
int mainCatId = BaseClass.MainCatId;
foreach (KeyValuesBase key in FromTitleToCat.MainCat)
{
if (key.Value2 == MainCatName)
mainCatId = int.Parse(key.Key);
}
string url = BaseClass.LoginApiUrl + "?obj=product/product&act=get";
StringBuilder s = new StringBuilder();
//s.Append("&act=get");
s.Append("&begin_price=" + BeginPrice);
s.Append("&end_price=" + EndPrice);
s.Append("&begin_sale_count=" + BeginSaleCount);
s.Append("&end_sale_count=" + EndSaleCount);
s.Append("&get_count=" + GetCount);
s.Append("&site_id=" + SiteId);
s.Append("&main_cat_id=" + mainCatId);
string html = GetHtml.PostUtf8(url, UserLogin.GetSignaturex() + s);
var m = BaseRegex.GetMatchRegex(ref html, @"<product ([^>]+?)>");
for (int i = 0; m != null && i < m.Count; i++)
{
string list = m[i].Groups[0].Value;
int siteId = BaseRegex.GetIntJson(ref list, "site_id");
TaobaoProductBase t = new TaobaoProductBase();
t.FromId = BaseRegex.GetLongJson(ref list, "product_id");
t.Titlte = BaseRegex.GetStringJson(ref list, "name").Replace("2017","2018");
bool had = false;
foreach (string k in NoKeyword)
{
if (t.Titlte.Contains(k))
{
had = true;
break;
}
}
if (had)
continue;
t.FromType = siteId == 1 ? "淘宝" : "拼多多";
t.Cat1 = BaseRegex.GetIntJson(ref list, "cat1");
if (t.Cat1 == 194)
continue;
decimal free = BaseRegex.GetDecimalJson(ref list, "express_price");
if (OnlyFree && free > 0)
continue;
t.Cat2 = BaseRegex.GetIntJson(ref list, "cat2");
t.Cat3 = BaseRegex.GetIntJson(ref list, "cat3");
t.MainCatId = BaseRegex.GetIntJson(ref list, "main_cid");
t.Price = BaseRegex.GetDecimalJson(ref list, "price");
t.SaleCount = BaseRegex.GetIntJson(ref list, "sold_count");
had = Product.Exists(p => p != null && p.FromId == t.FromId);
if (!had)
{
Product.Add(t);
ProductTemp.Add(t);
}
}
}
public void BuildTitle()
{
Running = true;
MaxCount = Product.Count;
for (int i = 0; i < Product.Count && Running; i++)
{
ThisCount = i + 1;
var p = Product[i];
if (p == null || p.Build)
continue;
string title = p.Titlte;
string mainKey = GetMainKeywords(title);
if (string.IsNullOrEmpty(mainKey))
{
LogSave.Add(Name, "没有找到主关键词");
continue;
}
string newTitle = GetAllTitle(mainKey);
if (string.IsNullOrEmpty(newTitle) && mainKey.Split(' ').Length==4)
{
newTitle = GetAllTitle(mainKey.Substring(0,mainKey.LastIndexOf(" ")));
}
if (string.IsNullOrEmpty(newTitle))
{
LogSave.Add(Name, "生成标题失败:" + mainKey);
continue;
}
p.Build = true;
p.Titlte = newTitle;
LogSave.Add(Name, "生成标题成功:" + p.Titlte);
BigDataCenter.ProductTemp.Add(p);
if (BuildAutoAdd)
{
if (p.FromType == "内部数据")
p.FromType = "拼多多";
UploadPinduoduoMain.ProductStock.Add(p);
UploadPinduoduoMain.AddToDataBase(p);
}
}
ThisCount = MaxCount;
Running = false;
}
/// <summary>
/// 以关键词和指数获取标题
/// </summary>
/// <param name="keyword"></param>
/// <returns></returns>
public string GetIndexKeywordTitle(string keyword)
{
return "";
}
/// <summary>
/// 以关键词获取标题
/// </summary>
/// <param name="keyword"></param>
/// <returns></returns>
public string GetAllTitle(string keyword)
{
if (string.IsNullOrEmpty(keyword) || !keyword.Contains(" "))
return "";
Thread.Sleep(500);
string url = "http://t.qimake.cn/api.php?keyword=" + GetHtml.UrlEncodeUtf8(keyword);
string data = UserLogin.GetSignaturex() ;
string html = GetHtml.PostUtf8(url, data);
html = GetHtml.Decode(html).Replace("\\/", "/");
//var m = BaseRegex.GetMatchRegex(ref html, @"""title"":""([^""]+?)""");
string titles = "";
string[] tl = html.Split('\n');
foreach (var ti in tl)
{
string title = ti.Replace("2017", "2019").Replace("2018", "2019");
title = BaseRegex.Clear(title, "(【[^】]+】)");
title = BaseRegex.Clear(title, @"(买[\d]+发[\d]+)");
title = BaseRegex.Clear(title, @"(满[\d]+减[\d]+)");
if (titles.Contains(title) || title.Length < 4)
continue;
if (title.Length > 60)
title = title.Substring(0, 60);
titles += title.Substring(4);
}
//for (int j = 0; m != null && j < m.Count; j++)
//{
// string title = m[j].Groups[1].Value;
// if (titles.Contains(title) || title.Length < 4)
// continue;
// titles += title.Substring(4);
//}
BaseRegex.Relpace(ref titles, "([0-9a-zA-Z-_:&;/])", "");
List<string> keyx = new Segment().Get(titles);
List<string> key = new List<string>();
string noKey = ",正品,限量,代购,限时,厂家,天猫,微信,京东,淘宝,韩国,日本,美国,正版,真品,真皮,,,";
for (int i = 0; keyx != null && i < keyx.Count; i++)
{
if (key.Contains(keyx[i]) || noKey.Contains(keyx[i]) || keyx[i].Contains("正") || keyx[i].Contains("真")
|| AlibabaShop.TitleCantHad.Contains(keyx[i]) || AlibabaShop.NoBrandName.Contains(keyx[i]) || AlibabaShop.NoTitleWords.Contains(keyx[i]))
continue;
key.Add(keyx[i]);
}
if (key.Count < 30)
return "";
string[] l = keyword.Split(' ');
for (int i = 0; i < l.Length; i++)
{
if (AlibabaShop.NoTitleWords.Contains(l[i]) || AlibabaShop.NoBrandName.Contains(l[i]) ||
AlibabaShop.TitleCantHad.Contains(l[i]))
l[i] = "";
}
string newTitle = l[0];
int times = 0;
while (newTitle.Length < 8 && times < 1000)
{
times++;
int r = new Random(Guid.NewGuid().GetHashCode()).Next(key.Count);
if (newTitle.Contains(key[r]))
continue;
newTitle += key[r];
}
if (l.Length > 1)
newTitle += l[1];
times = 0;
while (newTitle.Length < 16 && times < 1000)
{
times++;
int r = new Random(Guid.NewGuid().GetHashCode()).Next(key.Count);
if (newTitle.Contains(key[r]))
continue;
newTitle += key[r];
}
if (l.Length > 2)
newTitle += l[2];
while (newTitle.Length < 22 && times < 1000)
{
times++;
int r = new Random(Guid.NewGuid().GetHashCode()).Next(key.Count);
if (newTitle.Contains(key[r]))
continue;
newTitle += key[r];
}
if (l.Length > 3)
newTitle += l[3];
times = 0;
while (BaseClass.GetCharLength(newTitle) < CollectKeywordByTaobao.MaxTitleLen * 2 - 2 && times < 1000)
{
times++;
int r = new Random(Guid.NewGuid().GetHashCode()).Next(key.Count);
if (newTitle.Contains(key[r]))
continue;
newTitle += key[r];
}
//超强过滤
for (int i = 0; i < AlibabaShop.NoBrandName.Count; i++)
{
if (newTitle.Contains(AlibabaShop.NoBrandName[i]))
newTitle = newTitle.Replace(AlibabaShop.NoBrandName[i], "");
}
while (BaseClass.GetCharLength(newTitle) < CollectKeywordByTaobao.MaxTitleLen * 2 - 2 && times < 1000)
{
times++;
int r = new Random(Guid.NewGuid().GetHashCode()).Next(key.Count);
if (newTitle.Contains(key[r]))
continue;
newTitle += key[r];
}
return newTitle;
}
public string GetMainKeywords(string title)
{
if (string.IsNullOrEmpty(title))
return "";
BaseRegex.Relpace(ref title, "([0-9a-zA-Z-_:&;/])", "");
title = title.Replace(" ", "").Replace("新款", "").Replace("包邮", "").Replace("正品", "").Replace("韩版", "")
.Replace("可爱", "").Replace("卡通", "").Replace("时尚", "").Replace("新品", "").Replace("特价", "").Replace("热销", "").Trim();
if (title.Length < 8)
return "";
Dictionary<string, int> keys = new Dictionary<string, int>();
for (int i = 2; i < title.Length; i++)
{
for (int j = 0; j < title.Length - i; j++)
{
string key = title.Substring(j, i).Trim();
if (string.IsNullOrEmpty(key) || keys.ContainsKey(key))
continue;
if (Keywords.ContainsKey(key))
keys.Add(key, Keywords[key] * 5 ^ (key.Length - 2));
}
}
string outKey = "";
if (keys.Count < 4)
{
List<string> keyx = new Segment().Get(title);
for (int i = 0; i < keyx.Count&& keys.Count<4; i++)
{
if(keys.ContainsKey(keyx[i]))
continue;
keys.Add(keyx[i],1);
}
}
if (keys.Count < 5)
{
foreach (KeyValuePair<string, int> key in keys)
{
outKey += " " + key.Key;
}
return outKey.Trim();
}
string key1 = "", key2 = "", key3 = "", key4 = "";
int count1 = 0, count2 = 0, count3 = 0, count4 = 0;
foreach (KeyValuePair<string, int> key in keys)
{
if (count1 < key.Value)
{
count1 = key.Value;
key1 = key.Key;
}
else if (count2 < key.Value)
{
count2 = key.Value;
key2 = key.Key;
}
else if (count3 < key.Value)
{
count3 = key.Value;
key3 = key.Key;
}
else if (count4 < key.Value)
{
count4 = key.Value;
key4 = key.Key;
}
}
outKey = key1 + " " + key2 + " " + key3 + " " + key4;
return outKey.Trim();
}
public void Ini()
{
string fileName = "lbigdata.dll";
if (!FileBase.Exists(fileName))
{
GetHtml.DownloadFile("http://dl.qixiaoke.com/lbigdata.html", fileName);
}
string[] l = FileBase.ReadAllLines(fileName, Encoding.UTF8);
if (l == null || l.Length < 300000)
{
GetHtml.DownloadFile("http://dl.qixiaoke.com/lbigdata.html", fileName);
l = FileBase.ReadAllLines(fileName, Encoding.UTF8);
}
if (l == null || l.Length < 300000)
{
MessageBox.Show("初始化大数据异常,请检查网络", "提示");
return;
}
}
public void IniKeyword()
{
string[] l = "防狼,辣椒水,迷情,刀,枪,听话,催泪,瓦斯".Split(',');
for (int i = 0; i < l.Length; i++)
{
if (string.IsNullOrEmpty(l[i]))
continue;
NoKeyword.Add(l[i].Trim());
}
//string fileName = "lbigdata.dll";
string fileName = "Segment.dll";
l = FileBase.ReadAllLines(fileName, Encoding.UTF8);
if (l == null || l.Length < 300000)
{
MessageBox.Show("初始化大数据失败,请重新打开软件", "提示");
return;
}
for (int i = 0; i < l.Length; i++)
{
if (string.IsNullOrEmpty(l[i]) || !l[i].Contains("\t"))
continue;
string[] ll = l[i].Split('\t');
string keyword = ll[0].Replace(" ", "");
int rank = int.Parse(ll[1]);
if (!Keywords.ContainsKey(keyword))
Keywords.Add(keyword, rank);
}
}
/// <summary>
/// 删除下架产品
/// </summary>
/// <param name="id">产品ID</param>
/// <param name="site">网站ID</param>
public static void Delete(long id, int site)
{
//string url = BaseClass.BigDataApiUrl + "?obj=product/product";
//string data = "&act=delete&site_id=" + site + "&id=" + id;
//string html = GetHtml.PostUtf8(url, UserLogin.GetSignaturex() + data);
}
}
}