public async Task <RecognitionResultModel> RecognizeAsync(string audioFileName, IConfiguration configuration) { JObject jsonResult = null; string responseJsonString, status, artist = string.Empty, kind, errorDescription, contentRoot; int errorCode = 0; try { contentRoot = configuration.GetValue <string>(WebHostDefaults.ContentRootKey); var mp3FilebytesArray = File.ReadAllBytes(contentRoot + @"\Downloads\Music\" + audioFileName + ".mp3"); responseJsonString = await Upload(mp3FilebytesArray); if (responseJsonString.Equals(string.Empty)) { errorDescription = "Sorry! An error occure in recognition of the song"; errorCode = -1; } jsonResult = (JObject)JsonConvert.DeserializeObject(responseJsonString); status = jsonResult["status"].Value <string>(); if (!status.Equals("success") || !responseJsonString.Contains("artist") || !responseJsonString.Contains("itunes") || !responseJsonString.Contains("kind")) { errorDescription = "Sorry! we cannot recognize the artist of the music"; errorCode = -1; } else { artist = jsonResult["result"]["artist"].Value <string>(); kind = jsonResult["result"]["itunes"]["kind"].Value <string>(); if (!kind.Equals("song")) { errorDescription = "music is not a song"; errorCode = -1; } else { errorDescription = ""; errorCode = 0; } } } catch (Exception ex) { errorDescription = ex.Message; errorCode = -1; } RecognitionResultModel recognitionModel = new RecognitionResultModel() { artist = artist, errorCode = errorCode, errorDescription = errorDescription }; return(recognitionModel); }
//[ActionName("YoutubeSearch")] //[Route("youtubeSearch")] public async Task <IActionResult> Index(string YoutubeUri, string JobId) { ViewBag.JobId = JobId; try { VideoDownloader videoDownloader = new VideoDownloader(); await _hubContext.Clients.Group(JobId).SendAsync("Index", "Start downloading video ..."); string VideoFileName = videoDownloader.DownloadAsync(YoutubeUri, _configuration, _hubContext, JobId); if (VideoFileName != string.Empty) { await _hubContext.Clients.Group(JobId).SendAsync("Index", "video downloaded successfully"); await Task.Delay(500); VideoConverter videoConverter = new VideoConverter(); await _hubContext.Clients.Group(JobId).SendAsync("Index", "start converting video into mp3 format ..."); int errorCode = videoConverter.ConvertAsync(VideoFileName, _configuration, _hubContext, JobId); if (errorCode == 0) { await _hubContext.Clients.Group(JobId).SendAsync("Index", "video converted successfully"); await Task.Delay(500); AudioRecongnition audioRecongnition = new AudioRecongnition(); await _hubContext.Clients.Group(JobId).SendAsync("Index", "start recognition process of mp3 music ..."); RecognitionResultModel recognitionResultModel = await audioRecongnition.RecognizeAsync(VideoFileName, _configuration); if (recognitionResultModel.errorCode == 0) { await _hubContext.Clients.Group(JobId).SendAsync("Index", "mp3 music recognized successfully successfully"); await Task.Delay(500); YoutubeSearcher youtubeSearcher = new YoutubeSearcher(); await _hubContext.Clients.Group(JobId).SendAsync("Index", "Searching for videos related to the artist..."); List <VideoTempleteModel> listVideo = await youtubeSearcher.Run(recognitionResultModel.artist); await _hubContext.Clients.Group(JobId).SendAsync("Index", "Getting youtube result..."); await Task.Delay(500); ViewBag.error = "Video list is empty!"; return(View("Index", listVideo));; } else { ViewBag.error = recognitionResultModel.errorDescription; return(View("Index", new List <VideoTempleteModel>())); } } else { ViewBag.error = "Sorry! An Error occure in converting video into mp3 music"; return(View("Index", new List <VideoTempleteModel>())); } } else { ViewBag.error = "sorry! An error occure in downloading video"; return(View("Index", new List <VideoTempleteModel>())); } } catch (Exception ex) { ViewBag.error = "Unknown error ocured - " + ex.Message; return(View("Index", new List <VideoTempleteModel>())); } }