This commit is contained in:
lijianyou
2025-09-30 15:00:32 +08:00
parent 1b262f06b8
commit b4e759cf69
202 changed files with 22035 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
using dy.net.model;
using SqlSugar;
using System.Threading.Tasks;
namespace dy.net.service
{
public class CommonService
{
private readonly ISqlSugarClient sqlSugarClient;
public CommonService(ISqlSugarClient sqlSugarClient)
{
this.sqlSugarClient = sqlSugarClient;
}
public AppConfig GetConfig()
{
return sqlSugarClient.Queryable<AppConfig>().First();
}
public AppConfig InitConfig(AppConfig config)
{
var conf = GetConfig();
if (conf != null)
return conf;
else
{
var add = sqlSugarClient.Insertable<AppConfig>(config).ExecuteCommand();
return config;
}
}
public async Task<bool> UpdateConfig(AppConfig config) {
var cc = await sqlSugarClient.Queryable<AppConfig>().FirstAsync(x=>x.Id== config.Id);
if (cc == null)
return false;
else {
cc.Cron = config.Cron;
cc.BatchCount = config.BatchCount;
}
var update = await sqlSugarClient.Updateable<AppConfig>(cc).ExecuteCommandAsync();
return update > 0 ;
}
/// <summary>
/// 兼容旧版将之前同步了的我收藏的数据进行更新
/// </summary>
public void UpdateCollectViedoType()
{
var collectViedos= sqlSugarClient.Queryable<DyCollectVideo>().Where(x=>string.IsNullOrEmpty(x.ViedoType)).ToList();
if (collectViedos.Any())
{
collectViedos.ForEach(x => {
x.ViedoType = "2";
});
sqlSugarClient.Updateable(collectViedos).ExecuteCommand();
}
}
}
}
+90
View File
@@ -0,0 +1,90 @@
using dy.net.dto;
using dy.net.model;
using dy.net.repository;
using dy.net.utils;
using System.ComponentModel;
using System.Threading.Tasks;
namespace dy.net.service
{
public class DyCollectVideoService
{
private readonly DyCollectVideoRepository _dyCollectVideoRepository;
public DyCollectVideoService(DyCollectVideoRepository dyCollectVideoRepository)
{
_dyCollectVideoRepository = dyCollectVideoRepository;
}
public async Task<bool> batchInsert(List<DyCollectVideo> videos)
{
// 边界处理:如果传入的列表为空,直接返回成功(或根据业务返回false)
if (videos == null || !videos.Any())
return true;
// 1. 提取待插入的所有AwemeId(去重,减少数据库查询压力)
var newAwemeIds = videos.Select(x => x.AwemeId)
.Distinct()
.ToList();
// 2. 查询数据库中已存在的AwemeId(只查需要的字段,提高效率)
var existingAwemeIds = await _dyCollectVideoRepository
.Query(x => newAwemeIds.Contains(x.AwemeId)) // 使用Query方法构建查询
.Select(x => x.AwemeId) // 只获取AwemeId,减少数据传输
.ToListAsync();
// 3. 过滤出数据库中不存在的视频(只保留新记录)
var videosToInsert = videos
.Where(video => !existingAwemeIds.Contains(video.AwemeId))
.ToList();
// 4. 如果没有需要插入的新记录,直接返回成功
if (!videosToInsert.Any())
return true;
// 5. 批量插入过滤后的新记录
var insertedCount = await _dyCollectVideoRepository.InsertRangeAsync(videosToInsert);
// 返回是否插入成功(至少插入一条)
return insertedCount > 0;
}
public async Task<VideoStaticsDto> GetStatics()
{
List<DyCollectVideo> list = await this._dyCollectVideoRepository.GetAllAsync();
if (!list.Any())
return new VideoStaticsDto();
var Categories = list.GroupBy(x => x.Tag1).Select(x => new VideoStaticsItemDto { Name = x.Key, Count = x.LongCount() }).OrderByDescending(p=>p.Count).ToList();
Categories.Where(x => string.IsNullOrWhiteSpace(x.Name)).ToList().ForEach(x => x.Name = "其他");
var data = new VideoStaticsDto
{
AuthorCount = list.Select(x => x.AuthorId).Distinct().Count(),
CategoryCount=list.Select(x=>x.Tag1).Distinct().Count(),
VideoCount = list.Count,
Categories= Categories,
FavoriteCount= list.Count(x => x.ViedoType=="1"),
CollectCount= list.Count(x => x.ViedoType=="2"),
ViedoSizeTotal = ByteToGbConverter.ConvertBytesToGb(list.Sum(x=>x.FileSize))
};
data.Authors = list.GroupBy(x => x.Author).Select(x => new VideoStaticsItemDto { Name = x.Key, Count = x.LongCount(), Icon=x.FirstOrDefault().AuthorAvatarUrl }).OrderByDescending(d=>d.Count).ToList();
return data;
}
//分页查询
public async Task<(List<DyCollectVideo> list, int totalCount)> GetPagedAsync(int pageIndex, int pageSize, string tag = null, string author = null,string viedoType=null,List<string>? dates=null)
{
return await _dyCollectVideoRepository.GetPagedAsync(pageIndex, pageSize, tag, author, viedoType, dates);
}
}
}
+70
View File
@@ -0,0 +1,70 @@
using dy.net.model;
using dy.net.repository;
using System.Linq.Expressions;
namespace dy.net.service
{
public class DyCookieService
{
private readonly DyCookieRepository _cookieRepository;
public DyCookieService(DyCookieRepository cookieRepository)
{
_cookieRepository = cookieRepository;
}
public Task<List<DyUserCookies>> GetAllCookies()
{
return _cookieRepository.GetAllCookies();
}
public async Task<bool> Add(DyUserCookies dyUserCookies)
{
return await _cookieRepository.InsertAsync(dyUserCookies);
}
public bool Init(DyUserCookies dyUserCookies)
{
var exist = _cookieRepository.GetFirst(x => x.Id == dyUserCookies.Id);
if(exist != null)
{
return false;
}
return _cookieRepository.Insert(dyUserCookies);
}
// 查询单个
public async Task<DyUserCookies> GetByIdAsync(string id)
{
return await _cookieRepository.GetByIdAsync(id);
}
// 查询列表(可加条件)
public async Task<(List<DyUserCookies> list, int totalCount)> GetPagedAsync(int pageIndex, int pageSize)
{
return await _cookieRepository.GetPagedAsync(pageIndex, pageSize);
}
// 更新
public async Task<bool> UpdateAsync(DyUserCookies dyUserCookies)
{
return await _cookieRepository.UpdateAsync(dyUserCookies);
}
// 删除(根据主键)
public async Task<bool> DeleteByIdAsync(string id)
{
return await _cookieRepository.DeleteByIdAsync(id);
}
// 批量删除
public async Task<int> DeleteByIdsAsync(IEnumerable<string> ids)
{
return await _cookieRepository.DeleteByIdsAsync(ids.Cast<object>());
}
}
}
+314
View File
@@ -0,0 +1,314 @@
using dy.net.dto;
using dy.net.utils;
using Newtonsoft.Json;
using System;
using System.Net;
using System.Net.Http;
namespace dy.net.service
{
public class DyHttpClientService
{
private static readonly string CollectApi = "https://www.douyin.com/aweme/v1/web/aweme/listcollection/";
private static readonly string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36";
// 随机数生成器(避免重复实例化,保证随机性)
private readonly IHttpClientFactory _clientFactory;
public DyHttpClientService(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
/// <summary>
/// 查询用户收藏的视频
/// </summary>
/// <param name="cursor"></param>
/// <param name="count"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public async Task<CollectVideoInfo> SyncCollectVideos(string cursor, string count, string cookie)
{
if (string.IsNullOrEmpty(cursor))
{
throw new ArgumentException($"“{nameof(cursor)}”不能为 null 或空。", nameof(cursor));
}
if (string.IsNullOrEmpty(count))
{
throw new ArgumentException($"“{nameof(count)}”不能为 null 或空。", nameof(count));
}
if (string.IsNullOrEmpty(cookie))
{
throw new ArgumentException($"“{nameof(cookie)}”不能为 null 或空。", nameof(cookie));
}
try
{
using var httpClient = _clientFactory.CreateClient("douyin");
if (httpClient.DefaultRequestHeaders.Contains("Cookie"))
{
httpClient.DefaultRequestHeaders.Remove("Cookie");
}
httpClient.DefaultRequestHeaders.Add("Cookie", cookie);
var dics = CreateUserCollectHeader();
dics.Add("cursor", cursor);
dics.Add("count", count);
try
{
var token = await TokenManager.GenRealMsTokenAsync();
dics.Add("msToken", token);
}
catch (Exception ex)
{
Serilog.Log.Error($"获取mstoken失败{ex.Message}");
}
var endPoint = BogusManager.XbModel2Endpoint(CollectApi, dics, UserAgent);
var respose = await httpClient.PostAsync(endPoint, null);
if (respose.IsSuccessStatusCode)
{
var data = await respose.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<CollectVideoInfo>(data);
}
else
{
Serilog.Log.Error($"SyncCollectVideos fail: {respose.StatusCode}");
return null;
}
}
catch (Exception ex)
{
Serilog.Log.Error($"SyncCollectVideos error: {ex.Message}");
return null;
}
}
/// <summary>
/// 查询用户喜欢的视频
/// </summary>
/// <param name="cursor"></param>
/// <param name="secUserId"></param>
/// <param name="cookie"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public async Task<CollectVideoInfo> SyncFavoriteVideos(string cursor, string secUserId, string cookie)
{
if (string.IsNullOrEmpty(cursor))
{
throw new ArgumentException($"“{nameof(cursor)}”不能为 null 或空。", nameof(cursor));
}
if (string.IsNullOrEmpty(secUserId))
{
throw new ArgumentException($"“{nameof(secUserId)}”不能为 null 或空。", nameof(secUserId));
}
if (string.IsNullOrEmpty(cookie))
{
throw new ArgumentException($"“{nameof(cookie)}”不能为 null 或空。", nameof(cookie));
}
try
{
using var httpClient = _clientFactory.CreateClient("douyinfav");
if (httpClient.DefaultRequestHeaders.Contains("Cookie"))
{
httpClient.DefaultRequestHeaders.Remove("Cookie");
}
httpClient.DefaultRequestHeaders.Add("Cookie", cookie);
var dics = CreateUserFavoriteParams();
dics.Add("max_cursor", cursor);
dics.Add("sec_user_id", secUserId);
// 构建请求URL
string baseUrl = "https://www.douyin.com/aweme/v1/web/aweme/favorite/";
var queryString = new FormUrlEncodedContent(dics);
string fullUrl = $"{baseUrl}?{await queryString.ReadAsStringAsync()}";
var respose = await httpClient.GetAsync(fullUrl);
if (respose.IsSuccessStatusCode)
{
var data = await respose.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<CollectVideoInfo>(data);
}
else
{
Serilog.Log.Error($"SyncFavoriteVideos fail: {respose.StatusCode}");
return null;
}
}
catch (Exception ex)
{
Serilog.Log.Error($"SyncFavoriteVideos error: {ex.Message}");
return null;
}
}
/// <summary>
/// 下载文件并保存到本地
/// </summary>
/// <param name="videoUrl">文件地址</param>
/// <param name="savePath">保存路径</param>
public async Task<bool> DownloadAsync(string videoUrl, string savePath, string cookie)
{
try
{
// 防止文件被占用,先删除已存在的文件
if (File.Exists(savePath))
{
File.Delete(savePath);
}
// 创建HTTP客户端(设置超时时间和请求头)
using (var httpClient = _clientFactory.CreateClient("douyin"))
{
if (httpClient.DefaultRequestHeaders.Contains("Cookie"))
{
httpClient.DefaultRequestHeaders.Remove("Cookie");
}
httpClient.DefaultRequestHeaders.Add("Cookie", cookie);
httpClient.Timeout = TimeSpan.FromMinutes(5); // 设置5分钟超时
// 获取视频流
using (var response = await httpClient.GetAsync(videoUrl, HttpCompletionOption.ResponseHeadersRead))
{
response.EnsureSuccessStatusCode(); // 确保请求成功
// 获取文件总大小(用于进度显示)
long? totalBytes = response.Content.Headers.ContentLength;
// 读取流并写入文件
using (var stream = await response.Content.ReadAsStreamAsync())
using (var fileStream = new FileStream(savePath, FileMode.CreateNew))
{
byte[] buffer = new byte[8192];
int bytesRead;
long totalRead = 0;
// 循环读取并写入
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fileStream.WriteAsync(buffer, 0, bytesRead);
totalRead += bytesRead;
// 显示下载进度
if (totalBytes.HasValue)
{
double progress = (double)totalRead / totalBytes.Value * 100;
//Console.Write($"\r下载进度:{progress:F2}% ({totalRead}/{totalBytes.Value} bytes)");
}
}
//Console.WriteLine(); // 进度显示结束后换行
}
}
}
return true;
}
catch (Exception ex)
{
Serilog.Log.Error($"DownloadVideoAsync fail: {ex.Message}");
Serilog.Log.Error($"DownloadVideoAsync fail: {ex.StackTrace}");
return false;
}
}
/// <summary>
/// 清理文件名中的非法字符
/// </summary>
static string SanitizeFileName(string fileName)
{
foreach (char c in Path.GetInvalidFileNameChars())
{
fileName = fileName.Replace(c, '_');
}
// 限制文件名长度
return fileName.Length > 100 ? fileName.Substring(0, 100) : fileName;
}
// 创建包含默认值的字典
Dictionary<string, string> CreateUserCollectHeader()
{
return new Dictionary<string, string>
{
{"device_platform", "webapp"},
{"aid", "6383"},
{"channel", "channel_pc_web"},
{"pc_client_type", "1"},
{"version_code", "290100"},
{"version_name", "29.1.0"},
{"cookie_enabled", "true"},
{"screen_width", "1920"},
{"screen_height", "1080"},
{"browser_language", "zh-CN"},
{"browser_platform", "Win32"},
{"browser_name", "Chrome"},
{"browser_version", "130.0.0.0"},
{"browser_online", "true"},
{"engine_name", "Blink"},
{"engine_version", "130.0.0.0"},
{"os_name", "Windows"},
{"os_version", "10"},
{"cpu_core_num", "12"},
{"device_memory", "8"},
{"platform", "PC"},
{"downlink", "10"},
{"effective_type", "4g"},
{"from_user_page", "1"},
{"locate_query", "false"},
{"need_time_list", "1"},
{"pc_libra_divert", "Windows"},
{"publish_video_strategy_type", "2"},
{"round_trip_time", "0"},
{"show_live_replay_strategy", "1"},
{"time_list_query", "0"},
{"whale_cut_token", ""},
{"update_version_code", "170400"}
};
}
Dictionary<string, string> CreateUserFavoriteParams()
{
var parameters = new Dictionary<string, string>
{
{"device_platform", "webapp"},
{"aid", "6383"},
{"channel", "channel_pc_web"},
{"min_cursor", "0"},
{"whale_cut_token", ""},
{"cut_version", "1"},
{"count", "18"},
{"publish_video_strategy_type", "2"},
{"update_version_code", "170400"},
{"pc_client_type", "1"},
{"pc_libra_divert", "Windows"},
{"support_h265", "1"},
{"support_dash", "1"},
{"cpu_core_num", "20"},
{"version_code", "170400"},
{"version_name", "17.4.0"},
{"cookie_enabled", "true"},
{"screen_width", "1536"},
{"screen_height", "960"},
{"browser_language", "zh-CN"},
{"browser_platform", "Win32"},
{"browser_name", "Chrome"},
{"browser_version", "140.0.0.0"},
{"browser_online", "true"},
{"engine_name", "Blink"},
{"engine_version", "140.0.0.0"},
{"os_name", "Windows"},
{"os_version", "10"},
{"device_memory", "8"},
{"platform", "PC"},
{"downlink", "10"},
{"effective_type", "4g"},
{"round_trip_time", "0"},
{"webid", "7516440221375268388"}
};
return parameters;
}
}
}
+138
View File
@@ -0,0 +1,138 @@
using Quartz;
using dy.net.job;
namespace dy.net.service
{
public class QuartzJobService
{
private readonly ISchedulerFactory _schedulerFactory;
public QuartzJobService(ISchedulerFactory schedulerFactory)
{
_schedulerFactory = schedulerFactory;
}
/// <summary>
///
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public async Task StartJob(string expression)
{
await StartCollectJob(expression);
await Task.Delay(10000);
//如果是数字则加1分钟,减少并发
if (int.TryParse(expression, out int cron)) {
cron++;
expression= cron.ToString();
}
await StartFavoriteJob(expression);
}
private async Task<bool> StartCollectJob(string expression)
{
try
{
var __scheduler1 = await _schedulerFactory.GetScheduler();
var jobKey = new JobKey("dy.job.key.collect", "group1");
var triggerKey = new TriggerKey("dy.trigger.key.collect", "group1");
var jobDetail = await __scheduler1.GetJobDetail(jobKey);
if (jobDetail != null)
{
//await __scheduler1.Shutdown();
await __scheduler1.DeleteJob(jobKey);//删掉原来的
}
IJobDetail job = JobBuilder.Create<DouYinCollectSyncJob>()
.WithIdentity(jobKey)
.Build();
ITrigger trigger;
//var expression = Appsettings.Get("Interval");
if (CronExpression.IsValidExpression(expression))
{
trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey)
.WithCronSchedule(expression)
.StartAt(DateTime.Now.AddSeconds(30))
.StartNow()
.Build();
}
else
{
int Interval = int.TryParse(expression, out int _Interval) ? _Interval : 30;
trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey)
//.StartNow()
.StartAt(DateTime.Now.AddSeconds(3))
.WithSimpleSchedule(x => x
.WithIntervalInMinutes(Interval)
.RepeatForever())
.Build();
}
// Tell Quartz to schedule the job using our trigger
await __scheduler1.ScheduleJob(job, trigger);
}
catch (Exception ex)
{
Serilog.Log.Error("start dy.collect job error", ex);
return false;
}
return true;
}
private async Task<bool> StartFavoriteJob(string expression)
{
try
{
var __scheduler1 = await _schedulerFactory.GetScheduler();
var jobKey = new JobKey("dy.job.key.favorite", "group1");
var triggerKey = new TriggerKey("dy.trigger.key.favorite", "group1");
var jobDetail = await __scheduler1.GetJobDetail(jobKey);
if (jobDetail != null)
{
//await __scheduler1.Shutdown();
await __scheduler1.DeleteJob(jobKey);//删掉原来的
}
IJobDetail job = JobBuilder.Create<DouYinFavoritSyncJob>()
.WithIdentity(jobKey)
.Build();
ITrigger trigger;
//var expression = Appsettings.Get("Interval");
if (CronExpression.IsValidExpression(expression))
{
trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey)
.WithCronSchedule(expression)
.StartAt(DateTime.Now.AddSeconds(30))
.StartNow()
.Build();
}
else
{
int Interval = int.TryParse(expression, out int _Interval) ? _Interval : 30;
trigger = TriggerBuilder.Create()
.WithIdentity(triggerKey)
//.StartNow()
.StartAt(DateTime.Now.AddSeconds(3))
.WithSimpleSchedule(x => x
.WithIntervalInMinutes(Interval)
.RepeatForever())
.Build();
}
// Tell Quartz to schedule the job using our trigger
await __scheduler1.ScheduleJob(job, trigger);
}
catch (Exception ex)
{
Serilog.Log.Error("start dy.favorite job error", ex);
return false;
}
return true;
}
}
}
+38
View File
@@ -0,0 +1,38 @@
using dy.net.dto;
using dy.net.model;
using dy.net.repository;
namespace dy.net.service
{
public class UserService
{
private readonly UserRepository _userRepository;
public UserService(UserRepository userRepository)
{
_userRepository = userRepository;
}
public async Task<(int code, string erro)> UpdatePwd(UpdatePwdRequest loginUser)
{
return await _userRepository.UpdatePwd(loginUser);
}
public async Task<LoginUserInfo> GetUser()
{
return await _userRepository.GetUser();
}
public async Task<bool> UpdateAvatar(string avatar)
{
return await _userRepository.UpdateAvatar(avatar);
}
public (int code, string erro) InitUser(LoginUserInfo userInfo)
{
return _userRepository.InitUser(userInfo);
}
}
}