Files
KJD/TEMUKJD/KJDForms/CollectForms/FriendShopCollectForm.cs
T

358 lines
21 KiB
C#

using AllBase;
using AllBase.Helper;
using AllBase.Model.Result;
using TemuKJD.KJDForms.BaseForms;
using TemuKJD.KJDForms.ToolForms;
using NaiBa.Util.Extension;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TemuKJD.KJDForms.CollectForms
{
public partial class FriendShopCollectForm : Form
{
List<DouGoodsCollectInfo> lstProduct = new List<DouGoodsCollectInfo>();
CancellationTokenSource cancellationTokenSource = null;
long productCountMin = 0;
long productCountMax = 99999999;
long shopSaleCountMin = 0;
long shopSaleCountMax = 99999999;
/// <summary>
/// 体验分数最小值
/// </summary>
decimal scoreMin = 0;
/// <summary>
/// 体验分数最大值
/// </summary>
decimal scoreMax = 100;
public FriendShopCollectForm()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="cookies"></param>
/// <param name="searchText">搜索关键字</param>
/// <param name="cursor">当前搜索的游标</param>
/// <param name="count">要搜索的总数</param>
/// <param name="sort">排序依据 0:综合 2:销量</param>
/// <returns></returns>
private string GetFriendShops(string cookies, string searchText, string cursor = "0", string count = "50", string sort = "0")
{
HttpWebResponse response = null;
string result;
try
{
string url = "https://lianmengapi-lf.snssdk.com/selection/search/shop?channel=App%20Store&slide_guide_has_shown=1&minor_status=0"
+ "&cursor=" + cursor + "&count=" + count + "&search_text=" + GetHtml.UrlEncodeUtf8(searchText) + "&request_tag_from=m&order_by=&sort=" + sort;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//https://lianmengapi-lf.snssdk.com/selection/search/shop?channel=App%20Store&slide_guide_has_shown=1&minor_status=0&cursor=0&count=50&search_text=%e8%bf%9e%e8%a1%a3%e8%a3%99&request_tag_from=m&order_by=&sort=0
//https://lianmengapi-lf.snssdk.com/selection/search/shop?channel=App%20Store&slide_guide_has_shown=1&minor_status=0&cursor=80&count=100&search_text=%E8%BF%9E%E8%A1%A3%E8%A3%99&request_tag_from=m&order_by=&sort=0");
//gfsitesid=MTI1NzAyNzU1NHwxNjI2MDc2NDI1NDN8fDQyOTIxMTA1Mjk2MDgzNzUHBwcHBwcH; gfsitesid=MTI1NzAyNzU1NHwxNjI2MDc2NDI1NDN8fDQyOTIxMTA1Mjk2MDgzNzUHBwcHBwcH; gfsitesid=MTI1NzAyNzU1NHwxNjI2MDc2NDI1NDN8fDQyOTIxMTA1Mjk2MDgzNzUHBwcHBwcH; gfsitesid=MTI1NzAyNzU1NHwxNjI2MDc2NDI1NDN8fDQyOTIxMTA1Mjk2MDgzNzUHBwcHBwcH; gftoken=MTI1NzAyNzU1NHwxNjI2MDc2NDI1NDN8fDAGBgYGBgY; Hm_lvt_b6520b076191ab4b36812da4c90f7a5e=1626076426; MONITOR_WEB_ID=ff46fe79-c1bd-4d03-b18d-39e78c59e3cf; passport_csrf_token_default=7565758120670bc073d791d6c2516599; passport_csrf_token=7565758120670bc073d791d6c2516599; ttcid=84ce6ad7a7c54817876bc354ce50e63998; ttwid=1%7C6M4WENwiVxCUI00v2KqThQe4VmQM34O0AXRdgU3S7ns%7C1626076427%7C6238c10c68cd01bf99c1a8fa834c4250f32a8b2ccc2adc1029ef451cab53cd60; n_mh=7LrrRmeUceh3vjPZWa4C3h7B_APxodsUr7zuYy75V90; sso_uid_tt=2ef4d6078ca6ad08e04251ff869d1454; sso_uid_tt_ss=2ef4d6078ca6ad08e04251ff869d1454; toutiao_sso_user=1f5ff878e13850a8a76d1c5002053dbb; toutiao_sso_user_ss=1f5ff878e13850a8a76d1c5002053dbb; odin_tt=fa6e9e56199abc6fe54781e77c689540450d6e2513913a8b02ae5b1253072129714f9118b1da0c09bfb9397d4dc5c318604bae278e4cf15e9845340f22d65e69; uidpasaddaehruigqreajf=0; tt_scid=rx5XfMYxzTA66ZKDdmoMcC4qrQQzNeZh-wTo-K3sfH8pDdVFgiS9pBpGCcVx2ORX5bdd; ucas_sso_c0_ss=CkEKBTEuMC4wEJ6Ihe7Jof31YBjmJiC9k9DP4oyUBCiwITC39dDb7fTPB0CT6q-HBkiTnuyJBlCOvKLE77ansWBYbhIU5OPHMBH5sjM45bXzqY54qXYF2OY; ucas_sso_c0=CkEKBTEuMC4wEJ6Ihe7Jof31YBjmJiC9k9DP4oyUBCiwITC39dDb7fTPB0CT6q-HBkiTnuyJBlCOvKLE77ansWBYbhIU5OPHMBH5sjM45bXzqY54qXYF2OY; ucas_c0_ss=CkEKBTEuMC4wEKSIhsLaof31YBjmJiC9k9DP4oyUBCiwITC39dDb7fTPB0CT6q-HBkiTnuyJBlCOvKLE77ansWBYbhIUpAxRIGvlmWnW3egoiY9Qkn_jA-M; sid_guard=0f3e1676339ab15de4697e624ff85b58%7C1626076435%7C5184000%7CFri%2C+10-Sep-2021+07%3A53%3A55+GMT; uid_tt=40ce5c67070509846fb296d5d132a38a; uid_tt_ss=40ce5c67070509846fb296d5d132a38a; sid_tt=0f3e1676339ab15de4697e624ff85b58; sessionid=0f3e1676339ab15de4697e624ff85b58; sessionid_ss=0f3e1676339ab15de4697e624ff85b58; ucas_c0=CkEKBTEuMC4wEKSIhsLaof31YBjmJiC9k9DP4oyUBCiwITC39dDb7fTPB0CT6q-HBkiTnuyJBlCOvKLE77ansWBYbhIUpAxRIGvlmWnW3egoiY9Qkn_jA-M; PHPSESSID=ff529a6f01eaf19e6a00245e676d1fa0; PHPSESSID_SS=ff529a6f01eaf19e6a00245e676d1fa0; need_choose_shop=0; Hm_lpvt_b6520b076191ab4b36812da4c90f7a5e=1626076436; login_info=%7B%22for_im_reply%22%3A%22ff529a6f01eaf19e6a00245e676d1fa0%22%7D
//passport_csrf_token_default=b45f8a5deab82f55ee0a9b2452c78a2d; passport_csrf_token=b45f8a5deab82f55ee0a9b2452c78a2d; n_mh=7LrrRmeUceh3vjPZWa4C3h7B_APxodsUr7zuYy75V90; sso_uid_tt=77d4f2eb5023c34a6a9a7cebb2b382db; sso_uid_tt_ss=77d4f2eb5023c34a6a9a7cebb2b382db; toutiao_sso_user=a884ccbcc49dad835d33cdc613c62438; toutiao_sso_user_ss=a884ccbcc49dad835d33cdc613c62438; odin_tt=ef6c903d84d840d6849ac89bba2da6f261fd3ae8b722c34f3e85ac154b69709e145deb2aef4fa2dbb777d1d6c53146dcc3ade71ffb17f315e410d911b0eff5db; ucas_sso_c0_ss=CkEKBTEuMC4wEKSIiN6mnor2YBjmJiC9k9DP4oyUBCiwITC39dDb7fTPB0CG0rCHBkiGhu2JBlCOvKLE77ansWBYbhIUtfYvQwb4NE052EdbcrL95yHsFrs; ucas_sso_c0=CkEKBTEuMC4wEKSIiN6mnor2YBjmJiC9k9DP4oyUBCiwITC39dDb7fTPB0CG0rCHBkiGhu2JBlCOvKLE77ansWBYbhIUtfYvQwb4NE052EdbcrL95yHsFrs; ucas_c0_ss=CkEKBTEuMC4wEKSIid6mnor2YBjmJiC9k9DP4oyUBCiwITC39dDb7fTPB0CG0rCHBkiGhu2JBlCOvKLE77ansWBYbhIU-APRWpBUAsOmIf4repethZ5coPw; sid_guard=630400240480860367e63fa726e7238f%7C1626089734%7C5184000%7CFri%2C+10-Sep-2021+11%3A35%3A34+GMT; uid_tt=1b1612b50857532656f4f623275adb40; uid_tt_ss=1b1612b50857532656f4f623275adb40; sid_tt=630400240480860367e63fa726e7238f; sessionid=630400240480860367e63fa726e7238f; sessionid_ss=630400240480860367e63fa726e7238f; ucas_c0=CkEKBTEuMC4wEKSIid6mnor2YBjmJiC9k9DP4oyUBCiwITC39dDb7fTPB0CG0rCHBkiGhu2JBlCOvKLE77ansWBYbhIU-APRWpBUAsOmIf4repethZ5coPw; PHPSESSID=840c94272b784072021cecf2aefccb30; PHPSESSID_SS=840c94272b784072021cecf2aefccb30; MONITOR_WEB_ID=e8b18a1d-c085-4692-bba2-3972f9dc16e7; buyin_shop_type=24; ttwid=1%7CuycbM01qMO-TgJUFec3jiZ_znHlSLKDYKGuzE_vWz90%7C1626242695%7C4bccbd38df4bd7ff3265ca69959cdc6a6dd7df547437ab4e891149ff37a464cf; SASID=SID2_3492329951615696327; BUYIN_SASID=SID2_3492329951615696327");
request.ContentType = "application/x-www-form-urlencoded";
//request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36";
request.Headers.Set(HttpRequestHeader.Cookie, cookies);
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
response = (HttpWebResponse)ex.Response;
result = GetProxyHtml.ReadResponse(response);
return result;
}
else return ex.Message;
}
catch (Exception e)
{
if (response != null) response.Close();
return e.Message;
}
result = GetProxyHtml.ReadResponse(response);
return result;
}
private string GetShopDetail(string productId)
{
HttpWebResponse response = null;
string result;
try
{
string url = "http://ec.snssdk.com/product/ajaxitem?b_type_new=0&device_id=0&is_outside=1&id=" + productId + "&abParams=0";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = "https://haohuo.jinritemai.com/";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.509";
request.Headers.Add("origin", @"https://haohuo.jinritemai.com");
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
response = (HttpWebResponse)ex.Response;
result = GetProxyHtml.ReadResponse(response);
return result;
}
else return ex.Message;
}
catch (Exception e)
{
if (response != null) response.Close();
return e.Message;
}
result = GetProxyHtml.ReadResponse(response);
return result;
}
private void btnCollect_Click(object sender, EventArgs e)
{
if (btnCollect.Text != "开始采集")
{
cancellationTokenSource.Cancel();
return;
}
var cookieShop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.LoginStatus == "登录成功");
if (cookieShop == null)
{
BaseForms.BaseMsgForm.ShowError("请登录店铺账号后搜索");
return;
}
var searchTextArr = this.txtKeyWords.Text.Split(new string[] { "\r", "\n", ",", " " }, StringSplitOptions.RemoveEmptyEntries);
if (searchTextArr.Length == 0)
{
BaseForms.BaseMsgForm.ShowError("请至少输入一个关键词");
return;
}
productCountMin = txtProductCountMin.Text.ParseToLong();
productCountMax = txtProductCountMax.Text.ParseToLong();
shopSaleCountMin = txtShopSalesMin.Text.ParseToLong();
shopSaleCountMax = txtShopSalesMax.Text.ParseToLong();
scoreMin = txtScoreRateMin.Text.ParseToDecimal();
scoreMax = txtScoreRateMax.Text.ParseToDecimal();
int totalPage = txtTotalPage.Text.ParseToInt();
lstProduct.Clear();
this.dgvCollect.Rows.Clear();
var sortType = cmbSort.SelectedIndex <= 0 ? "0" : "2";
cancellationTokenSource = new CancellationTokenSource();
int n = 0;
int total = searchTextArr.Count() * totalPage;
this.statusBar.lblTaskStatus.Text = "开始采集...";
this.statusBar.pgbTaskProgress.Value = n;
this.statusBar.pgbTaskProgress.Text = n + "/" + total;
this.statusBar.pgbTaskProgress.Maximum = total;
btnCollect.Text = "停止采集";
Task.Run(() =>
{
foreach (var searchText in searchTextArr)
{
if (cancellationTokenSource.IsCancellationRequested)
break;
var exportFileName = "采集导出\\抖音同行采集" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv";
try
{
if (!Directory.Exists("采集导出"))
Directory.CreateDirectory("采集导出");
StringBuilder sb = new StringBuilder();
sb.AppendLine("关键词,店铺编号,店铺名称,体验分数,店铺销量,店铺商品总数,商品链接");
File.AppendAllText(exportFileName, sb.ToString(), Encoding.Default);
}
catch (Exception ex) { }
for (int page = 0; page < totalPage; page++)
{
if (cancellationTokenSource.IsCancellationRequested)
break;
n++;
Invoke(new Action(() =>
{
this.statusBar.lblTaskStatus.Text = "正在采集同行商品:" + searchText + ",当前采集到第 " + (page + 1) + " 页";
this.statusBar.pgbTaskProgress.Value = n;
this.statusBar.pgbTaskProgress.Text = n + "/" + total;
this.statusBar.pgbTaskProgress.Maximum = total;
}));
//var friendProductResult = GetFriendShops(cookieShop.Cookies, searchText, (page * 10).ToString(), (totalPage * 10).ToString(), sortType);
var friendProductResult = GetFriendShops(GlobalConfig.DouUserCookies, searchText, (page * 10).ToString(), (totalPage * 10).ToString(), sortType);
if (friendProductResult.Contains("用户未登录"))
{
Invoke(new Action(() =>
{
ToolForms.DouUserLoginForm dForm = new ToolForms.DouUserLoginForm();
dForm.ShowDialog();
}));
}
friendProductResult = GetFriendShops(GlobalConfig.DouUserCookies, searchText, (page * 10).ToString(), (totalPage * 10).ToString(), sortType);
if (friendProductResult.Contains("用户未登录"))
{
BaseMsgForm.ShowError("用户登录失败,请登录抖音账号后再试!");
break;
}
else if (!string.IsNullOrEmpty(friendProductResult) && friendProductResult.Contains("\"status_code\":0"))
{
JObject jObj = JObject.Parse(friendProductResult);
if (jObj["shops"] == null)
continue;
foreach (var shop in jObj["shops"])
{
if (cancellationTokenSource.IsCancellationRequested)
break;
var shopStr = JsonHelper.SerializeObject(shop);
DouGoodsCollectInfo good = new DouGoodsCollectInfo();
good.Keyword = searchText;
good.Id = BaseRegex.GetStringJson(ref shopStr, "product_id");
good.ShopName = BaseRegex.GetStringJson(ref shopStr, "name");
//good.ShopId = BaseRegex.GetStringJson(ref shopStr, "shop_id");
good.CommentsRate = BaseRegex.GetStringJson(ref shopStr, "score").ParseToDouble();
good.Sales = BaseRegex.GetStringJson(ref shopStr, "sales");
// 筛选条件
if (good.Sales.ParseToLong() < this.shopSaleCountMin || good.Sales.ParseToLong() > this.shopSaleCountMax
|| good.CommentsRate.ParseToDecimal() < scoreMin || good.CommentsRate.ParseToDecimal() > scoreMax)
{
continue;
}
Thread.Sleep(Randomx.GetId(500, 1000));
string detailResult = GetShopDetail(good.Id);
if (string.IsNullOrEmpty(detailResult) || !detailResult.Contains("\"st\":0"))
{
Thread.Sleep(Randomx.GetId(500, 1000));
detailResult = GetShopDetail(good.Id);
}
if (!string.IsNullOrEmpty(detailResult) && detailResult.Contains("\"st\":0"))
{
good.ShopProductCount = BaseRegex.GetStringJson(ref detailResult, "shop_product_count").ParseToInt();
// 筛选条件
if (good.ShopProductCount < this.productCountMin || good.ShopProductCount > this.productCountMax)
{
continue;
}
}
// 获取可用于店铺查询的店铺ID
Thread.Sleep(Randomx.GetId(300, 600));
string shopResult = Work.BLL.Collect.HaoWuCollectBLL.getHtml("https://ec.snssdk.com/product/fxgajaxstaticitem?" +
"b_type_new=0&device_id=0&is_outside=1&id=" + good.Id + "&preview=0", "", "utf8");
if (!string.IsNullOrEmpty(shopResult))
good.ShopId = BaseRegex.GetStringJson(ref shopResult, "shop_id");
try
{
this.lstProduct.Add(good);
StringBuilder sb = new StringBuilder();
sb.AppendLine(good.Keyword + "," + good.ShopId + ","
+ good.ShopName + "," + good.CommentsRate + "," + good.Sales + "," + good.ShopProductCount + ","
+ "https://haohuo.jinritemai.com/views/product/item2?id=" + good.Id);
File.AppendAllText(exportFileName, sb.ToString(), Encoding.Default);
}
catch (Exception ex)
{
}
Invoke(new Action(() =>
{
var index = this.dgvCollect.Rows.Add(false, good.Keyword, good.ShopId, good.ShopName, good.CommentsRate, good.Sales,
good.ShopProductCount, "https://haohuo.jinritemai.com/views/product/item2?id=" + good.Id, "删除");
}));
}
}
}
}
Invoke(new Action(() =>
{
btnCollect.Text = "开始采集";
this.statusBar.lblTaskStatus.Text = "采集结束,共采集到商品" + this.lstProduct.Count + "个";
this.statusBar.pgbTaskProgress.Value = n;
this.statusBar.pgbTaskProgress.Text = n + "/" + total;
this.statusBar.pgbTaskProgress.Maximum = total;
}));
}, cancellationTokenSource.Token);
}
private void btnLoginDouyin_Click(object sender, EventArgs e)
{
DouUserLoginForm dForm = new DouUserLoginForm();
dForm.ShowDialog();
}
private void FriendShopCollectForm_Load(object sender, EventArgs e)
{
this.cmbSort.SelectedIndex = 0;
}
private void dgvCollect_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.ColumnIndex < 0)
return;
var goodsId = dgvCollect.Rows[e.RowIndex].Cells["colProductId"].Value.ToString();
var columnName = dgvCollect.Columns[e.ColumnIndex].Name;
if (columnName == "colProductId")
{
string url = goodsId;
GetHtml.OpenUrl(url);
}
}
private void btnExport_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "表格文件(*.csv)|*.csv";
saveFileDialog1.FileName = "抖音同行采集" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("关键词,店铺编号,店铺名称,体验分数,店铺销量,店铺商品总数,商品链接");
foreach (var goodsInfo in lstProduct)
{
sb.AppendLine(goodsInfo.Keyword + "," + goodsInfo.ShopId + ","
+ goodsInfo.ShopName + "," + goodsInfo.CommentsRate + "," + goodsInfo.Sales + "," + goodsInfo.ShopProductCount + ","
+ "https://haohuo.jinritemai.com/views/product/item2?id=" + goodsInfo.Id);
}
File.WriteAllText(saveFileDialog1.FileName, sb.ToString(), Encoding.Default);
BaseForms.BaseMsgForm.ShowInfo("导出成功");
}
}
catch (Exception ex)
{
BaseForms.BaseMsgForm.ShowError(ex.Message);
}
}
}
}