Skip to content

Commit 424cb5d

Browse files
committed
Used YouTube data service API for import
1 parent 8dff8e3 commit 424cb5d

8 files changed

+277
-104
lines changed

CSharpWpfYouTube.csproj

-5
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@
4141
<PackageReference Include="RestoreWindowPlace" Version="2.1.0" />
4242
</ItemGroup>
4343

44-
<ItemGroup>
45-
<Folder Include="Models\" />
46-
<Folder Include="Services\" />
47-
</ItemGroup>
48-
4944
<ItemGroup>
5045
<None Update="LICENSE">
5146
<CopyToOutputDirectory>Never</CopyToOutputDirectory>

MainViewModel.cs

+28-31
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@ public partial class MainViewModel : ObservableRecipient
1717

1818
private AppSettings _appSettings;
1919
private MainService _mainService;
20+
private YouTubeDataService _youtubeDataService;
2021
private List<VideoInfo> _videoInfoList;
2122

2223
public MainViewModel(string dbFilePath)
2324
{
2425
_appSettings = new AppSettings();
2526
_mainService = new MainService(dbFilePath);
27+
// Note: if this key doesn't work (e.g. expired), get your key at:
28+
// https://console.cloud.google.com/apis/api/youtube.googleapis.com/overview
29+
string youtubeDataApiKey = "AIzaSyCUV6j6vCUTD9W2aiTOV-6XkV0Yl8tjFiA";
30+
_youtubeDataService = new YouTubeDataService(youtubeDataApiKey);
2631
_videoInfoList = new List<VideoInfo>();
2732
InitializeWebView2();
2833

2934
Version ver = Environment.Version;
30-
AppTitle = $"{App.AppName} - by Peter Sun (.NET {ver.Major}.{ver.Minor}.{ver.Build} Runtime, WPF WebView2, CommunityToolkit.Mvvm, " +
35+
AppTitle = $"{App.AppName} - by Peter Sun (.NET {ver.Major}.{ver.Minor}.{ver.Build} runtime, WPF WebView2, CommunityToolkit.Mvvm, " +
3136
"EntityFrameworkCore.Sqlite, ModernWpfUI, RestoreWindowPlace)";
3237
#if DEBUG
3338
AppTitle += " - Debug";
@@ -39,7 +44,7 @@ public MainViewModel(string dbFilePath)
3944
public WebView2? WebView2Control { get; private set; }
4045
// Video address in the textbox (whenever navigated to)
4146
[ObservableProperty]
42-
string _currentVideoUri = string.Empty;
47+
string _currentVideoUrl = string.Empty;
4348
[ObservableProperty]
4449
ObservableCollection<string> _videoGroupList = new ObservableCollection<string>();
4550
[ObservableProperty]
@@ -167,18 +172,18 @@ partial void OnSelectedVideoInfoChanged(VideoInfo? value)
167172
{
168173
// Link is like https://www.youtube.com/watch?v=d_l-st8Q1S0,
169174
// https://www.youtube.com/results?search_query=....."
170-
_currentVideoUri = value.Link;
175+
_currentVideoUrl = value.Link;
171176
}
172177
else
173-
{
174-
_currentVideoUri = VideoInfo.YouTubeHomeUri;
178+
{
179+
_currentVideoUrl = VideoInfo.YouTubeHomeUri;
175180
}
176181

177182
// YouTube tab (go to the last navigated video uri)
178-
BindWebView2Control(_currentVideoUri);
179-
OnPropertyChanged(nameof(CurrentVideoUri));
183+
BindWebView2Control(_currentVideoUrl);
184+
OnPropertyChanged(nameof(CurrentVideoUrl));
180185

181-
_mainService.UpdateAppSetting(AppSettings.SelectedVideoLinkName, _currentVideoUri);
186+
_mainService.UpdateAppSetting(AppSettings.SelectedVideoLinkName, _currentVideoUrl);
182187

183188
if (value.Description.IsNotBlank())
184189
{
@@ -200,7 +205,7 @@ private void GoToVideoUrl()
200205
{
201206
try
202207
{
203-
BindWebView2Control(CurrentVideoUri);
208+
BindWebView2Control(CurrentVideoUrl);
204209
}
205210
catch (Exception ex)
206211
{
@@ -209,32 +214,24 @@ private void GoToVideoUrl()
209214
}
210215

211216
[RelayCommand]
212-
private void ImportCurrentVideo()
217+
private async void ImportCurrentVideo()
213218
{
214-
if (!GeneralHelper.IsValidUri(_currentVideoUri))
219+
if (!GeneralHelper.IsValidUri(_currentVideoUrl))
215220
{
216221
StatusMessage = "A video needs to be navigated to for 'Import'";
217222
return;
218223
}
219224

220-
if (!GeneralHelper.IsYouTubeVideoUri(_currentVideoUri))
225+
if (!GeneralHelper.IsYouTubeVideoUri(_currentVideoUrl))
221226
{
222227
StatusMessage = "Must be a YouTube video to be imported";
223228
return;
224229
}
225230

226231
try
227232
{
228-
GeneralHelper.CleanYouTubeUri(ref _currentVideoUri);
229-
var videoInfo = new VideoInfo
230-
{
231-
// With YouTubeVideoUri, from https://www.youtube.com/watch?v=d_l-st8Q1S0,
232-
// make https://img.youtube.com/vi/d_l-st8Q1S0/0.jpg
233-
CoverUrl = _currentVideoUri.Replace("www.youtube", "img.youtube")
234-
.Replace("watch?v=", "vi/") + "/0.jpg",
235-
Link = _currentVideoUri,
236-
};
237-
233+
GeneralHelper.CleanYouTubeUri(ref _currentVideoUrl);
234+
VideoInfo videoInfo = await _youtubeDataService.CreateYouTubeVideoMatch(_currentVideoUrl);
238235
_mainService.ImportVideo(_videoInfoList, _selectedVideoGroup, ref videoInfo,
239236
out bool isNewVideo, out string statusMessage);
240237
if (isNewVideo)
@@ -262,9 +259,9 @@ private async void OpenAtYouTubeWebSite()
262259
{
263260
try
264261
{
265-
if (_currentVideoUri.IsNotBlank() && GeneralHelper.IsValidUri(_currentVideoUri))
262+
if (_currentVideoUrl.IsNotBlank() && GeneralHelper.IsValidUri(_currentVideoUrl))
266263
{
267-
await GeneralHelper.ExecuteOpenUrlCommandAsync(_currentVideoUri);
264+
await GeneralHelper.ExecuteOpenUrlCommandAsync(_currentVideoUrl);
268265
}
269266
else
270267
{
@@ -289,15 +286,15 @@ private void InitializeWebView2()
289286
{
290287
// Always update the textbox (so can copy to clipboard)
291288
// not the same as YouTube behavior (only update at the top)
292-
CurrentVideoUri = WebView2Control.Source.AbsoluteUri;
289+
CurrentVideoUrl = WebView2Control.Source.AbsoluteUri;
293290
};
294291
OnPropertyChanged(nameof(WebView2Control));
295292
}
296293

297294
// Ensure to avoid empty YouTube control
298295
private VideoInfo EnsureInitialVideoInfo(List<VideoInfo> videoInfoList, string selectedVideoLink)
299296
{
300-
VideoInfo? initialVideoInfo = null;
297+
VideoInfo? initialVideoInfo = null;
301298
if (selectedVideoLink.IsNotBlank())
302299
{
303300
initialVideoInfo = videoInfoList.FirstOrDefault(x => x.Link == selectedVideoLink);
@@ -311,20 +308,20 @@ private VideoInfo EnsureInitialVideoInfo(List<VideoInfo> videoInfoList, string s
311308
else
312309
{
313310
// Create defaults on empty. Videos imported by a user (from UI) will be from YouTube.
314-
_currentVideoUri = "https://github.com/psun247/ShazamDesk";
311+
_currentVideoUrl = "https://github.com/psun247/ShazamDesk";
315312
initialVideoInfo = new VideoInfo
316313
{
317314
Description = "WPF ChatGPT + Shazam by Peter Sun",
318315
CoverUrl = "/CSharpWpfYouTube;component/Resources/Info.png",
319-
Link = _currentVideoUri,
316+
Link = _currentVideoUrl,
320317
};
321318
_mainService.ImportVideo(_videoInfoList, VideoInfo.HomeVideoGroup, ref initialVideoInfo,
322319
out bool _, out string _);
323320
var videoInfo = new VideoInfo
324321
{
325-
Description = "French Open Novak Djokovic vs Carlos Alcaraz",
326-
CoverUrl = "https://img.youtube.com/vi/K6VuFCwUMnQ/0.jpg",
327-
Link = "https://www.youtube.com/watch?v=K6VuFCwUMnQ",
322+
Description = "Jannik Sinner v Daniil Medvedev Extended Highlights | Australian Open 2024 Final",
323+
CoverUrl = "https://img.youtube.com/vi/b90INDbXX7Y/0.jpg",
324+
Link = "https://www.youtube.com/watch?v=b90INDbXX7Y",
328325
};
329326
_mainService.ImportVideo(_videoInfoList, VideoInfo.HomeVideoGroup, ref videoInfo,
330327
out bool _, out string _);

0 commit comments

Comments
 (0)