Files
douyin/service/DouyinMigrationSourceResolver.cs
T

76 lines
3.6 KiB
C#

using dy.net.model.dto;
using dy.net.model.entity;
using dy.net.model.response;
using SqlSugar;
namespace dy.net.service
{
public class DouyinMigrationSourceResolver
{
private readonly DouyinHttpClientService _http;
private readonly ISqlSugarClient _db;
public DouyinMigrationSourceResolver(DouyinHttpClientService http, ISqlSugarClient db)
{
_http = http;
_db = db;
}
public async Task<IReadOnlyList<string>> ResolveAsync(DouyinVideo video, DouyinCookie cookie, CancellationToken cancellationToken)
{
if (video == null || cookie == null || string.IsNullOrWhiteSpace(cookie.Cookies)) return Array.Empty<string>();
var category = string.IsNullOrWhiteSpace(video.CateId)
? null
: await _db.Queryable<DouyinCollectCate>().InSingleAsync(video.CateId);
var followed = video.ViedoType == VideoTypeEnum.dy_follows
? await _db.Queryable<DouyinFollowed>().Where(x => x.UperId == video.AuthorId).FirstAsync()
: null;
var cursor = "0";
for (var page = 0; page < 500 && !cancellationToken.IsCancellationRequested; page++)
{
var response = await FetchAsync(video, cookie, category, followed, cursor);
if (response?.StatusCode != 0 || response.AwemeList == null) return Array.Empty<string>();
var match = response.AwemeList.FirstOrDefault(x => x.AwemeId == video.AwemeId);
if (match != null) return ExtractUrls(match);
if (response.HasMore != 1) break;
var next = response.Cursor ?? response.MaxCursor;
if (string.IsNullOrWhiteSpace(next) || next == cursor) break;
cursor = next;
}
return Array.Empty<string>();
}
private async Task<DouyinVideoInfoResponse> FetchAsync(
DouyinVideo video,
DouyinCookie cookie,
DouyinCollectCate category,
DouyinFollowed followed,
string cursor) => video.ViedoType switch
{
VideoTypeEnum.dy_favorite when !string.IsNullOrWhiteSpace(cookie.SecUserId) =>
await _http.SyncFavoriteVideos("18", cursor, cookie.SecUserId, cookie.Cookies),
VideoTypeEnum.dy_follows when !string.IsNullOrWhiteSpace(followed?.SecUid) =>
await _http.SyncUpderPostVideos("18", cursor, followed.SecUid, cookie.Cookies),
VideoTypeEnum.dy_custom_collect when !string.IsNullOrWhiteSpace(category?.XId) =>
await _http.SyncCollectVideosByCollectId(cursor, "18", cookie.Cookies, category.XId),
VideoTypeEnum.dy_mix when !string.IsNullOrWhiteSpace(category?.XId ?? video.CateXId) =>
await _http.SyncMixViedosByMixId(cursor, "18", cookie.Cookies, category?.XId ?? video.CateXId),
VideoTypeEnum.dy_series when !string.IsNullOrWhiteSpace(category?.XId ?? video.CateXId) =>
await _http.SyncSeriesViedosByMSeriesId(cursor, "18", cookie.Cookies, category?.XId ?? video.CateXId),
_ => await _http.SyncCollectVideos(cursor, "18", cookie.Cookies)
};
private static IReadOnlyList<string> ExtractUrls(Aweme aweme)
{
return (aweme.Video?.BitRate ?? new List<VideoBitRate>())
.Where(x => x?.PlayAddr?.UrlList != null)
.OrderByDescending(x => x.BitRateValue)
.SelectMany(x => x.PlayAddr.UrlList)
.Where(x => !string.IsNullOrWhiteSpace(x))
.Distinct()
.ToArray();
}
}
}