public async Task <TopWordsResult> GetTopWordsFromArtistLyrics(TopWordsParams topWordsParams) { _logger.LogInformation($"Looking for artist with Id {topWordsParams.ArtistId}"); await NotifyClient(topWordsParams.ConnectionId, "Starting.."); if (TopWordsFileExists(topWordsParams.ArtistId)) { _logger.LogInformation("File found, done!"); return(GetTopWordsFromFile(topWordsParams.ArtistId)); } var songsPageInfo = await _crawlerService.GetSongsPageInfo(topWordsParams.ArtistId); if (songsPageInfo.SongsUrls.Count == 0) { _logger.LogInformation("No songs found"); return(new TopWordsResult(message: "No songs were found..")); } _logger.LogInformation($"{songsPageInfo.SongsUrls.Count} songs urls found"); await NotifyClient(topWordsParams.ConnectionId, $"{songsPageInfo.SongsUrls.Count} songs urls found"); for (int i = 0; i < songsPageInfo.SongsUrls.Take(_apiSettings.CrawlSongsCount).Count(); i++) { _logger.LogInformation($"Crawling song #{i + 1} from {songsPageInfo.ArtistName}.."); await NotifyClient(topWordsParams.ConnectionId, $"Crawling song #{i + 1} from {songsPageInfo.ArtistName}.."); string songLyrics = await _crawlerService.GetSongLyrics(songsPageInfo.SongsUrls[i]); _artistLyrics.Add(songLyrics); } _logger.LogInformation("Analyzing.."); await NotifyClient(topWordsParams.ConnectionId, "Analyzing.."); var topWords = _wordFrequencyService.GetWordsFrequencies(_artistLyrics); var result = new TopWordsResult(artist: new Artist(topWordsParams.ArtistId, songsPageInfo.ArtistName), message: "OK", words: topWords); await SaveTopWordsToFile(result); _logger.LogInformation("Done!"); return(result); }
private async Task SaveTopWordsToFile(TopWordsResult topWordsResult) { _logger.LogInformation("Saving file.."); await File.WriteAllTextAsync(GetArtistFileName(topWordsResult.Artist.Id), JsonConvert.SerializeObject(topWordsResult)); }