This repository has been archived by the owner on May 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormMain.cs
363 lines (332 loc) · 14.3 KB
/
FormMain.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Drawing;
using Newtonsoft.Json;
namespace Tiesto.Podcast
{
public partial class FormMain : Form
{
private static string PODCAST_LIST_URL = "http://tiestoapi-env-prod.elasticbeanstalk.com/podcasts?from=0&size=15";
private static string PODCAST_TRACK_URL = "http://tiestoapi-env-prod.elasticbeanstalk.com/podcast/$id/podcastTracks";
private static bool isListNotEmpty = false;
private LocalData localdata;
public FormMain()
{
InitializeComponent();
InitializeListView();
localdata = new LocalData();
this.Text = "Tiësto Clublife - Podcast Grabber v" + Application.ProductVersion.Substring(0, 3);
}
private void FormMain_Shown(object sender, EventArgs e)
{
if (File.Exists(localdata.PathIni))
{
Download.Folder = File.ReadAllText(localdata.PathIni);
textBox1.Text = Download.Folder;
}
else
{
Download.Folder = Application.StartupPath;
textBox1.Text = Download.Folder;
}
button2.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
checkBox1.Checked = localdata.SaveAsCue;
checkBox2.Checked = localdata.SaveAsTxt;
button1_Click(sender, e);
}
private void InitializeListView()
{
listView1.GridLines = true;
listView1.BackColor = Color.SkyBlue;
listView1.FullRowSelect = true;
listView1.Activation = ItemActivation.Standard;
listView1.View = View.Details;
listView1.Columns.Add("ID", 0, HorizontalAlignment.Left);
listView1.Columns.Add("Start", 50, HorizontalAlignment.Center);
listView1.Columns.Add("Artist - Title", 240, HorizontalAlignment.Left);
}
// refresh podcast list
private async void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = false;
button6.Enabled = false;
string json_data = await Tiesto.GetJsonData(PODCAST_LIST_URL);
if (string.IsNullOrWhiteSpace(json_data))
{
MessageBox.Show("Error tidak dapat mendapatkan data!\r\nCek koneksi internet anda...", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
button1.Enabled = true;
return;
}
File.WriteAllText(localdata.PodcastJson, json_data);
var json = JsonConvert.DeserializeObject<Tiesto.PodcastList>(json_data);
comboBox1.Enabled = false;
comboBox1.Items.Clear();
foreach (var pod in json.podcasts)
{
ComboboxItem item = new ComboboxItem();
item.Id = pod.podcast.id;
item.Title = Data.Normalize(pod.podcast.title);
item.Episode = pod.podcast.episodeNumber.ToString();
item.Duration = pod.podcast.duration;
item.Url = pod.podcast.mp4Url;
double drelease = Convert.ToDouble(pod.podcast.releaseDate.ToString().Substring(0, 10));
DateTime release = Data.UnixTimeStampToDateTime(drelease);
item.Release = release;
item.Year = release.Year.ToString();
comboBox1.Items.Add(item);
}
comboBox1.Enabled = true;
comboBox1.SelectedIndex = 0;
comboBox1.Focus();
button1.Enabled = true;
}
// load podcast info & tracklist
private async void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var data = (comboBox1.SelectedItem as ComboboxItem);
button2.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
label9.Text = data.Title;
label8.Text = data.Release.ToString();
label7.Text = string.Empty;
Download.FileName = data.Title + ".m4a";
Download.Id = data.Id;
if (data.Url.Contains("http"))
{
Download.Url = (comboBox1.SelectedItem as ComboboxItem).Url;
string json_data = await Tiesto.GetJsonData(PODCAST_TRACK_URL.Replace("$id", Download.Id));
if (string.IsNullOrWhiteSpace(json_data))
{
MessageBox.Show("Error tidak dapat mendapatkan data!\r\nCek koneksi internet anda...", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
return;
}
File.WriteAllText(localdata.TracklistJson, json_data);
var json = JsonConvert.DeserializeObject<Tiesto.Mix>(json_data);
listView1.Items.Clear();
int trackCount = 0;
foreach (var x in json.mixPodcastTracks)
{
foreach (var y in x.tracks)
{
trackCount++;
ListViewItem item1 = new ListViewItem(y.track.id);
var ts = TimeSpan.FromSeconds(y.track.starttime);
string durasi = string.Format("{0}:{1}", ts.Minutes.ToString("00"), ts.Seconds.ToString("00"));
item1.SubItems.Add(durasi);
item1.SubItems.Add(Data.Normalize(y.track.title));
listView1.Items.Add(item1);
}
}
if (trackCount == 0)
{
label7.Text = "0 Track";
isListNotEmpty = false;
}
else
{
label7.Text = $"{trackCount.ToString()} Tracks";
isListNotEmpty = true;
}
button2.Enabled = true;
button4.Enabled = isListNotEmpty && (checkBox1.Checked || checkBox2.Checked);
button5.Enabled = true;
button6.Enabled = File.Exists("wget.exe");
}
}
// idm
private void button2_Click(object sender, EventArgs e)
{
string idm = null;
if (File.Exists(localdata.CustomIdm))
{
idm = File.ReadAllText(localdata.CustomIdm);
}
else
{
string[] idmpath = {
@"C:\Program Files (x86)\Internet Download Manager\IDMan.exe",
@"C:\Program Files\Internet Download Manager\IDMan.exe",
@"IDMan.exe" };
foreach (string exe in idmpath)
{
if (File.Exists(exe)) idm = exe;
}
}
if (idm != null)
{
AskToSaveTrackList();
File.AppendAllText(localdata.HistoryLog, $"[{DateTime.Now}][IDM] {Download.FileName} - {Download.Url}\r\n");
ProcessStartInfo exec = new ProcessStartInfo();
exec.FileName = idm;
exec.Arguments = $"/d \"{Download.Url}\" /p \"{Download.Folder}\" /f \"{Download.FileName}\" /n";
Process.Start(exec);
}
else
{
MessageBox.Show("IDM tidak ditemukan! Silahkan pindah app ini ke dalam folder IDM dan jalankan kembali.",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// save folder
private void button3_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowser.ShowDialog();
if (result == DialogResult.OK)
{
Download.Folder = folderBrowser.SelectedPath;
textBox1.Text = Download.Folder;
File.WriteAllText(localdata.PathIni, Download.Folder);
}
}
// save tracklist
private void button4_Click(object sender, EventArgs e)
{
bool result = false;
if (checkBox1.Checked)
{
SaveAsCue();
result = true;
}
if (checkBox2.Checked)
{
SaveAsTxt();
result = true;
}
if (result) MessageBox.Show("Track list berhasil disimpan.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
// copy clipboard
private void button5_Click(object sender, EventArgs e)
{
if (Download.Url != null)
{
Clipboard.SetText(Download.Url);
MessageBox.Show("Link sudah dicopy ke clipboard.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
// wget
private void button6_Click(object sender, EventArgs e)
{
if (File.Exists("wget.exe"))
{
AskToSaveTrackList();
File.AppendAllText(localdata.HistoryLog, $"[{DateTime.Now}][WGET] {Download.FileName} - {Download.Url}\r\n");
string pathSave = Path.Combine(Download.Folder, Download.FileName);
string pathTemp = Path.Combine(Download.Folder, "temp-podcast.wget");
string wgetArgs = $"--output-document=\"{pathTemp}\" --tries=0 --continue";
if (Download.Url.ToLower().StartsWith("https"))
wgetArgs += " --no-check-certificate";
ProcessStartInfo exec = new ProcessStartInfo();
exec.FileName = "wget.exe";
exec.Arguments = wgetArgs + $" \"{Download.Url}\"";
this.Hide();
Process proc = Process.Start(exec);
proc.WaitForExit();
int exitCode = proc.ExitCode;
string msgCode = "Proses download dibatalkan karena sesuatu.";
switch (exitCode)
{
case 0:
try
{
File.Move(pathTemp, pathSave);
msgCode = "Download Complete!\nSaved as " + Path.GetFileName(pathSave);
}
catch (Exception)
{
msgCode = "Download Complete!\nTapi file tidak bisa direname!\nTemp: "
+ Path.GetFileName(pathTemp) + "\nSave: "+ Path.GetFileName(pathSave);
}
break;
case 1:
msgCode = "Generic error code.";
break;
case 2:
msgCode = "Parse error — for instance, when parsing command-line options, the .wgetrc or .netrc…";
break;
case 3:
msgCode = "File I/O error.";
break;
case 4:
msgCode = "Network failure.";
break;
case 5:
msgCode = "SSL verification failure.";
break;
case 6:
msgCode = "Username/password authentication failure.";
break;
case 7:
msgCode = "Protocol errors.";
break;
case 8:
msgCode = "Server issued an error response.";
break;
}
MessageBox.Show(msgCode, "WGet Downloader", MessageBoxButtons.OK, (exitCode == 0) ? MessageBoxIcon.Information : MessageBoxIcon.Stop);
this.Show();
}
else
{
MessageBox.Show("wget.exe tidak ditemukan! Silahkan copy wget.exe ke dalam ini dan jalankan kembali.",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void AskToSaveTrackList()
{
string pathCue = Path.Combine(Download.Folder, Download.FileName.Replace("m4a", "cue"));
string pathTxt = Path.Combine(Download.Folder, Download.FileName.Replace("m4a", "txt"));
if (isListNotEmpty)
{
if ((checkBox1.Checked && !File.Exists(pathCue)) || (checkBox2.Checked && !File.Exists(pathTxt)))
{
DialogResult askToSave = MessageBox.Show("Simpan track list?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (askToSave == DialogResult.Yes)
{
if (checkBox1.Checked)
SaveAsCue();
if (checkBox2.Checked)
SaveAsTxt();
}
}
}
}
private void SaveAsTxt()
{
var data = (comboBox1.SelectedItem as ComboboxItem);
var json_data = File.ReadAllText(localdata.TracklistJson);
var json = JsonConvert.DeserializeObject<Tiesto.Mix>(json_data);
string savelist = Path.Combine(Download.Folder, Download.FileName.Replace("m4a", "txt"));
if (File.Exists(savelist))
File.Delete(savelist);
foreach (var x in json.mixPodcastTracks)
{
File.AppendAllText(savelist, $"== {Data.GetPerformer(Data.Normalize(x.artist))} ==\r\n");
foreach (var y in x.tracks)
{
var ts = TimeSpan.FromSeconds(y.track.starttime);
string durasi = $"{ts.Minutes.ToString("00")}:{ts.Seconds.ToString("00")}";
File.AppendAllText(savelist, $"[{durasi}] {Data.Normalize(y.track.title)}\r\n");
}
}
}
private void SaveAsCue()
{
var data = (comboBox1.SelectedItem as ComboboxItem);
var json_data = File.ReadAllText(localdata.TracklistJson);
Cue.Write(data, json_data);
}
private void checkbox_Changed(object sender, EventArgs e)
{
localdata.SaveAs(checkBox1.Checked, checkBox2.Checked);
button4.Enabled = isListNotEmpty && (checkBox1.Checked || checkBox2.Checked);
}
}
}