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
/
MainForm.cs
187 lines (166 loc) · 5.33 KB
/
MainForm.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using ShopeeReplayDownloader.JsonInterfaces;
using System.Text.RegularExpressions;
namespace ShopeeReplayDownloader;
public partial class MainForm : Form
{
ShopeeReplayDownloader downloader = new();
Dictionary<long, ShopeeRecordData> dictionary = new();
List<Uri> partitions = new();
public MainForm()
{
InitializeComponent();
SetStatusReady();
}
private void SetStatusReady()
{
statusLabel.Text = "Siap";
}
private void SetStatusBusy(string text)
{
statusLabel.Text = text;
}
private long? SelectedId => (long?)recordListBox.SelectedItem;
private ShopeeRecordData? SelectedRecord
{
get
{
var id = SelectedId;
if (id == null) return null;
return dictionary[(long)id];
}
}
private void OnSelectRecord(object sender, EventArgs e)
{
var obj = SelectedRecord;
if (obj == null) return;
partitions.Clear();
startNumeric.Value = 0;
endNumeric.Value = 0;
var formattedDuration = "";
var seconds = obj.ReplayInfo.EstimatedDuration / 1e3;
var minutes = double.Floor(seconds / 60);
seconds %= 60;
var hours = double.Floor(minutes / 60);
minutes %= 60;
if (hours > 0)
formattedDuration += $"{hours}jam ";
if (minutes > 0)
formattedDuration += $"{minutes}menit ";
if (seconds > 0)
formattedDuration += $"{seconds}detik ";
titleRichText.Text = obj.ReplayInfo.Title;
usernameLabel.Text = obj.ReplayInfo.Username;
durationLabel.Text = formattedDuration;
likesLabel.Text = obj.ReplayInfo.LikeCount.ToString();
viewsLabel.Text = obj.ReplayInfo.ViewCount.ToString();
directoryTextBox.Text = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyVideos),
$"Shopee\\{obj.ReplayInfo.SessionId}\\{obj.ReplayInfo.RecordId}"
);
}
private void OnSession(object sender, EventArgs e)
{
var button = (Button)sender;
SetStatusBusy("Membaca session...");
button.Enabled = false;
OnSessionAsync().ContinueWith(task => Invoke(delegate
{
button.Enabled = true;
SetStatusReady();
}));
}
private async Task OnSessionAsync()
{
var id = sessionTextBox.Text.Trim();
if (id.StartsWith("http"))
{
var groups = CaptureSessionRegex().Match(id).Groups;
id = groups[1].Value.Trim();
Invoke(delegate { sessionTextBox.Text = id; });
}
if (!ValidSessionRegex().IsMatch(id))
{
Invoke(delegate
{
MessageBox.Show(
"ID harus berupa numerik.",
"ID tidak valid!"
);
});
return;
}
dictionary = await downloader.GetRecordsFromSessionAsync(id);
Invoke(delegate
{
recordListBox.Items.Clear();
foreach (var key in dictionary.Keys)
{
recordListBox.Items.Add(key);
}
});
}
[GeneratedRegex("session(?:%3D|%3d|=)([\\d]+)(?:%26|&)?")]
private static partial Regex CaptureSessionRegex();
[GeneratedRegex("(\\d+)")]
private static partial Regex ValidSessionRegex();
private void OnPrepare(object sender, EventArgs e) { _ = OnPrepareAsync(); }
private async Task OnPrepareAsync()
{
var obj = SelectedRecord;
if (obj == null) return;
var path = directoryTextBox.Text;
Invoke(SetStatusBusy,"Mendownload playlist hls...");
Directory.CreateDirectory(path);
partitions = await downloader.DownloadHlsPlaylistAsync(obj, path);
Invoke(delegate
{
SetStatusReady();
startNumeric.Value = 0;
endNumeric.Value = partitions.Count;
});
}
private void OnDownload(object sender, EventArgs e)
{
var button = (Button)sender;
button.Enabled = false;
OnDownloadAsync().ContinueWith(
task => Invoke(delegate { button.Enabled = true; })
);
}
private async Task OnDownloadAsync()
{
Invoke(SetStatusBusy, "Mendownload partisi...");
await downloader.DownloadAllVideoPartitionsAsync(
partitions,
directoryTextBox.Text,
(int)startNumeric.Value,
(int)endNumeric.Value,
(current, length) => Invoke(
SetStatusBusy,
$"Mendownload partisi {current}/{length}..."
)
);
Invoke(SetStatusReady);
}
private void OnMerge(object sender, EventArgs e)
{
var button = (Button)sender;
button.Enabled = false;
OnMergeAsync().ContinueWith(
task => Invoke(delegate { button.Enabled = true; })
);
}
private async Task OnMergeAsync()
{
Invoke(SetStatusBusy, "Mengonversikan video...");
await downloader.ConvertPartitionsToMp4Async(
partitions,
directoryTextBox.Text,
(int)startNumeric.Value,
(int)endNumeric.Value,
(current, max) => Invoke(
SetStatusBusy,
$"Mengonversikan video {current}/{max}..."
));
}
}