This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShopeeReplayDownloader.cs
158 lines (147 loc) · 5.52 KB
/
ShopeeReplayDownloader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using RestSharp;
using RestSharp.Serializers.Json;
using ShopeeReplayDownloader.JsonInterfaces;
using System.Diagnostics;
namespace ShopeeReplayDownloader;
internal class ShopeeReplayDownloader : IDisposable
{
private readonly RestClient client = new(configureSerialization: e => e.UseSystemTextJson());
public void Dispose()
{
client.Dispose();
}
public async Task<long[]> GetRecordIdsFromSessionAsync(string session)
=> await GetRecordIdsFromSessionAsync(long.Parse(session));
public async Task<long[]> GetRecordIdsFromSessionAsync(long session)
{
var request = new RestRequest($"https://live.shopee.co.id/api/v1/replay?session_id={session}");
var response = await client.GetAsync<ShopeeSession>(request);
return response?.Data.RecordIds ?? Array.Empty<long>();
}
public async Task<Dictionary<long, ShopeeRecordData>> GetRecordsFromSessionAsync(string session)
=> await GetRecordsFromSessionAsync(long.Parse(session));
public async Task<Dictionary<long, ShopeeRecordData>> GetRecordsFromSessionAsync(long session)
{
var ids = await GetRecordIdsFromSessionAsync(session);
var map = new Dictionary<long, ShopeeRecordData>();
foreach (var id in ids)
{
map.Add(id, await GetRecordAsync(id));
}
return map;
}
public async Task<ShopeeRecordData> GetRecordAsync(string record)
=> await GetRecordAsync(long.Parse(record));
public async Task<ShopeeRecordData> GetRecordAsync(long record)
{
var request = new RestRequest($"https://live.shopee.co.id/api/v1/replay/{record}");
var response = await client.GetAsync<ShopeeRecord>(request);
return response!.Data;
}
public async Task<List<Uri>> DownloadHlsPlaylistAsync(
ShopeeRecordData data, string directoryPath)
{
var playlistPath = Path.Combine(directoryPath, "playlist.m3u8");
if (!File.Exists(playlistPath))
{
var request = new RestRequest(data.ReplayInfo.RecordUrl);
var buffer = await client.DownloadDataAsync(request);
await File.WriteAllBytesAsync(playlistPath, buffer!);
}
var partitions = new List<Uri>();
var lines = File.ReadAllLines(playlistPath);
foreach (var line in lines)
{
if (line.StartsWith("#")) continue;
var uri = new Uri(data.ReplayInfo.RecordUrl, line);
partitions.Add(uri);
}
return partitions;
}
private static async Task LoopThroughParitionsAsync(
List<Uri> partitions,
string directoryPath,
int? start,
int? end,
Func<Uri, string, int, int, int, Task> callback)
{
start ??= 0;
start = Math.Max((int)start, 0);
end ??= partitions.Count;
end = Math.Min((int)end, partitions.Count);
var length = (int)end - (int)start;
var startInt = (int)start;
var endInt = (int)end;
for (var i = startInt; i <= endInt; i++)
{
var uri = partitions[i];
var file = Path.Join(directoryPath, Path.GetFileName(uri.LocalPath));
await callback(uri, file, startInt, endInt, i);
}
}
public async Task DownloadAllVideoPartitionsAsync(
List<Uri> partitions,
string directoryPath,
int? start = null,
int? end = null,
Action<int, int>? onProgress = null)
{
await LoopThroughParitionsAsync(partitions, directoryPath, start, end,
async (uri, file, start, end, i) =>
{
if (File.Exists(file)) return;
var buffer = await client.DownloadDataAsync(
new RestRequest(uri)
);
await File.WriteAllBytesAsync(file, buffer!);
onProgress?.Invoke(i - start + 1, end - start);
});
}
public async Task ConvertPartitionsToMp4Async(
List<Uri> partitions,
string directoryPath,
int? start = null,
int? end = null,
Action<int, int>? onProgress = null)
{
var mp4File = Path.Join(directoryPath, $"__convert_{start}-{end}.mp4");
if (File.Exists(mp4File)) return;
var tsFile = Path.Join(directoryPath, $"__convert_{start}-{end}.ts");
if (!File.Exists(tsFile))
{
using var stream = new FileStream(tsFile, FileMode.Create);
await LoopThroughParitionsAsync(partitions, directoryPath, start, end,
async (uri, file, start, end, i) =>
{
onProgress?.Invoke(i - start, end - start);
using var current = new FileStream(file, FileMode.Open);
await current.CopyToAsync(stream);
});
}
var task = Task.Run(delegate
{
using var ffmpeg = new Process()
{
StartInfo = {
FileName = "ffmpeg.exe",
ArgumentList = {
"-i", tsFile,
"-c:a", "copy",
"-c:v", "copy",
mp4File
}
},
};
ffmpeg.Start();
ffmpeg.WaitForInputIdle();
ffmpeg.WaitForExit();
});
var tsSize = new FileInfo(tsFile).Length;
while (!task.IsCompleted)
{
var progress = (double)(new FileInfo(mp4File).Length / tsSize);
onProgress?.Invoke(Math.Min(95, (int)(progress * 95)), 100);
}
await task;
}
}