-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.vb
166 lines (126 loc) · 5.23 KB
/
MainForm.vb
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
Imports System.Text.RegularExpressions
Imports System.ComponentModel
Public Class MainForm
Private Download As Boolean = False
Private PlaySound As Boolean = True
Private ProcessInfo As ProcessStartInfo
Private Process As Process
Private Delegate Sub InvokeWithString(ByVal Text As String)
Private Sub DownloadButton_Click(sender As Object, e As EventArgs) Handles DownloadButton.Click
DownloadVideo()
End Sub
Private Sub LinkTextBox_KeyUp(sender As Object, e As KeyEventArgs) Handles LinkTextBox.KeyUp
If e.KeyCode = Keys.Enter Then
DownloadVideo()
End If
End Sub
Private Sub LinkTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles LinkTextBox.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
End If
End Sub
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckUpdate()
End Sub
Private Sub UpdateButton_Click(sender As Object, e As EventArgs) Handles UpdateButton.Click
CheckUpdate()
End Sub
Private Sub DownloadVideo()
If LinkTextBox.Text = Nothing Then
MsgBox("網址欄位不能留空!", MsgBoxStyle.Exclamation, "警告")
LinkTextBox.Focus()
ElseIf Not Regex.IsMatch(LinkTextBox.Text, "http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?") Then
MsgBox("網址無效!", MsgBoxStyle.Critical, "錯誤")
LinkTextBox.Focus()
LinkTextBox.SelectAll()
Else
Download = True
PlaySound = True
DownloadButton.Enabled = False
LinkTextBox.Enabled = False
UpdateButton.Enabled = False
DownloadButton.Text = "正在下載影片"
YouTubeDL("--format ""best[ext=mp4]/best[ext=flv]/best"" " & LinkTextBox.Text)
End If
End Sub
Private Sub CheckUpdate()
LinkTextBox.Enabled = False
DownloadButton.Enabled = False
UpdateButton.Enabled = False
UpdateButton.Text = "正在檢查更新"
YouTubeDL("--update")
End Sub
Private Sub YouTubeDL(Arguments As String)
Try
Process.Kill()
Catch ex As Exception
End Try
OutputTextBox.Clear()
'Try
ProcessInfo = New ProcessStartInfo("youtube-dl.exe", Arguments)
Dim SystemEncoding As System.Text.Encoding
System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
With ProcessInfo
.UseShellExecute = False
.RedirectStandardError = True
.RedirectStandardOutput = True
.RedirectStandardInput = True
.CreateNoWindow = True
.StandardOutputEncoding = SystemEncoding
.StandardErrorEncoding = SystemEncoding
End With
Process = New Process With {.StartInfo = ProcessInfo, .EnableRaisingEvents = True}
AddHandler Process.ErrorDataReceived, AddressOf AsyncDataReceived
AddHandler Process.OutputDataReceived, AddressOf AsyncDataReceived
Process.Start()
Process.BeginOutputReadLine()
Process.BeginErrorReadLine()
'Catch ex As Win32Exception
'MsgBox(ex.Message, MsgBoxStyle.Critical, "錯誤")
'Environment.Exit(ex.NativeErrorCode)
'End Try
End Sub
Private Sub AsyncDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
Me.Invoke(New InvokeWithString(AddressOf SyncOutput), e.Data)
End Sub
Private Sub SyncOutput(ByVal Text As String)
OutputTextBox.AppendText(Text & Environment.NewLine)
OutputTextBox.ScrollToCaret()
StatusCheck(Text)
End Sub
Private Sub StatusCheck(ByVal Text As String)
Try
If Text.Contains("ERROR: Unsupported URL") Then
PlaySound = False
MsgBox("網址不支援!", MsgBoxStyle.Critical, "錯誤")
ElseIf Text.Contains("ERROR: Unable to download webpage") Then
PlaySound = False
MsgBox("無法下載影片!", MsgBoxStyle.Critical, "錯誤")
End If
Catch ex As Exception
End Try
If Process.HasExited Then
If Download Then
If PlaySound Then
My.Computer.Audio.Play(My.Resources.Finish, AudioPlayMode.Background)
End If
Download = False
End If
LinkTextBox.Enabled = True
DownloadButton.Enabled = True
UpdateButton.Enabled = True
DownloadButton.Text = "下載影片"
UpdateButton.Text = "檢查更新"
LinkTextBox.Focus()
LinkTextBox.SelectAll()
End If
End Sub
Private Sub AboutButton_Click(sender As Object, e As EventArgs) Handles AboutButton.Click
MsgBox("Program developed by Jason Kwok" & vbCrLf & _
"Source code available on GitHub" & vbCrLf & _
vbCrLf & _
"Special thanks:" & vbCrLf & _
"youtube-dl by Ricardo Garcia" & vbCrLf & _
"Show DOS Output by Anonymous", MsgBoxStyle.OkOnly, "關於「簡易影片下載器」")
End Sub
End Class