-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented deleting non-existing YouTube videos worker task
- Loading branch information
Showing
8 changed files
with
274 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
{ | ||
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/cc8LgDS2hbwSNWMuUs5HbzmLrU8\"", | ||
"eventId": null, | ||
"items": [ | ||
{ | ||
"ageGating": null, | ||
"contentDetails": { | ||
"caption": "false", | ||
"contentRating": null, | ||
"countryRestriction": null, | ||
"definition": "sd", | ||
"dimension": "2d", | ||
"duration": "PT8M10S", | ||
"hasCustomThumbnail": null, | ||
"licensedContent": true, | ||
"projection": "rectangular", | ||
"regionRestriction": null, | ||
"ETag": null | ||
}, | ||
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/4kozuWKvk4OF11WJLNKFA-kNhHQ\"", | ||
"fileDetails": null, | ||
"id": "gOMhN-hfMtY", | ||
"kind": "youtube#video", | ||
"liveStreamingDetails": null, | ||
"localizations": null, | ||
"monetizationDetails": null, | ||
"player": { | ||
"embedHeight": null, | ||
"embedHtml": "<iframe width=\"480\" height=\"270\" src=\"//www.youtube.com/embed/gOMhN-hfMtY\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe>", | ||
"embedWidth": null, | ||
"ETag": null | ||
}, | ||
"processingDetails": null, | ||
"projectDetails": null, | ||
"recordingDetails": null, | ||
"snippet": { | ||
"categoryId": "10", | ||
"channelId": "UC20vb-R_px4CguHzzBPhoyQ", | ||
"channelTitle": "EminemVEVO", | ||
"defaultAudioLanguage": "en-US", | ||
"defaultLanguage": null, | ||
"description": "Music video by Eminem performing Stan. YouTube view counts pre-VEVO: 3,965,564. (C) 2002 Aftermath Entertainment/Interscope Records", | ||
"liveBroadcastContent": "none", | ||
"localized": { | ||
"description": "Music video by Eminem performing Stan. YouTube view counts pre-VEVO: 3,965,564. (C) 2002 Aftermath Entertainment/Interscope Records", | ||
"title": "Eminem - Stan (Long Version) ft. Dido", | ||
"ETag": null | ||
}, | ||
"publishedAt": "2009-12-25T06:54:41.000Z", | ||
"tags": [ | ||
"Eminem", | ||
"Aftermath", | ||
"Pop" | ||
], | ||
"thumbnails": { | ||
"default": { | ||
"height": 90, | ||
"url": "https://i.ytimg.com/vi/gOMhN-hfMtY/default.jpg", | ||
"width": 120, | ||
"ETag": null | ||
}, | ||
"high": { | ||
"height": 360, | ||
"url": "https://i.ytimg.com/vi/gOMhN-hfMtY/hqdefault.jpg", | ||
"width": 480, | ||
"ETag": null | ||
}, | ||
"maxres": { | ||
"height": 720, | ||
"url": "https://i.ytimg.com/vi/gOMhN-hfMtY/maxresdefault.jpg", | ||
"width": 1280, | ||
"ETag": null | ||
}, | ||
"medium": { | ||
"height": 180, | ||
"url": "https://i.ytimg.com/vi/gOMhN-hfMtY/mqdefault.jpg", | ||
"width": 320, | ||
"ETag": null | ||
}, | ||
"standard": { | ||
"height": 480, | ||
"url": "https://i.ytimg.com/vi/gOMhN-hfMtY/sddefault.jpg", | ||
"width": 640, | ||
"ETag": null | ||
}, | ||
"ETag": null | ||
}, | ||
"title": "Eminem - Stan (Long Version) ft. Dido", | ||
"ETag": null | ||
}, | ||
"statistics": { | ||
"commentCount": 87044, | ||
"dislikeCount": 46908, | ||
"favoriteCount": 0, | ||
"likeCount": 1156974, | ||
"viewCount": 206912088, | ||
"ETag": null | ||
}, | ||
"status": { | ||
"embeddable": true, | ||
"failureReason": null, | ||
"license": "youtube", | ||
"privacyStatus": "public", | ||
"publicStatsViewable": true, | ||
"publishAt": null, | ||
"rejectionReason": null, | ||
"uploadStatus": "processed", | ||
"ETag": null | ||
}, | ||
"suggestions": null, | ||
"topicDetails": { | ||
"relevantTopicIds": [ | ||
"/m/0glt670", | ||
"/m/04rlf", | ||
"/m/04rlf", | ||
"/m/0glt670" | ||
], | ||
"topicCategories": [ | ||
"https://en.wikipedia.org/wiki/Hip_hop_music", | ||
"https://en.wikipedia.org/wiki/Music" | ||
], | ||
"topicIds": null, | ||
"ETag": null | ||
} | ||
} | ||
], | ||
"kind": "youtube#videoListResponse", | ||
"nextPageToken": null, | ||
"pageInfo": { | ||
"resultsPerPage": 1, | ||
"totalResults": 1, | ||
"ETag": null | ||
}, | ||
"prevPageToken": null, | ||
"tokenPagination": null, | ||
"visitorId": null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Services/MusicX.Services.DataProviders/YouTubeDataProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace MusicX.Services.DataProviders | ||
{ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
using Google.Apis.Services; | ||
using Google.Apis.YouTube.v3; | ||
|
||
using MusicX.Common; | ||
|
||
public class YouTubeDataProvider | ||
{ | ||
// TODO: Extract to config or DB. The API key is limited by IP so its OK to be publicly visible. | ||
private const string ApiKey = "AIzaSyACdUSMzCxCknVFZoJPAenh6nakrGj1eug"; | ||
|
||
public async Task<bool> CheckIfVideoExists(string youtubeId) | ||
{ | ||
var youtubeService = new YouTubeService( | ||
new BaseClientService.Initializer | ||
{ | ||
ApiKey = ApiKey, ApplicationName = GlobalConstants.ApplicationName, GZipEnabled = true, | ||
}); | ||
|
||
var searchVideoRequest = youtubeService.Videos.List("snippet"); | ||
searchVideoRequest.Id = youtubeId; | ||
var result = await searchVideoRequest.ExecuteAsync(); | ||
return result.Items.Count != 0; | ||
} | ||
} | ||
} | ||
|
||
// Queryable data for video: snippet,contentDetails,player,recordingDetails,statistics,status,topicDetails | ||
|
||
//// var searchListRequest = youtubeService.Search.List("snippet"); | ||
//// searchListRequest.Q = "Google"; // Replace with your search term. | ||
//// searchListRequest.MaxResults = 50; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/Worker/MusicX.Worker.Tasks/CheckForDeletedVideosTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
namespace MusicX.Worker.Tasks | ||
{ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using MusicX.Common.Models; | ||
using MusicX.Data.Common.Repositories; | ||
using MusicX.Data.Models; | ||
using MusicX.Services.DataProviders; | ||
using MusicX.Worker.Common; | ||
|
||
public class CheckForDeletedVideosTask : BaseTask | ||
{ | ||
private readonly IDeletableEntityRepository<SongMetadata> songMetadataRepository; | ||
|
||
private readonly YouTubeDataProvider youTubeDataProvider; | ||
|
||
public CheckForDeletedVideosTask(IServiceProvider serviceProvider) | ||
: base(serviceProvider) | ||
{ | ||
this.songMetadataRepository = serviceProvider.GetService<IDeletableEntityRepository<SongMetadata>>(); | ||
this.youTubeDataProvider = new YouTubeDataProvider(); | ||
} | ||
|
||
public override Task<string> DoWork(string parameters) | ||
{ | ||
return this.DoWork<Input, Output>(parameters, this.DoWork); | ||
} | ||
|
||
public override WorkerTask Recreate(WorkerTask currentTask) | ||
{ | ||
var runAfter = (currentTask.RunAfter ?? DateTime.UtcNow).AddDays(7).Date.AddHours(20); // 20:00 after 7 days | ||
|
||
var workerTask = new WorkerTask | ||
{ | ||
TypeName = currentTask.TypeName, | ||
Parameters = currentTask.Parameters, | ||
Priority = currentTask.Priority, | ||
RunAfter = runAfter, | ||
}; | ||
return workerTask; | ||
} | ||
|
||
private async Task<Output> DoWork(Input input) | ||
{ | ||
try | ||
{ | ||
var youtubeIds = this.songMetadataRepository.All().Where(x => x.Type == SongMetadataType.YouTubeVideoId).ToList(); | ||
foreach (var songMetadata in youtubeIds) | ||
{ | ||
Console.Write($"Checking video for song #{songMetadata.SongId} ({songMetadata.Value}) => "); | ||
if (!await this.youTubeDataProvider.CheckIfVideoExists(songMetadata.Value)) | ||
{ | ||
this.songMetadataRepository.Delete(songMetadata); | ||
await this.songMetadataRepository.SaveChangesAsync(); | ||
Console.WriteLine("DELETED"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("OK"); | ||
} | ||
} | ||
|
||
return new Output { Success = true }; | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
return new Output { Success = false, Exception = e.ToString() }; | ||
} | ||
} | ||
|
||
public class Input | ||
{ | ||
} | ||
|
||
public class Output : BaseTaskOutput | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters