Files
KJD/Work/CollectClass/CollectKeywordByTaobao.cs
2025-06-29 19:38:31 +08:00

1171 lines
51 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 System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Linq;
using AllBase;
using Newtonsoft.Json.Linq;
using Work.Api;
using Work.ManageClass;
using NaiBa.Util.Extension;
namespace Work.CollectClass
{
public class CollectKeywordByTaobao
{
public string Name;
public static int PageTop = 0;
public static int MaxCount = 0;
public static int ThisCount = 0;
public static int MinPrice = 0;
public static int MaxPrice = 0;
public static int MinSale = 0;
public static int MaxSale = 0;
public static int MainImageCut = 0;
public static int MaxTitleLen = 30;
public static int FessionCount = 1;
public static int MainCatId = 0;
public static int coupon = 0;
public static int tkRate = 0;
public static int sales = 0;
public static int MaxSingleKeywordProduct = 10000;
public static List<TaobaoProductBase> Product = new List<TaobaoProductBase>();
public static List<TaobaoProductBase> ProductTemp = new List<TaobaoProductBase>();
public static List<ProductTaoKeBase> ProductTaoKeTemp = new List<ProductTaoKeBase>();
public static Dictionary<string, List<string>> KeywordFiles = new Dictionary<string, List<string>>();
public static List<long> MallId = new List<long>();
public string Keywords, OutKeyword;
public static string AddKeyword = "";
public static bool Running = false;
public static bool AutoAddToUpList = false, TaobaoCollect = false, TaoKeCollect = false, PinduoduoCollect = false, PinduoduoShopCollect = false,
MeiLiZheCollect = false, OnlyFree = false, AllCatAdd = false, Alibaba = false, DataCenter = false, AutoAdd = false, Waiting = false,
SaveCollect = false, BaoYou = false, TaoJiJi = false;
public static string outKeywordrich;
public static string addKeywordrich;
public CollectKeywordByTaobao(string keywords, string outKeyword, string addKeyword, string name)
{
Keywords = keywords;
OutKeyword = outKeyword;
AddKeyword = addKeyword;
Name = name;
}
public static string strContent = "";
public void Get()
{
if (string.IsNullOrEmpty(strContent))
{
string path = AppDomain.CurrentDomain.BaseDirectory + "anti_content.js";
strContent = File.ReadAllText(path);
}
Running = true;
Product.Clear();
ProductTemp.Clear();
//ProductTaoKeTemp.Clear();
string[] l = Keywords.Split('\n');
string[] noKeyword = OutKeyword.Split('\n');
MaxCount = PageTop * l.Length;
List<Thread> ts = new List<Thread>();
for (int i = 0; i < l.Length; i++)
{
string keyword = l[i].Trim();
if (string.IsNullOrEmpty(keyword))
continue;
int pidCount = 0;
//if (PinduoduoCollect)
//{
// string url= "http://pcat.qixiaoke.com/taobao/pinduoduo_search.php?keyword=" + GetHtml.UrlEncodeUtf8(keyword);
// string data = UserLogin.GetSignaturex()+ "&pi=" + MinPrice + "&pa=" + MaxPrice+ "&pi=" + MinPrice + "&pa=" + MaxPrice;
// string html = GetHtml.PostUtf8(url, data);
// var m = BaseRegex.GetMatchRegex(ref html, @"""attrs"":\{([^\}]+)\}");
// for (int j = 0; m!=null && j < m.Count; j++)
// {
// }
//}
if (TaoKeCollect)
PageTop = 1000;
for (int k = 0; TaoKeCollect && k <= PageTop; k++)
{
if (pidCount >= MaxSingleKeywordProduct)
break;
string url = "http://www.dataoke.com/qlist/?px=zh&sales=" + sales + "&tk_rate=" + tkRate + "&coupon_money=" + coupon + "&page=" + (k + 1);
if (keyword != "无")
url += "&keywords=" + GetHtml.UrlEncodeUtf8(keyword);
string refere = "www.dataoke.com";
string html = GetPostTaobaoHtml.UploadTaobao(url, refere, true);
var m = BaseRegex.GetMatchRegex(ref html, @"<a href=""([^>]+?)""\s*target=""_blank""\s*title=""([^>]+?)""");
if (m != null && m.Count == 0)
{
break;
}
for (int q = 0; m != null && q < m.Count; q++)
{
if (pidCount >= MaxSingleKeywordProduct)
break;
string urlx = "http://www.dataoke.com/" + m[q].Groups[1].Value;
string title = m[q].Groups[2].Value;
html = GetPostTaobaoHtml.UploadTaobao(urlx, refere, true);
string pattern = @"<a\s*ui-mta=""clickStat""[\w\W]+?href=""(https://[^>]+?)""";
var mc = BaseRegex.GetMatchRegex(ref html, pattern);
if (mc != null && mc.Count == 0)
continue;
string taobaoUrl = mc[0].Groups[1].Value;
string id = BaseRegex.GetMatchRegexString(taobaoUrl, @"id=([\d]+)");
if (string.IsNullOrEmpty(id))
continue;
//pattern = @"<span style=""font-size: 14px;"">券后价<b style=""font-size: 30px; font-weight: bold;""><i>¥</i>([\w\W]+?)</b></span>";
pattern = @"券后价[\s\S]+?<i>¥</i>([\w\W]+?)</b></span>";
mc = BaseRegex.GetMatchRegex(ref html, pattern);
if (mc != null && mc.Count == 0)
continue;
string quanHou = mc[0].Groups[1].Value;
pattern = @"<span style=""font-size: 14px;"">原价<b style=""font-family: Arial""><i>¥</i>([\w\W]+?)</b></span>";
mc = BaseRegex.GetMatchRegex(ref html, pattern);
if (mc != null && mc.Count == 0)
continue;
string yuanPrice = mc[0].Groups[1].Value;
pattern = @"<span style=""font-size: 14px;"">销量<b style=""font-size: 14px;"">([\w\W]+?)</b></span>";
mc = BaseRegex.GetMatchRegex(ref html, pattern);
if (mc != null && mc.Count == 0)
continue;
string xiaoLiang = mc[0].Groups[1].Value;
pattern = @"<span>佣金<b>([\w\W]+?)%</b></span>";
mc = BaseRegex.GetMatchRegex(ref html, pattern);
if (mc != null && mc.Count == 0)
continue;
string yongJin = mc[0].Groups[1].Value;
pattern = @"<span>优惠券<b><i>¥</i>([\w\W]+?)</b></span>";
mc = BaseRegex.GetMatchRegex(ref html, pattern);
if (mc != null && mc.Count == 0)
continue;
string youHuei = mc[0].Groups[1].Value;
string keId = BaseRegex.GetMatchRegexString(m[q].Groups[1].Value, @"id=([\d]+)");
if (string.IsNullOrEmpty(id))
continue;
ProductTaoKeBase t = new ProductTaoKeBase();
t.Title = title;
t.TaoKeId = long.Parse(keId);
t.TaoKeUrl = urlx;
t.toId = long.Parse(id);
t.taoBaoUrl = taobaoUrl;
t.quanHou = quanHou;
t.yuanPrice = yuanPrice;
t.xiaoLiang = xiaoLiang;
t.yongJin = yongJin;
t.youHuei = youHuei;
pidCount++;
//ProductTaoKeTemp.Add(t);
int sals = 0;
if (t.xiaoLiang.Contains("万"))
{
decimal d = Decimal.Parse(BaseRegex.GetMatchRegexString(t.xiaoLiang, @"([\d\.]+)"));
sals = (int)(d * 10000);
}
else
{
sals = int.Parse(t.xiaoLiang);
}
Threadx.ThreadListWait(ref ts, new Thread(new GetPinduoduoTitleCat(title, keyword, Name, sals, decimal.Parse(yuanPrice), q, t.toId, t).Get), 5);
if (!Running)
goto EndAll;
}
}
//// 采集美丽折
//if (MeiLiZheCollect)
//{
//}
for (int j = 1; PinduoduoCollect && j <= PageTop; j++)
{
if (keyword == "无")
break;
if (pidCount >= MaxSingleKeywordProduct)
break;
if (!CollectKeywordByTaobao.Running)
return;
string strTime = BaseClass.GetTime(DateTime.Now).ToString() + "000";
string str = "";
for (int k = 0; k < 16; k++)
{
str += new Random(Guid.NewGuid().GetHashCode()).Next(9);
}
string strToken = str + "-" + strTime;
string strNumber = "";
string fun2 = string.Format(@"getEncode('{0}','{1}','{2}')", strTime, strToken, strNumber);
string codex = UploadPinduoduoSet.ExecuteScript(fun2, strContent);
string url = "http://apiv3.yangkeduo.com/search?page=" + j + "&q=" + GetHtml.UrlEncodeUtf8(keyword) + "&size=50&requery=0&sort=_sales&list_id=search_UHpDve&filter=price," + MinPrice + "," + MaxPrice;
string userAgent =
"android Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MLA-AL10 Build/HUAWEIMLA-AL10) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 phh_android_version/3.63.0 phh_android_build/228842 phh_android_channel/yyb";
//userAgent = BaseClass.ChromeUserAgent;
string html = GetHtmlUrl(url);
if (string.IsNullOrEmpty(html) || !html.Contains("\"icon\""))
{
Thread.Sleep(20 * 1000);
html = GetHtml.GetUtf8(url, userAgent);
}
if (string.IsNullOrEmpty(html) || !html.Contains("\"icon\""))
{
Thread.Sleep(40 * 1000);
html = GetHtml.GetUtf8(url, userAgent);
}
var m = BaseRegex.GetMatchRegex(ref html, @"\{""thumb_url"":[\w\W]+?""short_name"":([^\}]+?)\}");
if (m != null && m.Count == 0)
{
break;
}
for (int k = 0; m != null && k < m.Count; k++)
{
while (Running && Waiting)
Thread.Sleep(100);
if (!CollectKeywordByTaobao.Running)
return;
string list = m[k].Groups[0].Value;
TaobaoProductBase t = new TaobaoProductBase();
t.FromId = BaseRegex.GetLongJson(ref list, "goods_id");
if (t.FromId == 0)
{
continue;
}
bool had = false;
for (int n = 0; n < Product.Count; n++)
{
if (Product[n].FromId == t.FromId)
{
had = true;
break;
}
}
if (had)
{
continue;
}
t.FromType = "拼多多";
t.Keyword = keyword;
t.Price = BaseRegex.GetIntJson(ref list, "price") / 100m;
t.SaleCount = BaseRegex.GetIntJson(ref list, "sales");
t.Titlte = BaseRegex.GetStringJson(ref list, "goods_name");
if (string.IsNullOrEmpty(t.Titlte))
continue;
t.Titlte = AddTitleKeyword(t.Titlte);
t.MainImage = MainImageCut;
string country = BaseRegex.GetStringJson(ref list, "country");
if (string.IsNullOrEmpty(t.Titlte) || !string.IsNullOrEmpty(country))
{
continue;
}
bool hadNoKeyword = false;
foreach (string s in noKeyword)
{
if (string.IsNullOrEmpty(s))
continue;
if (t.Titlte.Contains(s.Trim()))
{
hadNoKeyword = true;
break;
}
}
if (hadNoKeyword)
continue;
if (t.Price < MinPrice || t.Price > MaxPrice || t.SaleCount < MinSale || t.SaleCount > MaxSale)
continue;
if (pidCount >= MaxSingleKeywordProduct)
break;
pidCount++;
string cats = BaseRegex.GetMatchRegexString(ref list, @"""cate_list"":\[([\d,]+)\]");
if (!string.IsNullOrEmpty(cats))
{
string[] li = cats.Split(',');
if (li.Length == 4)
{
t.Cat1 = int.Parse(li[0]);
t.Cat2 = int.Parse(li[1]);
t.Cat3 = int.Parse(li[2]);
t.Cat4 = int.Parse(li[3]);
}
}
else
{
MobileDetail md = new MobileDetail(Name, t.FromId);
ProductCollectBase pp = md.Get();
t.Cat1 = pp.cat1;
t.Cat2 = pp.cat2;
t.Cat3 = pp.cat3;
t.Cat4 = pp.cat4;
t.CatId = pp.cat_id;
}
t.CatId = t.Cat4 > 0 ? t.Cat4 : t.Cat3;
var ft = new FromTitleToCat();
t.MainCatId = ft.GetMainCatId(t.Cat1);
t.MainCatName = ft.MainCatName;
t.MainImage = MainImageCut;
if (t.MainCatId == 0 || MainCatId > 0 && MainCatId != t.MainCatId)
{
continue;
}
if (KeywordFiles.ContainsKey(keyword))
{
for (int n = 0; n < CollectKeywordByTaobao.FessionCount; n++)
{
var tt = t.Clone();
tt.Titlte = GetTitleFission(MaxTitleLen, keyword, KeywordFiles[keyword]);
Product.Add(tt);
ProductTemp.Add(tt);
if (AutoAdd)
{
UploadPinduoduoMain.ProductStock.Add(t);
UploadPinduoduoMain.AddToDataBase(t);
}
}
continue;
}
Product.Add(t);
ProductTemp.Add(t);
if (AutoAdd)
{
UploadPinduoduoMain.ProductStock.Add(t);
UploadPinduoduoMain.AddToDataBase(t);
}
if (!Running)
goto EndAll;
//Thread.Sleep(1000);
}
Thread.Sleep(1000);
ThisCount = i * PageTop + j;
}
for (int j = 1; TaobaoCollect && j <= PageTop; j++)
{
if (keyword == "无")
break;
if (pidCount >= MaxSingleKeywordProduct)
break;
string url = "https://s.taobao.com/search?sourceId=tb.item&search_type=item&ssid=s5-e&s_from=newHeader&q="
+ GetHtml.UrlEncodeUtf8(keyword) +
"&suggest=0_1&_input_charset=utf-8&wq=s&suggest_query=s&source=suggest&bcoffset=0&ntoffset=0&p4ppushleft=%2C44&sort=sale-desc&s=" +
(44 * (j - 1));
if (BaoYou)
url += "&fs=1&baoyou=1";
string refere = "https://s.taobao.com/";
string html = GetPostTaobaoHtml.UploadTaobao(url, refere, true);
if (html.Contains("https://login.taobao.com/"))
{
MessageBox.Show("请登录淘宝用户");
return;
}
string htmlxt = "";
if (html.Contains("滑动"))
{
long time;
string data = "{\"isSec\":0}";
string sign = GetSign("", data, out time);
url = "https://api.m.taobao.com/h5/mtop.user.getusersimple/1.0/?appKey=12574478&t=" + time + "&sign=" + sign +
"&api=mtop.user.getUserSimple&v=1.0&H5Request=true&type=jsonp&dataType=jsonp&callback=mtopjsonp1&data=" + GetHtml.UrlEncodeUtf8(data);
//获取用户的所有地址编号
htmlxt = GetPostTaobaoHtml.UploadTaobaoTokenx(url, url, true);
if (string.IsNullOrEmpty(htmlxt) || !htmlxt.Contains("_m_h5_tk"))//没有登录获取token获取失败
{
return;
}
string h5Tkt = BaseRegex.GetMatchRegexString(ref htmlxt, "_m_h5_tk=([^_]+?)_");
string addCookies = BaseRegex.GetMatchRegexString(ref htmlxt, "(_m_h5_tk=[^;]+?;)") + " " +
BaseRegex.GetMatchRegexString(ref htmlxt, "(_m_h5_tk_enc=[^;]+?;)");
string cookies = GetPostTaobaoHtml.GetCookies(url, "");
cookies = BaseRegex.Relpace(cookies, "(_m_h5_tk=[^;]+?;)", "");
cookies = BaseRegex.Relpace(cookies, "(_m_h5_tk_enc=[^;]+?;)", "");
cookies += addCookies;
data = "{\"q\":\"" + keyword + "\",\"search\":\"提交\",\"tab\":\"all\",\"sst\":\"1\",\"n\":20,\"buying\":\"buyitnow\",\"m\":\"api4h5\",\"token4h5\":\"\",\"abtest\":\"55\",\"wlsort\":\"55\",\"page\":" + j + "}";
sign = GetSign(h5Tkt, data, out time);
url = " https://acs.m.taobao.com/h5/mtop.taobao.wsearch.h5search/1.0/?jsv=2.3.16&appKey=12574478&t=" + time
+ "&sign=" + sign + "&api=mtop.taobao.wsearch.h5search&v=1.0&H5Request=true&ecode=1&AntiCreep=true&AntiFlool=true&type=jsonp&dataType=jsonp&callback=mtopjsonp1&data=" + GetHtml.UrlEncodeUtf8(data);
refere = "";
htmlxt = GetPostTaobaoHtml.UploadTaobao(url, refere, true, cookies);
}
if (string.IsNullOrEmpty(html) || !html.Contains("view_price") && string.IsNullOrEmpty(htmlxt))
{
Thread.Sleep(10 * 1000);
html = GetPostTaobaoHtml.UploadTaobao(url, refere, true);
}
if (string.IsNullOrEmpty(html) || !html.Contains("view_price") && string.IsNullOrEmpty(htmlxt))
{
Thread.Sleep(30 * 1000);
html = GetPostTaobaoHtml.UploadTaobao(url, refere, true);
}
var m = BaseRegex.GetMatchRegex(ref html, @"""nid"":""([\d]+)"".*?""raw_title"":""([^""]+?)"".*?""view_price"":""([^""]+?)"".*?""view_sales"":""([\d\.万]+)");
//string url = BaseClass.LoginApiUrl + "?obj=product/taobao_search_bak&act=search";
//string data = UserLogin.GetSignaturex() + "&keyword=" + GetHtml.UrlEncodeUtf8(keyword) + "&page=" + j + "&startPrice=" + MinPrice + "&endPrice=" + MaxPrice;
//string html = GetHtml.PostUtf8(url, data);
//html = GetHtml.Decode(html).Replace("\\/","/");
//m = BaseRegex.GetMatchRegex(ref html, @"\{([^\}]+)\}");
for (int k = 0; k < m.Count; k++)
{
while (Running && Waiting)
Thread.Sleep(100);
if (!Running)
return;
string list = m[k].Groups[0].Value;
decimal free = BaseRegex.GetDecimalJson(ref list, "view_fee");
if (free > 0 && OnlyFree)
continue;
long pid = long.Parse(m[k].Groups[1].Value);
string title = m[k].Groups[2].Value;
int sals = 0;
if (m[k].Groups[4].Value.Contains("万"))
{
decimal d = Decimal.Parse(BaseRegex.GetMatchRegexString(m[k].Groups[4].Value, @"([\d\.]+)"));
sals = (int)(d * 10000);
}
else
{
sals = int.Parse(m[k].Groups[4].Value);
}
decimal price = decimal.Parse(m[k].Groups[3].Value);
if (price < MinPrice || price > MaxPrice || sals > MaxSale)
continue;
//long pid = BaseRegex.GetLongJson(ref list, "num_iid");
//string title = BaseRegex.GetStringJson(ref list, "title");
//int sals = BaseRegex.GetIntJson(ref list, "volume");
//decimal price = BaseRegex.GetDecimalJson(ref list, "zk_final_price");
if (string.IsNullOrEmpty(title) || title.Contains("天猫超市"))
{
LogSave.Add(Name, "过滤天猫超市");
continue;
}
if (sals > MaxSale)
continue;
if (sals < MinSale)
{
LogSave.Add(Name, "销量过低退出");
goto End;
}
bool hadNoKeyword = false;
foreach (string s in noKeyword)
{
if (string.IsNullOrEmpty(s))
continue;
if (title.Contains(s.Trim()))
{
title = title.Replace(s.Trim(), "");
//break;
}
}
if (hadNoKeyword)
continue;
title = AddTitleKeyword(title);
if (KeywordFiles.ContainsKey(keyword))
{
title = GetTitleFission(MaxTitleLen, keyword, KeywordFiles[keyword]);
}
bool had = false;
for (int n = 0; n < Product.Count; n++)
{
if (Product[n].Titlte == title || Product[n].FromId == pid)
{
had = true;
break;
}
}
if (had)
{
LogSave.Add(Name, "标题已经存在:" + title);
continue;
}
if (pidCount >= MaxSingleKeywordProduct)
break;
pidCount++;
for (int n = 1; KeywordFiles.ContainsKey(keyword) && n < FessionCount; n++)
{
title = GetTitleFission(MaxTitleLen, keyword, KeywordFiles[keyword]);
Threadx.ThreadListWait(ref ts, new Thread(new GetPinduoduoTitleCat(title, keyword, Name, sals, price, j, pid).Get), 20);
if (n == 1)
Thread.Sleep(5 * 1000);
else
Thread.Sleep(1 * 1000);
}
if (KeywordFiles.ContainsKey(keyword))
{
title = GetTitleFission(MaxTitleLen, keyword, KeywordFiles[keyword]);
}
Threadx.ThreadListWait(ref ts, new Thread(new GetPinduoduoTitleCat(title, keyword, Name, sals, price, j, pid).Get), 20);
if (!Running)
goto EndAll;
}
m = BaseRegex.GetMatchRegex(ref htmlxt, @"{[^>]+?}");
for (int k = 0; k < m.Count; k++)
{
while (Running && Waiting)
Thread.Sleep(100);
if (!Running)
return;
string list = m[k].Groups[0].Value;
//decimal free = BaseRegex.GetDecimalJson(ref list, "view_fee");
//if (free > 0 && OnlyFree)
// continue;
long pid = BaseRegex.GetLongJson(ref list, "item_id");
string title = BaseRegex.GetStringJson(ref list, "title");
int sals = BaseRegex.GetIntJson(ref list, "sold");
decimal price = BaseRegex.GetDecimalJson(ref list, "price");
if (price < MinPrice || price > MaxPrice || sals > MaxSale)
continue;
//long pid = BaseRegex.GetLongJson(ref list, "num_iid");
//string title = BaseRegex.GetStringJson(ref list, "title");
//int sals = BaseRegex.GetIntJson(ref list, "volume");
//decimal price = BaseRegex.GetDecimalJson(ref list, "zk_final_price");
if (string.IsNullOrEmpty(title) || title.Contains("天猫超市"))
{
LogSave.Add(Name, "过滤天猫超市");
continue;
}
if (sals > MaxSale)
continue;
if (sals < MinSale)
{
LogSave.Add(Name, "销量过低退出");
goto End;
}
bool hadNoKeyword = false;
foreach (string s in noKeyword)
{
if (string.IsNullOrEmpty(s))
continue;
if (title.Contains(s.Trim()))
{
title = title.Replace(s.Trim(), "");
//break;
}
}
if (hadNoKeyword)
continue;
title = AddTitleKeyword(title);
if (KeywordFiles.ContainsKey(keyword))
{
title = GetTitleFission(MaxTitleLen, keyword, KeywordFiles[keyword]);
}
bool had = false;
for (int n = 0; n < Product.Count; n++)
{
if (Product[n].Titlte == title || Product[n].FromId == pid)
{
had = true;
continue;
}
}
if (had)
{
LogSave.Add(Name, "标题已经存在:" + title);
continue;
}
if (pidCount >= MaxSingleKeywordProduct)
break;
pidCount++;
for (int n = 1; KeywordFiles.ContainsKey(keyword) && n < FessionCount; n++)
{
title = GetTitleFission(MaxTitleLen, keyword, KeywordFiles[keyword]);
Threadx.ThreadListWait(ref ts, new Thread(new GetPinduoduoTitleCat(title, keyword, Name, sals, price, j, pid).Get), 20);
if (n == 1)
Thread.Sleep(5 * 1000);
else
Thread.Sleep(1 * 1000);
}
if (KeywordFiles.ContainsKey(keyword))
{
title = GetTitleFission(MaxTitleLen, keyword, KeywordFiles[keyword]);
}
Threadx.ThreadListWait(ref ts, new Thread(new GetPinduoduoTitleCat(title, keyword, Name, sals, price, j, pid).Get), 20);
if (!Running)
goto EndAll;
}
LogSave.Add(Name, "关键词:" + keyword + " 分页:" + j);
if (AllCatAdd)
{
LogSave.Add(Name, "关键词:" + keyword + "数据已经加入到群控上货");
}
Thread.Sleep(1000);
ThisCount = i * PageTop + j;
}
for (int j = 1; TaoJiJi && j <= PageTop; j++)
{
if (keyword == "无")
break;
if (pidCount >= MaxSingleKeywordProduct)
break;
if (!CollectKeywordByTaobao.Running)
return;
string url = "https://api.tjjapp.com/Api2_24_0/goods/goodslist?nowpage=" + j + "&keyword=" + GetHtml.UrlEncodeUtf8(keyword) + "&minimum_price=" + MinPrice + "&highest_price=" + MaxPrice + "&timestamp=" + DateTime.Now.Ticks;
string html = GetHtmlUrl(url);
JObject json = JObject.Parse(html);
if (json["result"].ToString() == "1")
{
var goods = json["goods"];
if (goods != null)
{
foreach (var good in goods)
{
while (Running && Waiting)
Thread.Sleep(100);
if (!CollectKeywordByTaobao.Running)
return;
TaobaoProductBase t = new TaobaoProductBase();
t.FromId = good["goodsId"].ParseToLong();
if (t.FromId == 0)
{
continue;
}
if (Product.Where(p => p.FromId == t.FromId).Any())
{
continue;
}
t.FromType = "淘集集";
t.Keyword = keyword;
t.Price = good["crazyPrice"].ParseToDecimal();
t.SaleCount = good["saleNum"].ParseToString().Replace(",", string.Empty).Replace("人抢购", string.Empty).ParseToInt();
t.Titlte = good["goodsName"].ParseToString();
if (string.IsNullOrEmpty(t.Titlte))
{
continue;
}
t.Titlte = AddTitleKeyword(t.Titlte);
t.MainImage = MainImageCut;
if (noKeyword.Where(p => t.Title.Contains(p.ParseToString().Trim())).Any())
{
continue;
}
if (t.Price < MinPrice || t.Price > MaxPrice || t.SaleCount < MinSale || t.SaleCount > MaxSale)
{
continue;
}
if (pidCount >= MaxSingleKeywordProduct)
{
break;
}
pidCount++;
Threadx.ThreadListWait(ref ts, new Thread(new GetPinduoduoTitleCat(t.Titlte, keyword, Name, t.SaleCount, t.Price, j, t.FromId, t.FromType).Get), 20);
if (!Running)
goto EndAll;
}
}
if (string.IsNullOrEmpty(json["nextPage"].ParseToString()))
{
break;
}
}
Thread.Sleep(1000);
ThisCount = i * PageTop + j;
}
if (Alibaba)
{
SearchKeywordByAlibaba sa = new SearchKeywordByAlibaba(Name);
int webCursor = 0;
sa.GetKeyword(keyword, ref webCursor);
}
End:
int ii = 0;
}
Threadx.ThreadListWait(ref ts);
EndAll:
ThisCount = MaxCount;
Running = false;
}
public string GetSign(string h5Tk, string data, out long time)
{
string appkey = "12574478";
System.DateTime starttime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
DateTime datetime = DateTime.Now;
time = (long)(datetime - starttime).TotalMilliseconds; // 相差毫秒数
string code = h5Tk + "&" + time + "&" + appkey + "&" + data;
return Md5Base.GetMd5(code).ToLower();
}
public string GetHtmlUrl(string url)
{
try
{
string userAgen = "android Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI MLA-AL10 Build/HUAWEIMLA-AL10) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36 phh_android_version/3.23.0 phh_android_build/228842 phh_android_channel/qihu360";
WebClientx web = new WebClientx();
web.Headers.Add("Accept", "*/*");
web.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
web.Headers.Add("User-Agent", userAgen);
web.Headers.Add("Referer", url);
string h = web.Headers.ToString();
web.Proxy = null;
byte[] b = web.DownloadData(url);
return Encoding.UTF8.GetString(b);
}
catch (Exception e)
{
return "";
}
}
/// <summary>
/// 获取标题
/// </summary>
/// <param name="maxLen"></param>
/// <param name="mainKeyword"></param>
/// <param name="keys"></param>
/// <returns></returns>
public static string GetTitleFission(int maxLen, string mainKeyword, List<string> keys)
{
Thread.Sleep(1);
string temp = "";
for (int j = 0; j < 200 && BaseClass.GetCharLength(temp) < maxLen * 2; j++)
{
if (!temp.Contains(mainKeyword))
{
if (BaseClass.GetCharLength(temp) > maxLen * 2 - 12)
temp += mainKeyword;
else
{
if (new Random(Guid.NewGuid().GetHashCode()).Next(10) == 1)
temp += mainKeyword;
}
}
string k = keys[new Random(Guid.NewGuid().GetHashCode()).Next(keys.Count)];
if (k.Contains("\t"))
k = k.Substring(0, k.IndexOf("\t"));
if (temp.Contains(k))
continue;
temp += k;
}
//if (temp.Length > maxLen * 2)
temp = BaseClass.SubCharLength(temp, maxLen * 2);
return temp;
}
/// <summary>
/// 随机增加关键词到标题中
/// </summary>
/// <param name="title"></param>
/// <returns></returns>
public string AddTitleKeyword(string title)
{
if (string.IsNullOrEmpty(AddKeyword) || string.IsNullOrEmpty(title))
return title;
string[] l = AddKeyword.Split('\n');
for (int i = 0; i < 100 && title.Length < 55; i++)
{
string s = l[new Random(Guid.NewGuid().GetHashCode()).Next(l.Length)];
if (string.IsNullOrEmpty(s) || title.Contains(AddKeyword))
continue;
if (new Random(Guid.NewGuid().GetHashCode()).Next(0, 10) < 5)
{
title = s.Trim() + title;
}
else
{
title += s.Trim();
}
}
return title;
}
public static void GetKeywordsDir(string dir, string noKeyword)
{
KeywordFiles.Clear();
Dictionary<string, bool> noKey = new Dictionary<string, bool>();
noKey.Add("元", false);
noKey.Add("限购", false);
noKey.Add("限量", false);
noKey.Add("淘宝", false);
noKey.Add("天猫", false);
noKey.Add("官方", false);
noKey.Add("旗舰店", false);
noKey.Add("企业店", false);
noKey.Add("最", false);
noKey.Add("厂家", false);
string[] kk = noKeyword.Split('\n');
for (int i = 0; i < kk.Length; i++)
{
string ke = kk[i].Trim();
if (string.IsNullOrEmpty(ke) || noKey.ContainsKey(ke))
continue;
noKey.Add(ke, false);
}
string[] l = Directory.GetFiles(dir);
string canNotKeywords = "";
for (int i = 0; i < l.Length; i++)
{
string name = Path.GetFileNameWithoutExtension(l[i]);
List<string> keywords = new List<string>();
string[] ll = FileBase.ReadAllLines(l[i]);
if (ll == null || ll.Length < 50)
{
canNotKeywords += "、" + name;
continue;
}
for (int j = 0; j < ll.Length; j++)
{
if (string.IsNullOrEmpty(ll[j]))
continue;
string key = ll[j].Trim().Replace("\t", " ");
if (key.Contains(" "))
key = key.Substring(0, key.IndexOf(" "));
bool had = false;
foreach (var k in noKey)
{
if (key.Contains(k.Key))
{
had = true;
break;
}
}
if (had || keywords.Contains(key))
continue;
keywords.Add(key);
}
if (KeywordFiles.ContainsKey(name) || keywords.Count < 20)
continue;
KeywordFiles.Add(name, keywords);
}
if (!string.IsNullOrEmpty(canNotKeywords))
{
MessageBox.Show("文件描述词过少,跳过\r\n每个文件请至少添加50个描述词一行一个\r\n不符合条件关键词:\r\n" + canNotKeywords.Substring(1), "描述词过少");
}
}
}
public class GetPinduoduoTitleCat
{
public string title, keyword, Name;
public string fromType;
public int sals, j;
public decimal price;
public long pid;
public ProductTaoKeBase ke;
public ProductMeiLiZhe meiLiZheProduct;
public GetPinduoduoTitleCat(string t, string k, string n, int s, decimal p, int jj, long pi)
{
title = t;
keyword = k;
Name = n;
sals = s;
price = p;
j = jj;
pid = pi;
}
public GetPinduoduoTitleCat(string t, string k, string n, int s, decimal p, int jj, long pi, string fromType)
{
title = t;
keyword = k;
Name = n;
sals = s;
price = p;
j = jj;
pid = pi;
this.fromType = fromType;
}
public GetPinduoduoTitleCat(string t, string k, string n, int s, decimal p, int jj, long pi, ProductTaoKeBase taoke)
{
title = t;
keyword = k;
Name = n;
sals = s;
price = p;
j = jj;
pid = pi;
ke = taoke;
}
public GetPinduoduoTitleCat(string t, string k, string n, int s, decimal p, int jj, long pi, ProductMeiLiZhe meilizhe)
{
title = t;
keyword = k;
Name = n;
sals = s;
price = p;
j = jj;
pid = pi;
meiLiZheProduct = meilizhe;
}
public void Get()
{
FromTitleToCat f = new FromTitleToCat();
f.MainServer = true;
bool had = f.Get(title, pid, 1);
if (!had)
{
LogSave.Add(Name, "未找到分类:" + title);
return;
}
TaobaoProductBase p = new TaobaoProductBase();
int cat4;
int.TryParse(f.Cat4, out cat4);
p.FromId = pid;
p.SaleCount = sals;
p.Price = price;
p.Cat1 = int.Parse(f.Cat1);
p.Cat2 = int.Parse(f.Cat2);
p.Cat3 = int.Parse(f.Cat3);
p.Cat4 = cat4;
p.MainCatId = f.GetMainCatId(p.Cat1);
p.MainCatName = f.MainCatName;
p.MainImage = CollectKeywordByTaobao.MainImageCut;
p.Keyword = keyword;
p.Titlte = title;
if (string.IsNullOrEmpty(fromType))
{
p.FromType = "淘宝";
}
else
{
p.FromType = fromType;
}
if (ke != null)
{
p.Title = "";
p.TaoKeId = ke.TaoKeId;
p.TaoKeUrl = ke.TaoKeUrl;
p.toId = ke.toId;
p.taoBaoUrl = ke.taoBaoUrl;
p.quanHou = ke.quanHou;
p.yuanPrice = ke.yuanPrice;
p.xiaoLiang = ke.xiaoLiang;
p.yongJin = ke.yongJin;
p.youHuei = ke.youHuei;
}
if (meiLiZheProduct != null)
{
p.Title = meiLiZheProduct.ProductName;
p.MeiLiZheId = meiLiZheProduct.ProductId;
p.MeiLiZheUrl = meiLiZheProduct.Url;
p.yuanPrice = meiLiZheProduct.PriceStr;
p.quanHou = meiLiZheProduct.PriceWidthTicketStr;
p.youHuei = meiLiZheProduct.TicketPriceStr;
p.FromId = meiLiZheProduct.TaobaoId;
p.taoBaoUrl = meiLiZheProduct.SourceUrl;
p.xiaoLiang = meiLiZheProduct.SaleCount.ToString();
}
int id = cat4 > 0 ? p.Cat4 : p.Cat3;
int cid = PinduoduoCatList.GetNewCat3(id);
if (cid != id && PinduoduoCatList.CatIds.ContainsKey(cid))
{
var ci = PinduoduoCatList.CatIds[cid];
p.CatId = ci.id;
p.Cat1 = ci.cat1;
p.Cat2 = ci.cat2;
if (ci.cat3 == 0)
{
foreach (KeyValuePair<int, CatBase> c3 in PinduoduoCatList.CatIds)
{
if (c3.Value.cat2 == ci.cat2 && c3.Value.cat3 > 0)
{
ci.cat3 = c3.Value.cat3;
break;
}
}
}
p.Cat3 = ci.cat3;
if (ci.cat4 == 0 && ci.cat3 > 0)
{
foreach (KeyValuePair<int, CatBase> c4 in PinduoduoCatList.CatIds)
{
if (c4.Value.cat3 == ci.cat3 && c4.Value.cat4 > 0)
{
ci.cat4 = c4.Value.cat4;
break;
}
}
}
p.Cat4 = ci.cat4;
p.MainCatId = ci.main_cat_id;
}
p.MainCatId = f.GetMainCatId(p.Cat1);
p.MainCatName = f.MainCatName;
if (p.MainCatId == 0 || CollectKeywordByTaobao.MainCatId > 0 && CollectKeywordByTaobao.MainCatId != p.MainCatId)
{
return;
}
CollectKeywordByTaobao.Product.Add(p);
CollectKeywordByTaobao.ProductTemp.Add(p);
if (CollectKeywordByTaobao.AutoAdd)
{
UploadPinduoduoMain.ProductStock.Add(p);
UploadPinduoduoMain.AddToDataBase(p);
}
LogSave.Add(Name, "关键词:" + keyword + " 分页:" + j + " " + title);
}
}
public class ProductResurBase
{
public long FromId = 0;
public string FormShopName = "";
public long GoodId = 0;
public string ShopName = "";
public string Title = "";
public string Status = "";
}
public class PromPlan
{
public long planName = 0;
public string goodName = "";
public long goodId = 0;
public string shopName = "";
public string title = "";
public string status = "";
}
public class TaobaoProductBase
{
public ProductCollectBase pcb = null;
public string Keyword = "";
public long FromId = 0;
public long FromIdBak = 0;
public decimal Price = 0;
public int SaleCount = 0;
public int Pv = 0;
public string Titlte = "";
public int Cat1 = 0;
public int Cat2 = 0;
public int Cat3 = 0;
public int Cat4 = 0;
public int CatId = 0;
public int AddPrice = 0;
public int MainImage = 0;
public int MainCatId = 0;
public int UserTimes = 0;
public string UseShopName = "";
public string MainCatName = "";
public string reason = "";
public string country = "";
public string logisticsTime = "";
/// <summary>
/// 是否使用
/// </summary>
public bool Uped = false;
public bool Error = false;
/// <summary>
/// 是否生成
/// </summary>
public bool Build = false;
public bool OffLine = false;
/// <summary>
/// 按照分类匹配过了
/// </summary>
public bool CatSame = false;
public long PinId;
public long GoodsId;
public long GoodsSn;
public long MailId;
public string FromType = "";
public string Message = "";
public string Delete = "";
public string Arrange = "";
public bool Active = false;
public string ShopName = "";
public string AddTime = "";
public string UpTime = "";
public string guid = Guid.NewGuid().ToString();
public int favcnt = 0;
public int FindTimes = 0;
//大淘客
public string Title = "";
public long TaoKeId = 0;
public string TaoKeUrl = "";
public long toId = 0;
public string taoBaoUrl = "";
public string quanHou = "";
public string yuanPrice = "";
public string xiaoLiang = "";
public string yongJin = "";
public string youHuei = "";
// 美丽折
public string MeiLiZheId = "";
public string MeiLiZheUrl = "";
//多多进宝
public long jinbaoCount = 0;
public long jinbaoAmount = 0;
//虚假相关Id
/// <summary>
/// 4 京东 5苏宁 6微店
/// </summary>
public string ViGoodsName = "";
public long ViGoodsId = -1;
public string ViPrice = "";
public string ViPriceMaxMin = "";
public int ViPriceMin = 0;
public int ViPriceMax = 0;
public int ViExpressPrice = 0;
/// <summary>
/// 容错颠倒的上家ID,首字母为0:比如2661959050 ,formId=661959052
/// </summary>
public string TolerantFormId = "";
public string repeatTitltId = "";
public int FromSiteId
{
get
{
if (FromType == "拼多多" || FromType == "内部数据")
return 2;
if (FromType == "淘宝")
return 1;
if (FromType == "阿里巴巴")
return 3;
if (FromType == "淘集集")
return 4;
return 2;
}
}
public TaobaoProductBase Clone()
{
string html = JsonHelper.SerializeObject(this);
TaobaoProductBase tp = JsonHelper.DeserializeJsonToObject<TaobaoProductBase>(html);
return tp;
}
}
}