forked from mesutde/Delphi-Youtube-Downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmUpdateControl.pas
More file actions
212 lines (181 loc) · 6.06 KB
/
frmUpdateControl.pas
File metadata and controls
212 lines (181 loc) · 6.06 KB
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
unit frmUpdateControl;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, YTDownloader,
Vcl.StdCtrls, ShellAPI,IdHTTP,System.Net.HttpClient, System.Net.URLClient,WinInet;
type
TfrmUpdate = class(TForm)
Panel1: TPanel;
pnlUpdateButton: TPanel;
btnUpdateControl: TButton;
btnUpdate: TButton;
grpUpdate: TGroupBox;
lblLocalYtdlp: TLabel;
lblUpdateYtdlp: TLabel;
lnkYTDLPCurrentDownloadUrl: TLinkLabel;
procedure btnUpdateControlClick(Sender: TObject);
function GetYTDLPVersion(const ExePath: string): string;
procedure btnUpdateClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmUpdate: TfrmUpdate;
implementation
{$R *.dfm}
function TfrmUpdate.GetYTDLPVersion(const ExePath: string): string;
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
SecurityAttrs: TSecurityAttributes;
hReadPipe, hWritePipe: THandle;
Buffer: array [0 .. 4096] of Byte;
BytesRead: DWORD;
WorkingDir: string;
TempStr: UTF8String;
begin
Result := '';
FillChar(SecurityAttrs, SizeOf(SecurityAttrs), 0);
SecurityAttrs.nLength := SizeOf(SecurityAttrs);
SecurityAttrs.bInheritHandle := True;
if not CreatePipe(hReadPipe, hWritePipe, @SecurityAttrs, 0) then
Exit;
try
FillChar(StartInfo, SizeOf(StartInfo), 0);
StartInfo.cb := SizeOf(StartInfo);
StartInfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
StartInfo.wShowWindow := SW_HIDE;
StartInfo.hStdOutput := hWritePipe;
StartInfo.hStdError := hWritePipe;
WorkingDir := ExtractFilePath(ExePath);
if CreateProcess(nil, PChar(ExePath + ' --version'), nil, nil, True,
CREATE_NO_WINDOW, nil, PChar(WorkingDir), StartInfo, ProcInfo) then
begin
try
WaitForSingleObject(ProcInfo.hProcess, 5000);
if ReadFile(hReadPipe, Buffer, SizeOf(Buffer) - 1, BytesRead, nil) then
begin
// UTF-8 dizisini stringe çevir ve temizle
SetLength(TempStr, BytesRead);
Move(Buffer[0], TempStr[1], BytesRead);
// Yalnýzca rakamlarý, nokta ve tire karakterlerini tut
Result := '';
for var i := 1 to Length(TempStr) do
begin
if (TempStr[i] in ['0' .. '9', '.', '-']) then
Result := Result + Char(TempStr[i]);
end;
Result := Trim(Result);
end;
finally
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
end
else
begin
Result := 'Versiyon alýnamadý: ' + IntToStr(GetLastError);
end;
finally
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
end;
end;
procedure ShellOpen(const Url: string; const Params: string = '');
begin
ShellExecute(0, 'Open', PChar(Url), PChar(Params), nil, SW_SHOWNORMAL);
end;
function DownloadFile(const Url, Destination: string): Boolean;
var
hSession, hConnect: HINTERNET;
Buffer: array[0..1023] of Byte;
BytesRead: DWORD;
FileStream: TFileStream;
begin
Result := False;
hSession := InternetOpen('DelphiApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(hSession) then
try
hConnect := InternetOpenUrl(hSession, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
if Assigned(hConnect) then
try
FileStream := TFileStream.Create(Destination, fmCreate);
try
repeat
InternetReadFile(hConnect, @Buffer, SizeOf(Buffer), BytesRead);
if BytesRead > 0 then
FileStream.Write(Buffer, BytesRead);
until BytesRead = 0;
Result := True;
finally
FileStream.Free;
end;
finally
InternetCloseHandle(hConnect);
end;
finally
InternetCloseHandle(hSession);
end;
end;
procedure TfrmUpdate.btnUpdateClick(Sender: TObject);
var
DownloaderUrl, ToolsPath, OldYtDlpPath, NewYtDlpPath: string;
DownloadSuccess: Boolean;
YTDownloader: TYTDownloader;
begin
// Ýndirme linkini alýn
DownloaderUrl := YTDownloader.GetNewUrl;
ShowMessage(DownloaderUrl);
ShowMessage(IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName) + 'Tools'));
// Uygulamanýn bulunduðu Tools klasörü
ToolsPath := IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName) + 'Tools');
OldYtDlpPath := ToolsPath + 'yt-dlp.exe';
NewYtDlpPath := ToolsPath + 'yt-dlp_new.exe';
// Tools klasörünün mevcut olup olmadýðýný kontrol et
if not DirectoryExists(ToolsPath) then
begin
ShowMessage('Tools klasörü bulunamadý: ' + ToolsPath);
Exit;
end;
try
// Yeni yt-dlp.exe'yi Tools klasörüne indirme
DownloadSuccess := DownloadFile(DownloaderUrl, NewYtDlpPath);
if DownloadSuccess then
begin
// Eski yt-dlp.exe'nin yedeðini oluþturun
if FileExists(OldYtDlpPath) then
begin
DeleteFile(OldYtDlpPath); // Eski dosyayý sil
end;
// Yeni dosyayý eski dosyanýn yerine taþý
RenameFile(NewYtDlpPath, OldYtDlpPath);
ShowMessage('Yeni yt-dlp.exe baþarýyla güncellendi.');
end
else
begin
ShowMessage('Yeni yt-dlp.exe indirilirken bir hata oluþtu.');
if FileExists(NewYtDlpPath) then
DeleteFile(NewYtDlpPath); // Geçici indirmeyi sil
end;
except
on E: Exception do
ShowMessage('Bir hata oluþtu: ' + E.Message);
end;
end;
procedure TfrmUpdate.btnUpdateControlClick(Sender: TObject);
var
LocalYTDLPVersion, CurrentYTDLPVersion: String;
YTDownloader: TYTDownloader;
begin
LocalYTDLPVersion := GetYTDLPVersion('Tools\yt-dlp.exe');
CurrentYTDLPVersion := YTDownloader.GetReleaseVersion2
('https://github.com/yt-dlp/yt-dlp/releases');
lblLocalYtdlp.Caption := 'Sisteminizdeki Sürüm : ' + LocalYTDLPVersion;
lblUpdateYtdlp.Caption := 'Release Sürüm : ' + CurrentYTDLPVersion;
lnkYTDLPCurrentDownloadUrl.Caption := YTDownloader.GetNewUrl;
end;
end.